blob: dcb8b2a32f09eb8d8fac607be7db331acffc6eff [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Daniel Sandler9e426aa2010-04-01 10:42:33 -04002#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Daniel Sandler9e426aa2010-04-01 10:42:33 -040018import sys
19
Mike Frysingere37aa5f2019-09-23 19:14:13 -040020from color import Coloring
Daniel Sandler9e426aa2010-04-01 10:42:33 -040021from command import Command
22from git_command import GitCommand
Daniel Sandler9e426aa2010-04-01 10:42:33 -040023
Mike Frysingere37aa5f2019-09-23 19:14:13 -040024
25class RebaseColoring(Coloring):
26 def __init__(self, config):
27 Coloring.__init__(self, config, 'rebase')
28 self.project = self.printer('project', attr='bold')
29 self.fail = self.printer('fail', fg='red')
30
31
Daniel Sandler9e426aa2010-04-01 10:42:33 -040032class Rebase(Command):
33 common = True
34 helpSummary = "Rebase local branches on upstream branch"
35 helpUsage = """
36%prog {[<project>...] | -i <project>...}
37"""
38 helpDescription = """
39'%prog' uses git rebase to move local changes in the current topic branch to
40the HEAD of the upstream history, useful when you have made commits in a topic
41branch but need to incorporate new upstream changes "underneath" them.
42"""
43
44 def _Options(self, p):
45 p.add_option('-i', '--interactive',
46 dest="interactive", action="store_true",
47 help="interactive rebase (single project only)")
48
Mike Frysinger4a077982019-09-23 18:54:30 -040049 p.add_option('--fail-fast',
50 dest='fail_fast', action='store_true',
51 help='Stop rebasing after first error is hit')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070052 p.add_option('-f', '--force-rebase',
53 dest='force_rebase', action='store_true',
54 help='Pass --force-rebase to git rebase')
55 p.add_option('--no-ff',
56 dest='no_ff', action='store_true',
57 help='Pass --no-ff to git rebase')
58 p.add_option('-q', '--quiet',
59 dest='quiet', action='store_true',
60 help='Pass --quiet to git rebase')
61 p.add_option('--autosquash',
62 dest='autosquash', action='store_true',
63 help='Pass --autosquash to git rebase')
64 p.add_option('--whitespace',
65 dest='whitespace', action='store', metavar='WS',
66 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050067 p.add_option('--auto-stash',
68 dest='auto_stash', action='store_true',
69 help='Stash local modifications before starting')
Xiaohui Chen0b4cb322016-01-27 14:33:51 -080070 p.add_option('-m', '--onto-manifest',
71 dest='onto_manifest', action='store_true',
72 help='Rebase onto the manifest version instead of upstream '
73 'HEAD. This helps to make sure the local tree stays '
74 'consistent if you previously synced to a manifest.')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070075
Daniel Sandler9e426aa2010-04-01 10:42:33 -040076 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +090077 all_projects = self.GetProjects(args)
78 one_project = len(all_projects) == 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040079
80 if opt.interactive and not one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070081 print('error: interactive rebase not supported with multiple projects',
82 file=sys.stderr)
David James8d201162013-10-11 17:03:19 -070083 if len(args) == 1:
84 print('note: project %s is mapped to more than one path' % (args[0],),
85 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -040086 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040087
Mike Frysingerd5c306b2019-08-07 17:23:23 -040088 # Setup the common git rebase args that we use for all projects.
89 common_args = ['rebase']
90 if opt.whitespace:
91 common_args.append('--whitespace=%s' % opt.whitespace)
92 if opt.quiet:
93 common_args.append('--quiet')
94 if opt.force_rebase:
95 common_args.append('--force-rebase')
96 if opt.no_ff:
97 common_args.append('--no-ff')
98 if opt.autosquash:
99 common_args.append('--autosquash')
100 if opt.interactive:
101 common_args.append('-i')
102
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400103 config = self.manifest.manifestProject.config
104 out = RebaseColoring(config)
105 out.redirect(sys.stdout)
106
Mike Frysinger4a077982019-09-23 18:54:30 -0400107 ret = 0
David Pursehouse8a68ff92012-09-24 12:15:13 +0900108 for project in all_projects:
Mike Frysinger4a077982019-09-23 18:54:30 -0400109 if ret and opt.fail_fast:
110 break
111
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400112 cb = project.CurrentBranch
113 if not cb:
114 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +0900115 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700116 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400117 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400118 # ignore branches with detatched HEADs
119 continue
120
121 upbranch = project.GetBranch(cb)
122 if not upbranch.LocalMerge:
123 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700124 print("error: project %s does not track any remote branches"
125 % project.relpath, file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400126 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400127 # ignore branches without remotes
128 continue
129
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400130 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800131 if opt.onto_manifest:
132 args.append('--onto')
133 args.append(project.revisionExpr)
134
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700135 args.append(upbranch.LocalMerge)
136
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400137 out.project('project %s: rebasing %s -> %s',
138 project.relpath, cb, upbranch.LocalMerge)
139 out.nl()
140 out.flush()
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400141
Joe Hansche5e572342012-03-05 11:41:19 -0500142 needs_stash = False
143 if opt.auto_stash:
144 stash_args = ["update-index", "--refresh", "-q"]
145
146 if GitCommand(project, stash_args).Wait() != 0:
147 needs_stash = True
148 # Dirty index, requires stash...
149 stash_args = ["stash"]
150
151 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400152 ret += 1
153 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500154
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400155 if GitCommand(project, args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400156 ret += 1
157 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500158
159 if needs_stash:
160 stash_args.append('pop')
161 stash_args.append('--quiet')
162 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400163 ret += 1
164
165 if ret:
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400166 out.fail('%i projects had errors', ret)
167 out.nl()
168
Mike Frysinger4a077982019-09-23 18:54:30 -0400169 return ret