Chromium Code Reviews| Index: git_rebase_update.py |
| diff --git a/git_rebase_update.py b/git_rebase_update.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..3646498f628e89945ae4cfa4ae036bcc37a41ea0 |
| --- /dev/null |
| +++ b/git_rebase_update.py |
| @@ -0,0 +1,227 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
|
agable
2014/03/21 01:14:21
docstrong
|
| +import argparse |
| +import collections |
| +import logging |
| +import sys |
| + |
| +from pprint import pformat |
| + |
| +from git_common import run, root, tags, topo_iter, once, hash_one, rebase |
|
agable
2014/03/21 01:14:21
Honestly might be more reasonable to do
import git
|
| +from git_common import get_or_create_merge_base, squash_current_branch, run_both |
| +from git_common import get_branch_tree, branches, remove_merge_base |
| +from git_common import current_branch, set_config, config |
| + |
| +from git_freezer import freeze, thaw |
| + |
| + |
| +def find_return_branch(): |
| + """Finds the branch which we should return to after rebase-update completes |
| + its entire action without any conflicts. |
| + """ |
| + STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' |
| + return_branch = config(STARTING_BRANCH_KEY) |
| + if return_branch is None: |
| + return_branch = current_branch() |
| + if return_branch != 'HEAD': |
| + set_config(STARTING_BRANCH_KEY, return_branch) |
| + |
| + return return_branch |
| + |
| + |
| +def check_for_rebase(): |
| + from git_common import in_rebase |
| + |
| + if in_rebase(): |
| + # TODO(iannucci): Be able to resume rebase with flags like --continue, |
| + # etc. |
| + print ( |
| + 'Rebase in progress. Please complete the rebase before running ' |
| + '`git rebase-update`.' |
| + ) |
| + sys.exit(1) |
| + |
| + |
| +def fetch_remotes(branch_tree): |
| + """Fetches all remotes which are needed to update |branch_tree|.""" |
| + |
| + fetch_tags = False |
| + remotes = set() |
| + tag_set = tags() |
| + for parent in branch_tree.itervalues(): |
| + if parent in tag_set: |
| + fetch_tags = True |
| + else: |
| + full_ref = run('rev-parse', '--symbolic-full-name', parent) |
| + if full_ref.startswith('refs/remotes'): |
| + parts = full_ref.split('/') |
| + remote_name = parts[2] |
| + remotes.add(remote_name) |
| + |
| + fetch_args = [] |
| + if fetch_tags: |
| + fetch_args.append('--tags') |
| + fetch_args.append('--multiple') |
| + fetch_args.extend(remotes) |
| + # TODO(iannucci): Should we fetch git-svn? |
| + |
| + if not fetch_args: # pragma: no cover |
| + print 'Nothing to fetch.' |
| + else: |
| + out, err = run_both('fetch', *fetch_args) |
| + for data, stream in zip((out, err), (sys.stdout, sys.stderr)): |
| + if data: |
| + print >> stream, data |
| + |
| + |
| +def remove_empty_branches(branch_tree): |
| + tag_set = tags() |
| + ensure_root_checkout = once(lambda: run('checkout', root())) |
| + |
| + downstreams = collections.defaultdict(list) |
| + for branch, parent in topo_iter(branch_tree, top_down=False): |
| + downstreams[parent].append(branch) |
| + |
| + if hash_one(branch) == hash_one(parent): |
| + ensure_root_checkout() |
| + |
| + logging.debug('branch %s merged to %s', branch, parent) |
| + |
| + for down in downstreams[branch]: |
| + if parent in tag_set: |
| + set_config('branch.%s.remote', '.') |
| + set_config('branch.%s.merge', 'refs/tags/parent') |
| + print ('Reparented %s to track %s [tag] (was tracking %s)' |
| + % (down, parent, branch)) |
| + else: |
| + run('branch', '--set-upstream-to', parent, down) |
| + print ('Reparented %s to track %s (was tracking %s)' |
| + % (down, parent, branch)) |
| + |
| + print run('branch', '-d', branch) |
| + |
| + |
| +def rebase_branch(branch, parent, start_hash): |
| + logging.debug('considering %s(%s) -> %s(%s) : %s', |
| + branch, hash_one(branch), parent, hash_one(parent), start_hash) |
| + |
| + # If parent has FROZEN commits, don't base branch on top of them. Instead, |
| + # base branch on top of whatever commit is before them. |
| + back_ups = 0 |
| + orig_parent = parent |
| + while run('log', '-n1', '--format=%s', parent).startswith('FREEZE'): |
| + back_ups += 1 |
| + parent = run('rev-parse', parent+'~') |
| + |
| + if back_ups: |
| + logging.debug('Backed parent up by %d from %s to %s', |
| + back_ups, orig_parent, parent) |
| + |
| + if hash_one(parent) != start_hash: |
| + # Try a plain rebase first |
| + print 'Rebasing:', branch |
| + if not rebase(parent, start_hash, branch, abort=True).success: |
| + # TODO(iannucci): Find collapsible branches in a smarter way? |
| + # Maybe squashing will help? |
| + print "Failed! Attempting to squash", branch, "...", |
| + squash_branch = branch+"_squash_attempt" |
| + run('checkout', '-b', squash_branch) |
| + squash_current_branch(merge_base=start_hash) |
| + |
| + # Try to rebase the branch_squash_attempt branch to see if it's empty. |
| + squash_ret = rebase(parent, start_hash, squash_branch, abort=True) |
| + empty_rebase = hash_one(squash_branch) == hash_one(parent) |
| + run('checkout', branch) |
| + run('branch', '-D', squash_branch) |
| + if squash_ret.success and empty_rebase: |
| + print 'Success!' |
| + squash_current_branch(merge_base=start_hash) |
| + rebase(parent, start_hash, branch) |
| + else: |
| + # rebase and leave in mid-rebase state. |
| + |
| + # TODO(iannucci): Allow user to mark branch as 'dormant' so that |
| + # rebase-update can skip it. |
| + rebase(parent, start_hash, branch) |
| + print squash_ret.message |
| + print 'Failure :(' |
| + print 'Your working copy is in mid-rebase. Please completely resolve ' |
| + print 'and run `git rebase-update` again.' |
| + sys.exit(1) |
| + else: |
| + print '%s up-to-date' % branch |
| + |
| + remove_merge_base(branch) |
| + get_or_create_merge_base(branch) |
| + |
| + |
| +def main(args=None): |
| + try: |
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('--verbose', '-v', action='store_true') |
| + opts = parser.parse_args(args) |
| + |
| + if opts.verbose: # pragma: no cover |
| + logging.getLogger().setLevel(logging.DEBUG) |
| + |
| + # TODO(iannucci): snapshot all branches somehow, so we can implement |
| + # `git rebase-update --undo`. |
| + # * Perhaps just copy packed-refs + refs/ + logs/ to the side? |
| + # * commit them to a secret ref? |
| + # * Then we could view a summary of each run as a |
| + # `diff --stat` on that secret ref. |
| + |
| + check_for_rebase() |
| + |
| + return_branch = find_return_branch() |
| + |
| + if current_branch() == 'HEAD': |
| + if run('status', '--porcelain'): |
| + print 'Cannot rebase-update with detached head + uncommitted changes.' |
| + return 1 |
| + else: |
| + freeze() # just in case there are any local changes. |
| + |
| + skipped, branch_tree = get_branch_tree() |
| + for branch in skipped: |
| + print 'Skipping %s: No upstream specified' % branch |
| + |
| + fetch_remotes(branch_tree) |
| + |
| + branch_to_merge_base = {} |
| + for branch, parent in branch_tree.iteritems(): |
| + branch_to_merge_base[branch] = get_or_create_merge_base(branch, parent) |
| + |
| + print 'branch_tree: %s' % pformat(branch_tree) |
| + print 'branch_to_merge_base: %s' % pformat(branch_to_merge_base) |
| + |
| + # Rebase each branch starting with the root-most branches and working |
| + # towards the leaves. |
| + for branch, parent in topo_iter(branch_tree): |
| + rebase_branch(branch, parent, branch_to_merge_base[branch]) |
| + |
| + remove_empty_branches(branch_tree) |
| + |
| + # return_branch may not be there any more. |
| + if return_branch in branches(): |
| + run('checkout', return_branch) |
| + thaw() |
| + else: |
| + root_branch = root() |
| + if return_branch != 'HEAD': |
| + print ( |
| + "%r was merged with its parent, checking out %r instead." |
| + % (return_branch, root_branch) |
| + ) |
| + run('checkout', root_branch) |
| + except SystemExit as se: |
| + return se.code |
| + |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': # pragma: no cover |
| + sys.exit(main(sys.argv[1:])) |