Skip to content

Commit 8338f77

Browse files
kusmagitster
authored andcommitted
checkout: do not corrupt HEAD on empty repo
In abe1998 ("git checkout -b: allow switching out of an unborn branch"), a code-path overly-optimisticly assumed that a branch-name was specified. This is not always the case, and as a result a NULL-pointer was attempted printed to .git/HEAD. This could lead to at least two different failure modes: 1) vsnprintf formated the NULL-string as something useful (e.g "(null)") 2) vsnprintf crashed Neither were very convenient for formatting a new HEAD-reference. To fix this, reintroduce some strictness so we only take this new codepath if a banch-name was specified. Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent abe1998 commit 8338f77

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
11001100
if (opts.writeout_stage)
11011101
die(_("--ours/--theirs is incompatible with switching branches."));
11021102

1103-
if (!new.commit) {
1103+
if (!new.commit && opts.new_branch) {
11041104
unsigned char rev[20];
11051105
int flag;
11061106

t/t2015-checkout-unborn.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' '
4646
test_cmp expect actual
4747
'
4848

49+
test_expect_success 'checking out in a newly created repo' '
50+
test_create_repo empty &&
51+
(
52+
cd empty &&
53+
git symbolic-ref HEAD >expect &&
54+
test_must_fail git checkout &&
55+
git symbolic-ref HEAD >actual &&
56+
test_cmp expect actual
57+
)
58+
'
59+
4960
test_done

0 commit comments

Comments
 (0)