diff mbox series

[v2,4/4] ez: let ENROLL_BASE default to '@{upstream}'

Message ID 20230219-allow-remote-branches-as-base-v2-4-8db83bda1403@gmail.com (mailing list archive)
State Accepted
Headers show
Series ez: allow remote-tracking branches as ENROLL_BASE | expand

Commit Message

Philippe Blain March 6, 2023, 6:02 p.m. UTC
It is a common worflow to set 'branch.<name>.merge' and
'branch.<name>.remote' to the remote-tracking branch that corresponds to
the default branch of the canonical repository of the project (say
'upstream/master'), such that 'git rebase -i' automatically "does the
right thing" and rebases only the commits in your feature branch that
are not reachable from 'upstream/master'. The revision syntax
'@{upstream}' (short: '@{u}') can be used to denote the configured
upstream branch of the current branch.

Support that workflow better by allowing the 'ENROLL_BASE' argument to
'b4 prep --enroll' to be ommitted and default to @{upstream}.

To avoid a RuntimeError when enroll_base is @{upstream} (either from the
default or because it was given litterally on the command line) and the
current branch has no configured upstream, add some error-checking to
the 'git rev-parse --verify --abbrev-ref' invocation along with a clear
message. Let other error situations be handled by the second 'git
rev-parse --verify' invocation later on.
---
 b4/command.py | 4 ++--
 b4/ez.py      | 6 +++++-
 2 files changed, 7 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/b4/command.py b/b4/command.py
index e7656a5..1ae73d2 100644
--- a/b4/command.py
+++ b/b4/command.py
@@ -290,8 +290,8 @@  def setup_parser() -> argparse.ArgumentParser:
     ag_prepn.add_argument('-F', '--from-thread', metavar='MSGID', dest='msgid',
                           help='When creating a new branch, use this thread')
     ag_prepe = sp_prep.add_argument_group('Enroll existing branch', 'Enroll existing branch for prep work')
-    ag_prepe.add_argument('-e', '--enroll', dest='enroll_base',
-                          help='Enroll current branch, using the passed tag, branch, or commit as fork base')
+    ag_prepe.add_argument('-e', '--enroll', dest='enroll_base', nargs='?', const='@{upstream}',
+                          help='Enroll current branch, using its configured upstream branch as fork base, or the passed tag, branch, or commit')
     sp_prep.set_defaults(func=cmd_prep)
 
     # b4 trailers
diff --git a/b4/ez.py b/b4/ez.py
index 53a4b2f..0a2936a 100644
--- a/b4/ez.py
+++ b/b4/ez.py
@@ -373,7 +373,11 @@  def start_new_series(cmdargs: argparse.Namespace) -> None:
         # Convert @{upstream}, @{push} to an abbreviated ref
         gitargs = ['rev-parse', '--abbrev-ref', '--verify', enroll_base]
         ecode, out = b4.git_run_command(None, gitargs)
-        if out:
+        if ecode > 0:
+            if enroll_base == '@{upstream}' or enroll_base == '@{u}':
+                logger.critical('CRITICAL: current branch has no configured upstream')
+                sys.exit(1)
+        elif out:
             enroll_base = out.strip()
         # Is it a branch?
         gitargs = ['show-ref', f'refs/heads/{enroll_base}', f'refs/remotes/{enroll_base}']