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