Skip to content

Commit 3b36854

Browse files
peffgitster
authored andcommitted
t: add tests for cloning remotes with detached HEAD
We didn't test this setup at all, and doing so reveals a few bugs: 1. Cloning a repository with an orphaned detached HEAD (i.e., one that points to history that is not referenced by any ref) will fail. 2. Cloning a repository with a detached HEAD that points to a tag will cause us to write a bogus "refs/tags/..." ref into the HEAD symbolic ref. We should probably detach instead. 3. Cloning a repository with a detached HEAD that points to a branch will cause us to checkout that branch. This is a known limitation of the git protocol (we have to guess at HEAD's destination, since the symref contents aren't shown to us). This test serves to document the desired behavior, which can only be achieved once the git protocol learns to share symref information. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bac9c06 commit 3b36854

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

t/t5707-clone-detached.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/sh
2+
3+
test_description='test cloning a repository with detached HEAD'
4+
. ./test-lib.sh
5+
6+
head_is_detached() {
7+
git --git-dir=$1/.git rev-parse --verify HEAD &&
8+
test_must_fail git --git-dir=$1/.git symbolic-ref HEAD
9+
}
10+
11+
test_expect_success 'setup' '
12+
echo one >file &&
13+
git add file &&
14+
git commit -m one &&
15+
echo two >file &&
16+
git commit -a -m two &&
17+
git tag two &&
18+
echo three >file &&
19+
git commit -a -m three
20+
'
21+
22+
test_expect_success 'clone repo (detached HEAD points to branch)' '
23+
git checkout master^0 &&
24+
git clone "file://$PWD" detached-branch
25+
'
26+
test_expect_success 'cloned HEAD matches' '
27+
echo three >expect &&
28+
git --git-dir=detached-branch/.git log -1 --format=%s >actual &&
29+
test_cmp expect actual
30+
'
31+
test_expect_failure 'cloned HEAD is detached' '
32+
head_is_detached detached-branch
33+
'
34+
35+
test_expect_success 'clone repo (detached HEAD points to tag)' '
36+
git checkout two^0 &&
37+
git clone "file://$PWD" detached-tag
38+
'
39+
test_expect_success 'cloned HEAD matches' '
40+
echo two >expect &&
41+
git --git-dir=detached-tag/.git log -1 --format=%s >actual &&
42+
test_cmp expect actual
43+
'
44+
test_expect_failure 'cloned HEAD is detached' '
45+
head_is_detached detached-tag
46+
'
47+
48+
test_expect_success 'clone repo (detached HEAD points to history)' '
49+
git checkout two^ &&
50+
git clone "file://$PWD" detached-history
51+
'
52+
test_expect_success 'cloned HEAD matches' '
53+
echo one >expect &&
54+
git --git-dir=detached-history/.git log -1 --format=%s >actual &&
55+
test_cmp expect actual
56+
'
57+
test_expect_success 'cloned HEAD is detached' '
58+
head_is_detached detached-history
59+
'
60+
61+
test_expect_failure 'clone repo (orphan detached HEAD)' '
62+
git checkout master^0 &&
63+
echo four >file &&
64+
git commit -a -m four &&
65+
git clone "file://$PWD" detached-orphan
66+
'
67+
test_expect_failure 'cloned HEAD matches' '
68+
echo four >expect &&
69+
git --git-dir=detached-orphan/.git log -1 --format=%s >actual &&
70+
test_cmp expect actual
71+
'
72+
test_expect_failure 'cloned HEAD is detached' '
73+
head_is_detached detached-orphan
74+
'
75+
76+
test_done

0 commit comments

Comments
 (0)