blob: 31553b48dfc82285f643eca19336c821a0a3b2e5 [file] [log] [blame]
Josh Triplettbf7930c2011-07-21 13:10:54 -07001#!/bin/sh
2
3test_description='fetch/push involving ref namespaces'
Johannes Schindelin3275f4e2020-11-18 23:44:31 +00004GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00005export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
Josh Triplettbf7930c2011-07-21 13:10:54 -07007. ./test-lib.sh
8
9test_expect_success setup '
Brandon Williamsf1762d72016-12-14 14:39:52 -080010 git config --global protocol.ext.allow user &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070011 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) &&
Hariom Vermaf8692112020-02-23 18:57:09 +000026 git init --bare pushee &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070027 git init puller
28'
29
30test_expect_success 'pushing into a repository using a ref namespace' '
31 (
32 cd original &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +000033 git push pushee-namespaced main &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070034 git ls-remote pushee-namespaced >actual &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +000035 printf "$commit1\trefs/heads/main\n" >expected &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070036 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 &&
SZEDER Gáborec21ac82018-08-19 23:57:24 +020050 test_must_be_empty actual &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070051 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
57test_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 &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +000062 printf "$commit1 commit\trefs/remotes/pushee-namespaced/main\n" >expected &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070063 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.
77test_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 &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +000082 printf "$commit1 commit\trefs/namespaces/namespace/refs/heads/main\n" >expected &&
Josh Triplettbf7930c2011-07-21 13:10:54 -070083 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
Lukas Fleischer948bfa22015-11-05 07:07:31 +010089test_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 &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +000093 printf "$commit1\trefs/heads/main\n" >expected &&
Lukas Fleischer948bfa22015-11-05 07:07:31 +010094 test_cmp expected actual
95'
96
97test_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 &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000101 printf "$commit1\trefs/heads/main\n" >expected &&
Lukas Fleischer948bfa22015-11-05 07:07:31 +0100102 printf "$commit0\trefs/tags/0\n" >>expected &&
103 printf "$commit1\trefs/tags/1\n" >>expected &&
104 test_cmp expected actual
105'
106
107test_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 &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000111 printf "$commit1\trefs/heads/main\n" >expected &&
Lukas Fleischer948bfa22015-11-05 07:07:31 +0100112 test_cmp expected actual
113'
114
115test_expect_success 'try to update a hidden ref' '
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000116 test_config -C pushee transfer.hideRefs refs/heads/main &&
117 test_must_fail git -C original push pushee-namespaced main
Lukas Fleischer948bfa22015-11-05 07:07:31 +0100118'
119
120test_expect_success 'try to update a ref that is not hidden' '
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000121 test_config -C pushee transfer.hideRefs refs/namespaces/namespace/refs/heads/main &&
122 git -C original push pushee-namespaced main
Lukas Fleischer948bfa22015-11-05 07:07:31 +0100123'
124
125test_expect_success 'try to update a hidden full ref' '
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000126 test_config -C pushee transfer.hideRefs "^refs/namespaces/namespace/refs/heads/main" &&
127 test_must_fail git -C original push pushee-namespaced main
Lukas Fleischer948bfa22015-11-05 07:07:31 +0100128'
129
Jeff King533e0882019-05-23 02:11:21 -0400130test_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
142test_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
150test_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
Hariom Verma4ef34642020-02-23 18:57:10 +0000158test_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" &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000163 test_must_fail git push unborn-namespaced HEAD:main &&
Hariom Verma4ef34642020-02-23 18:57:10 +0000164 git -C unborn config receive.denyCurrentBranch updateInstead &&
Johannes Schindelin3275f4e2020-11-18 23:44:31 +0000165 git push unborn-namespaced HEAD:main
Hariom Verma4ef34642020-02-23 18:57:10 +0000166 )
167'
168
Josh Triplettbf7930c2011-07-21 13:10:54 -0700169test_done