From 669b988637d89df5bb923db8034638a9286019da Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 29 Apr 2021 17:06:53 -0400 Subject: [PATCH] git-rb-all: add live debug support to fix checked out worktrees --- .bin/git-rb-all | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/.bin/git-rb-all b/.bin/git-rb-all index a2f7922..b44b449 100755 --- a/.bin/git-rb-all +++ b/.bin/git-rb-all @@ -16,6 +16,7 @@ assert sys.version_info >= (3, 6), f'Python 3.6+ required but found {sys.version PROG = os.path.basename(__file__) +DEBUG = False class Terminal: @@ -58,6 +59,12 @@ class Color: return cls.BAD + msg + cls.NORMAL +def dbg(msg): + """Print a debug |msg|.""" + if DEBUG: + print(msg, file=sys.stderr) + + def fatal(msg): """Show an error |msg| then exit.""" print(Color.bad(f'{PROG}: error: {msg}'), file=sys.stderr) @@ -70,7 +77,8 @@ def git(args, **kwargs): kwargs.setdefault('stdout', subprocess.PIPE) kwargs.setdefault('stderr', subprocess.STDOUT) kwargs.setdefault('encoding', 'utf-8') - #print('+', 'git', *args) + if DEBUG: + print('+', 'git', *args) return subprocess.run(['git'] + args, **kwargs) @@ -127,6 +135,9 @@ def get_parser(): parser.add_argument( '--catchup', action='store_true', help='run git-rb-catchup when rebasing') + parser.add_argument( + '-d', '--debug', action='store_true', + help='enable debug output') parser.add_argument( '-q', '--quiet', dest='git_options', action=AppendOption, default=['-q'], help='passthru to git rebase') @@ -138,8 +149,14 @@ def main(argv): parser = get_parser() opts = parser.parse_args(argv) + global DEBUG + DEBUG = opts.debug + # Switch to the top dir in case the working dir doesn't exist in every branch. - topdir = git(['rev-parse', '--show-toplevel']).stdout.strip() + try: + topdir = git(['rev-parse', '--show-toplevel'], stderr=subprocess.PIPE).stdout.strip() + except subprocess.CalledProcessError as e: + sys.exit(f'{os.path.basename(sys.argv[0])}: {Path.cwd()}:\n{e}\n{e.stderr.strip()}') os.chdir(topdir) # Example output: @@ -159,8 +176,11 @@ def main(argv): branch_width = max(branch_width, len(branch)) if head == '*': curr_state = branch - if not (worktreepath and worktreepath != topdir): local_count += 1 + elif not (worktreepath and worktreepath != topdir): + local_count += 1 + else: + dbg(f'{worktreepath}:{branch}: Skipping branch checked out in diff worktree') # If there are no branches to rebase, go silently. if not local_count: return 0 -- 2.39.5