diff mbox series

[v2,2/4] ez: also check remote-tracking branches when ENROLL_BASE is not a branch

Message ID 20230219-allow-remote-branches-as-base-v2-2-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
When invoking 'b4 prep --enroll ENROLL_BASE' with a revision ENROLL_BASE
that does not correspond to a branch, ez.py::start_new_series tries to
find branches where the commit corresponding to the given revision is
found. If there is only one such branch, it is used as the
'base-branch'.

The function 'git_branch_contains' used to find where the commit lives
invokes 'git branch --contains', so only local branches are checked.
This means that if the given revision corresponds to a remote-tracking
branch, git_branch_contains won't recognize it and we will hit:

    CRITICAL: No other branch contains %s: cannot use as fork base

Ensure that remote-tracking branches are also checked by adding an
optional 'checkall' argument to 'git_branch_contains', that in turns
adds a '--all' argument to the 'git branch' invocation, such that both
local and remote-tracking branches are checked. Add that argument to the
call in 'start_new_series'.

An alternative would be to unconditionnally invoke 'git branch' with
'--all' in 'git_branch_contains', but this function is also called in
pr.py::main and ty.py::auto_locate_pr to check if the tip commit of the
given pull requests already exists in a local branch, and it seems safer
to not change behaviour for these two functions.
---
 b4/__init__.py | 4 +++-
 b4/ez.py       | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/b4/__init__.py b/b4/__init__.py
index 3138f3c..78418c1 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -2986,8 +2986,10 @@  def git_revparse_tag(gitdir: Optional[str], tagname: str) -> Optional[str]:
     return out.strip()
 
 
-def git_branch_contains(gitdir: Optional[str], commit_id: str) -> List[str]:
+def git_branch_contains(gitdir: Optional[str], commit_id: str, checkall: bool = False) -> List[str]:
     gitargs = ['branch', '--format=%(refname:short)', '--contains', commit_id]
+    if checkall:
+        gitargs.append('--all')
     lines = git_get_command_lines(gitdir, gitargs)
     return lines
 
diff --git a/b4/ez.py b/b4/ez.py
index c3b0237..ce080f1 100644
--- a/b4/ez.py
+++ b/b4/ez.py
@@ -390,7 +390,7 @@  def start_new_series(cmdargs: argparse.Namespace) -> None:
                 raise RuntimeError('Object %s not found' % enroll_base)
             forkpoint = out.strip()
             # check branches where this object lives
-            heads = b4.git_branch_contains(None, forkpoint)
+            heads = b4.git_branch_contains(None, forkpoint, checkall=True)
             if mybranch not in heads:
                 logger.critical('CRITICAL: object %s does not exist on current branch', enroll_base)
                 sys.exit(1)