]> git.wh0rd.org - home.git/commitdiff
git-rb-all: add live debug support to fix checked out worktrees
authorMike Frysinger <vapier@gentoo.org>
Thu, 29 Apr 2021 21:06:53 +0000 (17:06 -0400)
committerMike Frysinger <vapier@gentoo.org>
Thu, 29 Apr 2021 21:06:53 +0000 (17:06 -0400)
.bin/git-rb-all

index a2f7922a9c84358ed6edca4bcb612b3fcfd0a547..b44b449def815e1cb6bdb0249c1595d46ba432a6 100755 (executable)
@@ -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