blob: cf536e9da1bd1b10e8f80ed7a79c6e37d293bd7c [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):
42 p.add_option('-i', '--interactive',
David Pursehouseabdf7502020-02-12 14:58:39 +090043 dest="interactive", action="store_true",
44 help="interactive rebase (single project only)")
Daniel Sandler9e426aa2010-04-01 10:42:33 -040045
Mike Frysinger4a077982019-09-23 18:54:30 -040046 p.add_option('--fail-fast',
47 dest='fail_fast', action='store_true',
48 help='Stop rebasing after first error is hit')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070049 p.add_option('-f', '--force-rebase',
50 dest='force_rebase', action='store_true',
51 help='Pass --force-rebase to git rebase')
52 p.add_option('--no-ff',
Mike Frysingerc58ec4d2020-02-17 14:36:08 -050053 dest='ff', default=True, action='store_false',
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070054 help='Pass --no-ff to git rebase')
55 p.add_option('-q', '--quiet',
56 dest='quiet', action='store_true',
57 help='Pass --quiet to git rebase')
58 p.add_option('--autosquash',
59 dest='autosquash', action='store_true',
60 help='Pass --autosquash to git rebase')
61 p.add_option('--whitespace',
62 dest='whitespace', action='store', metavar='WS',
63 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050064 p.add_option('--auto-stash',
65 dest='auto_stash', action='store_true',
66 help='Stash local modifications before starting')
Xiaohui Chen0b4cb322016-01-27 14:33:51 -080067 p.add_option('-m', '--onto-manifest',
68 dest='onto_manifest', action='store_true',
69 help='Rebase onto the manifest version instead of upstream '
70 'HEAD. This helps to make sure the local tree stays '
71 'consistent if you previously synced to a manifest.')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070072
Daniel Sandler9e426aa2010-04-01 10:42:33 -040073 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +090074 all_projects = self.GetProjects(args)
75 one_project = len(all_projects) == 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040076
77 if opt.interactive and not one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070078 print('error: interactive rebase not supported with multiple projects',
79 file=sys.stderr)
David James8d201162013-10-11 17:03:19 -070080 if len(args) == 1:
81 print('note: project %s is mapped to more than one path' % (args[0],),
David Pursehouseabdf7502020-02-12 14:58:39 +090082 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -040083 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040084
Mike Frysingerd5c306b2019-08-07 17:23:23 -040085 # Setup the common git rebase args that we use for all projects.
86 common_args = ['rebase']
87 if opt.whitespace:
88 common_args.append('--whitespace=%s' % opt.whitespace)
89 if opt.quiet:
90 common_args.append('--quiet')
91 if opt.force_rebase:
92 common_args.append('--force-rebase')
Mike Frysingerc58ec4d2020-02-17 14:36:08 -050093 if not opt.ff:
Mike Frysingerd5c306b2019-08-07 17:23:23 -040094 common_args.append('--no-ff')
95 if opt.autosquash:
96 common_args.append('--autosquash')
97 if opt.interactive:
98 common_args.append('-i')
99
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400100 config = self.manifest.manifestProject.config
101 out = RebaseColoring(config)
102 out.redirect(sys.stdout)
103
Mike Frysinger4a077982019-09-23 18:54:30 -0400104 ret = 0
David Pursehouse8a68ff92012-09-24 12:15:13 +0900105 for project in all_projects:
Mike Frysinger4a077982019-09-23 18:54:30 -0400106 if ret and opt.fail_fast:
107 break
108
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400109 cb = project.CurrentBranch
110 if not cb:
111 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +0900112 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700113 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400114 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400115 # ignore branches with detatched HEADs
116 continue
117
118 upbranch = project.GetBranch(cb)
119 if not upbranch.LocalMerge:
120 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700121 print("error: project %s does not track any remote branches"
122 % project.relpath, file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400123 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400124 # ignore branches without remotes
125 continue
126
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400127 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800128 if opt.onto_manifest:
129 args.append('--onto')
130 args.append(project.revisionExpr)
131
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700132 args.append(upbranch.LocalMerge)
133
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400134 out.project('project %s: rebasing %s -> %s',
135 project.relpath, cb, upbranch.LocalMerge)
136 out.nl()
137 out.flush()
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700138
Joe Hansche5e572342012-03-05 11:41:19 -0500139 needs_stash = False
140 if opt.auto_stash:
141 stash_args = ["update-index", "--refresh", "-q"]
142
143 if GitCommand(project, stash_args).Wait() != 0:
144 needs_stash = True
145 # Dirty index, requires stash...
146 stash_args = ["stash"]
147
148 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400149 ret += 1
150 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500151
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400152 if GitCommand(project, args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400153 ret += 1
154 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500155
156 if needs_stash:
157 stash_args.append('pop')
158 stash_args.append('--quiet')
159 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400160 ret += 1
161
162 if ret:
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400163 out.fail('%i projects had errors', ret)
164 out.nl()
165
Mike Frysinger4a077982019-09-23 18:54:30 -0400166 return ret