blob: 346eb9cd244fce757df3a17667d913899d1892bc [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
20from command import Command
21from git_command import GitCommand
Daniel Sandler9e426aa2010-04-01 10:42:33 -040022
23class Rebase(Command):
24 common = True
25 helpSummary = "Rebase local branches on upstream branch"
26 helpUsage = """
27%prog {[<project>...] | -i <project>...}
28"""
29 helpDescription = """
30'%prog' uses git rebase to move local changes in the current topic branch to
31the HEAD of the upstream history, useful when you have made commits in a topic
32branch but need to incorporate new upstream changes "underneath" them.
33"""
34
35 def _Options(self, p):
36 p.add_option('-i', '--interactive',
37 dest="interactive", action="store_true",
38 help="interactive rebase (single project only)")
39
Mike Frysinger4a077982019-09-23 18:54:30 -040040 p.add_option('--fail-fast',
41 dest='fail_fast', action='store_true',
42 help='Stop rebasing after first error is hit')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070043 p.add_option('-f', '--force-rebase',
44 dest='force_rebase', action='store_true',
45 help='Pass --force-rebase to git rebase')
46 p.add_option('--no-ff',
47 dest='no_ff', action='store_true',
48 help='Pass --no-ff to git rebase')
49 p.add_option('-q', '--quiet',
50 dest='quiet', action='store_true',
51 help='Pass --quiet to git rebase')
52 p.add_option('--autosquash',
53 dest='autosquash', action='store_true',
54 help='Pass --autosquash to git rebase')
55 p.add_option('--whitespace',
56 dest='whitespace', action='store', metavar='WS',
57 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050058 p.add_option('--auto-stash',
59 dest='auto_stash', action='store_true',
60 help='Stash local modifications before starting')
Xiaohui Chen0b4cb322016-01-27 14:33:51 -080061 p.add_option('-m', '--onto-manifest',
62 dest='onto_manifest', action='store_true',
63 help='Rebase onto the manifest version instead of upstream '
64 'HEAD. This helps to make sure the local tree stays '
65 'consistent if you previously synced to a manifest.')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070066
Daniel Sandler9e426aa2010-04-01 10:42:33 -040067 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +090068 all_projects = self.GetProjects(args)
69 one_project = len(all_projects) == 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040070
71 if opt.interactive and not one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070072 print('error: interactive rebase not supported with multiple projects',
73 file=sys.stderr)
David James8d201162013-10-11 17:03:19 -070074 if len(args) == 1:
75 print('note: project %s is mapped to more than one path' % (args[0],),
76 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -040077 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040078
Mike Frysingerd5c306b2019-08-07 17:23:23 -040079 # Setup the common git rebase args that we use for all projects.
80 common_args = ['rebase']
81 if opt.whitespace:
82 common_args.append('--whitespace=%s' % opt.whitespace)
83 if opt.quiet:
84 common_args.append('--quiet')
85 if opt.force_rebase:
86 common_args.append('--force-rebase')
87 if opt.no_ff:
88 common_args.append('--no-ff')
89 if opt.autosquash:
90 common_args.append('--autosquash')
91 if opt.interactive:
92 common_args.append('-i')
93
Mike Frysinger4a077982019-09-23 18:54:30 -040094 ret = 0
David Pursehouse8a68ff92012-09-24 12:15:13 +090095 for project in all_projects:
Mike Frysinger4a077982019-09-23 18:54:30 -040096 if ret and opt.fail_fast:
97 break
98
Daniel Sandler9e426aa2010-04-01 10:42:33 -040099 cb = project.CurrentBranch
100 if not cb:
101 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +0900102 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700103 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400104 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400105 # ignore branches with detatched HEADs
106 continue
107
108 upbranch = project.GetBranch(cb)
109 if not upbranch.LocalMerge:
110 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700111 print("error: project %s does not track any remote branches"
112 % project.relpath, file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400113 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400114 # ignore branches without remotes
115 continue
116
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400117 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800118 if opt.onto_manifest:
119 args.append('--onto')
120 args.append(project.revisionExpr)
121
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700122 args.append(upbranch.LocalMerge)
123
Sarah Owenscecd1d82012-11-01 22:59:27 -0700124 print('# %s: rebasing %s -> %s'
125 % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400126
Joe Hansche5e572342012-03-05 11:41:19 -0500127 needs_stash = False
128 if opt.auto_stash:
129 stash_args = ["update-index", "--refresh", "-q"]
130
131 if GitCommand(project, stash_args).Wait() != 0:
132 needs_stash = True
133 # Dirty index, requires stash...
134 stash_args = ["stash"]
135
136 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400137 ret += 1
138 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500139
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400140 if GitCommand(project, args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400141 ret += 1
142 continue
Joe Hansche5e572342012-03-05 11:41:19 -0500143
144 if needs_stash:
145 stash_args.append('pop')
146 stash_args.append('--quiet')
147 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysinger4a077982019-09-23 18:54:30 -0400148 ret += 1
149
150 if ret:
151 print('error: %i projects had errors' % (ret,), file=sys.stderr)
152 return ret