blob: 20662b11aed33680f77fcc64bf4b735b0667386d [file] [log] [blame]
Daniel Sandler9e426aa2010-04-01 10:42:33 -04001#
2# Copyright (C) 2010 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17
18from command import Command
19from git_command import GitCommand
20from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M
21from error import GitError
22
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')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070058
Daniel Sandler9e426aa2010-04-01 10:42:33 -040059 def Execute(self, opt, args):
60 all = self.GetProjects(args)
61 one_project = len(all) == 1
62
63 if opt.interactive and not one_project:
64 print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
65 return -1
66
67 for project in all:
68 cb = project.CurrentBranch
69 if not cb:
70 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070071 print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040072 return -1
73 # ignore branches with detatched HEADs
74 continue
75
76 upbranch = project.GetBranch(cb)
77 if not upbranch.LocalMerge:
78 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070079 print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040080 return -1
81 # ignore branches without remotes
82 continue
83
Daniel Sandler9e426aa2010-04-01 10:42:33 -040084 args = ["rebase"]
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070085
86 if opt.whitespace:
87 args.append('--whitespace=%s' % opt.whitespace)
88
89 if opt.quiet:
90 args.append('--quiet')
91
92 if opt.force_rebase:
93 args.append('--force-rebase')
94
95 if opt.no_ff:
96 args.append('--no-ff')
97
98 if opt.autosquash:
99 args.append('--autosquash')
100
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400101 if opt.interactive:
102 args.append("-i")
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700103
104 args.append(upbranch.LocalMerge)
105
106 print >>sys.stderr, '# %s: rebasing %s -> %s' % \
107 (project.relpath, cb, upbranch.LocalMerge)
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400108
Joe Hansche5e572342012-03-05 11:41:19 -0500109 needs_stash = False
110 if opt.auto_stash:
111 stash_args = ["update-index", "--refresh", "-q"]
112
113 if GitCommand(project, stash_args).Wait() != 0:
114 needs_stash = True
115 # Dirty index, requires stash...
116 stash_args = ["stash"]
117
118 if GitCommand(project, stash_args).Wait() != 0:
119 return -1
120
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400121 if GitCommand(project, args).Wait() != 0:
122 return -1
Joe Hansche5e572342012-03-05 11:41:19 -0500123
124 if needs_stash:
125 stash_args.append('pop')
126 stash_args.append('--quiet')
127 if GitCommand(project, stash_args).Wait() != 0:
128 return -1