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