config: unify code paths to get global config paths
[git] / t / t5509-fetch-push-namespaces.sh
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_NAMESPACE=namespace \
99                 git -C pushee -c transfer.hideRefs=refs/namespaces/namespace/refs/tags \
100                 ls-remote "ext::git %s ." >actual &&
101         printf "$commit1\trefs/heads/main\n" >expected &&
102         printf "$commit0\trefs/tags/0\n" >>expected &&
103         printf "$commit1\trefs/tags/1\n" >>expected &&
104         test_cmp expected actual
105 '
106
107 test_expect_success 'hide full refs with transfer.hideRefs' '
108         GIT_NAMESPACE=namespace \
109                 git -C pushee -c transfer.hideRefs="^refs/namespaces/namespace/refs/tags" \
110                 ls-remote "ext::git %s ." >actual &&
111         printf "$commit1\trefs/heads/main\n" >expected &&
112         test_cmp expected actual
113 '
114
115 test_expect_success 'try to update a hidden ref' '
116         test_config -C pushee transfer.hideRefs refs/heads/main &&
117         test_must_fail git -C original push pushee-namespaced main
118 '
119
120 test_expect_success 'try to update a ref that is not hidden' '
121         test_config -C pushee transfer.hideRefs refs/namespaces/namespace/refs/heads/main &&
122         git -C original push pushee-namespaced main
123 '
124
125 test_expect_success 'try to update a hidden full ref' '
126         test_config -C pushee transfer.hideRefs "^refs/namespaces/namespace/refs/heads/main" &&
127         test_must_fail git -C original push pushee-namespaced main
128 '
129
130 test_expect_success 'set up ambiguous HEAD' '
131         git init ambiguous &&
132         (
133                 cd ambiguous &&
134                 git commit --allow-empty -m foo &&
135                 git update-ref refs/namespaces/ns/refs/heads/one HEAD &&
136                 git update-ref refs/namespaces/ns/refs/heads/two HEAD &&
137                 git symbolic-ref refs/namespaces/ns/HEAD \
138                         refs/namespaces/ns/refs/heads/two
139         )
140 '
141
142 test_expect_success 'clone chooses correct HEAD (v0)' '
143         GIT_NAMESPACE=ns git -c protocol.version=0 \
144                 clone ambiguous ambiguous-v0 &&
145         echo refs/heads/two >expect &&
146         git -C ambiguous-v0 symbolic-ref HEAD >actual &&
147         test_cmp expect actual
148 '
149
150 test_expect_success 'clone chooses correct HEAD (v2)' '
151         GIT_NAMESPACE=ns git -c protocol.version=2 \
152                 clone ambiguous ambiguous-v2 &&
153         echo refs/heads/two >expect &&
154         git -C ambiguous-v2 symbolic-ref HEAD >actual &&
155         test_cmp expect actual
156 '
157
158 test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
159         (
160                 cd original &&
161                 git init unborn &&
162                 git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
163                 test_must_fail git push unborn-namespaced HEAD:main &&
164                 git -C unborn config receive.denyCurrentBranch updateInstead &&
165                 git push unborn-namespaced HEAD:main
166         )
167 '
168
169 test_done