blob: 3d1a63e4be1fd5302bdbd5c8fce55228acd80f4c [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):
Mike Frysinger4f210542021-06-14 16:05:19 -040030 COMMON = True
Daniel Sandler9e426aa2010-04-01 10:42:33 -040031 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',
Mike Frysingerc177f942021-05-04 08:06:36 -040049 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',
Mike Frysingerc177f942021-05-04 08:06:36 -040052 help='pass --force-rebase to git rebase')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070053 p.add_option('--no-ff',
Mike Frysingerc58ec4d2020-02-17 14:36:08 -050054 dest='ff', default=True, action='store_false',
Mike Frysingerc177f942021-05-04 08:06:36 -040055 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',
Mike Frysingerc177f942021-05-04 08:06:36 -040058 help='pass --autosquash to git rebase')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070059 p.add_option('--whitespace',
60 dest='whitespace', action='store', metavar='WS',
Mike Frysingerc177f942021-05-04 08:06:36 -040061 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',
Mike Frysingerc177f942021-05-04 08:06:36 -040064 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',
Mike Frysingerc177f942021-05-04 08:06:36 -040067 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):
LaMont Jonescc879a92021-11-18 22:40:18 +000072 all_projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only)
David Pursehouse8a68ff92012-09-24 12:15:13 +090073 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)
LaMont Jonescc879a92021-11-18 22:40:18 +0000101 _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only)
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400102
Mike Frysinger4a077982019-09-23 18:54:30 -0400103 ret = 0
David Pursehouse8a68ff92012-09-24 12:15:13 +0900104 for project in all_projects:
Mike Frysinger4a077982019-09-23 18:54:30 -0400105 if ret and opt.fail_fast:
106 break
107
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400108 cb = project.CurrentBranch
109 if not cb:
110 if one_project:
LaMont Jonescc879a92021-11-18 22:40:18 +0000111 print("error: project %s has a detached HEAD" % _RelPath(project),
Sarah Owenscecd1d82012-11-01 22:59:27 -0700112 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400113 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400114 # ignore branches with detatched HEADs
115 continue
116
117 upbranch = project.GetBranch(cb)
118 if not upbranch.LocalMerge:
119 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700120 print("error: project %s does not track any remote branches"
LaMont Jonescc879a92021-11-18 22:40:18 +0000121 % _RelPath(project), file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400122 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400123 # ignore branches without remotes
124 continue
125
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400126 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800127 if opt.onto_manifest:
128 args.append('--onto')
129 args.append(project.revisionExpr)
130
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700131 args.append(upbranch.LocalMerge)
132
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400133 out.project('project %s: rebasing %s -> %s',
LaMont Jonescc879a92021-11-18 22:40:18 +0000134 _RelPath(project), cb, upbranch.LocalMerge)
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400135 out.nl()
136 out.flush()
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700137
Joe Hansche5e572342012-03-05 11:41:19 -0500138 needs_stash = False
139 if opt.auto_stash:
140 stash_args = ["update-index", "--refresh", "-q"]
141
142 if GitCommand(project, stash_args).Wait() != 0:
143 needs_stash = True
144 # Dirty index, requires stash...
145 stash_args = ["stash"]
146
147 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400148 ret += 1
149 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500150
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400151 if GitCommand(project, args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400152 ret += 1
153 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500154
155 if needs_stash:
156 stash_args.append('pop')
157 stash_args.append('--quiet')
158 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400159 ret += 1
160
161 if ret:
Mike Frysingere37aa5f2019-09-23 19:14:13 -0400162 out.fail('%i projects had errors', ret)
163 out.nl()
164
Mike Frysinger4a077982019-09-23 18:54:30 -0400165 return ret