2010-04-01 14:42:33 +00:00
|
|
|
# Copyright (C) 2010 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2019-09-23 23:14:13 +00:00
|
|
|
from color import Coloring
|
2010-04-01 14:42:33 +00:00
|
|
|
from command import Command
|
|
|
|
from git_command import GitCommand
|
|
|
|
|
2019-09-23 23:14:13 +00:00
|
|
|
|
|
|
|
class RebaseColoring(Coloring):
|
|
|
|
def __init__(self, config):
|
|
|
|
Coloring.__init__(self, config, 'rebase')
|
|
|
|
self.project = self.printer('project', attr='bold')
|
|
|
|
self.fail = self.printer('fail', fg='red')
|
|
|
|
|
|
|
|
|
2010-04-01 14:42:33 +00:00
|
|
|
class Rebase(Command):
|
2021-06-14 20:05:19 +00:00
|
|
|
COMMON = True
|
2010-04-01 14:42:33 +00:00
|
|
|
helpSummary = "Rebase local branches on upstream branch"
|
|
|
|
helpUsage = """
|
|
|
|
%prog {[<project>...] | -i <project>...}
|
|
|
|
"""
|
|
|
|
helpDescription = """
|
|
|
|
'%prog' uses git rebase to move local changes in the current topic branch to
|
|
|
|
the HEAD of the upstream history, useful when you have made commits in a topic
|
|
|
|
branch but need to incorporate new upstream changes "underneath" them.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _Options(self, p):
|
2021-04-13 18:57:40 +00:00
|
|
|
g = p.get_option_group('--quiet')
|
|
|
|
g.add_option('-i', '--interactive',
|
2020-02-12 05:58:39 +00:00
|
|
|
dest="interactive", action="store_true",
|
|
|
|
help="interactive rebase (single project only)")
|
2010-04-01 14:42:33 +00:00
|
|
|
|
2019-09-23 22:54:30 +00:00
|
|
|
p.add_option('--fail-fast',
|
|
|
|
dest='fail_fast', action='store_true',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='stop rebasing after first error is hit')
|
2010-07-16 00:40:41 +00:00
|
|
|
p.add_option('-f', '--force-rebase',
|
|
|
|
dest='force_rebase', action='store_true',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='pass --force-rebase to git rebase')
|
2010-07-16 00:40:41 +00:00
|
|
|
p.add_option('--no-ff',
|
2020-02-17 19:36:08 +00:00
|
|
|
dest='ff', default=True, action='store_false',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='pass --no-ff to git rebase')
|
2010-07-16 00:40:41 +00:00
|
|
|
p.add_option('--autosquash',
|
|
|
|
dest='autosquash', action='store_true',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='pass --autosquash to git rebase')
|
2010-07-16 00:40:41 +00:00
|
|
|
p.add_option('--whitespace',
|
|
|
|
dest='whitespace', action='store', metavar='WS',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='pass --whitespace to git rebase')
|
2012-03-05 16:41:19 +00:00
|
|
|
p.add_option('--auto-stash',
|
|
|
|
dest='auto_stash', action='store_true',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='stash local modifications before starting')
|
2016-01-27 22:33:51 +00:00
|
|
|
p.add_option('-m', '--onto-manifest',
|
|
|
|
dest='onto_manifest', action='store_true',
|
2021-05-04 12:06:36 +00:00
|
|
|
help='rebase onto the manifest version instead of upstream '
|
|
|
|
'HEAD (this helps to make sure the local tree stays '
|
|
|
|
'consistent if you previously synced to a manifest)')
|
2010-07-16 00:40:41 +00:00
|
|
|
|
2010-04-01 14:42:33 +00:00
|
|
|
def Execute(self, opt, args):
|
2012-09-24 03:15:13 +00:00
|
|
|
all_projects = self.GetProjects(args)
|
|
|
|
one_project = len(all_projects) == 1
|
2010-04-01 14:42:33 +00:00
|
|
|
|
|
|
|
if opt.interactive and not one_project:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('error: interactive rebase not supported with multiple projects',
|
|
|
|
file=sys.stderr)
|
2013-10-12 00:03:19 +00:00
|
|
|
if len(args) == 1:
|
|
|
|
print('note: project %s is mapped to more than one path' % (args[0],),
|
2020-02-12 05:58:39 +00:00
|
|
|
file=sys.stderr)
|
2019-08-07 21:19:24 +00:00
|
|
|
return 1
|
2010-04-01 14:42:33 +00:00
|
|
|
|
2019-08-07 21:23:23 +00:00
|
|
|
# Setup the common git rebase args that we use for all projects.
|
|
|
|
common_args = ['rebase']
|
|
|
|
if opt.whitespace:
|
|
|
|
common_args.append('--whitespace=%s' % opt.whitespace)
|
|
|
|
if opt.quiet:
|
|
|
|
common_args.append('--quiet')
|
|
|
|
if opt.force_rebase:
|
|
|
|
common_args.append('--force-rebase')
|
2020-02-17 19:36:08 +00:00
|
|
|
if not opt.ff:
|
2019-08-07 21:23:23 +00:00
|
|
|
common_args.append('--no-ff')
|
|
|
|
if opt.autosquash:
|
|
|
|
common_args.append('--autosquash')
|
|
|
|
if opt.interactive:
|
|
|
|
common_args.append('-i')
|
|
|
|
|
2019-09-23 23:14:13 +00:00
|
|
|
config = self.manifest.manifestProject.config
|
|
|
|
out = RebaseColoring(config)
|
|
|
|
out.redirect(sys.stdout)
|
|
|
|
|
2019-09-23 22:54:30 +00:00
|
|
|
ret = 0
|
2012-09-24 03:15:13 +00:00
|
|
|
for project in all_projects:
|
2019-09-23 22:54:30 +00:00
|
|
|
if ret and opt.fail_fast:
|
|
|
|
break
|
|
|
|
|
2010-04-01 14:42:33 +00:00
|
|
|
cb = project.CurrentBranch
|
|
|
|
if not cb:
|
|
|
|
if one_project:
|
2013-03-05 08:26:46 +00:00
|
|
|
print("error: project %s has a detached HEAD" % project.relpath,
|
2012-11-02 05:59:27 +00:00
|
|
|
file=sys.stderr)
|
2019-08-07 21:19:24 +00:00
|
|
|
return 1
|
2010-04-01 14:42:33 +00:00
|
|
|
# ignore branches with detatched HEADs
|
|
|
|
continue
|
|
|
|
|
|
|
|
upbranch = project.GetBranch(cb)
|
|
|
|
if not upbranch.LocalMerge:
|
|
|
|
if one_project:
|
2012-11-02 05:59:27 +00:00
|
|
|
print("error: project %s does not track any remote branches"
|
|
|
|
% project.relpath, file=sys.stderr)
|
2019-08-07 21:19:24 +00:00
|
|
|
return 1
|
2010-04-01 14:42:33 +00:00
|
|
|
# ignore branches without remotes
|
|
|
|
continue
|
|
|
|
|
2019-08-07 21:23:23 +00:00
|
|
|
args = common_args[:]
|
2016-01-27 22:33:51 +00:00
|
|
|
if opt.onto_manifest:
|
|
|
|
args.append('--onto')
|
|
|
|
args.append(project.revisionExpr)
|
|
|
|
|
2010-07-16 00:40:41 +00:00
|
|
|
args.append(upbranch.LocalMerge)
|
|
|
|
|
2019-09-23 23:14:13 +00:00
|
|
|
out.project('project %s: rebasing %s -> %s',
|
|
|
|
project.relpath, cb, upbranch.LocalMerge)
|
|
|
|
out.nl()
|
|
|
|
out.flush()
|
2010-07-16 00:40:41 +00:00
|
|
|
|
2012-03-05 16:41:19 +00:00
|
|
|
needs_stash = False
|
|
|
|
if opt.auto_stash:
|
|
|
|
stash_args = ["update-index", "--refresh", "-q"]
|
|
|
|
|
|
|
|
if GitCommand(project, stash_args).Wait() != 0:
|
|
|
|
needs_stash = True
|
|
|
|
# Dirty index, requires stash...
|
|
|
|
stash_args = ["stash"]
|
|
|
|
|
|
|
|
if GitCommand(project, stash_args).Wait() != 0:
|
2019-09-23 22:54:30 +00:00
|
|
|
ret += 1
|
|
|
|
continue
|
2012-03-05 16:41:19 +00:00
|
|
|
|
2010-04-01 14:42:33 +00:00
|
|
|
if GitCommand(project, args).Wait() != 0:
|
2019-09-23 22:54:30 +00:00
|
|
|
ret += 1
|
|
|
|
continue
|
2012-03-05 16:41:19 +00:00
|
|
|
|
|
|
|
if needs_stash:
|
|
|
|
stash_args.append('pop')
|
|
|
|
stash_args.append('--quiet')
|
|
|
|
if GitCommand(project, stash_args).Wait() != 0:
|
2019-09-23 22:54:30 +00:00
|
|
|
ret += 1
|
|
|
|
|
|
|
|
if ret:
|
2019-09-23 23:14:13 +00:00
|
|
|
out.fail('%i projects had errors', ret)
|
|
|
|
out.nl()
|
|
|
|
|
2019-09-23 22:54:30 +00:00
|
|
|
return ret
|