diff mbox series

[b4] ez: fall back to regular merge-base if --fork-point mode fails

Message ID 20240210-ez-merge-base-no-fork-point-v1-1-b3f82cdd3bee@gmail.com (mailing list archive)
State Accepted
Headers show
Series [b4] ez: fall back to regular merge-base if --fork-point mode fails | expand

Commit Message

Philippe Blain Feb. 10, 2024, 9:18 p.m. UTC
If the 'git merge-base --fork-point' invocation in
ez::get_base_forkpoint fails and returns nothing, we abort, saying
basebranch and mybranch have no common ancestors.

Depending on how the current branch was created, and the state of the
branch's reflog, it is possible for this invocation to fail even if the
current branch and the chosen base branch do share a common ancestor,
for reasons expplained in git-merge-base(1) [1].

Support that situation better by adding a second invocation, using the
regular merge-base syntax 'git merge-base mybranch basebranch', and only
fail if that second invocation also returns nothing.

[1] https://git-scm.com/docs/git-merge-base#_discussion_on_fork_point_mode

Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
---
 b4/ez.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)


---
base-commit: e790d97805add21c1c5fe22642e403b8c8889ed6
change-id: 20240210-ez-merge-base-no-fork-point-a8b51551ff36

Comments

Konstantin Ryabitsev Feb. 13, 2024, 6:56 p.m. UTC | #1
On Sat, 10 Feb 2024 16:18:16 -0500, Philippe Blain wrote:
> If the 'git merge-base --fork-point' invocation in
> ez::get_base_forkpoint fails and returns nothing, we abort, saying
> basebranch and mybranch have no common ancestors.
> 
> Depending on how the current branch was created, and the state of the
> branch's reflog, it is possible for this invocation to fail even if the
> current branch and the chosen base branch do share a common ancestor,
> for reasons expplained in git-merge-base(1) [1].
> 
> [...]

Applied, thanks!

[1/1] ez: fall back to regular merge-base if --fork-point mode fails
      commit: b7ccd0f51896e38b995fe0e36f2388f65535593c

Best regards,
diff mbox series

Patch

diff --git a/b4/ez.py b/b4/ez.py
index 7baff8f..bad8c4a 100644
--- a/b4/ez.py
+++ b/b4/ez.py
@@ -233,8 +233,11 @@  def get_base_forkpoint(basebranch: str, mybranch: Optional[str] = None) -> str:
     gitargs = ['merge-base', '--fork-point', basebranch]
     lines = b4.git_get_command_lines(None, gitargs)
     if not lines:
-        logger.critical('CRITICAL: Could not find common ancestor with %s', basebranch)
-        raise RuntimeError('Branches %s and %s have no common ancestors' % (basebranch, mybranch))
+        gitargs = ['merge-base', mybranch, basebranch]
+        lines = b4.git_get_command_lines(None, gitargs)
+        if not lines:
+            logger.critical('CRITICAL: Could not find common ancestor with %s', basebranch)
+            raise RuntimeError('Branches %s and %s have no common ancestors' % (basebranch, mybranch))
     forkpoint = lines[0]
     logger.debug('Fork-point between %s and %s is %s', mybranch, basebranch, forkpoint)