Skip to content

Commit

Permalink
builtin/checkout: handle -B from detached HEAD correctly
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Tay Ray Chuan authored and Junio C Hamano committed Aug 9, 2010
1 parent 02ac983 commit cc70148
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion branch.c
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions t/t2018-checkout-branch.sh
Original file line number Diff line number Diff line change
@@ -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 &&

0 comments on commit cc70148

Please sign in to comment.