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 <jarhar@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
changes/15/5068315/6
Joey Arhar 1 year ago committed by LUCI CQ
parent 911ccd864c
commit 0e40b92d9e

@ -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

@ -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)

Loading…
Cancel
Save