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

Re: Bug: "git checkout -b" should be allowed in empty repo

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 6, 2012, 02:06 UTC
Message-ID
<7vobtcbtqa.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20120130215043.GB16149@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
> Is it really worth warning? After all, by definition you are not leaving
> any commits or useful work behind.

I actually do not know if this change itself is worth doing, but if we were to do this, then I think the user benefits from the warning.

The patch is made on maint-1.7.6 track for no good reason, so it may have some merge conflicts around "resolve_ref()" vs "resolve_refdup()" if we were to apply it on a more modern codebase, but the resolution should be trivial.

---
Subject: [PATCH] git checkout -b: allow switching out of an unborn branch

Running "git checkout -b another" immediately after "git init" when you do not even have a commit on 'master' is forbidden, with a readable message:

    $ git checkout -b another
    fatal: You are on a branch yet to be born

It is readable but not easily understandable unless the user knows what "yet to be born" really means.

So let's try allowing it and see what happens. I strongly suspect that this may just shift the confusion one step further without adding much value to the resulting system, because the next question that would come to somebody who does not understand what "yet to be born" is is "why don't I see 'master' in the output from 'git branch' command?", and the new warning may not be descriptive enough to explain what the user is doing.

The early part of switch_branches() that computes old is probably be better moved to the caller cmd_checkout() and used in the new code that detects the "unborn" case, and passed as to switch_branches() as the third parameter. Such improvements and tests are left as an exercise for the interested and motivated, as usual ;-)

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/checkout.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 4c20dae..5894f40 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -916,6 +916,19 @@ static int parse_branchname_arg(int argc, const char **argv,
 	return argcount;
 }
 
+static int switch_unborn_to_new_branch(struct checkout_opts *opts, const char *old_ref)
+{
+	int status;
+	struct strbuf branch_ref = STRBUF_INIT;
+
+	strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
+	warning(_("Leaving the unborn branch '%s' behind..."),
+		skip_prefix(old_ref, "refs/heads/"));
+	status = create_symref("HEAD", branch_ref.buf, "checkout -b");
+	strbuf_release(&branch_ref);
+	return status;
+}
+
 int cmd_checkout(int argc, const char **argv, const char *prefix)
 {
 	struct checkout_opts opts;
@@ -1089,5 +1102,16 @@ 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) {
+		unsigned char rev[20];
+		int flag, status;
+		const char *old_ref = resolve_ref("HEAD", rev, 0, &flag);
+
+		if ((flag & REF_ISSYMREF) && is_null_sha1(rev)) {
+			status = switch_unborn_to_new_branch(&opts, old_ref);
+			free((char *)old_ref);
+			return status;
+		}
+	}
 	return switch_branches(&opts, &new);
 }
-- 
1.7.9.172.ge26ae
Previous: Jeff KingNext: Junio C Hamano
Message 8 of 34 in “Bug: "git checkout -b" should be allowed in empty repo”
  1. Michael HaggertyJan 29, 2012
  2. Junio C HamanoJan 29, 2012
  3. Junio C HamanoJan 29, 2012
  4. Michael HaggertyJan 30, 2012
  5. Junio C HamanoJan 30, 2012
  6. Junio C HamanoJan 30, 2012
  7. Jeff KingJan 30, 2012
  8. Junio C HamanoFeb 6, 2012
  9. Junio C HamanoFeb 6, 2012
  10. Jeff KingFeb 6, 2012
  11. Andrew ArdillFeb 6, 2012
  12. Jeff KingFeb 6, 2012
  13. Michael HaggertyFeb 6, 2012
  14. Jeff KingFeb 6, 2012
  15. Junio C HamanoFeb 6, 2012
  16. Jeff KingFeb 6, 2012
  17. Michael HaggertyFeb 7, 2012
  18. demerphqFeb 6, 2012
  19. Junio C HamanoFeb 6, 2012
  20. Jeff KingFeb 6, 2012
  21. Junio C HamanoFeb 6, 2012
  22. Junio C HamanoFeb 6, 2012
  23. Jeff KingFeb 6, 2012
  24. Michael HaggertyFeb 6, 2012
  25. Junio C HamanoFeb 6, 2012
  26. Jeff KingJan 30, 2012
  27. branch --edit-description: protect against mistyped branch nameJunio C Hamano, Feb 6, 2012
  28. Junio C HamanoFeb 6, 2012
  29. Jeff KingFeb 6, 2012
  30. Michael HaggertyJan 31, 2012
  31. Johannes SixtJan 31, 2012
  32. demerphqJan 31, 2012
  33. Jakub NarebskiJan 31, 2012
  34. Michael HaggertyJan 31, 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.