blob: 9bc4460cff3a8c136393c6c94f3e9221d7e0fc51 [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)
Mike Frysingera850ca22019-08-07 17:19:24 -040074 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040075
Mike Frysingerd5c306b2019-08-07 17:23:23 -040076 # Setup the common git rebase args that we use for all projects.
77 common_args = ['rebase']
78 if opt.whitespace:
79 common_args.append('--whitespace=%s' % opt.whitespace)
80 if opt.quiet:
81 common_args.append('--quiet')
82 if opt.force_rebase:
83 common_args.append('--force-rebase')
84 if opt.no_ff:
85 common_args.append('--no-ff')
86 if opt.autosquash:
87 common_args.append('--autosquash')
88 if opt.interactive:
89 common_args.append('-i')
90
David Pursehouse8a68ff92012-09-24 12:15:13 +090091 for project in all_projects:
Daniel Sandler9e426aa2010-04-01 10:42:33 -040092 cb = project.CurrentBranch
93 if not cb:
94 if one_project:
David Pursehouse2f9e7e42013-03-05 17:26:46 +090095 print("error: project %s has a detached HEAD" % project.relpath,
Sarah Owenscecd1d82012-11-01 22:59:27 -070096 file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -040097 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -040098 # ignore branches with detatched HEADs
99 continue
100
101 upbranch = project.GetBranch(cb)
102 if not upbranch.LocalMerge:
103 if one_project:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700104 print("error: project %s does not track any remote branches"
105 % project.relpath, file=sys.stderr)
Mike Frysingera850ca22019-08-07 17:19:24 -0400106 return 1
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400107 # ignore branches without remotes
108 continue
109
Mike Frysingerd5c306b2019-08-07 17:23:23 -0400110 args = common_args[:]
Xiaohui Chen0b4cb322016-01-27 14:33:51 -0800111 if opt.onto_manifest:
112 args.append('--onto')
113 args.append(project.revisionExpr)
114
Shawn O. Pearcea22f99a2010-07-15 17:40:41 -0700115 args.append(upbranch.LocalMerge)
116
Sarah Owenscecd1d82012-11-01 22:59:27 -0700117 print('# %s: rebasing %s -> %s'
118 % (project.relpath, cb, upbranch.LocalMerge), file=sys.stderr)
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400119
Joe Hansche5e572342012-03-05 11:41:19 -0500120 needs_stash = False
121 if opt.auto_stash:
122 stash_args = ["update-index", "--refresh", "-q"]
123
124 if GitCommand(project, stash_args).Wait() != 0:
125 needs_stash = True
126 # Dirty index, requires stash...
127 stash_args = ["stash"]
128
129 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysingera850ca22019-08-07 17:19:24 -0400130 return 1
Joe Hansche5e572342012-03-05 11:41:19 -0500131
Daniel Sandler9e426aa2010-04-01 10:42:33 -0400132 if GitCommand(project, args).Wait() != 0:
Mike Frysingera850ca22019-08-07 17:19:24 -0400133 return 1
Joe Hansche5e572342012-03-05 11:41:19 -0500134
135 if needs_stash:
136 stash_args.append('pop')
137 stash_args.append('--quiet')
138 if GitCommand(project, stash_args).Wait() != 0:
Mike Frysingera850ca22019-08-07 17:19:24 -0400139 return 1