Skip to content

Commit 8ced1aa

Browse files
arachsysgitster
authored andcommitted
git-checkout: disallow --detach on unborn branch
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent abe1998 commit 8ced1aa

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

builtin/checkout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ static int switch_unborn_to_new_branch(struct checkout_opts *opts)
921921
int status;
922922
struct strbuf branch_ref = STRBUF_INIT;
923923

924+
if (!opts->new_branch)
925+
die(_("You are on a branch yet to be born"));
924926
strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
925927
status = create_symref("HEAD", branch_ref.buf, "checkout -b");
926928
strbuf_release(&branch_ref);

t/t2017-checkout-orphan.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,10 @@ test_expect_success '--orphan refuses to switch if a merge is needed' '
116116
git reset --hard
117117
'
118118

119+
test_expect_success 'cannot --detach on an unborn branch' '
120+
git checkout master &&
121+
git checkout --orphan new &&
122+
test_must_fail git checkout --detach
123+
'
124+
119125
test_done

0 commit comments

Comments
 (0)