[email protected] | 97231b5 | 2014-03-26 06:54:55 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ |
| 7 | Tool to update all branches to have the latest changes from their upstreams. |
| 8 | """ |
| 9 | |
| 10 | import argparse |
| 11 | import collections |
| 12 | import logging |
| 13 | import sys |
| 14 | import textwrap |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 15 | import os |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 16 | |
[email protected] | 937159d | 2014-09-24 17:25:45 | [diff] [blame] | 17 | from fnmatch import fnmatch |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 18 | from pprint import pformat |
| 19 | |
| 20 | import git_common as git |
| 21 | |
| 22 | |
| 23 | STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 24 | STARTING_WORKDIR_KEY = 'depot-tools.rebase-update.starting-workdir' |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 25 | |
| 26 | |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 27 | def find_return_branch_workdir(): |
| 28 | """Finds the branch and working directory which we should return to after |
| 29 | rebase-update completes. |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 30 | |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 31 | These values may persist across multiple invocations of rebase-update, if |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 32 | rebase-update runs into a conflict mid-way. |
| 33 | """ |
agable | 7aa2ddd | 2016-06-21 14:47:00 | [diff] [blame] | 34 | return_branch = git.get_config(STARTING_BRANCH_KEY) |
| 35 | workdir = git.get_config(STARTING_WORKDIR_KEY) |
[email protected] | bf157b4 | 2014-04-12 00:14:41 | [diff] [blame] | 36 | if not return_branch: |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 37 | workdir = os.getcwd() |
| 38 | git.set_config(STARTING_WORKDIR_KEY, workdir) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 39 | return_branch = git.current_branch() |
| 40 | if return_branch != 'HEAD': |
| 41 | git.set_config(STARTING_BRANCH_KEY, return_branch) |
| 42 | |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 43 | return return_branch, workdir |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def fetch_remotes(branch_tree): |
| 47 | """Fetches all remotes which are needed to update |branch_tree|.""" |
| 48 | fetch_tags = False |
| 49 | remotes = set() |
| 50 | tag_set = git.tags() |
[email protected] | 937159d | 2014-09-24 17:25:45 | [diff] [blame] | 51 | fetchspec_map = {} |
agable | 7aa2ddd | 2016-06-21 14:47:00 | [diff] [blame] | 52 | all_fetchspec_configs = git.get_config_regexp(r'^remote\..*\.fetch') |
[email protected] | 0d9e59c | 2016-01-09 08:08:41 | [diff] [blame] | 53 | for fetchspec_config in all_fetchspec_configs: |
[email protected] | 937159d | 2014-09-24 17:25:45 | [diff] [blame] | 54 | key, _, fetchspec = fetchspec_config.partition(' ') |
| 55 | dest_spec = fetchspec.partition(':')[2] |
| 56 | remote_name = key.split('.')[1] |
| 57 | fetchspec_map[dest_spec] = remote_name |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 58 | for parent in branch_tree.itervalues(): |
| 59 | if parent in tag_set: |
| 60 | fetch_tags = True |
| 61 | else: |
| 62 | full_ref = git.run('rev-parse', '--symbolic-full-name', parent) |
[email protected] | 937159d | 2014-09-24 17:25:45 | [diff] [blame] | 63 | for dest_spec, remote_name in fetchspec_map.iteritems(): |
| 64 | if fnmatch(full_ref, dest_spec): |
| 65 | remotes.add(remote_name) |
| 66 | break |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 67 | |
| 68 | fetch_args = [] |
| 69 | if fetch_tags: |
| 70 | # Need to fetch all because we don't know what remote the tag comes from :( |
| 71 | # TODO(iannucci): assert that the tags are in the remote fetch refspec |
| 72 | fetch_args = ['--all'] |
| 73 | else: |
| 74 | fetch_args.append('--multiple') |
| 75 | fetch_args.extend(remotes) |
| 76 | # TODO(iannucci): Should we fetch git-svn? |
| 77 | |
| 78 | if not fetch_args: # pragma: no cover |
| 79 | print 'Nothing to fetch.' |
| 80 | else: |
[email protected] | 43f4ecc | 2014-05-08 19:22:47 | [diff] [blame] | 81 | git.run_with_stderr('fetch', *fetch_args, stdout=sys.stdout, |
| 82 | stderr=sys.stderr) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def remove_empty_branches(branch_tree): |
| 86 | tag_set = git.tags() |
| 87 | ensure_root_checkout = git.once(lambda: git.run('checkout', git.root())) |
| 88 | |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 89 | deletions = {} |
| 90 | reparents = {} |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 91 | downstreams = collections.defaultdict(list) |
| 92 | for branch, parent in git.topo_iter(branch_tree, top_down=False): |
| 93 | downstreams[parent].append(branch) |
| 94 | |
[email protected] | aa1dfda | 2015-12-04 19:09:32 | [diff] [blame] | 95 | # If branch and parent have the same tree, then branch has to be marked |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 96 | # for deletion and its children and grand-children reparented to parent. |
[email protected] | aa1dfda | 2015-12-04 19:09:32 | [diff] [blame] | 97 | if git.hash_one(branch+":") == git.hash_one(parent+":"): |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 98 | ensure_root_checkout() |
| 99 | |
| 100 | logging.debug('branch %s merged to %s', branch, parent) |
| 101 | |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 102 | # Mark branch for deletion while remembering the ordering, then add all |
| 103 | # its children as grand-children of its parent and record reparenting |
| 104 | # information if necessary. |
| 105 | deletions[branch] = len(deletions) |
| 106 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 107 | for down in downstreams[branch]: |
[email protected] | 512d97d | 2014-03-31 23:36:10 | [diff] [blame] | 108 | if down in deletions: |
| 109 | continue |
| 110 | |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 111 | # Record the new and old parent for down, or update such a record |
| 112 | # if it already exists. Keep track of the ordering so that reparenting |
| 113 | # happen in topological order. |
| 114 | downstreams[parent].append(down) |
| 115 | if down not in reparents: |
| 116 | reparents[down] = (len(reparents), parent, branch) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 117 | else: |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 118 | order, _, old_parent = reparents[down] |
| 119 | reparents[down] = (order, parent, old_parent) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 120 | |
[email protected] | d421f6a | 2015-12-02 18:13:54 | [diff] [blame] | 121 | # Apply all reparenting recorded, in order. |
| 122 | for branch, value in sorted(reparents.iteritems(), key=lambda x:x[1][0]): |
| 123 | _, parent, old_parent = value |
| 124 | if parent in tag_set: |
| 125 | git.set_branch_config(branch, 'remote', '.') |
| 126 | git.set_branch_config(branch, 'merge', 'refs/tags/%s' % parent) |
| 127 | print ('Reparented %s to track %s [tag] (was tracking %s)' |
| 128 | % (branch, parent, old_parent)) |
| 129 | else: |
| 130 | git.run('branch', '--set-upstream-to', parent, branch) |
| 131 | print ('Reparented %s to track %s (was tracking %s)' |
| 132 | % (branch, parent, old_parent)) |
| 133 | |
| 134 | # Apply all deletions recorded, in order. |
| 135 | for branch, _ in sorted(deletions.iteritems(), key=lambda x: x[1]): |
| 136 | print git.run('branch', '-d', branch) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def rebase_branch(branch, parent, start_hash): |
| 140 | logging.debug('considering %s(%s) -> %s(%s) : %s', |
| 141 | branch, git.hash_one(branch), parent, git.hash_one(parent), |
| 142 | start_hash) |
| 143 | |
| 144 | # If parent has FROZEN commits, don't base branch on top of them. Instead, |
| 145 | # base branch on top of whatever commit is before them. |
| 146 | back_ups = 0 |
| 147 | orig_parent = parent |
| 148 | while git.run('log', '-n1', '--format=%s', |
| 149 | parent, '--').startswith(git.FREEZE): |
| 150 | back_ups += 1 |
| 151 | parent = git.run('rev-parse', parent+'~') |
| 152 | |
| 153 | if back_ups: |
| 154 | logging.debug('Backed parent up by %d from %s to %s', |
| 155 | back_ups, orig_parent, parent) |
| 156 | |
| 157 | if git.hash_one(parent) != start_hash: |
| 158 | # Try a plain rebase first |
| 159 | print 'Rebasing:', branch |
[email protected] | 384039b | 2014-10-13 21:01:00 | [diff] [blame] | 160 | rebase_ret = git.rebase(parent, start_hash, branch, abort=True) |
| 161 | if not rebase_ret.success: |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 162 | # TODO(iannucci): Find collapsible branches in a smarter way? |
| 163 | print "Failed! Attempting to squash", branch, "...", |
Andrew Moylan | bfc4082 | 2017-10-25 00:23:36 | [diff] [blame] | 164 | sys.stdout.flush() |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 165 | squash_branch = branch+"_squash_attempt" |
| 166 | git.run('checkout', '-b', squash_branch) |
| 167 | git.squash_current_branch(merge_base=start_hash) |
| 168 | |
| 169 | # Try to rebase the branch_squash_attempt branch to see if it's empty. |
| 170 | squash_ret = git.rebase(parent, start_hash, squash_branch, abort=True) |
| 171 | empty_rebase = git.hash_one(squash_branch) == git.hash_one(parent) |
| 172 | git.run('checkout', branch) |
| 173 | git.run('branch', '-D', squash_branch) |
| 174 | if squash_ret.success and empty_rebase: |
| 175 | print 'Success!' |
| 176 | git.squash_current_branch(merge_base=start_hash) |
| 177 | git.rebase(parent, start_hash, branch) |
| 178 | else: |
[email protected] | 8b4900e | 2014-10-27 20:26:04 | [diff] [blame] | 179 | print "Failed!" |
| 180 | print |
| 181 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 182 | # rebase and leave in mid-rebase state. |
[email protected] | 384039b | 2014-10-13 21:01:00 | [diff] [blame] | 183 | # This second rebase attempt should always fail in the same |
| 184 | # way that the first one does. If it magically succeeds then |
| 185 | # something very strange has happened. |
| 186 | second_rebase_ret = git.rebase(parent, start_hash, branch) |
[email protected] | 8b4900e | 2014-10-27 20:26:04 | [diff] [blame] | 187 | if second_rebase_ret.success: # pragma: no cover |
| 188 | print "Second rebase succeeded unexpectedly!" |
| 189 | print "Please see: http://crbug.com/425696" |
| 190 | print "First rebased failed with:" |
| 191 | print rebase_ret.stderr |
| 192 | else: |
| 193 | print "Here's what git-rebase (squashed) had to say:" |
| 194 | print |
| 195 | print squash_ret.stdout |
| 196 | print squash_ret.stderr |
| 197 | print textwrap.dedent( |
| 198 | """\ |
| 199 | Squashing failed. You probably have a real merge conflict. |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 200 | |
[email protected] | 8b4900e | 2014-10-27 20:26:04 | [diff] [blame] | 201 | Your working copy is in mid-rebase. Either: |
| 202 | * completely resolve like a normal git-rebase; OR |
| 203 | * abort the rebase and mark this branch as dormant: |
| 204 | git config branch.%s.dormant true |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 205 | |
[email protected] | 8b4900e | 2014-10-27 20:26:04 | [diff] [blame] | 206 | And then run `git rebase-update` again to resume. |
| 207 | """ % branch) |
| 208 | return False |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 209 | else: |
| 210 | print '%s up-to-date' % branch |
| 211 | |
| 212 | git.remove_merge_base(branch) |
| 213 | git.get_or_create_merge_base(branch) |
| 214 | |
| 215 | return True |
| 216 | |
| 217 | |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 218 | def main(args=None): |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 219 | parser = argparse.ArgumentParser() |
| 220 | parser.add_argument('--verbose', '-v', action='store_true') |
[email protected] | 74374f9 | 2015-09-10 23:47:24 | [diff] [blame] | 221 | parser.add_argument('--keep-going', '-k', action='store_true', |
| 222 | help='Keep processing past failed rebases.') |
[email protected] | b9f2751 | 2014-08-08 15:52:33 | [diff] [blame] | 223 | parser.add_argument('--no_fetch', '--no-fetch', '-n', |
| 224 | action='store_true', |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 225 | help='Skip fetching remotes.') |
| 226 | opts = parser.parse_args(args) |
| 227 | |
| 228 | if opts.verbose: # pragma: no cover |
| 229 | logging.getLogger().setLevel(logging.DEBUG) |
| 230 | |
| 231 | # TODO(iannucci): snapshot all branches somehow, so we can implement |
| 232 | # `git rebase-update --undo`. |
| 233 | # * Perhaps just copy packed-refs + refs/ + logs/ to the side? |
| 234 | # * commit them to a secret ref? |
| 235 | # * Then we could view a summary of each run as a |
| 236 | # `diff --stat` on that secret ref. |
| 237 | |
| 238 | if git.in_rebase(): |
| 239 | # TODO(iannucci): Be able to resume rebase with flags like --continue, |
| 240 | # etc. |
| 241 | print ( |
| 242 | 'Rebase in progress. Please complete the rebase before running ' |
| 243 | '`git rebase-update`.' |
| 244 | ) |
| 245 | return 1 |
| 246 | |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 247 | return_branch, return_workdir = find_return_branch_workdir() |
| 248 | os.chdir(git.run('rev-parse', '--show-toplevel')) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 249 | |
| 250 | if git.current_branch() == 'HEAD': |
| 251 | if git.run('status', '--porcelain'): |
| 252 | print 'Cannot rebase-update with detached head + uncommitted changes.' |
| 253 | return 1 |
| 254 | else: |
| 255 | git.freeze() # just in case there are any local changes. |
| 256 | |
| 257 | skipped, branch_tree = git.get_branch_tree() |
| 258 | for branch in skipped: |
| 259 | print 'Skipping %s: No upstream specified' % branch |
| 260 | |
| 261 | if not opts.no_fetch: |
| 262 | fetch_remotes(branch_tree) |
| 263 | |
| 264 | merge_base = {} |
| 265 | for branch, parent in branch_tree.iteritems(): |
| 266 | merge_base[branch] = git.get_or_create_merge_base(branch, parent) |
| 267 | |
| 268 | logging.debug('branch_tree: %s' % pformat(branch_tree)) |
| 269 | logging.debug('merge_base: %s' % pformat(merge_base)) |
| 270 | |
| 271 | retcode = 0 |
[email protected] | 74374f9 | 2015-09-10 23:47:24 | [diff] [blame] | 272 | unrebased_branches = [] |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 273 | # Rebase each branch starting with the root-most branches and working |
| 274 | # towards the leaves. |
| 275 | for branch, parent in git.topo_iter(branch_tree): |
| 276 | if git.is_dormant(branch): |
| 277 | print 'Skipping dormant branch', branch |
| 278 | else: |
| 279 | ret = rebase_branch(branch, parent, merge_base[branch]) |
| 280 | if not ret: |
| 281 | retcode = 1 |
[email protected] | 74374f9 | 2015-09-10 23:47:24 | [diff] [blame] | 282 | |
| 283 | if opts.keep_going: |
| 284 | print '--keep-going set, continuing with next branch.' |
| 285 | unrebased_branches.append(branch) |
| 286 | if git.in_rebase(): |
| 287 | git.run_with_retcode('rebase', '--abort') |
| 288 | if git.in_rebase(): # pragma: no cover |
| 289 | print 'Failed to abort rebase. Something is really wrong.' |
| 290 | break |
| 291 | else: |
| 292 | break |
| 293 | |
| 294 | if unrebased_branches: |
| 295 | print |
| 296 | print 'The following branches could not be cleanly rebased:' |
| 297 | for branch in unrebased_branches: |
| 298 | print ' %s' % branch |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 299 | |
| 300 | if not retcode: |
| 301 | remove_empty_branches(branch_tree) |
| 302 | |
| 303 | # return_branch may not be there any more. |
| 304 | if return_branch in git.branches(): |
| 305 | git.run('checkout', return_branch) |
| 306 | git.thaw() |
| 307 | else: |
| 308 | root_branch = git.root() |
| 309 | if return_branch != 'HEAD': |
| 310 | print ( |
| 311 | "%r was merged with its parent, checking out %r instead." |
| 312 | % (return_branch, root_branch) |
| 313 | ) |
| 314 | git.run('checkout', root_branch) |
Robert Iannucci | c87e36a | 2017-01-24 01:16:54 | [diff] [blame] | 315 | |
| 316 | # return_workdir may also not be there any more. |
[email protected] | 196809e | 2015-06-12 02:10:04 | [diff] [blame] | 317 | if return_workdir: |
Robert Iannucci | c87e36a | 2017-01-24 01:16:54 | [diff] [blame] | 318 | try: |
| 319 | os.chdir(return_workdir) |
| 320 | except OSError as e: |
| 321 | print ( |
| 322 | "Unable to return to original workdir %r: %s" |
| 323 | % (return_workdir, e) |
| 324 | ) |
[email protected] | bf157b4 | 2014-04-12 00:14:41 | [diff] [blame] | 325 | git.set_config(STARTING_BRANCH_KEY, '') |
[email protected] | dabb78b | 2015-06-11 23:17:28 | [diff] [blame] | 326 | git.set_config(STARTING_WORKDIR_KEY, '') |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 327 | |
| 328 | return retcode |
| 329 | |
| 330 | |
| 331 | if __name__ == '__main__': # pragma: no cover |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 332 | try: |
| 333 | sys.exit(main()) |
| 334 | except KeyboardInterrupt: |
| 335 | sys.stderr.write('interrupted\n') |
| 336 | sys.exit(1) |