| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Tool to update all branches to have the latest changes from their upstreams. | 7 Tool to update all branches to have the latest changes from their upstreams. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import argparse | 10 import argparse |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 print 'Nothing to fetch.' | 80 print 'Nothing to fetch.' |
| 81 else: | 81 else: |
| 82 git.run_with_stderr('fetch', *fetch_args, stdout=sys.stdout, | 82 git.run_with_stderr('fetch', *fetch_args, stdout=sys.stdout, |
| 83 stderr=sys.stderr) | 83 stderr=sys.stderr) |
| 84 | 84 |
| 85 | 85 |
| 86 def remove_empty_branches(branch_tree): | 86 def remove_empty_branches(branch_tree): |
| 87 tag_set = git.tags() | 87 tag_set = git.tags() |
| 88 ensure_root_checkout = git.once(lambda: git.run('checkout', git.root())) | 88 ensure_root_checkout = git.once(lambda: git.run('checkout', git.root())) |
| 89 | 89 |
| 90 deletions = set() | 90 deletions = {} |
| 91 reparents = {} |
| 91 downstreams = collections.defaultdict(list) | 92 downstreams = collections.defaultdict(list) |
| 92 for branch, parent in git.topo_iter(branch_tree, top_down=False): | 93 for branch, parent in git.topo_iter(branch_tree, top_down=False): |
| 93 downstreams[parent].append(branch) | 94 downstreams[parent].append(branch) |
| 94 | 95 |
| 96 # If branch and parent have the same state, then branch has to be marked |
| 97 # for deletion and its children and grand-children reparented to parent. |
| 95 if git.hash_one(branch) == git.hash_one(parent): | 98 if git.hash_one(branch) == git.hash_one(parent): |
| 96 ensure_root_checkout() | 99 ensure_root_checkout() |
| 97 | 100 |
| 98 logging.debug('branch %s merged to %s', branch, parent) | 101 logging.debug('branch %s merged to %s', branch, parent) |
| 99 | 102 |
| 103 # Mark branch for deletion while remembering the ordering, then add all |
| 104 # its children as grand-children of its parent and record reparenting |
| 105 # information if necessary. |
| 106 deletions[branch] = len(deletions) |
| 107 |
| 100 for down in downstreams[branch]: | 108 for down in downstreams[branch]: |
| 101 if down in deletions: | 109 if down in deletions: |
| 102 continue | 110 continue |
| 103 | 111 |
| 104 if parent in tag_set: | 112 # Record the new and old parent for down, or update such a record |
| 105 git.set_branch_config(down, 'remote', '.') | 113 # if it already exists. Keep track of the ordering so that reparenting |
| 106 git.set_branch_config(down, 'merge', 'refs/tags/%s' % parent) | 114 # happen in topological order. |
| 107 print ('Reparented %s to track %s [tag] (was tracking %s)' | 115 downstreams[parent].append(down) |
| 108 % (down, parent, branch)) | 116 if down not in reparents: |
| 117 reparents[down] = (len(reparents), parent, branch) |
| 109 else: | 118 else: |
| 110 git.run('branch', '--set-upstream-to', parent, down) | 119 order, _, old_parent = reparents[down] |
| 111 print ('Reparented %s to track %s (was tracking %s)' | 120 reparents[down] = (order, parent, old_parent) |
| 112 % (down, parent, branch)) | |
| 113 | 121 |
| 114 deletions.add(branch) | 122 # Apply all reparenting recorded, in order. |
| 115 print git.run('branch', '-d', branch) | 123 for branch, value in sorted(reparents.iteritems(), key=lambda x:x[1][0]): |
| 124 _, parent, old_parent = value |
| 125 if parent in tag_set: |
| 126 git.set_branch_config(branch, 'remote', '.') |
| 127 git.set_branch_config(branch, 'merge', 'refs/tags/%s' % parent) |
| 128 print ('Reparented %s to track %s [tag] (was tracking %s)' |
| 129 % (branch, parent, old_parent)) |
| 130 else: |
| 131 git.run('branch', '--set-upstream-to', parent, branch) |
| 132 print ('Reparented %s to track %s (was tracking %s)' |
| 133 % (branch, parent, old_parent)) |
| 134 |
| 135 # Apply all deletions recorded, in order. |
| 136 for branch, _ in sorted(deletions.iteritems(), key=lambda x: x[1]): |
| 137 print git.run('branch', '-d', branch) |
| 116 | 138 |
| 117 | 139 |
| 118 def rebase_branch(branch, parent, start_hash): | 140 def rebase_branch(branch, parent, start_hash): |
| 119 logging.debug('considering %s(%s) -> %s(%s) : %s', | 141 logging.debug('considering %s(%s) -> %s(%s) : %s', |
| 120 branch, git.hash_one(branch), parent, git.hash_one(parent), | 142 branch, git.hash_one(branch), parent, git.hash_one(parent), |
| 121 start_hash) | 143 start_hash) |
| 122 | 144 |
| 123 # If parent has FROZEN commits, don't base branch on top of them. Instead, | 145 # If parent has FROZEN commits, don't base branch on top of them. Instead, |
| 124 # base branch on top of whatever commit is before them. | 146 # base branch on top of whatever commit is before them. |
| 125 back_ups = 0 | 147 back_ups = 0 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 319 |
| 298 return retcode | 320 return retcode |
| 299 | 321 |
| 300 | 322 |
| 301 if __name__ == '__main__': # pragma: no cover | 323 if __name__ == '__main__': # pragma: no cover |
| 302 try: | 324 try: |
| 303 sys.exit(main()) | 325 sys.exit(main()) |
| 304 except KeyboardInterrupt: | 326 except KeyboardInterrupt: |
| 305 sys.stderr.write('interrupted\n') | 327 sys.stderr.write('interrupted\n') |
| 306 sys.exit(1) | 328 sys.exit(1) |
| OLD | NEW |