From 237b84e76cc330406323bca01ecc44b828291bf5 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Wed, 19 Feb 2025 11:45:40 -0800 Subject: [PATCH] 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 Reviewed-by: Josip Sokcevic Auto-Submit: Paul Irish Reviewed-by: Gavin Mak --- git_rebase_update.py | 24 ++++++++++++++++++------ tests/git_rebase_update_test.py | 3 +++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/git_rebase_update.py b/git_rebase_update.py index 32e562761..d15b21118 100755 --- a/git_rebase_update.py +++ b/git_rebase_update.py @@ -18,10 +18,15 @@ from pprint import pformat import gclient_utils import git_common as git +import setup_color + +from third_party import colorama STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' 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(): """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)) +def format_branch_name(branch): + return BRIGHT + branch + RESET + def rebase_branch(branch, parent, start_hash, no_squash): logging.debug('considering %s(%s) -> %s(%s) : %s', branch, 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: # Try a plain rebase first - print('Rebasing:', branch) + print('Rebasing:', format_branch_name(branch)) consider_squashing = git.get_num_commits(branch) != 1 and not ( no_squash) rebase_ret = git.rebase(parent, @@ -180,7 +188,10 @@ def rebase_branch(branch, parent, start_hash, no_squash): if not consider_squashing: print(mid_rebase_message) return False - print("Failed! Attempting to squash", branch, "...", end=' ') + print("Failed! Attempting to squash", + format_branch_name(branch), + "...", + end=' ') sys.stdout.flush() squash_branch = branch + "_squash_attempt" git.run('checkout', '-b', squash_branch) @@ -225,7 +236,7 @@ def rebase_branch(branch, parent, start_hash, no_squash): print(mid_rebase_message) return False else: - print('%s up-to-date' % branch) + print('%s up-to-date' % format_branch_name(branch)) git.remove_merge_base(branch) git.get_or_create_merge_base(branch) @@ -324,7 +335,7 @@ def main(args=None): if branches_to_rebase: skipped = set(skipped).intersection(branches_to_rebase) 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: fetch_remotes(branch_tree) @@ -345,7 +356,7 @@ def main(args=None): if branches_to_rebase and branch not in branches_to_rebase: continue if git.is_dormant(branch): - print('Skipping dormant branch', branch) + print('Skipping dormant branch', format_branch_name(branch)) else: ret = rebase_branch(branch, parent, merge_base[branch], opts.no_squash) @@ -369,7 +380,7 @@ def main(args=None): print() print('The following branches could not be cleanly rebased:') for branch in unrebased_branches: - print(' %s' % branch) + print(' %s' % format_branch_name(branch)) if not retcode: if not opts.keep_empty: @@ -406,6 +417,7 @@ def main(args=None): if __name__ == '__main__': # pragma: no cover + setup_color.init() try: sys.exit(main()) except KeyboardInterrupt: diff --git a/tests/git_rebase_update_test.py b/tests/git_rebase_update_test.py index a69eb88fb..663b4f4d0 100755 --- a/tests/git_rebase_update_test.py +++ b/tests/git_rebase_update_test.py @@ -6,6 +6,7 @@ import os import sys +from unittest import mock DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 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.to_schema_refs += ['origin/main'] + mock.patch('git_rebase_update.RESET', '').start() + mock.patch('git_rebase_update.BRIGHT', '').start() def tearDown(self): self.origin.nuke()