]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5512-ls-remote.sh
The third batch
[thirdparty/git.git] / t / t5512-ls-remote.sh
CommitLineData
7c2c6ee7
SP
1#!/bin/sh
2
3test_description='git ls-remote'
4
bc925ce3 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
7c2c6ee7
SP
8. ./test-lib.sh
9
3227ddc9
DL
10generate_references () {
11 for ref
12 do
13 oid=$(git rev-parse "$ref") &&
14 printf '%s\t%s\n' "$oid" "$ref" || return 1
15 done
16}
17
aa962fef
JK
18test_expect_success 'set up fake upload-pack' '
19 # This can be used to simulate an upload-pack that just shows the
20 # contents of the "input" file (prepared with the test-tool pkt-line
21 # helper), and does not do any negotiation (since ls-remote does not
22 # need it).
23 write_script cat-input <<-\EOF
24 # send our initial advertisement/response
25 cat input
26 # soak up the flush packet from the client
27 cat
28 EOF
29'
30
8a649be7
TK
31test_expect_success 'dies when no remote found' '
32 test_must_fail git ls-remote
33'
34
7c2c6ee7 35test_expect_success setup '
7c2c6ee7
SP
36 >file &&
37 git add file &&
38 test_tick &&
39 git commit -m initial &&
40 git tag mark &&
1fb20dfd
HN
41 git tag mark1.1 &&
42 git tag mark1.2 &&
43 git tag mark1.10 &&
ef343f41
DL
44 git show-ref --tags -d >expected.tag.raw &&
45 sed -e "s/ / /" expected.tag.raw >expected.tag &&
46 generate_references HEAD >expected.all &&
47 git show-ref -d >refs &&
48 sed -e "s/ / /" refs >>expected.all &&
7c2c6ee7 49
8a649be7
TK
50 git remote add self "$(pwd)/.git" &&
51 git remote add self2 "."
7c2c6ee7
SP
52'
53
54test_expect_success 'ls-remote --tags .git' '
7c2c6ee7 55 git ls-remote --tags .git >actual &&
82ebb0b6 56 test_cmp expected.tag actual
7c2c6ee7
SP
57'
58
59test_expect_success 'ls-remote .git' '
7c2c6ee7 60 git ls-remote .git >actual &&
82ebb0b6 61 test_cmp expected.all actual
7c2c6ee7
SP
62'
63
64test_expect_success 'ls-remote --tags self' '
7c2c6ee7 65 git ls-remote --tags self >actual &&
82ebb0b6 66 test_cmp expected.tag actual
7c2c6ee7
SP
67'
68
69test_expect_success 'ls-remote self' '
7c2c6ee7 70 git ls-remote self >actual &&
82ebb0b6 71 test_cmp expected.all actual
7c2c6ee7
SP
72'
73
1fb20dfd 74test_expect_success 'ls-remote --sort="version:refname" --tags self' '
3227ddc9
DL
75 generate_references \
76 refs/tags/mark \
77 refs/tags/mark1.1 \
78 refs/tags/mark1.2 \
79 refs/tags/mark1.10 >expect &&
1fb20dfd
HN
80 git ls-remote --sort="version:refname" --tags self >actual &&
81 test_cmp expect actual
82'
83
84test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
3227ddc9
DL
85 generate_references \
86 refs/tags/mark1.10 \
87 refs/tags/mark1.2 \
88 refs/tags/mark1.1 \
89 refs/tags/mark >expect &&
1fb20dfd
HN
90 git ls-remote --sort="-version:refname" --tags self >actual &&
91 test_cmp expect actual
92'
93
94test_expect_success 'ls-remote --sort="-refname" --tags self' '
3227ddc9
DL
95 generate_references \
96 refs/tags/mark1.2 \
97 refs/tags/mark1.10 \
98 refs/tags/mark1.1 \
99 refs/tags/mark >expect &&
1fb20dfd
HN
100 git ls-remote --sort="-refname" --tags self >actual &&
101 test_cmp expect actual
102'
103
8a649be7 104test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' '
9c00de5a 105 test_must_fail git ls-remote
9c00de5a
TRC
106'
107
8a649be7
TK
108test_expect_success 'succeeds when no remote specified but only one found' '
109 test_when_finished git remote add self2 "." &&
110 git remote remove self2 &&
111 git ls-remote
112'
113
114test_expect_success 'use "origin" when no remote specified and multiple found' '
cefb2a5e
TRC
115 URL="$(pwd)/.git" &&
116 echo "From $URL" >exp_err &&
117
118 git remote add origin "$URL" &&
119 git ls-remote 2>actual_err >actual &&
120
121 test_cmp exp_err actual_err &&
9c00de5a 122 test_cmp expected.all actual
9c00de5a
TRC
123'
124
cefb2a5e 125test_expect_success 'suppress "From <url>" with -q' '
cefb2a5e 126 git ls-remote -q 2>actual_err &&
5a85a25e 127 ! test_cmp exp_err actual_err
cefb2a5e
TRC
128'
129
9c00de5a 130test_expect_success 'use branch.<name>.remote if possible' '
9c00de5a
TRC
131 #
132 # Test that we are indeed using branch.<name>.remote, not "origin", even
133 # though the "origin" remote has been set.
134 #
135
136 # setup a new remote to differentiate from "origin"
137 git clone . other.git &&
138 (
139 cd other.git &&
51b85471 140 echo "$(git rev-parse HEAD) HEAD" &&
9c00de5a
TRC
141 git show-ref | sed -e "s/ / /"
142 ) >exp &&
143
cefb2a5e
TRC
144 URL="other.git" &&
145 echo "From $URL" >exp_err &&
146
147 git remote add other $URL &&
bc925ce3 148 git config branch.main.remote other &&
9c00de5a 149
cefb2a5e
TRC
150 git ls-remote 2>actual_err >actual &&
151 test_cmp exp_err actual_err &&
9c00de5a 152 test_cmp exp actual
9c00de5a
TRC
153'
154
9c00de5a 155test_expect_success 'confuses pattern as remote when no remote specified' '
e9d983f1
NTND
156 if test_have_prereq MINGW
157 then
158 # Windows does not like asterisks in pathname
bc925ce3 159 does_not_exist=main
e9d983f1 160 else
bc925ce3 161 does_not_exist="refs*main"
e9d983f1
NTND
162 fi &&
163 cat >exp <<-EOF &&
164 fatal: '\''$does_not_exist'\'' does not appear to be a git repository
348c44e7
JH
165 fatal: Could not read from remote repository.
166
167 Please make sure you have the correct access rights
168 and the repository exists.
fe6c64ab 169 EOF
9c00de5a 170 #
46284dd1
HV
171 # Do not expect "git ls-remote <pattern>" to work; ls-remote needs
172 # <remote> if you want to feed <pattern>, just like you cannot say
173 # fetch <branch>.
bc925ce3 174 # We could just as easily have used "main"; the "*" emphasizes its
9c00de5a 175 # role as a pattern.
e9d983f1 176 test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
1108cea7 177 test_cmp exp actual
9c00de5a
TRC
178'
179
a8724773 180test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
9ddc5ac9
JK
181 {
182 git ls-remote --exit-code ./no-such-repository
183 status=$?
184 } &&
a8724773
MS
185 test $status != 2 && test $status != 0
186'
187
188test_expect_success 'Report success even when nothing matches' '
189 git ls-remote other.git "refs/nsn/*" >actual &&
d3c6751b 190 test_must_be_empty actual
a8724773
MS
191'
192
193test_expect_success 'Report no-match with --exit-code' '
194 test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
d3c6751b 195 test_must_be_empty actual
a8724773
MS
196'
197
198test_expect_success 'Report match with --exit-code' '
199 git ls-remote --exit-code other.git "refs/tags/*" >actual &&
1fb20dfd 200 git ls-remote . tags/mark* >expect &&
a8724773
MS
201 test_cmp expect actual
202'
203
2bc31d16
JK
204test_expect_success 'set up some extra tags for ref hiding' '
205 git tag magic/one &&
206 git tag magic/two
207'
208
daebaa78
JH
209for configsection in transfer uploadpack
210do
211 test_expect_success "Hide some refs with $configsection.hiderefs" '
212 test_config $configsection.hiderefs refs/tags &&
213 git ls-remote . >actual &&
214 test_unconfig $configsection.hiderefs &&
ef343f41
DL
215 git ls-remote . >expect.raw &&
216 sed -e "/ refs\/tags\//d" expect.raw >expect &&
daebaa78
JH
217 test_cmp expect actual
218 '
2bc31d16
JK
219
220 test_expect_success "Override hiding of $configsection.hiderefs" '
221 test_when_finished "test_unconfig $configsection.hiderefs" &&
222 git config --add $configsection.hiderefs refs/tags &&
223 git config --add $configsection.hiderefs "!refs/tags/magic" &&
224 git config --add $configsection.hiderefs refs/tags/magic/one &&
225 git ls-remote . >actual &&
226 grep refs/tags/magic/two actual &&
227 ! grep refs/tags/magic/one actual
228 '
229
daebaa78
JH
230done
231
2bc31d16
JK
232test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs' '
233 test_config uploadpack.hiderefs refs/tags &&
234 test_config transfer.hiderefs "!refs/tags/magic" &&
235 git ls-remote . >actual &&
236 grep refs/tags/magic actual
237'
238
e20b4192
JK
239test_expect_success 'protocol v2 supports hiderefs' '
240 test_config uploadpack.hiderefs refs/tags &&
241 git -c protocol.version=2 ls-remote . >actual &&
242 ! grep refs/tags actual
243'
244
99c08d4e 245test_expect_success 'ls-remote --symref' '
7484cf53 246 git fetch origin &&
20272ee8 247 echo "ref: refs/heads/main HEAD" >expect.v2 &&
3227ddc9
DL
248 generate_references \
249 HEAD \
20272ee8
JK
250 refs/heads/main >>expect.v2 &&
251 echo "ref: refs/remotes/origin/main refs/remotes/origin/HEAD" >>expect.v2 &&
3227ddc9 252 oid=$(git rev-parse HEAD) &&
20272ee8 253 echo "$oid refs/remotes/origin/HEAD" >>expect.v2 &&
3227ddc9 254 generate_references \
bc925ce3 255 refs/remotes/origin/main \
3227ddc9
DL
256 refs/tags/mark \
257 refs/tags/mark1.1 \
258 refs/tags/mark1.10 \
20272ee8
JK
259 refs/tags/mark1.2 >>expect.v2 &&
260 # v0 does not show non-HEAD symrefs
261 grep -v "ref: refs/remotes" <expect.v2 >expect.v0 &&
262 git -c protocol.version=0 ls-remote --symref >actual.v0 &&
263 test_cmp expect.v0 actual.v0 &&
264 git -c protocol.version=2 ls-remote --symref >actual.v2 &&
265 test_cmp expect.v2 actual.v2
99c08d4e
TG
266'
267
268test_expect_success 'ls-remote with filtered symref (refname)' '
74ad99b1 269 rev=$(git rev-parse HEAD) &&
270 cat >expect <<-EOF &&
bc925ce3 271 ref: refs/heads/main HEAD
74ad99b1 272 $rev HEAD
99c08d4e 273 EOF
d6747adf 274 git ls-remote --symref . HEAD >actual &&
99c08d4e
TG
275 test_cmp expect actual
276'
277
c4716236 278test_expect_success 'ls-remote with filtered symref (--heads)' '
99c08d4e 279 git symbolic-ref refs/heads/foo refs/tags/mark &&
c4716236 280 cat >expect.v2 <<-EOF &&
99c08d4e 281 ref: refs/tags/mark refs/heads/foo
74ad99b1 282 $rev refs/heads/foo
bc925ce3 283 $rev refs/heads/main
99c08d4e 284 EOF
c4716236
JK
285 grep -v "^ref: refs/tags/" <expect.v2 >expect.v0 &&
286 git -c protocol.version=0 ls-remote --symref --heads . >actual.v0 &&
287 test_cmp expect.v0 actual.v0 &&
288 git -c protocol.version=2 ls-remote --symref --heads . >actual.v2 &&
289 test_cmp expect.v2 actual.v2
99c08d4e
TG
290'
291
13e67aa3
JK
292test_expect_success 'indicate no refs in v0 standards-compliant empty remote' '
293 # Git does not produce an output like this, but it does match the
294 # standard and is produced by other implementations like JGit. So
295 # hard-code the case we care about.
296 #
297 # The actual capabilities do not matter; there are none that would
298 # change how ls-remote behaves.
299 oid=0000000000000000000000000000000000000000 &&
300 test-tool pkt-line pack >input.q <<-EOF &&
301 $oid capabilities^{}Qcaps-go-here
302 0000
303 EOF
304 q_to_nul <input.q >input &&
eb398797 305
eb398797
JT
306 # --exit-code asks the command to exit with 2 when no
307 # matching refs are found.
13e67aa3 308 test_expect_code 2 git ls-remote --exit-code --upload-pack=./cat-input .
eb398797 309'
99c08d4e 310
4539c218
JK
311test_expect_success 'ls-remote works outside repository' '
312 # It is important for this repo to be inside the nongit
313 # area, as we want a repo name that does not include
314 # slashes (because those inhibit some of our configuration
315 # lookups).
316 nongit git init --bare dst.git &&
317 nongit git ls-remote dst.git
318'
319
47bd3d0c
SG
320test_expect_success 'ls-remote --sort fails gracefully outside repository' '
321 # Use a sort key that requires access to the referenced objects.
322 nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
6789275d 323 test_grep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
47bd3d0c
SG
324'
325
631f0f8c
JK
326test_expect_success 'ls-remote patterns work with all protocol versions' '
327 git for-each-ref --format="%(objectname) %(refname)" \
bc925ce3 328 refs/heads/main refs/remotes/origin/main >expect &&
e6c43097
JK
329 git -c protocol.version=0 ls-remote . main >actual.v0 &&
330 test_cmp expect actual.v0 &&
bc925ce3 331 git -c protocol.version=2 ls-remote . main >actual.v2 &&
631f0f8c
JK
332 test_cmp expect actual.v2
333'
334
6a139cdd
JK
335test_expect_success 'ls-remote prefixes work with all protocol versions' '
336 git for-each-ref --format="%(objectname) %(refname)" \
337 refs/heads/ refs/tags/ >expect &&
e6c43097
JK
338 git -c protocol.version=0 ls-remote --heads --tags . >actual.v0 &&
339 test_cmp expect actual.v0 &&
6a139cdd
JK
340 git -c protocol.version=2 ls-remote --heads --tags . >actual.v2 &&
341 test_cmp expect actual.v2
342'
343
aa962fef
JK
344test_expect_success 'v0 clients can handle multiple symrefs' '
345 # Modern versions of Git will not return multiple symref capabilities
346 # for v0, so we have to hard-code the response. Note that we will
347 # always use both v0 and object-format=sha1 here, as the hard-coded
348 # response reflects a server that only supports those.
349 oid=1234567890123456789012345678901234567890 &&
350 symrefs="symref=refs/remotes/origin/HEAD:refs/remotes/origin/main" &&
351 symrefs="$symrefs symref=HEAD:refs/heads/main" &&
352
353 # Likewise we want to make sure our parser is not fooled by the string
354 # "symref" appearing as part of an earlier cap. But there is no way to
355 # do that via upload-pack, as arbitrary strings can appear only in a
356 # "symref" value itself (where we skip past the values as a whole)
357 # and "agent" (which always appears after "symref", so putting our
358 # parser in a confused state is less interesting).
359 caps="some other caps including a-fake-symref-cap" &&
360
361 test-tool pkt-line pack >input.q <<-EOF &&
362 $oid HEADQ$caps $symrefs
363 $oid refs/heads/main
364 $oid refs/remotes/origin/HEAD
365 $oid refs/remotes/origin/main
366 0000
367 EOF
368 q_to_nul <input.q >input &&
369
370 cat >expect <<-EOF &&
371 ref: refs/heads/main HEAD
372 $oid HEAD
373 $oid refs/heads/main
374 ref: refs/remotes/origin/main refs/remotes/origin/HEAD
375 $oid refs/remotes/origin/HEAD
376 $oid refs/remotes/origin/main
377 EOF
378
379 git ls-remote --symref --upload-pack=./cat-input . >actual &&
380 test_cmp expect actual
381'
382
7c2c6ee7 383test_done