git-checkout: disallow --detach on unborn branch
authorChris Webb <chris@arachsys.com>
Tue, 26 Jun 2012 15:06:42 +0000 (16:06 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Jun 2012 18:11:14 +0000 (11:11 -0700)
abe199808c (git checkout -b: allow switching out of an unborn branch)
introduced a bug demonstrated by

git checkout --orphan foo
git checkout --detach
git symbolic-ref HEAD

which gives 'refs/heads/(null)'.

This happens because we strbuf_addf(&branch_ref, "refs/heads/%s",
opts->new_branch) when opts->new_branch can be NULL for --detach.

Catch and forbid this case, adding a test to t2017 to catch it in
future.

Signed-off-by: Chris Webb <chris@arachsys.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2017-checkout-orphan.sh
index b76e2c0451cbeb73e1fd227c343413b1c8a33cc7..a94b553d054b28eee83ce6b9bd54be4659279239 100644 (file)
@@ -921,6 +921,8 @@ static int switch_unborn_to_new_branch(struct checkout_opts *opts)
        int status;
        struct strbuf branch_ref = STRBUF_INIT;
 
+       if (!opts->new_branch)
+               die(_("You are on a branch yet to be born"));
        strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
        status = create_symref("HEAD", branch_ref.buf, "checkout -b");
        strbuf_release(&branch_ref);
index 0e3b8582f2a3edebb0d9eea7fcebaa88f305fd73..655f278c5f87926311979732eb6c129a14dd87d3 100755 (executable)
@@ -116,4 +116,10 @@ test_expect_success '--orphan refuses to switch if a merge is needed' '
        git reset --hard
 '
 
+test_expect_success 'cannot --detach on an unborn branch' '
+       git checkout master &&
+       git checkout --orphan new &&
+       test_must_fail git checkout --detach
+'
+
 test_done