Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t5509-fetch-push-namespaces.sh
blob05090feaf9289843214cdaba4813fd50a7deec77
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_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
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
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
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
126 test_expect_success 'try to update a hidden full ref' '
127 test_config -C pushee transfer.hideRefs "^refs/namespaces/namespace/refs/heads/main" &&
128 test_must_fail git -C original push pushee-namespaced main
131 test_expect_success 'set up ambiguous HEAD' '
132 git init ambiguous &&
134 cd ambiguous &&
135 git commit --allow-empty -m foo &&
136 git update-ref refs/namespaces/ns/refs/heads/one HEAD &&
137 git update-ref refs/namespaces/ns/refs/heads/two HEAD &&
138 git symbolic-ref refs/namespaces/ns/HEAD \
139 refs/namespaces/ns/refs/heads/two
143 test_expect_success 'clone chooses correct HEAD (v0)' '
144 GIT_NAMESPACE=ns git -c protocol.version=0 \
145 clone ambiguous ambiguous-v0 &&
146 echo refs/heads/two >expect &&
147 git -C ambiguous-v0 symbolic-ref HEAD >actual &&
148 test_cmp expect actual
151 test_expect_success 'clone chooses correct HEAD (v2)' '
152 GIT_NAMESPACE=ns git -c protocol.version=2 \
153 clone ambiguous ambiguous-v2 &&
154 echo refs/heads/two >expect &&
155 git -C ambiguous-v2 symbolic-ref HEAD >actual &&
156 test_cmp expect actual
159 test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
161 cd original &&
162 git init unborn &&
163 git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
164 test_must_fail git push unborn-namespaced HEAD:main &&
165 git -C unborn config receive.denyCurrentBranch updateInstead &&
166 git push unborn-namespaced HEAD:main
170 test_done