From 0e40b92d9ec7f75c76a831b72ad1842efa830275 Mon Sep 17 00:00:00 2001 From: Joey Arhar Date: Tue, 2 Jan 2024 14:15:31 +0000 Subject: [PATCH] Don't try squashing only one commit This patch makes git rebase-update only try squashing when rebasing fails if there is more than one commit present in the branch it is rebasing. Squashing adds an extra step for the user when rebasing fails, and when there is only one commit, squashing is guaranteed not to help. Fixed: 1434450 Change-Id: I1952c1172dbf6a44060ed2da2d79ec07d67893d4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5068315 Commit-Queue: Joey Arhar Reviewed-by: Josip Sokcevic --- git_common.py | 14 +++++++++----- git_rebase_update.py | 30 +++++++++++++++++++----------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/git_common.py b/git_common.py index 2f4d37acd..fe80c5f93 100644 --- a/git_common.py +++ b/git_common.py @@ -1137,6 +1137,14 @@ def _extract_git_tuple(version_string): return tuple(int(x) for x in version.split('.')) +def get_num_commits(branch): + base = get_or_create_merge_base(branch) + if base: + commits_list = run('rev-list', '--count', branch, '^%s' % base, '--') + return int(commits_list) or None + return None + + def get_branches_info(include_tracking_status): format_string = ( '--format=%(refname:short):%(objectname:short):%(upstream:short):') @@ -1156,11 +1164,7 @@ def get_branches_info(include_tracking_status): commits = None if include_tracking_status: - base = get_or_create_merge_base(branch) - if base: - commits_list = run('rev-list', '--count', branch, '^%s' % base, - '--') - commits = int(commits_list) or None + commits = get_num_commits(branch) behind_match = re.search(r'behind (\d+)', tracking_status) behind = int(behind_match.group(1)) if behind_match else None diff --git a/git_rebase_update.py b/git_rebase_update.py index 13be0d6ab..f587dd02a 100755 --- a/git_rebase_update.py +++ b/git_rebase_update.py @@ -161,9 +161,24 @@ def rebase_branch(branch, parent, start_hash): if git.hash_one(parent) != start_hash: # Try a plain rebase first print('Rebasing:', branch) - rebase_ret = git.rebase(parent, start_hash, branch, abort=True) + consider_squashing = git.get_num_commits(branch) != 1 + rebase_ret = git.rebase(parent, + start_hash, + branch, + abort=consider_squashing) if not rebase_ret.success: - # TODO(iannucci): Find collapsible branches in a smarter way? + mid_rebase_message = textwrap.dedent("""\ + Your working copy is in mid-rebase. Either: + * completely resolve like a normal git-rebase; OR + * abort the rebase and mark this branch as dormant: + git rebase --abort && \\ + git config branch.%s.dormant true + + And then run `git rebase-update -n` to resume. + """ % branch) + if not consider_squashing: + print(mid_rebase_message) + return False print("Failed! Attempting to squash", branch, "...", end=' ') sys.stdout.flush() squash_branch = branch + "_squash_attempt" @@ -205,15 +220,8 @@ def rebase_branch(branch, parent, start_hash): print( textwrap.dedent("""\ Squashing failed. You probably have a real merge conflict. - - Your working copy is in mid-rebase. Either: - * completely resolve like a normal git-rebase; OR - * abort the rebase and mark this branch as dormant: - git rebase --abort && \\ - git config branch.%s.dormant true - - And then run `git rebase-update -n` to resume. - """ % branch)) + """)) + print(mid_rebase_message) return False else: print('%s up-to-date' % branch)