]> git.wh0rd.org - home.git/commitdiff
git-rb-all: handle empty trees & dirty trees better
authorMike Frysinger <vapier@gentoo.org>
Mon, 15 Feb 2021 12:10:33 +0000 (07:10 -0500)
committerMike Frysinger <vapier@gentoo.org>
Mon, 15 Feb 2021 12:10:33 +0000 (07:10 -0500)
.bin/git-rb-all

index 1d9ded3d9d0786b9cd6060223b1a0de4d94fd3ce..d2c6737b95c1f9a3bb6ca4f784106654124e40a9 100755 (executable)
@@ -45,6 +45,12 @@ class Color:
     BRACKET = _combine(Terminal.BOLD, Terminal.FG_BLUE)
 
 
     BRACKET = _combine(Terminal.BOLD, Terminal.FG_BLUE)
 
 
+def fatal(msg):
+    """Show an error |msg| then exit."""
+    print(f'{Color.BAD}{PROG}: error: {msg}{Color.NORMAL}', file=sys.stderr)
+    sys.exit(1)
+
+
 def git(args, **kwargs):
     """Run git."""
     kwargs.setdefault('check', True)
 def git(args, **kwargs):
     """Run git."""
     kwargs.setdefault('check', True)
@@ -105,8 +111,7 @@ def main(argv):
 
     # Skip if rebase is in progress.
     if rebase_inprogress():
 
     # Skip if rebase is in progress.
     if rebase_inprogress():
-        print(f'{Color.BAD}{PROG}: skipping due to active rebase{Color.NORMAL}')
-        return 1
+        fatal('skipping due to active rebase')
 
     # Switch to the top dir in case the working dir doesn't exist in every branch.
     topdir = git(['rev-parse', '--show-toplevel']).stdout.strip()
 
     # Switch to the top dir in case the working dir doesn't exist in every branch.
     topdir = git(['rev-parse', '--show-toplevel']).stdout.strip()
@@ -123,15 +128,21 @@ def main(argv):
 
     curr_state = None
     branch_width = 0
 
     curr_state = None
     branch_width = 0
+    local_count = 0
     for line in state:
         head, worktreepath, branch, tracking, ahead_behind = line.split('|')
         branch_width = max(branch_width, len(branch))
         if head == '*':
             curr_state = branch
     for line in state:
         head, worktreepath, branch, tracking, ahead_behind = line.split('|')
         branch_width = max(branch_width, len(branch))
         if head == '*':
             curr_state = branch
+        if not (worktreepath and worktreepath != topdir):
+            local_count += 1
+    # If there are no branches to rebase, go silently.
+    if not local_count:
+        return 0
     if not curr_state:
     if not curr_state:
-        print('Unable to resolve current branch', file=sys.stderr)
-        return 1
+        fatal('unable to resolve current branch')
 
 
+    is_dirty = None
     branches = {}
     for line in state:
         head, worktreepath, branch, tracking, ahead_behind = line.split('|')
     branches = {}
     for line in state:
         head, worktreepath, branch, tracking, ahead_behind = line.split('|')
@@ -158,6 +169,13 @@ def main(argv):
             print('fast forwarded')
             continue
 
             print('fast forwarded')
             continue
 
+        if is_dirty is None:
+            output = git(['status', '--porcelain']).stdout
+            is_dirty = any(x for x in output.splitlines() if '?' not in x[0:2])
+        if is_dirty:
+            print(f'{Color.BAD}unable to rebase: tree is dirty{Color.NORMAL}')
+            continue
+
         print(f'rebasing [{ahead_behind}] ', end='', flush=True)
         git(['checkout', '-q', branch])
         if opts.catchup:
         print(f'rebasing [{ahead_behind}] ', end='', flush=True)
         git(['checkout', '-q', branch])
         if opts.catchup: