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 <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Erik Faye-Lund committed
May 8, 2012 at 19:22 UTC
8338f771fe9720766dfcb25b6dca58832e24a0fd
2 files changed
+12
-1
builtin/checkout.c
+1
-1
@@ -1100,7 +1100,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1100
if (opts.writeout_stage)
1101
die(_("--ours/--theirs is incompatible with switching branches."));
1102
1103
- if (!new.commit) {
1103
+ if (!new.commit && opts.new_branch) {
1104
unsigned char rev[20];
1105
int flag;
1106
t/t2015-checkout-unborn.sh
+11
@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' '
46
test_cmp expect actual
47
'
48
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
+
60
test_done