Web lists-archives.com

[PATCH 3/3] checkout: allow -b/-B to work on a merge base




When we ran something like

    $ git checkout -b test master...

it would fail with the message

    fatal: Not a valid object name: 'master...'.

This was caused by the call to `create_branch` where `start_name` is
expected to be a valid rev. However, git-checkout allows the branch to
not be a valid rev in the case where we have "..." to specify getting
the merge base.

In the case where a branch with "..." is specified, use the oid instead
so that `start_name` is a valid rev.

Signed-off-by: Denton Liu <liu.denton@xxxxxxxxx>
---
 builtin/checkout.c         | 4 +++-
 t/t2018-checkout-branch.sh | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index ffa776c6e1..d99b3f3925 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1229,7 +1229,9 @@ static int parse_branchname_arg(int argc, const char **argv,
 	argv++;
 	argc--;
 
-	new_branch_info->name = arg;
+	new_branch_info->name = strstr(arg, "...") ?
+		xstrdup(oid_to_hex(rev)) :
+		arg;
 	setup_branch_path(new_branch_info);
 
 	if (!check_refname_format(new_branch_info->path, 0) &&
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index a3fa520d2e..d6ea556d84 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -65,7 +65,7 @@ test_expect_success 'checkout -b to a new branch, set to HEAD' '
 	do_checkout branch2
 '
 
-test_expect_failure 'checkout -b to a merge base' '
+test_expect_success 'checkout -b to a merge base' '
 	test_when_finished test_might_fail git branch -D branch2 &&
 	test_when_finished git checkout branch1 &&
 	git checkout -b branch2 branch1...
@@ -128,7 +128,7 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
 	do_checkout branch2 "" -B
 '
 
-test_expect_failure 'checkout -B to a merge base' '
+test_expect_success 'checkout -B to a merge base' '
 	git checkout branch1 &&
 	git branch -D branch2 &&
 
-- 
2.21.0.1033.g0e8cc1100c