blob: 44cdd2eb44e886d4ea39efff963a771c4b6c942a [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
40 def Execute(self, opt, args):
41 all = self.GetProjects(args)
42 one_project = len(all) == 1
43
44 if opt.interactive and not one_project:
45 print >>sys.stderr, 'error: interactive rebase not supported with multiple projects'
46 return -1
47
48 for project in all:
49 cb = project.CurrentBranch
50 if not cb:
51 if one_project:
52 print >>sys.stderr, "error: project %s has a detatched HEAD" % project.name
53 return -1
54 # ignore branches with detatched HEADs
55 continue
56
57 upbranch = project.GetBranch(cb)
58 if not upbranch.LocalMerge:
59 if one_project:
60 print >>sys.stderr, "error: project %s does not track any remote branches" % project.name
61 return -1
62 # ignore branches without remotes
63 continue
64
65 upstream = project.GetRevisionId()
66
67 args = ["rebase"]
68 if opt.interactive:
69 args.append("-i")
70 args.append(upstream)
71
72 print '# project %s: rebasing branch %s -> %s (%s)' % (
73 project.relpath, cb, upbranch.LocalMerge, upstream[0:7])
74 if GitCommand(project, args).Wait() != 0:
75 return -1