Skip to content

Commit 61adfd3

Browse files
peffgitster
authored andcommitted
consider only branches in guess_remote_head
The guess_remote_head function tries to figure out where a remote's HEAD is pointing by comparing the sha1 of the remote's HEAD with the sha1 of various refs found on the remote. However, we were too liberal in matching refs, and would match tags or remote tracking branches, even though these things could not possibly be referenced by the HEAD symbolic ref (since git will detach when checking them out). As a result, a clone of a remote repository with a detached HEAD might write "refs/tags/*" into our local HEAD, which is bogus. The resulting HEAD should be detached. The other related code path is remote.c's get_head_names() (which is used for, among other things, "set-head -a"). This was not affected, however, as that function feeds only refs from refs/heads to guess_remote_head. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b36854 commit 61adfd3

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

remote.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,9 @@ struct ref *guess_remote_head(const struct ref *head,
16671667

16681668
/* Look for another ref that points there */
16691669
for (r = refs; r; r = r->next) {
1670-
if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
1670+
if (r != head &&
1671+
!prefixcmp(r->name, "refs/heads/") &&
1672+
!hashcmp(r->old_sha1, head->old_sha1)) {
16711673
*tail = copy_ref(r);
16721674
tail = &((*tail)->next);
16731675
if (!all)

t/t5707-clone-detached.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test_expect_success 'cloned HEAD matches' '
4141
git --git-dir=detached-tag/.git log -1 --format=%s >actual &&
4242
test_cmp expect actual
4343
'
44-
test_expect_failure 'cloned HEAD is detached' '
44+
test_expect_success 'cloned HEAD is detached' '
4545
head_is_detached detached-tag
4646
'
4747

0 commit comments

Comments
 (0)