blob: 2c1752d7c448d242e2fdc28849d0cf07d2df2e27 [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
Daniel Sandler9e426aa2010-04-01 10:42:33 -040020
21class Rebase(Command):
22 common = True
23 helpSummary = "Rebase local branches on upstream branch"
24 helpUsage = """
25%prog {[<project>...] | -i <project>...}
26"""
27 helpDescription = """
28'%prog' uses git rebase to move local changes in the current topic branch to
29the HEAD of the upstream history, useful when you have made commits in a topic
30branch but need to incorporate new upstream changes "underneath" them.
31"""
32
33 def _Options(self, p):
34 p.add_option('-i', '--interactive',
35 dest="interactive", action="store_true",
36 help="interactive rebase (single project only)")
37
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070038 p.add_option('-f', '--force-rebase',
39 dest='force_rebase', action='store_true',
40 help='Pass --force-rebase to git rebase')
41 p.add_option('--no-ff',
42 dest='no_ff', action='store_true',
43 help='Pass --no-ff to git rebase')
44 p.add_option('-q', '--quiet',
45 dest='quiet', action='store_true',
46 help='Pass --quiet to git rebase')
47 p.add_option('--autosquash',
48 dest='autosquash', action='store_true',
49 help='Pass --autosquash to git rebase')
50 p.add_option('--whitespace',
51 dest='whitespace', action='store', metavar='WS',
52 help='Pass --whitespace to git rebase')
Joe Hansche5e572342012-03-05 11:41:19 -050053 p.add_option('--auto-stash',
54 dest='auto_stash', action='store_true',
55 help='Stash local modifications before starting')
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070056
Daniel Sandler9e426aa2010-04-01 10:42:33 -040057 def Execute(self, opt, args):
58 all = self.GetProjects(args)
59 one_project = len(all) == 1
60
61 if opt.interactive and not one_project:
62 print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
63 return -1
64
65 for project in all:
66 cb = project.CurrentBranch
67 if not cb:
68 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070069 print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040070 return -1
71 # ignore branches with detatched HEADs
72 continue
73
74 upbranch = project.GetBranch(cb)
75 if not upbranch.LocalMerge:
76 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070077 print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040078 return -1
79 # ignore branches without remotes
80 continue
81
Daniel Sandler9e426aa2010-04-01 10:42:33 -040082 args = ["rebase"]
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070083
84 if opt.whitespace:
85 args.append('--whitespace=%s' % opt.whitespace)
86
87 if opt.quiet:
88 args.append('--quiet')
89
90 if opt.force_rebase:
91 args.append('--force-rebase')
92
93 if opt.no_ff:
94 args.append('--no-ff')
95
96 if opt.autosquash:
97 args.append('--autosquash')
98
Daniel Sandler9e426aa2010-04-01 10:42:33 -040099 if opt.interactive:
100 args.append("-i")
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400101
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700102 args.append(upbranch.LocalMerge)
103
104 print >>sys.stderr, '# %s: rebasing %s -> %s' % \
105 (project.relpath, cb, upbranch.LocalMerge)
106
Joe Hansche5e572342012-03-05 11:41:19 -0500107 needs_stash = False
108 if opt.auto_stash:
109 stash_args = ["update-index", "--refresh", "-q"]
110
111 if GitCommand(project, stash_args).Wait() != 0:
112 needs_stash = True
113 # Dirty index, requires stash...
114 stash_args = ["stash"]
115
116 if GitCommand(project, stash_args).Wait() != 0:
117 return -1
118
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400119 if GitCommand(project, args).Wait() != 0:
120 return -1
Joe Hansche5e572342012-03-05 11:41:19 -0500121
122 if needs_stash:
123 stash_args.append('pop')
124 stash_args.append('--quiet')
125 if GitCommand(project, stash_args).Wait() != 0:
126 return -1