]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5512-ls-remote.sh
The ninth 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
8a649be7
TK
18test_expect_success 'dies when no remote found' '
19 test_must_fail git ls-remote
20'
21
7c2c6ee7 22test_expect_success setup '
7c2c6ee7
SP
23 >file &&
24 git add file &&
25 test_tick &&
26 git commit -m initial &&
27 git tag mark &&
1fb20dfd
HN
28 git tag mark1.1 &&
29 git tag mark1.2 &&
30 git tag mark1.10 &&
ef343f41
DL
31 git show-ref --tags -d >expected.tag.raw &&
32 sed -e "s/ / /" expected.tag.raw >expected.tag &&
33 generate_references HEAD >expected.all &&
34 git show-ref -d >refs &&
35 sed -e "s/ / /" refs >>expected.all &&
7c2c6ee7 36
8a649be7
TK
37 git remote add self "$(pwd)/.git" &&
38 git remote add self2 "."
7c2c6ee7
SP
39'
40
41test_expect_success 'ls-remote --tags .git' '
7c2c6ee7 42 git ls-remote --tags .git >actual &&
82ebb0b6 43 test_cmp expected.tag actual
7c2c6ee7
SP
44'
45
46test_expect_success 'ls-remote .git' '
7c2c6ee7 47 git ls-remote .git >actual &&
82ebb0b6 48 test_cmp expected.all actual
7c2c6ee7
SP
49'
50
51test_expect_success 'ls-remote --tags self' '
7c2c6ee7 52 git ls-remote --tags self >actual &&
82ebb0b6 53 test_cmp expected.tag actual
7c2c6ee7
SP
54'
55
56test_expect_success 'ls-remote self' '
7c2c6ee7 57 git ls-remote self >actual &&
82ebb0b6 58 test_cmp expected.all actual
7c2c6ee7
SP
59'
60
1fb20dfd 61test_expect_success 'ls-remote --sort="version:refname" --tags self' '
3227ddc9
DL
62 generate_references \
63 refs/tags/mark \
64 refs/tags/mark1.1 \
65 refs/tags/mark1.2 \
66 refs/tags/mark1.10 >expect &&
1fb20dfd
HN
67 git ls-remote --sort="version:refname" --tags self >actual &&
68 test_cmp expect actual
69'
70
71test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
3227ddc9
DL
72 generate_references \
73 refs/tags/mark1.10 \
74 refs/tags/mark1.2 \
75 refs/tags/mark1.1 \
76 refs/tags/mark >expect &&
1fb20dfd
HN
77 git ls-remote --sort="-version:refname" --tags self >actual &&
78 test_cmp expect actual
79'
80
81test_expect_success 'ls-remote --sort="-refname" --tags self' '
3227ddc9
DL
82 generate_references \
83 refs/tags/mark1.2 \
84 refs/tags/mark1.10 \
85 refs/tags/mark1.1 \
86 refs/tags/mark >expect &&
1fb20dfd
HN
87 git ls-remote --sort="-refname" --tags self >actual &&
88 test_cmp expect actual
89'
90
8a649be7 91test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' '
9c00de5a 92 test_must_fail git ls-remote
9c00de5a
TRC
93'
94
8a649be7
TK
95test_expect_success 'succeeds when no remote specified but only one found' '
96 test_when_finished git remote add self2 "." &&
97 git remote remove self2 &&
98 git ls-remote
99'
100
101test_expect_success 'use "origin" when no remote specified and multiple found' '
cefb2a5e
TRC
102 URL="$(pwd)/.git" &&
103 echo "From $URL" >exp_err &&
104
105 git remote add origin "$URL" &&
106 git ls-remote 2>actual_err >actual &&
107
108 test_cmp exp_err actual_err &&
9c00de5a 109 test_cmp expected.all actual
9c00de5a
TRC
110'
111
cefb2a5e 112test_expect_success 'suppress "From <url>" with -q' '
cefb2a5e 113 git ls-remote -q 2>actual_err &&
5a85a25e 114 ! test_cmp exp_err actual_err
cefb2a5e
TRC
115'
116
9c00de5a 117test_expect_success 'use branch.<name>.remote if possible' '
9c00de5a
TRC
118 #
119 # Test that we are indeed using branch.<name>.remote, not "origin", even
120 # though the "origin" remote has been set.
121 #
122
123 # setup a new remote to differentiate from "origin"
124 git clone . other.git &&
125 (
126 cd other.git &&
51b85471 127 echo "$(git rev-parse HEAD) HEAD" &&
9c00de5a
TRC
128 git show-ref | sed -e "s/ / /"
129 ) >exp &&
130
cefb2a5e
TRC
131 URL="other.git" &&
132 echo "From $URL" >exp_err &&
133
134 git remote add other $URL &&
bc925ce3 135 git config branch.main.remote other &&
9c00de5a 136
cefb2a5e
TRC
137 git ls-remote 2>actual_err >actual &&
138 test_cmp exp_err actual_err &&
9c00de5a 139 test_cmp exp actual
9c00de5a
TRC
140'
141
9c00de5a 142test_expect_success 'confuses pattern as remote when no remote specified' '
e9d983f1
NTND
143 if test_have_prereq MINGW
144 then
145 # Windows does not like asterisks in pathname
bc925ce3 146 does_not_exist=main
e9d983f1 147 else
bc925ce3 148 does_not_exist="refs*main"
e9d983f1
NTND
149 fi &&
150 cat >exp <<-EOF &&
151 fatal: '\''$does_not_exist'\'' does not appear to be a git repository
348c44e7
JH
152 fatal: Could not read from remote repository.
153
154 Please make sure you have the correct access rights
155 and the repository exists.
fe6c64ab 156 EOF
9c00de5a 157 #
46284dd1
HV
158 # Do not expect "git ls-remote <pattern>" to work; ls-remote needs
159 # <remote> if you want to feed <pattern>, just like you cannot say
160 # fetch <branch>.
bc925ce3 161 # We could just as easily have used "main"; the "*" emphasizes its
9c00de5a 162 # role as a pattern.
e9d983f1 163 test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
1108cea7 164 test_cmp exp actual
9c00de5a
TRC
165'
166
a8724773 167test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
9ddc5ac9
JK
168 {
169 git ls-remote --exit-code ./no-such-repository
170 status=$?
171 } &&
a8724773
MS
172 test $status != 2 && test $status != 0
173'
174
175test_expect_success 'Report success even when nothing matches' '
176 git ls-remote other.git "refs/nsn/*" >actual &&
d3c6751b 177 test_must_be_empty actual
a8724773
MS
178'
179
180test_expect_success 'Report no-match with --exit-code' '
181 test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
d3c6751b 182 test_must_be_empty actual
a8724773
MS
183'
184
185test_expect_success 'Report match with --exit-code' '
186 git ls-remote --exit-code other.git "refs/tags/*" >actual &&
1fb20dfd 187 git ls-remote . tags/mark* >expect &&
a8724773
MS
188 test_cmp expect actual
189'
190
2bc31d16
JK
191test_expect_success 'set up some extra tags for ref hiding' '
192 git tag magic/one &&
193 git tag magic/two
194'
195
daebaa78
JH
196for configsection in transfer uploadpack
197do
198 test_expect_success "Hide some refs with $configsection.hiderefs" '
199 test_config $configsection.hiderefs refs/tags &&
200 git ls-remote . >actual &&
201 test_unconfig $configsection.hiderefs &&
ef343f41
DL
202 git ls-remote . >expect.raw &&
203 sed -e "/ refs\/tags\//d" expect.raw >expect &&
daebaa78
JH
204 test_cmp expect actual
205 '
2bc31d16
JK
206
207 test_expect_success "Override hiding of $configsection.hiderefs" '
208 test_when_finished "test_unconfig $configsection.hiderefs" &&
209 git config --add $configsection.hiderefs refs/tags &&
210 git config --add $configsection.hiderefs "!refs/tags/magic" &&
211 git config --add $configsection.hiderefs refs/tags/magic/one &&
212 git ls-remote . >actual &&
213 grep refs/tags/magic/two actual &&
214 ! grep refs/tags/magic/one actual
215 '
216
daebaa78
JH
217done
218
2bc31d16
JK
219test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs' '
220 test_config uploadpack.hiderefs refs/tags &&
221 test_config transfer.hiderefs "!refs/tags/magic" &&
222 git ls-remote . >actual &&
223 grep refs/tags/magic actual
224'
225
e20b4192
JK
226test_expect_success 'protocol v2 supports hiderefs' '
227 test_config uploadpack.hiderefs refs/tags &&
228 git -c protocol.version=2 ls-remote . >actual &&
229 ! grep refs/tags actual
230'
231
99c08d4e 232test_expect_success 'ls-remote --symref' '
7484cf53 233 git fetch origin &&
bc925ce3 234 echo "ref: refs/heads/main HEAD" >expect &&
3227ddc9
DL
235 generate_references \
236 HEAD \
bc925ce3 237 refs/heads/main >>expect &&
3227ddc9
DL
238 oid=$(git rev-parse HEAD) &&
239 echo "$oid refs/remotes/origin/HEAD" >>expect &&
240 generate_references \
bc925ce3 241 refs/remotes/origin/main \
3227ddc9
DL
242 refs/tags/mark \
243 refs/tags/mark1.1 \
244 refs/tags/mark1.10 \
245 refs/tags/mark1.2 >>expect &&
b2f73b70
JT
246 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
247 # protocol v0 here.
8a1b0978 248 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref >actual &&
99c08d4e
TG
249 test_cmp expect actual
250'
251
252test_expect_success 'ls-remote with filtered symref (refname)' '
74ad99b1 253 rev=$(git rev-parse HEAD) &&
254 cat >expect <<-EOF &&
bc925ce3 255 ref: refs/heads/main HEAD
74ad99b1 256 $rev HEAD
99c08d4e 257 EOF
b2f73b70
JT
258 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
259 # protocol v0 here.
8a1b0978 260 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . HEAD >actual &&
99c08d4e
TG
261 test_cmp expect actual
262'
263
264test_expect_failure 'ls-remote with filtered symref (--heads)' '
265 git symbolic-ref refs/heads/foo refs/tags/mark &&
74ad99b1 266 cat >expect <<-EOF &&
99c08d4e 267 ref: refs/tags/mark refs/heads/foo
74ad99b1 268 $rev refs/heads/foo
bc925ce3 269 $rev refs/heads/main
99c08d4e 270 EOF
b2f73b70
JT
271 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
272 # protocol v0 here.
8a1b0978 273 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
99c08d4e
TG
274 test_cmp expect actual
275'
276
277test_expect_success 'ls-remote --symref omits filtered-out matches' '
74ad99b1 278 cat >expect <<-EOF &&
279 $rev refs/heads/foo
bc925ce3 280 $rev refs/heads/main
99c08d4e 281 EOF
b2f73b70
JT
282 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
283 # protocol v0 here.
8a1b0978 284 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
99c08d4e 285 test_cmp expect actual &&
8a1b0978 286 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . "refs/heads/*" >actual &&
99c08d4e
TG
287 test_cmp expect actual
288'
289
eb398797 290test_lazy_prereq GIT_DAEMON '
43a2afee 291 test_bool_env GIT_TEST_GIT_DAEMON true
eb398797
JT
292'
293
294# This test spawns a daemon, so run it only if the user would be OK with
295# testing with git-daemon.
296test_expect_success PIPE,JGIT,GIT_DAEMON 'indicate no refs in standards-compliant empty remote' '
fa840581 297 test_set_port JGIT_DAEMON_PORT &&
eb398797
JT
298 JGIT_DAEMON_PID= &&
299 git init --bare empty.git &&
300 >empty.git/git-daemon-export-ok &&
301 mkfifo jgit_daemon_output &&
302 {
303 jgit daemon --port="$JGIT_DAEMON_PORT" . >jgit_daemon_output &
304 JGIT_DAEMON_PID=$!
305 } &&
306 test_when_finished kill "$JGIT_DAEMON_PID" &&
307 {
308 read line &&
309 case $line in
310 Exporting*)
311 ;;
312 *)
313 echo "Expected: Exporting" &&
314 false;;
315 esac &&
316 read line &&
317 case $line in
318 "Listening on"*)
319 ;;
320 *)
321 echo "Expected: Listening on" &&
322 false;;
323 esac
324 } <jgit_daemon_output &&
325 # --exit-code asks the command to exit with 2 when no
326 # matching refs are found.
327 test_expect_code 2 git ls-remote --exit-code git://localhost:$JGIT_DAEMON_PORT/empty.git
328'
99c08d4e 329
4539c218
JK
330test_expect_success 'ls-remote works outside repository' '
331 # It is important for this repo to be inside the nongit
332 # area, as we want a repo name that does not include
333 # slashes (because those inhibit some of our configuration
334 # lookups).
335 nongit git init --bare dst.git &&
336 nongit git ls-remote dst.git
337'
338
47bd3d0c
SG
339test_expect_success 'ls-remote --sort fails gracefully outside repository' '
340 # Use a sort key that requires access to the referenced objects.
341 nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
342 test_i18ngrep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
343'
344
631f0f8c
JK
345test_expect_success 'ls-remote patterns work with all protocol versions' '
346 git for-each-ref --format="%(objectname) %(refname)" \
bc925ce3
JS
347 refs/heads/main refs/remotes/origin/main >expect &&
348 git -c protocol.version=1 ls-remote . main >actual.v1 &&
631f0f8c 349 test_cmp expect actual.v1 &&
bc925ce3 350 git -c protocol.version=2 ls-remote . main >actual.v2 &&
631f0f8c
JK
351 test_cmp expect actual.v2
352'
353
6a139cdd
JK
354test_expect_success 'ls-remote prefixes work with all protocol versions' '
355 git for-each-ref --format="%(objectname) %(refname)" \
356 refs/heads/ refs/tags/ >expect &&
357 git -c protocol.version=1 ls-remote --heads --tags . >actual.v1 &&
358 test_cmp expect actual.v1 &&
359 git -c protocol.version=2 ls-remote --heads --tags . >actual.v2 &&
360 test_cmp expect actual.v2
361'
362
7c2c6ee7 363test_done