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

[PATCH] builtin/checkout: handle -B from detached HEAD correctly

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Aug 9, 2010, 16:52 UTC
Message-ID
<1281372746-5088-1-git-send-email-rctay89@gmail.com>
Ensure that strcmp() isn't called when head is null.

Previously we were getting segfaults when checkout -B was done from a detached HEAD.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
  Junio, this goes on top of the 'tc/checkout-B' series.
 branch.c                   |    2 +-
 t/t2018-checkout-branch.sh |    6 ++++++
 2 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/branch.c b/branch.c
index 2ab42aa..93dc866 100644
--- a/branch.c
+++ b/branch.c
@@ -159,7 +159,7 @@ void create_branch(const char *head,
 			dont_change_ref = 1;
 		else if (!force)
 			die("A branch named '%s' already exists.", name);
-		else if (!is_bare_repository() && !strcmp(head, name))
+		else if (!is_bare_repository() && head && !strcmp(head, name))
 			die("Cannot force update the current branch.");
 		forcing = 1;
 	}
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index 1caffea..fa69016 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -124,6 +124,12 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
 	do_checkout branch2 "" -B
 '

+test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
+	git checkout $(git rev-parse --verify HEAD) &&
+
+	do_checkout branch2 "" -B
+'
+
 test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
 	git checkout branch1 &&

--
1.7.2.1.57.ga5ce7c
Message 1 of 1 in “builtin/checkout: handle -B from detached HEAD correctly”
  1. builtin/checkout: handle -B from detached HEAD correctlyTay Ray Chuan, Aug 9, 2010

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.