blob: 7c8e9389138f6f839c510a0c89b4f1b834ee47cc [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')
55
Daniel Sandler9e426aa2010-04-01 10:42:33 -040056 def Execute(self, opt, args):
57 all = self.GetProjects(args)
58 one_project = len(all) == 1
59
60 if opt.interactive and not one_project:
61 print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
62 return -1
63
64 for project in all:
65 cb = project.CurrentBranch
66 if not cb:
67 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070068 print >>sys.stderr, "error: project %s has a detatched HEAD" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040069 return -1
70 # ignore branches with detatched HEADs
71 continue
72
73 upbranch = project.GetBranch(cb)
74 if not upbranch.LocalMerge:
75 if one_project:
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070076 print >>sys.stderr, "error: project %s does not track any remote branches" % project.relpath
Daniel Sandler9e426aa2010-04-01 10:42:33 -040077 return -1
78 # ignore branches without remotes
79 continue
80
Daniel Sandler9e426aa2010-04-01 10:42:33 -040081 args = ["rebase"]
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -070082
83 if opt.whitespace:
84 args.append('--whitespace=%s' % opt.whitespace)
85
86 if opt.quiet:
87 args.append('--quiet')
88
89 if opt.force_rebase:
90 args.append('--force-rebase')
91
92 if opt.no_ff:
93 args.append('--no-ff')
94
95 if opt.autosquash:
96 args.append('--autosquash')
97
Daniel Sandler9e426aa2010-04-01 10:42:33 -040098 if opt.interactive:
99 args.append("-i")
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700100
101 args.append(upbranch.LocalMerge)
102
103 print >>sys.stderr, '# %s: rebasing %s -> %s' % \
104 (project.relpath, cb, upbranch.LocalMerge)
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400105
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400106 if GitCommand(project, args).Wait() != 0:
107 return -1