diff mbox series

[v2,3/4] ez: allow '@{upstream}' as ENROLL_BASE

Message ID 20230219-allow-remote-branches-as-base-v2-3-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
Invoking 'b4 prep --enroll @{u}' (or @{upstream}) does not work reliably
with the current code. In 'start_new_series', we take the code path that
checks on which branch the object given as ENROLL_BASE lives using 'git
branch --all --contains', so if multiple local branches build on top of
the current branch's upstream branch, they are all listed by this
invocation of 'git branch' and so we error with 'CRITICAL: Multiple
branches contain object @{u}, please pass a branch name as base'.

Fix that by invoking 'git rev-parse --abbrev-ref --verify ENROLL_BASE'
before the 'git show-branch' invocation. That command will convert @{u},
@{upstream} and @{push} [1-2] to an abbreviated ref like
'upstream/master'. If the given revision exist but can't be directly
converted to an abbreviated ref, the command will succeed and not output
anything, so only change ENROLL_BASE when we actually get an output. We
avoid checking the error code as we already invoke 'git rev-parse
--verify' later on to check if the object exists. We could use
'git_get_command_lines' instead, but we will check the error code in a
subsequent commit, so it makes more sense to use 'git_run_command'.

While at it, fix a "CRITICAL" error message that was missing a value for
the '%s' string formatting operator.

[1] https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltbranchnamegtupstreamemegemmasterupstreamememuem
[2] https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-emltbranchnamegtpushemegemmasterpushemempushem
---
 b4/ez.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/b4/ez.py b/b4/ez.py
index ce080f1..53a4b2f 100644
--- a/b4/ez.py
+++ b/b4/ez.py
@@ -370,6 +370,11 @@  def start_new_series(cmdargs: argparse.Namespace) -> None:
         seriesname = branchname
         slug = re.sub(r'\W+', '-', branchname).strip('-').lower()
         enroll_base = cmdargs.enroll_base
+        # 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:
+            enroll_base = out.strip()
         # Is it a branch?
         gitargs = ['show-ref', f'refs/heads/{enroll_base}', f'refs/remotes/{enroll_base}']
         lines = b4.git_get_command_lines(None, gitargs)
@@ -377,7 +382,7 @@  def start_new_series(cmdargs: argparse.Namespace) -> None:
             try:
                 forkpoint = get_base_forkpoint(enroll_base, mybranch)
             except RuntimeError as ex:
-                logger.critical('CRITICAL: could not use %s as enrollment base:')
+                logger.critical('CRITICAL: could not use %s as enrollment base:', enroll_base)
                 logger.critical('          %s', ex)
                 sys.exit(1)
             basebranch = enroll_base