Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
|
agable
2014/03/25 19:37:23
module docstring
iannucci
2014/03/26 01:39:49
Done.
| |
| 6 import argparse | |
| 7 import collections | |
| 8 import logging | |
| 9 import sys | |
| 10 | |
| 11 from pprint import pformat | |
| 12 | |
| 13 import git_common as git | |
| 14 | |
| 15 from git_freezer import freeze, thaw | |
|
agable
2014/03/25 19:37:23
See similar comment on another file -- I think the
iannucci
2014/03/26 01:39:49
Done.
| |
| 16 | |
| 17 | |
| 18 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' | |
| 19 | |
| 20 | |
| 21 def find_return_branch(): | |
| 22 """Finds the branch which we should return to after rebase-update completes | |
|
agable
2014/03/25 19:37:23
single line for first line of docstring
iannucci
2014/03/26 01:39:49
Done.
| |
| 23 its entire action without any conflicts. | |
| 24 """ | |
| 25 return_branch = git.config(STARTING_BRANCH_KEY) | |
| 26 if return_branch is None: | |
| 27 return_branch = git.current_branch() | |
| 28 if return_branch != 'HEAD': | |
| 29 git.set_config(STARTING_BRANCH_KEY, return_branch) | |
| 30 | |
| 31 return return_branch | |
| 32 | |
| 33 | |
| 34 def check_for_rebase(): | |
| 35 from git_common import in_rebase | |
|
agable
2014/03/25 19:37:23
remove import
iannucci
2014/03/26 01:39:49
oops, done
| |
| 36 | |
| 37 if in_rebase(): | |
|
agable
2014/03/25 19:37:23
git.in_rebase
iannucci
2014/03/26 01:39:49
Done.
| |
| 38 # TODO(iannucci): Be able to resume rebase with flags like --continue, | |
| 39 # etc. | |
| 40 print ( | |
|
agable
2014/03/25 19:37:23
This print is out of place. This method should eit
iannucci
2014/03/26 01:39:49
Function should not exist. Done.
| |
| 41 'Rebase in progress. Please complete the rebase before running ' | |
| 42 '`git rebase-update`.' | |
| 43 ) | |
| 44 return False | |
| 45 return True | |
| 46 | |
| 47 | |
| 48 def fetch_remotes(branch_tree): | |
| 49 """Fetches all remotes which are needed to update |branch_tree|.""" | |
| 50 | |
|
agable
2014/03/25 19:37:23
no newline
iannucci
2014/03/26 01:39:49
Done.
| |
| 51 fetch_tags = False | |
| 52 remotes = set() | |
| 53 tag_set = git.tags() | |
| 54 for parent in branch_tree.itervalues(): | |
| 55 if parent in tag_set: | |
| 56 fetch_tags = True | |
| 57 else: | |
| 58 full_ref = git.run('rev-parse', '--symbolic-full-name', parent) | |
| 59 if full_ref.startswith('refs/remotes'): | |
| 60 parts = full_ref.split('/') | |
| 61 remote_name = parts[2] | |
| 62 remotes.add(remote_name) | |
| 63 | |
| 64 fetch_args = [] | |
| 65 if fetch_tags: | |
| 66 # Need to fetch all because we don't know what remote the tag comes from :( | |
| 67 fetch_args.extend(('--tags', '--all')) | |
| 68 else: | |
| 69 fetch_args.append('--multiple') | |
| 70 fetch_args.extend(remotes) | |
| 71 # TODO(iannucci): Should we fetch git-svn? | |
| 72 | |
| 73 if not fetch_args: # pragma: no cover | |
| 74 print 'Nothing to fetch.' | |
| 75 else: | |
| 76 out, err = git.run_with_stderr('fetch', *fetch_args) | |
| 77 for data, stream in zip((out, err), (sys.stdout, sys.stderr)): | |
| 78 if data: | |
| 79 print >> stream, data | |
| 80 | |
| 81 | |
| 82 def remove_empty_branches(branch_tree): | |
| 83 tag_set = git.tags() | |
| 84 ensure_root_checkout = git.once(lambda: git.run('checkout', git.root())) | |
| 85 | |
| 86 downstreams = collections.defaultdict(list) | |
| 87 for branch, parent in git.topo_iter(branch_tree, top_down=False): | |
| 88 downstreams[parent].append(branch) | |
| 89 | |
| 90 if git.hash_one(branch) == git.hash_one(parent): | |
| 91 ensure_root_checkout() | |
| 92 | |
| 93 logging.debug('branch %s merged to %s', branch, parent) | |
| 94 | |
| 95 for down in downstreams[branch]: | |
| 96 if parent in tag_set: | |
| 97 git.set_config('branch.%s.remote' % down, '.') | |
|
agable
2014/03/25 19:37:23
You now have the set_branch_config methods
iannucci
2014/03/26 01:39:49
I knew I missed some!
| |
| 98 git.set_config('branch.%s.merge' % down, 'refs/tags/%s' % parent) | |
| 99 print ('Reparented %s to track %s [tag] (was tracking %s)' | |
| 100 % (down, parent, branch)) | |
| 101 else: | |
| 102 git.run('branch', '--set-upstream-to', parent, down) | |
| 103 print ('Reparented %s to track %s (was tracking %s)' | |
| 104 % (down, parent, branch)) | |
| 105 | |
| 106 print git.run('branch', '-d', branch) | |
| 107 | |
| 108 | |
| 109 def rebase_branch(branch, parent, start_hash): | |
| 110 logging.debug('considering %s(%s) -> %s(%s) : %s', | |
| 111 branch, git.hash_one(branch), parent, git.hash_one(parent), | |
| 112 start_hash) | |
| 113 | |
| 114 # If parent has FROZEN commits, don't base branch on top of them. Instead, | |
| 115 # base branch on top of whatever commit is before them. | |
| 116 back_ups = 0 | |
| 117 orig_parent = parent | |
| 118 while git.run('log', '-n1', '--format=%s', parent, '--').startswith('FREEZE'): | |
| 119 back_ups += 1 | |
| 120 parent = git.run('rev-parse', parent+'~') | |
| 121 | |
| 122 if back_ups: | |
| 123 logging.debug('Backed parent up by %d from %s to %s', | |
| 124 back_ups, orig_parent, parent) | |
| 125 | |
| 126 if git.hash_one(parent) != start_hash: | |
| 127 # Try a plain rebase first | |
| 128 print 'Rebasing:', branch | |
| 129 if not git.rebase(parent, start_hash, branch, abort=True).success: | |
| 130 # TODO(iannucci): Find collapsible branches in a smarter way? | |
| 131 print "Failed! Attempting to squash", branch, "...", | |
| 132 squash_branch = branch+"_squash_attempt" | |
| 133 git.run('checkout', '-b', squash_branch) | |
| 134 git.squash_current_branch(merge_base=start_hash) | |
| 135 | |
| 136 # Try to rebase the branch_squash_attempt branch to see if it's empty. | |
| 137 squash_ret = git.rebase(parent, start_hash, squash_branch, abort=True) | |
| 138 empty_rebase = git.hash_one(squash_branch) == git.hash_one(parent) | |
| 139 git.run('checkout', branch) | |
| 140 git.run('branch', '-D', squash_branch) | |
| 141 if squash_ret.success and empty_rebase: | |
| 142 print 'Success!' | |
| 143 git.squash_current_branch(merge_base=start_hash) | |
| 144 git.rebase(parent, start_hash, branch) | |
| 145 else: | |
| 146 # rebase and leave in mid-rebase state. | |
| 147 git.rebase(parent, start_hash, branch) | |
| 148 print squash_ret.message | |
| 149 print 'Squashing failed. You probably have a real merge conflict.' | |
|
agable
2014/03/25 19:37:23
Consider printing a textwrap.dedented triple-quote
iannucci
2014/03/26 01:39:49
Done.
| |
| 150 print | |
| 151 print 'Your working copy is in mid-rebase. Either:' | |
| 152 print ' * completely resolve like a normal git-rebase; OR' | |
| 153 print ' * abort the rebase and mark this branch as dormant: ' | |
| 154 print ' git config branch.%s.dormant true' % branch | |
| 155 print | |
| 156 print 'And then run `git rebase-update` again to resume.' | |
| 157 return False | |
| 158 else: | |
| 159 print '%s up-to-date' % branch | |
| 160 | |
| 161 git.remove_merge_base(branch) | |
| 162 git.get_or_create_merge_base(branch) | |
| 163 | |
| 164 return True | |
| 165 | |
| 166 | |
| 167 def main(args=()): | |
| 168 parser = argparse.ArgumentParser() | |
| 169 parser.add_argument('--verbose', '-v', action='store_true') | |
| 170 parser.add_argument('--no_fetch', '-n', action='store_true', | |
| 171 help='Skip fetching remotes.') | |
| 172 opts = parser.parse_args(args) | |
| 173 | |
| 174 if opts.verbose: # pragma: no cover | |
| 175 logging.getLogger().setLevel(logging.DEBUG) | |
| 176 | |
| 177 # TODO(iannucci): snapshot all branches somehow, so we can implement | |
| 178 # `git rebase-update --undo`. | |
| 179 # * Perhaps just copy packed-refs + refs/ + logs/ to the side? | |
| 180 # * commit them to a secret ref? | |
| 181 # * Then we could view a summary of each run as a | |
| 182 # `diff --stat` on that secret ref. | |
| 183 | |
| 184 if not check_for_rebase(): | |
| 185 return 1 | |
|
agable
2014/03/25 19:37:23
See other comment; should print here.
iannucci
2014/03/26 01:39:49
Done.
| |
| 186 | |
| 187 return_branch = find_return_branch() | |
| 188 | |
| 189 if git.current_branch() == 'HEAD': | |
| 190 if git.run('status', '--porcelain'): | |
| 191 print 'Cannot rebase-update with detached head + uncommitted changes.' | |
| 192 return 1 | |
| 193 else: | |
| 194 freeze() # just in case there are any local changes. | |
| 195 | |
| 196 skipped, branch_tree = git.get_branch_tree() | |
| 197 for branch in skipped: | |
| 198 print 'Skipping %s: No upstream specified' % branch | |
| 199 | |
| 200 if not opts.no_fetch: | |
| 201 fetch_remotes(branch_tree) | |
| 202 | |
| 203 merge_base = {} | |
| 204 for branch, parent in branch_tree.iteritems(): | |
| 205 merge_base[branch] = git.get_or_create_merge_base(branch, parent) | |
| 206 | |
| 207 logging.debug('branch_tree: %s' % pformat(branch_tree)) | |
| 208 logging.debug('merge_base: %s' % pformat(merge_base)) | |
| 209 | |
| 210 retcode = 0 | |
| 211 # Rebase each branch starting with the root-most branches and working | |
| 212 # towards the leaves. | |
| 213 for branch, parent in git.topo_iter(branch_tree): | |
| 214 if git.is_dormant(branch): | |
| 215 print 'Skipping dormant branch', branch | |
| 216 else: | |
| 217 ret = rebase_branch(branch, parent, merge_base[branch]) | |
| 218 if not ret: | |
| 219 retcode = 1 | |
| 220 break | |
| 221 | |
| 222 if not retcode: | |
| 223 remove_empty_branches(branch_tree) | |
| 224 | |
| 225 # return_branch may not be there any more. | |
| 226 if return_branch in git.branches(): | |
| 227 git.run('checkout', return_branch) | |
| 228 thaw() | |
| 229 else: | |
| 230 root_branch = git.root() | |
| 231 if return_branch != 'HEAD': | |
| 232 print ( | |
| 233 "%r was merged with its parent, checking out %r instead." | |
| 234 % (return_branch, root_branch) | |
| 235 ) | |
| 236 git.run('checkout', root_branch) | |
| 237 git.del_config(STARTING_BRANCH_KEY) | |
| 238 | |
| 239 return retcode | |
| 240 | |
| 241 | |
| 242 if __name__ == '__main__': # pragma: no cover | |
| 243 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |