Skip to contentSkip to conversation

builtin/checkout: handle -B from detached HEAD correctly

PATCH New Tay Ray Chuan 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

Ensure that strcmp() isn't called when head is null.

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

---

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(-)
branch.c +1 -1
index 2ab42aa..93dc866 100644
@@ -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;
 	}
t/t2018-checkout-branch.sh +6 -0
index 1caffea..fa69016 100755
@@ -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