Skip to content

Commit

Permalink
git-checkout: disallow --detach on unborn branch
Browse files Browse the repository at this point in the history
abe1998 (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>
  • Loading branch information
Chris Webb authored and Junio C Hamano committed Jun 26, 2012
1 parent abe1998 commit 8ced1aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions t/t2017-checkout-orphan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8ced1aa

Please sign in to comment.