blob: e0186d4d6db9233fe6dfc64e28020a2675384c2e [file] [log] [blame]
Daniel Sandler9e426aa2010-04-01 10:42:33 -04001# Copyright (C) 2010 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import sys
16
Mike Frysingere37aa5f2019-09-23 19:14:13 -040017from color import Coloring
Daniel Sandler9e426aa2010-04-01 10:42:33 -040018from command import Command
19from git_command import GitCommand
Daniel Sandler9e426aa2010-04-01 10:42:33 -040020
Mike Frysingere37aa5f2019-09-23 19:14:13 -040021
22class RebaseColoring(Coloring):
23 def __init__(self, config):
24 Coloring.__init__(self, config, 'rebase')
25 self.project = self.printer('project', attr='bold')
26 self.fail = self.printer('fail', fg='red')
27
28
Daniel Sandler9e426aa2010-04-01 10:42:33 -040029class Rebase(Command):
30 common = True
31 helpSummary = "Rebase local branches on upstream branch"
32 helpUsage = """
33%prog {[<project>...] | -i <project>...}
34"""
35 helpDescription = """
36'%prog' uses git rebase to move local changes in the current topic branch to
37the HEAD of the upstream history, useful when you have made commits in a topic
38branch but need to incorporate new upstream changes "underneath" them.
39"""
40
41 def _Options(self, p):
Mike Frysinger9180a072021-04-13 14:57:40 -040042 g = p.get_option_group('--quiet')
43 g.add_option('-i', '--interactive',
David Pursehouseabdf7502020-02-12 14:58:39 +090044 dest="interactive", action="store_true",
45 help="interactive rebase (single project only)")
Daniel Sandler9e426aa2010-04-01 10:42:33 -040046
Mike Frysinger4a077982019-09-23 18:54:30 -040047 p.add_option('--fail-fast',
48 dest='fail_fast', action='store_true',
49 help='Stop rebasing after first error is hit')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070050 p.add_option('-f', '--force-rebase',
51 dest='force_rebase', action='store_true',
52 help='Pass --force-rebase to git rebase')
53 p.add_option('--no-ff',
Mike Frysingerc58ec4d2020-02-17 14:36:08 -050054 dest='ff', default=True, action='store_false',
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070055 help='Pass --no-ff to git rebase')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070056 p.add_option('--autosquash',
57 dest='autosquash', action='store_true',
58 help='Pass --autosquash to git rebase')
59 p.add_option('--whitespace',
60 dest='whitespace', action='store', metavar='WS',
61 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050062 p.add_option('--auto-stash',
63 dest='auto_stash', action='store_true',
64 help='Stash local modifications before starting')
Xiaohui Chen0b4cb322016-01-27 14:33:51 -080065 p.add_option('-m', '--onto-manifest',
66 dest='onto_manifest', action='store_true',
67 help='Rebase onto the manifest version instead of upstream '
68 'HEAD. This helps to make sure the local tree stays '
69 'consistent if you previously synced to a manifest.')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070070
Daniel Sandler9e426aa2010-04-01 10:42:33 -040071 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +090072 all_projects = self.GetProjects(args)
73 one_project = len(all_projects) == 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040074
75 if opt.interactive and not one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070076 print('error: interactive rebase not supported with multiple projects',
77 file=sys.stderr)
David James8d201162013-10-11 17:03:19 -070078 if len(args) == 1:
79 print('note: project %s is mapped to more than one path' % (args[0],),
David Pursehouseabdf7502020-02-12 14:58:39 +090080 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -040081 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040082
Mike Frysingerd5c306b2019-08-07 17:23:23 -040083 # Setup the common git rebase args that we use for all projects.
84 common_args = ['rebase']
85 if opt.whitespace:
86 common_args.append('--whitespace=%s' % opt.whitespace)
87 if opt.quiet:
88 common_args.append('--quiet')
89 if opt.force_rebase:
90 common_args.append('--force-rebase')
Mike Frysingerc58ec4d2020-02-17 14:36:08 -050091 if not opt.ff:
Mike Frysingerd5c306b2019-08-07 17:23:23 -040092 common_args.append('--no-ff')
93 if opt.autosquash:
94 common_args.append('--autosquash')
95 if opt.interactive:
96 common_args.append('-i')
97
Mike Frysingere37aa5f2019-09-23 19:14:13 -040098 config = self.manifest.manifestProject.config
99 out = RebaseColoring(config)
100 out.redirect(sys.stdout)
101
Mike Frysinger4a077982019-09-23 18:54:30 -0400102 ret = 0
David Pursehouse8a68ff92012-09-24 12:15:13 +0900103 for project in all_projects:
Mike Frysinger4a077982019-09-23 18:54:30 -0400104 if ret and opt.fail_fast:
105 break
106
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400107 cb = project.CurrentBranch
108 if not cb:
109 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +0900110 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700111 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400112 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400113 # ignore branches with detatched HEADs
114 continue
115
116 upbranch = project.GetBranch(cb)
117 if not upbranch.LocalMerge:
118 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700119 print("error: project %s does not track any remote branches"
120 % project.relpath, file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400121 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400122 # ignore branches without remotes
123 continue
124
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400125 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800126 if opt.onto_manifest:
127 args.append('--onto')
128 args.append(project.revisionExpr)
129
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700130 args.append(upbranch.LocalMerge)
131
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400132 out.project('project %s: rebasing %s -> %s',
133 project.relpath, cb, upbranch.LocalMerge)
134 out.nl()
135 out.flush()
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700136
Joe Hansche5e572342012-03-05 11:41:19 -0500137 needs_stash = False
138 if opt.auto_stash:
139 stash_args = ["update-index", "--refresh", "-q"]
140
141 if GitCommand(project, stash_args).Wait() != 0:
142 needs_stash = True
143 # Dirty index, requires stash...
144 stash_args = ["stash"]
145
146 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400147 ret += 1
148 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500149
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400150 if GitCommand(project, args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400151 ret += 1
152 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500153
154 if needs_stash:
155 stash_args.append('pop')
156 stash_args.append('--quiet')
157 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400158 ret += 1
159
160 if ret:
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400161 out.fail('%i projects had errors', ret)
162 out.nl()
163
Mike Frysinger4a077982019-09-23 18:54:30 -0400164 return ret