Code

builtin/checkout: handle -B from detached HEAD correctly
authorTay Ray Chuan <rctay89@gmail.com>
Mon, 9 Aug 2010 16:52:26 +0000 (00:52 +0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Aug 2010 19:57:17 +0000 (12:57 -0700)
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>
branch.c
t/t2018-checkout-branch.sh

index 2ab42aaf4da38b4ea45ef7f0a0f6b807313d4a22..93dc866f8c09a2da2308c4fd677178b043f2d4d5 100644 (file)
--- 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;
        }
index 1caffeac074ed9a8e434a3d6e53c20b00e8672cc..fa69016381b0196c49472af51e36948ec7c5a2a9 100755 (executable)
@@ -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 &&