git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Denton Liu <liu.denton@gmail.com>
Date
Apr 25, 2019, 21:10 UTC
Message-ID
<031780431d790c16b3862d6f155693e197bb74ed.1556226502.git.liu.denton@gmail.com>
In-Reply-To
<cover.1556226502.git.liu.denton@gmail.com>
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@gmail.com>
---
 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
Previous: Denton LiuNext: Junio C Hamano
Message 8 of 21 in “checkout: allow -b/-B to work on a merge base”
  1. 0/3 checkout: allow -b/-B to work on a merge baseDenton Liu, Apr 25, 2019
  2. 1/3 t2018: cleanup in current testDenton Liu, Apr 25, 2019
  3. Eric SunshineApr 25, 2019
  4. Denton LiuApr 26, 2019
  5. Junio C HamanoApr 26, 2019
  6. Eric SunshineApr 26, 2019
  7. 2/3 t2018: demonstrate checkout -b merge base bugDenton Liu, Apr 25, 2019
  8. 3/3 checkout: allow -b/-B to work on a merge baseDenton Liu, Apr 25, 2019
  9. Junio C HamanoApr 26, 2019
  10. Junio C HamanoApr 26, 2019
  11. 0/3 allow checkout and branch to create branches on a merge baseDenton Liu, Apr 26, 2019
  12. 1/3 t2018: cleanup in current testDenton Liu, Apr 26, 2019
  13. 2/3 t2018: demonstrate checkout -b merge base bugDenton Liu, Apr 26, 2019
  14. 3/3 branch: make create_branch accept a merge base revDenton Liu, Apr 26, 2019
  15. Junio C HamanoApr 26, 2019
  16. Denton LiuApr 26, 2019
  17. Denton LiuApr 26, 2019
  18. Junio C HamanoApr 27, 2019
  19. 0/2 allow checkout and branch to create branches on a merge baseDenton Liu, Apr 27, 2019
  20. 1/2 t2018: cleanup in current testDenton Liu, Apr 27, 2019
  21. 2/2 branch: make create_branch accept a merge base revDenton Liu, Apr 27, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.