blob: 9464091fcaf9d034e7da31bbcd06da4f9509729a [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
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070040 p.add_option('-f', '--force-rebase',
41 dest='force_rebase', action='store_true',
42 help='Pass --force-rebase to git rebase')
43 p.add_option('--no-ff',
44 dest='no_ff', action='store_true',
45 help='Pass --no-ff to git rebase')
46 p.add_option('-q', '--quiet',
47 dest='quiet', action='store_true',
48 help='Pass --quiet to git rebase')
49 p.add_option('--autosquash',
50 dest='autosquash', action='store_true',
51 help='Pass --autosquash to git rebase')
52 p.add_option('--whitespace',
53 dest='whitespace', action='store', metavar='WS',
54 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050055 p.add_option('--auto-stash',
56 dest='auto_stash', action='store_true',
57 help='Stash local modifications before starting')
Xiaohui Chen0b4cb322016-01-27 14:33:51 -080058 p.add_option('-m', '--onto-manifest',
59 dest='onto_manifest', action='store_true',
60 help='Rebase onto the manifest version instead of upstream '
61 'HEAD. This helps to make sure the local tree stays '
62 'consistent if you previously synced to a manifest.')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070063
Daniel Sandler9e426aa2010-04-01 10:42:33 -040064 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +090065 all_projects = self.GetProjects(args)
66 one_project = len(all_projects) == 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040067
68 if opt.interactive and not one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070069 print('error: interactive rebase not supported with multiple projects',
70 file=sys.stderr)
David James8d201162013-10-11 17:03:19 -070071 if len(args) == 1:
72 print('note: project %s is mapped to more than one path' % (args[0],),
73 file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -040074 return -1
75
David Pursehouse8a68ff92012-09-24 12:15:13 +090076 for project in all_projects:
Daniel Sandler9e426aa2010-04-01 10:42:33 -040077 cb = project.CurrentBranch
78 if not cb:
79 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090080 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -070081 file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -040082 return -1
83 # ignore branches with detatched HEADs
84 continue
85
86 upbranch = project.GetBranch(cb)
87 if not upbranch.LocalMerge:
88 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -070089 print("error: project %s does not track any remote branches"
90 % project.relpath, file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -040091 return -1
92 # ignore branches without remotes
93 continue
94
Daniel Sandler9e426aa2010-04-01 10:42:33 -040095 args = ["rebase"]
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070096
97 if opt.whitespace:
98 args.append('--whitespace=%s' % opt.whitespace)
99
100 if opt.quiet:
101 args.append('--quiet')
102
103 if opt.force_rebase:
104 args.append('--force-rebase')
105
106 if opt.no_ff:
107 args.append('--no-ff')
108
109 if opt.autosquash:
110 args.append('--autosquash')
111
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400112 if opt.interactive:
113 args.append("-i")
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700114
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800115 if opt.onto_manifest:
116 args.append('--onto')
117 args.append(project.revisionExpr)
118
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700119 args.append(upbranch.LocalMerge)
120
Sarah Owenscecd1d82012-11-01 22:59:27 -0700121 print('# %s: rebasing %s -> %s'
122 % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400123
Joe Hansche5e572342012-03-05 11:41:19 -0500124 needs_stash = False
125 if opt.auto_stash:
126 stash_args = ["update-index", "--refresh", "-q"]
127
128 if GitCommand(project, stash_args).Wait() != 0:
129 needs_stash = True
130 # Dirty index, requires stash...
131 stash_args = ["stash"]
132
133 if GitCommand(project, stash_args).Wait() != 0:
134 return -1
135
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400136 if GitCommand(project, args).Wait() != 0:
137 return -1
Joe Hansche5e572342012-03-05 11:41:19 -0500138
139 if needs_stash:
140 stash_args.append('pop')
141 stash_args.append('--quiet')
142 if GitCommand(project, stash_args).Wait() != 0:
143 return -1