Colorize branch names for rebase-update

To ease the UX of finding the branch name when there's a conflict to
resolve.

Change-Id: I34d16f2f7bd897a60e96b0391772b5d8985f3cb6
Bug: none
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6278647
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Auto-Submit: Paul Irish <paulirish@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
changes/47/6278647/6
Paul Irish 1 week ago committed by LUCI CQ
parent 000a266e69
commit 237b84e76c

@ -18,10 +18,15 @@ from pprint import pformat
import gclient_utils import gclient_utils
import git_common as git import git_common as git
import setup_color
from third_party import colorama
STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch'
STARTING_WORKDIR_KEY = 'depot-tools.rebase-update.starting-workdir' STARTING_WORKDIR_KEY = 'depot-tools.rebase-update.starting-workdir'
RESET = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL
BRIGHT = colorama.Style.BRIGHT
def find_return_branch_workdir(): def find_return_branch_workdir():
"""Finds the branch and working directory which we should return to after """Finds the branch and working directory which we should return to after
@ -140,6 +145,9 @@ def remove_empty_branches(branch_tree):
print(git.run('branch', '-d', branch)) print(git.run('branch', '-d', branch))
def format_branch_name(branch):
return BRIGHT + branch + RESET
def rebase_branch(branch, parent, start_hash, no_squash): def rebase_branch(branch, parent, start_hash, no_squash):
logging.debug('considering %s(%s) -> %s(%s) : %s', branch, logging.debug('considering %s(%s) -> %s(%s) : %s', branch,
git.hash_one(branch), parent, git.hash_one(parent), git.hash_one(branch), parent, git.hash_one(parent),
@ -160,7 +168,7 @@ def rebase_branch(branch, parent, start_hash, no_squash):
if git.hash_one(parent) != start_hash: if git.hash_one(parent) != start_hash:
# Try a plain rebase first # Try a plain rebase first
print('Rebasing:', branch) print('Rebasing:', format_branch_name(branch))
consider_squashing = git.get_num_commits(branch) != 1 and not ( consider_squashing = git.get_num_commits(branch) != 1 and not (
no_squash) no_squash)
rebase_ret = git.rebase(parent, rebase_ret = git.rebase(parent,
@ -180,7 +188,10 @@ def rebase_branch(branch, parent, start_hash, no_squash):
if not consider_squashing: if not consider_squashing:
print(mid_rebase_message) print(mid_rebase_message)
return False return False
print("Failed! Attempting to squash", branch, "...", end=' ') print("Failed! Attempting to squash",
format_branch_name(branch),
"...",
end=' ')
sys.stdout.flush() sys.stdout.flush()
squash_branch = branch + "_squash_attempt" squash_branch = branch + "_squash_attempt"
git.run('checkout', '-b', squash_branch) git.run('checkout', '-b', squash_branch)
@ -225,7 +236,7 @@ def rebase_branch(branch, parent, start_hash, no_squash):
print(mid_rebase_message) print(mid_rebase_message)
return False return False
else: else:
print('%s up-to-date' % branch) print('%s up-to-date' % format_branch_name(branch))
git.remove_merge_base(branch) git.remove_merge_base(branch)
git.get_or_create_merge_base(branch) git.get_or_create_merge_base(branch)
@ -324,7 +335,7 @@ def main(args=None):
if branches_to_rebase: if branches_to_rebase:
skipped = set(skipped).intersection(branches_to_rebase) skipped = set(skipped).intersection(branches_to_rebase)
for branch in skipped: for branch in skipped:
print('Skipping %s: No upstream specified' % branch) print('Skipping %s: No upstream specified' % format_branch_name(branch))
if not opts.no_fetch: if not opts.no_fetch:
fetch_remotes(branch_tree) fetch_remotes(branch_tree)
@ -345,7 +356,7 @@ def main(args=None):
if branches_to_rebase and branch not in branches_to_rebase: if branches_to_rebase and branch not in branches_to_rebase:
continue continue
if git.is_dormant(branch): if git.is_dormant(branch):
print('Skipping dormant branch', branch) print('Skipping dormant branch', format_branch_name(branch))
else: else:
ret = rebase_branch(branch, parent, merge_base[branch], ret = rebase_branch(branch, parent, merge_base[branch],
opts.no_squash) opts.no_squash)
@ -369,7 +380,7 @@ def main(args=None):
print() print()
print('The following branches could not be cleanly rebased:') print('The following branches could not be cleanly rebased:')
for branch in unrebased_branches: for branch in unrebased_branches:
print(' %s' % branch) print(' %s' % format_branch_name(branch))
if not retcode: if not retcode:
if not opts.keep_empty: if not opts.keep_empty:
@ -406,6 +417,7 @@ def main(args=None):
if __name__ == '__main__': # pragma: no cover if __name__ == '__main__': # pragma: no cover
setup_color.init()
try: try:
sys.exit(main()) sys.exit(main())
except KeyboardInterrupt: except KeyboardInterrupt:

@ -6,6 +6,7 @@
import os import os
import sys import sys
from unittest import mock
DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS_ROOT) sys.path.insert(0, DEPOT_TOOLS_ROOT)
@ -67,6 +68,8 @@ class GitRebaseUpdateTest(git_test_utils.GitRepoReadWriteTestBase):
self.repo.git('branch', '--set-upstream-to', 'origin/main', 'branch_G') self.repo.git('branch', '--set-upstream-to', 'origin/main', 'branch_G')
self.repo.to_schema_refs += ['origin/main'] self.repo.to_schema_refs += ['origin/main']
mock.patch('git_rebase_update.RESET', '').start()
mock.patch('git_rebase_update.BRIGHT', '').start()
def tearDown(self): def tearDown(self):
self.origin.nuke() self.origin.nuke()

Loading…
Cancel
Save