Skip to content

Commit 7f08c68

Browse files
pcloudsgitster
authored andcommitted
clone: write detached HEAD in bare repositories
If we don't write, HEAD is still at refs/heads/master as initialized by init-db, which may or may not match remote's HEAD. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bafe763 commit 7f08c68

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

builtin/clone.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
764764
}
765765
} else if (remote_head) {
766766
/* Source had detached HEAD pointing somewhere. */
767-
if (!option_bare) {
768-
update_ref(reflog_msg.buf, "HEAD",
769-
remote_head->old_sha1,
770-
NULL, REF_NODEREF, DIE_ON_ERR);
771-
our_head_points_at = remote_head;
772-
}
767+
update_ref(reflog_msg.buf, "HEAD", remote_head->old_sha1,
768+
NULL, REF_NODEREF, DIE_ON_ERR);
769+
our_head_points_at = remote_head;
773770
} else {
774771
/* Nothing to checkout out */
775772
if (!option_no_checkout)

t/t5601-clone.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ test_expect_success setup '
1212
cd src &&
1313
>file &&
1414
git add file &&
15-
git commit -m initial
15+
git commit -m initial &&
16+
echo 1 >file &&
17+
git add file &&
18+
git commit -m updated
1619
)
1720
1821
'
@@ -88,6 +91,26 @@ test_expect_success 'clone --mirror' '
8891
8992
'
9093

94+
test_expect_success 'clone --mirror with detached HEAD' '
95+
96+
( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
97+
git clone --mirror src mirror.detached &&
98+
( cd src && git checkout - ) &&
99+
GIT_DIR=mirror.detached git rev-parse HEAD >actual &&
100+
test_cmp expected actual
101+
102+
'
103+
104+
test_expect_success 'clone --bare with detached HEAD' '
105+
106+
( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
107+
git clone --bare src bare.detached &&
108+
( cd src && git checkout - ) &&
109+
GIT_DIR=bare.detached git rev-parse HEAD >actual &&
110+
test_cmp expected actual
111+
112+
'
113+
91114
test_expect_success 'clone --bare names the local repository <name>.git' '
92115
93116
git clone --bare src &&

0 commit comments

Comments
 (0)