Junio C Hamano <gitster@xxxxxxxxx> writes: > Note that I am not saying that we shouldn't add support for special cases > with special case codepaths. > > Perhaps we would need to sprinkle more special case magic like this (this > is for the special case that arises from the same cause)? > ... And the special case for "checkout -b" may look like this. 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, adding tests and pleasant commit log message is left as an exercise for the interested and motivated, as usual ;-) builtin/checkout.c | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index f1984d9..195c40b 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -922,6 +922,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_reset(&branch_ref); + return status; +} + int cmd_checkout(int argc, const char **argv, const char *prefix) { struct checkout_opts opts; @@ -1093,5 +1106,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; + char *old_ref = resolve_refdup("HEAD", rev, 0, &flag); + + if ((flag & REF_ISSYMREF) && is_null_sha1(rev)) { + status = switch_unborn_to_new_branch(&opts, old_ref); + free(old_ref); + return status; + } + } return switch_branches(&opts, &new); } -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html