{"thread":{"id":"30473","subject":"[PATCH v2] checkout: do not corrupt HEAD on empty repo","startedAt":"2012-05-08T17:22:33Z","lastAt":"2012-05-08T17:22:33Z","messageCount":1,"participants":["Erik Faye-Lund"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"191124","messageId":"1336497753-4208-1-git-send-email-kusmabite@gmail.com","threadId":"30473","inReplyTo":null,"subject":"[PATCH v2] checkout: do not corrupt HEAD on empty repo","fromName":"Erik Faye-Lund","fromEmail":"kusmabite@gmail.com","sentAt":"2012-05-08T17:22:33Z","receivedAt":"2012-05-08T17:22:33Z","isPatch":true,"sender":{"key":"kusmabite@gmail.com","avatar":"https://avatars.githubusercontent.com/u/47073?v=4"},"body":"In abe1998 (\"git checkout -b: allow switching out of an unborn\nbranch\"), a code-path overly-optimisticly assumed that a\nbranch-name was specified. This is not always the case, and as\na result a NULL-pointer was attempted printed to .git/HEAD.\n\nThis could lead to at least two different failure modes:\n 1) vsnprintf formated the NULL-string as something useful (e.g\n    \"(null)\")\n 2) vsnprintf crashed\n\nNeither were very convenient for formatting a new HEAD-reference.\n\nTo fix this, reintroduce some strictness so we only take this\nnew codepath if a banch-name was specified.\n\nSigned-off-by: Erik Faye-Lund <kusmabite@gmail.com>\n---\n builtin/checkout.c         |    2 +-\n t/t2015-checkout-unborn.sh |   11 +++++++++++\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 23fc56d..35924d4 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -1091,7 +1091,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \tif (opts.writeout_stage)\n \t\tdie(_(\"--ours/--theirs is incompatible with switching branches.\"));\n \n-\tif (!new.commit) {\n+\tif (!new.commit && opts.new_branch) {\n \t\tunsigned char rev[20];\n \t\tint flag;\n \ndiff --git a/t/t2015-checkout-unborn.sh b/t/t2015-checkout-unborn.sh\nindex 6352b74..37bdced 100755\n--- a/t/t2015-checkout-unborn.sh\n+++ b/t/t2015-checkout-unborn.sh\n@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'checking out in a newly created repo' '\n+\ttest_create_repo empty &&\n+\t(\n+\t\tcd empty &&\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\ttest_must_fail git checkout &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\n-- \n1.7.10.msysgit.1\n"}]}