Raw
1 #!/bin/sh
2
3 test_description='fetch/push involving ref namespaces'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 . ./test-lib.sh
8
9 test_expect_success setup '
10 git config --global protocol.ext.allow user &&
11 test_tick &&
12 git init original &&
13 (
14 cd original &&
15 echo 0 >count &&
16 git add count &&
17 test_commit 0 &&
18 echo 1 >count &&
19 git add count &&
20 test_commit 1 &&
21 git remote add pushee-namespaced "ext::git --namespace=namespace %s ../pushee" &&
22 git remote add pushee-unnamespaced ../pushee
23 ) &&
24 commit0=$(cd original && git rev-parse HEAD^) &&
25 commit1=$(cd original && git rev-parse HEAD) &&
26 git init --bare pushee &&
27 git init puller
28 '
29
30 test_expect_success 'pushing into a repository using a ref namespace' '
31 (
32 cd original &&
33 git push pushee-namespaced main &&
34 git ls-remote pushee-namespaced >actual &&
35 printf "$commit1\trefs/heads/main\n" >expected &&
36 test_cmp expected actual &&
37 git push pushee-namespaced --tags &&
38 git ls-remote pushee-namespaced >actual &&
39 printf "$commit0\trefs/tags/0\n" >>expected &&
40 printf "$commit1\trefs/tags/1\n" >>expected &&
41 test_cmp expected actual &&
42 # Verify that the GIT_NAMESPACE environment variable works as well
43 GIT_NAMESPACE=namespace git ls-remote "ext::git %s ../pushee" >actual &&
44 test_cmp expected actual &&
45 # Verify that --namespace overrides GIT_NAMESPACE
46 GIT_NAMESPACE=garbage git ls-remote pushee-namespaced >actual &&
47 test_cmp expected actual &&
48 # Try a namespace with no content
49 git ls-remote "ext::git --namespace=garbage %s ../pushee" >actual &&
50 test_must_be_empty actual &&
51 git ls-remote pushee-unnamespaced >actual &&
52 sed -e "s|refs/|refs/namespaces/namespace/refs/|" expected >expected.unnamespaced &&
53 test_cmp expected.unnamespaced actual
54 )
55 '
56
57 test_expect_success 'pulling from a repository using a ref namespace' '
58 (
59 cd puller &&
60 git remote add -f pushee-namespaced "ext::git --namespace=namespace %s ../pushee" &&
61 git for-each-ref refs/ >actual &&
62 printf "$commit1 commit\trefs/remotes/pushee-namespaced/main\n" >expected &&
63 printf "$commit0 commit\trefs/tags/0\n" >>expected &&
64 printf "$commit1 commit\trefs/tags/1\n" >>expected &&
65 test_cmp expected actual
66 )
67 '
68
69 # This test with clone --mirror checks for possible regressions in clone
70 # or the machinery underneath it. It ensures that no future change
71 # causes clone to ignore refs in refs/namespaces/*. In particular, it
72 # protects against a regression caused by any future change to the refs
73 # machinery that might cause it to ignore refs outside of refs/heads/*
74 # or refs/tags/*. More generally, this test also checks the high-level
75 # functionality of using clone --mirror to back up a set of repos hosted
76 # in the namespaces of a single repo.
77 test_expect_success 'mirroring a repository using a ref namespace' '
78 git clone --mirror pushee mirror &&
79 (
80 cd mirror &&
81 git for-each-ref refs/ >actual &&
82 printf "$commit1 commit\trefs/namespaces/namespace/refs/heads/main\n" >expected &&
83 printf "$commit0 commit\trefs/namespaces/namespace/refs/tags/0\n" >>expected &&
84 printf "$commit1 commit\trefs/namespaces/namespace/refs/tags/1\n" >>expected &&
85 test_cmp expected actual
86 )
87 '
88
89 test_expect_success 'hide namespaced refs with transfer.hideRefs' '
90 GIT_NAMESPACE=namespace \
91 git -C pushee -c transfer.hideRefs=refs/tags \
92 ls-remote "ext::git %s ." >actual &&
93 printf "$commit1\trefs/heads/main\n" >expected &&
94 test_cmp expected actual
95 '
96
97 test_expect_success 'check that transfer.hideRefs does not match unstripped refs' '
98 git -C pushee pack-refs --all &&
99 GIT_NAMESPACE=namespace \
100 git -C pushee -c transfer.hideRefs=refs/namespaces/namespace/refs/tags \
101 ls-remote "ext::git %s ." >actual &&
102 printf "$commit1\trefs/heads/main\n" >expected &&
103 printf "$commit0\trefs/tags/0\n" >>expected &&
104 printf "$commit1\trefs/tags/1\n" >>expected &&
105 test_cmp expected actual
106 '
107
108 test_expect_success 'hide full refs with transfer.hideRefs' '
109 GIT_NAMESPACE=namespace \
110 git -C pushee -c transfer.hideRefs="^refs/namespaces/namespace/refs/tags" \
111 ls-remote "ext::git %s ." >actual &&
112 printf "$commit1\trefs/heads/main\n" >expected &&
113 test_cmp expected actual
114 '
115
116 test_expect_success 'try to update a hidden ref' '
117 test_config -C pushee transfer.hideRefs refs/heads/main &&
118 test_must_fail git -C original push pushee-namespaced main
119 '
120
121 test_expect_success 'try to update a ref that is not hidden' '
122 test_config -C pushee transfer.hideRefs refs/namespaces/namespace/refs/heads/main &&
123 git -C original push pushee-namespaced main
124 '
125
126 test_expect_success 'git-receive-pack(1) with transfer.hideRefs does not match unstripped refs during advertisement' '
127 git -C pushee update-ref refs/namespaces/namespace/refs/heads/foo/1 refs/namespaces/namespace/refs/heads/main &&
128 git -C pushee pack-refs --all &&
129 test_config -C pushee transfer.hideRefs refs/namespaces/namespace/refs/heads/foo &&
130 GIT_TRACE_PACKET="$(pwd)/trace" git -C original push pushee-namespaced main &&
131 test_grep refs/heads/foo/1 trace
132 '
133
134 test_expect_success 'try to update a hidden full ref' '
135 test_config -C pushee transfer.hideRefs "^refs/namespaces/namespace/refs/heads/main" &&
136 test_must_fail git -C original push pushee-namespaced main
137 '
138
139 test_expect_success 'set up ambiguous HEAD' '
140 git init ambiguous &&
141 (
142 cd ambiguous &&
143 git commit --allow-empty -m foo &&
144 git update-ref refs/namespaces/ns/refs/heads/one HEAD &&
145 git update-ref refs/namespaces/ns/refs/heads/two HEAD &&
146 git symbolic-ref refs/namespaces/ns/HEAD \
147 refs/namespaces/ns/refs/heads/two
148 )
149 '
150
151 test_expect_success 'clone chooses correct HEAD (v0)' '
152 GIT_NAMESPACE=ns git -c protocol.version=0 \
153 clone ambiguous ambiguous-v0 &&
154 echo refs/heads/two >expect &&
155 git -C ambiguous-v0 symbolic-ref HEAD >actual &&
156 test_cmp expect actual
157 '
158
159 test_expect_success 'clone chooses correct HEAD (v2)' '
160 GIT_NAMESPACE=ns git -c protocol.version=2 \
161 clone ambiguous ambiguous-v2 &&
162 echo refs/heads/two >expect &&
163 git -C ambiguous-v2 symbolic-ref HEAD >actual &&
164 test_cmp expect actual
165 '
166
167 test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
168 (
169 cd original &&
170 git init unborn &&
171 git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
172 test_must_fail git push unborn-namespaced HEAD:main &&
173 git -C unborn config receive.denyCurrentBranch updateInstead &&
174 git push unborn-namespaced HEAD:main
175 )
176 '
177
178 test_done