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. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tay Ray Chuan committed
Aug 10, 2010 at 00:52 UTC
cc70148385d5a36682d3b67fdaa726590577f257
2 files changed
+7
-1
branch.c
+1
-1
@@ -159,7 +159,7 @@ void create_branch(const char *head,
159
dont_change_ref = 1;
160
else if (!force)
161
die("A branch named '%s' already exists.", name);
162
- else if (!is_bare_repository() && !strcmp(head, name))
162
+ else if (!is_bare_repository() && head && !strcmp(head, name))
163
die("Cannot force update the current branch.");
164
forcing = 1;
165
}
t/t2018-checkout-branch.sh
+6
@@ -124,6 +124,12 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
124
do_checkout branch2 "" -B
125
'
126
127
+test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
128
+ git checkout $(git rev-parse --verify HEAD) &&
129
+
130
+ do_checkout branch2 "" -B
131
+'
132
+
133
test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
134
git checkout branch1 &&
135