Skip to content

Commit 51a94af

Browse files
committed
Fix "checkout --track -b newbranch" on detached HEAD
The test to make sure that checkout fails when --track was asked for and we cannot set up tracking information in t7201 was wrong, and it turns out that the implementation for that feature itself was buggy. This fixes it. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ed0a74 commit 51a94af

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

branch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ void create_branch(const char *head,
129129
die("Cannot setup tracking information; starting point is not a branch.");
130130
break;
131131
case 1:
132-
/* Unique completion -- good */
132+
/* Unique completion -- good, only if it is a real ref */
133+
if (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, "HEAD"))
134+
die("Cannot setup tracking information; starting point is not a branch.");
133135
break;
134136
default:
135137
die("Ambiguous object name: '%s'.", start_name);

t/t7201-co.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,14 @@ test_expect_success \
330330
test "$(git config branch.track2.merge)"
331331
git config branch.autosetupmerge false'
332332

333-
test_expect_success \
334-
'checkout w/--track from non-branch HEAD fails' '
335-
git checkout -b delete-me master &&
336-
rm .git/refs/heads/delete-me &&
337-
test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
338-
test_must_fail git checkout --track -b track'
333+
test_expect_success 'checkout w/--track from non-branch HEAD fails' '
334+
git checkout master^0 &&
335+
test_must_fail git symbolic-ref HEAD &&
336+
test_must_fail git checkout --track -b track &&
337+
test_must_fail git rev-parse --verify track &&
338+
test_must_fail git symbolic-ref HEAD &&
339+
test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
340+
'
339341

340342
test_expect_success 'checkout an unmerged path should fail' '
341343
rm -f .git/index &&

0 commit comments

Comments
 (0)