git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] checkout: do not corrupt HEAD on empty repo

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
May 8, 2012, 16:05 UTC
Message-ID
<1336493114-4984-1-git-send-email-kusmabite@gmail.com>

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) The CRT formated the NULL-string as something useful (e.g
    "(null)")
 2) The CRT crasheed.
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>
---
 builtin/checkout.c         |    2 +-
 t/t2015-checkout-unborn.sh |   11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 23fc56d..35924d4 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1091,7 +1091,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.writeout_stage)
 		die(_("--ours/--theirs is incompatible with switching branches."));
 
-	if (!new.commit) {
+	if (!new.commit && opts.new_branch) {
 		unsigned char rev[20];
 		int flag;
 
diff --git a/t/t2015-checkout-unborn.sh b/t/t2015-checkout-unborn.sh
index 6352b74..2fa9458 100755
--- a/t/t2015-checkout-unborn.sh
+++ b/t/t2015-checkout-unborn.sh
@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' '
 	test_cmp expect actual
 '
 
+test_expect_success 'checking out in a newly created repo' '
+	test_create_repo empty &&
+	(
+		cd empty &&
+		cat .git/HEAD >expect &&
+		test_must_fail git checkout &&
+		cat .git/HEAD >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
1.7.10.msysgit.1
Next: Erik Faye-Lund
Message 1 of 10 in “checkout: do not corrupt HEAD on empty repo”
  1. checkout: do not corrupt HEAD on empty repoErik Faye-Lund, May 8, 2012
  2. Erik Faye-LundMay 8, 2012
  3. Junio C HamanoMay 8, 2012
  4. Erik Faye-LundMay 8, 2012
  5. Junio C HamanoMay 8, 2012
  6. Junio C HamanoMay 8, 2012
  7. Erik Faye-LundMay 8, 2012
  8. Junio C HamanoMay 8, 2012
  9. Erik Faye-LundMay 8, 2012
  10. Andreas SchwabMay 8, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.