Sync with 'maint'
[alt-git.git] / t / t5512-ls-remote.sh
blob64b3491e4ee6f6272a0657e8a19ee67b97340c97
1 #!/bin/sh
3 test_description='git ls-remote'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 generate_references () {
12 for ref
14 oid=$(git rev-parse "$ref") &&
15 printf '%s\t%s\n' "$oid" "$ref" || return 1
16 done
19 test_expect_success 'set up fake upload-pack' '
20 # This can be used to simulate an upload-pack that just shows the
21 # contents of the "input" file (prepared with the test-tool pkt-line
22 # helper), and does not do any negotiation (since ls-remote does not
23 # need it).
24 write_script cat-input <<-\EOF
25 # send our initial advertisement/response
26 cat input
27 # soak up the flush packet from the client
28 cat
29 EOF
32 test_expect_success 'dies when no remote found' '
33 test_must_fail git ls-remote
36 test_expect_success setup '
37 >file &&
38 git add file &&
39 test_tick &&
40 git commit -m initial &&
41 git tag mark &&
42 git tag mark1.1 &&
43 git tag mark1.2 &&
44 git tag mark1.10 &&
45 git show-ref --tags -d >expected.tag.raw &&
46 sed -e "s/ / /" expected.tag.raw >expected.tag &&
47 generate_references HEAD >expected.all &&
48 git show-ref -d >refs &&
49 sed -e "s/ / /" refs >>expected.all &&
51 grep refs/heads/ expected.all >expected.branches &&
52 git remote add self "$(pwd)/.git" &&
53 git remote add self2 "."
56 test_expect_success 'ls-remote --tags .git' '
57 git ls-remote --tags .git >actual &&
58 test_cmp expected.tag actual
61 test_expect_success 'ls-remote .git' '
62 git ls-remote .git >actual &&
63 test_cmp expected.all actual
66 test_expect_success 'ls-remote --tags self' '
67 git ls-remote --tags self >actual &&
68 test_cmp expected.tag actual
71 test_expect_success 'ls-remote self' '
72 git ls-remote self >actual &&
73 test_cmp expected.all actual
76 test_expect_success 'ls-remote --branches self' '
77 git ls-remote --branches self >actual &&
78 test_cmp expected.branches actual &&
79 git ls-remote -b self >actual &&
80 test_cmp expected.branches actual
83 test_expect_success 'ls-remote -h is deprecated w/o warning' '
84 git ls-remote -h self >actual 2>warning &&
85 test_cmp expected.branches actual &&
86 test_grep ! deprecated warning
89 test_expect_success 'ls-remote --heads is deprecated and hidden w/o warning' '
90 test_expect_code 129 git ls-remote -h >short-help &&
91 test_grep ! -e --head short-help &&
92 git ls-remote --heads self >actual 2>warning &&
93 test_cmp expected.branches actual &&
94 test_grep ! deprecated warning
97 test_expect_success 'ls-remote --sort="version:refname" --tags self' '
98 generate_references \
99 refs/tags/mark \
100 refs/tags/mark1.1 \
101 refs/tags/mark1.2 \
102 refs/tags/mark1.10 >expect &&
103 git ls-remote --sort="version:refname" --tags self >actual &&
104 test_cmp expect actual
107 test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
108 generate_references \
109 refs/tags/mark1.10 \
110 refs/tags/mark1.2 \
111 refs/tags/mark1.1 \
112 refs/tags/mark >expect &&
113 git ls-remote --sort="-version:refname" --tags self >actual &&
114 test_cmp expect actual
117 test_expect_success 'ls-remote --sort="-refname" --tags self' '
118 generate_references \
119 refs/tags/mark1.2 \
120 refs/tags/mark1.10 \
121 refs/tags/mark1.1 \
122 refs/tags/mark >expect &&
123 git ls-remote --sort="-refname" --tags self >actual &&
124 test_cmp expect actual
127 test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' '
128 test_must_fail git ls-remote
131 test_expect_success 'succeeds when no remote specified but only one found' '
132 test_when_finished git remote add self2 "." &&
133 git remote remove self2 &&
134 git ls-remote
137 test_expect_success 'use "origin" when no remote specified and multiple found' '
138 URL="$(pwd)/.git" &&
139 echo "From $URL" >exp_err &&
141 git remote add origin "$URL" &&
142 git ls-remote 2>actual_err >actual &&
144 test_cmp exp_err actual_err &&
145 test_cmp expected.all actual
148 test_expect_success 'suppress "From <url>" with -q' '
149 git ls-remote -q 2>actual_err &&
150 ! test_cmp exp_err actual_err
153 test_expect_success 'use branch.<name>.remote if possible' '
155 # Test that we are indeed using branch.<name>.remote, not "origin", even
156 # though the "origin" remote has been set.
159 # setup a new remote to differentiate from "origin"
160 git clone . other.git &&
162 cd other.git &&
163 echo "$(git rev-parse HEAD) HEAD" &&
164 git show-ref | sed -e "s/ / /"
165 ) >exp &&
167 URL="other.git" &&
168 echo "From $URL" >exp_err &&
170 git remote add other $URL &&
171 git config branch.main.remote other &&
173 git ls-remote 2>actual_err >actual &&
174 test_cmp exp_err actual_err &&
175 test_cmp exp actual
178 test_expect_success 'confuses pattern as remote when no remote specified' '
179 if test_have_prereq MINGW
180 then
181 # Windows does not like asterisks in pathname
182 does_not_exist=main
183 else
184 does_not_exist="refs*main"
185 fi &&
186 cat >exp <<-EOF &&
187 fatal: '\''$does_not_exist'\'' does not appear to be a git repository
188 fatal: Could not read from remote repository.
190 Please make sure you have the correct access rights
191 and the repository exists.
194 # Do not expect "git ls-remote <pattern>" to work; ls-remote needs
195 # <remote> if you want to feed <pattern>, just like you cannot say
196 # fetch <branch>.
197 # We could just as easily have used "main"; the "*" emphasizes its
198 # role as a pattern.
199 test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
200 test_cmp exp actual
203 test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
205 git ls-remote --exit-code ./no-such-repository
206 status=$?
207 } &&
208 test $status != 2 && test $status != 0
211 test_expect_success 'Report success even when nothing matches' '
212 git ls-remote other.git "refs/nsn/*" >actual &&
213 test_must_be_empty actual
216 test_expect_success 'Report no-match with --exit-code' '
217 test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
218 test_must_be_empty actual
221 test_expect_success 'Report match with --exit-code' '
222 git ls-remote --exit-code other.git "refs/tags/*" >actual &&
223 git ls-remote . tags/mark* >expect &&
224 test_cmp expect actual
227 test_expect_success 'set up some extra tags for ref hiding' '
228 git tag magic/one &&
229 git tag magic/two
232 for configsection in transfer uploadpack
234 test_expect_success "Hide some refs with $configsection.hiderefs" '
235 test_config $configsection.hiderefs refs/tags &&
236 git ls-remote . >actual &&
237 test_unconfig $configsection.hiderefs &&
238 git ls-remote . >expect.raw &&
239 sed -e "/ refs\/tags\//d" expect.raw >expect &&
240 test_cmp expect actual
243 test_expect_success "Override hiding of $configsection.hiderefs" '
244 test_when_finished "test_unconfig $configsection.hiderefs" &&
245 git config --add $configsection.hiderefs refs/tags &&
246 git config --add $configsection.hiderefs "!refs/tags/magic" &&
247 git config --add $configsection.hiderefs refs/tags/magic/one &&
248 git ls-remote . >actual &&
249 grep refs/tags/magic/two actual &&
250 ! grep refs/tags/magic/one actual
253 done
255 test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs' '
256 test_config uploadpack.hiderefs refs/tags &&
257 test_config transfer.hiderefs "!refs/tags/magic" &&
258 git ls-remote . >actual &&
259 grep refs/tags/magic actual
262 test_expect_success 'protocol v2 supports hiderefs' '
263 test_config uploadpack.hiderefs refs/tags &&
264 git -c protocol.version=2 ls-remote . >actual &&
265 ! grep refs/tags actual
268 test_expect_success 'ls-remote --symref' '
269 git fetch origin &&
270 echo "ref: refs/heads/main HEAD" >expect.v2 &&
271 generate_references \
272 HEAD \
273 refs/heads/main >>expect.v2 &&
274 echo "ref: refs/remotes/origin/main refs/remotes/origin/HEAD" >>expect.v2 &&
275 oid=$(git rev-parse HEAD) &&
276 echo "$oid refs/remotes/origin/HEAD" >>expect.v2 &&
277 generate_references \
278 refs/remotes/origin/main \
279 refs/tags/mark \
280 refs/tags/mark1.1 \
281 refs/tags/mark1.10 \
282 refs/tags/mark1.2 >>expect.v2 &&
283 # v0 does not show non-HEAD symrefs
284 grep -v "ref: refs/remotes" <expect.v2 >expect.v0 &&
285 git -c protocol.version=0 ls-remote --symref >actual.v0 &&
286 test_cmp expect.v0 actual.v0 &&
287 git -c protocol.version=2 ls-remote --symref >actual.v2 &&
288 test_cmp expect.v2 actual.v2
291 test_expect_success 'ls-remote with filtered symref (refname)' '
292 rev=$(git rev-parse HEAD) &&
293 cat >expect <<-EOF &&
294 ref: refs/heads/main HEAD
295 $rev HEAD
297 git ls-remote --symref . HEAD >actual &&
298 test_cmp expect actual
301 test_expect_success 'ls-remote with filtered symref (--branches)' '
302 git symbolic-ref refs/heads/foo refs/tags/mark &&
303 cat >expect.v2 <<-EOF &&
304 ref: refs/tags/mark refs/heads/foo
305 $rev refs/heads/foo
306 $rev refs/heads/main
308 grep -v "^ref: refs/tags/" <expect.v2 >expect.v0 &&
309 git -c protocol.version=0 ls-remote --symref --branches . >actual.v0 &&
310 test_cmp expect.v0 actual.v0 &&
311 git -c protocol.version=2 ls-remote --symref --branches . >actual.v2 &&
312 test_cmp expect.v2 actual.v2
315 test_expect_success 'indicate no refs in v0 standards-compliant empty remote' '
316 # Git does not produce an output like this, but it does match the
317 # standard and is produced by other implementations like JGit. So
318 # hard-code the case we care about.
320 # The actual capabilities do not matter; there are none that would
321 # change how ls-remote behaves.
322 oid=0000000000000000000000000000000000000000 &&
323 test-tool pkt-line pack >input.q <<-EOF &&
324 $oid capabilities^{}Qcaps-go-here
325 0000
327 q_to_nul <input.q >input &&
329 # --exit-code asks the command to exit with 2 when no
330 # matching refs are found.
331 test_expect_code 2 git ls-remote --exit-code --upload-pack=./cat-input .
334 test_expect_success 'ls-remote works outside repository' '
335 # It is important for this repo to be inside the nongit
336 # area, as we want a repo name that does not include
337 # slashes (because those inhibit some of our configuration
338 # lookups).
339 nongit git init --bare dst.git &&
340 nongit git ls-remote dst.git
343 test_expect_success 'ls-remote --sort fails gracefully outside repository' '
344 # Use a sort key that requires access to the referenced objects.
345 nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
346 test_grep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
349 test_expect_success 'ls-remote patterns work with all protocol versions' '
350 git for-each-ref --format="%(objectname) %(refname)" \
351 refs/heads/main refs/remotes/origin/main >expect &&
352 git -c protocol.version=0 ls-remote . main >actual.v0 &&
353 test_cmp expect actual.v0 &&
354 git -c protocol.version=2 ls-remote . main >actual.v2 &&
355 test_cmp expect actual.v2
358 test_expect_success 'ls-remote prefixes work with all protocol versions' '
359 git for-each-ref --format="%(objectname) %(refname)" \
360 refs/heads/ refs/tags/ >expect &&
361 git -c protocol.version=0 ls-remote --branches --tags . >actual.v0 &&
362 test_cmp expect actual.v0 &&
363 git -c protocol.version=2 ls-remote --branches --tags . >actual.v2 &&
364 test_cmp expect actual.v2
367 test_expect_success 'v0 clients can handle multiple symrefs' '
368 # Modern versions of Git will not return multiple symref capabilities
369 # for v0, so we have to hard-code the response. Note that we will
370 # always use both v0 and object-format=sha1 here, as the hard-coded
371 # response reflects a server that only supports those.
372 oid=1234567890123456789012345678901234567890 &&
373 symrefs="symref=refs/remotes/origin/HEAD:refs/remotes/origin/main" &&
374 symrefs="$symrefs symref=HEAD:refs/heads/main" &&
376 # Likewise we want to make sure our parser is not fooled by the string
377 # "symref" appearing as part of an earlier cap. But there is no way to
378 # do that via upload-pack, as arbitrary strings can appear only in a
379 # "symref" value itself (where we skip past the values as a whole)
380 # and "agent" (which always appears after "symref", so putting our
381 # parser in a confused state is less interesting).
382 caps="some other caps including a-fake-symref-cap" &&
384 test-tool pkt-line pack >input.q <<-EOF &&
385 $oid HEADQ$caps $symrefs
386 $oid refs/heads/main
387 $oid refs/remotes/origin/HEAD
388 $oid refs/remotes/origin/main
389 0000
391 q_to_nul <input.q >input &&
393 cat >expect <<-EOF &&
394 ref: refs/heads/main HEAD
395 $oid HEAD
396 $oid refs/heads/main
397 ref: refs/remotes/origin/main refs/remotes/origin/HEAD
398 $oid refs/remotes/origin/HEAD
399 $oid refs/remotes/origin/main
402 git ls-remote --symref --upload-pack=./cat-input . >actual &&
403 test_cmp expect actual
406 test_expect_success 'helper with refspec capability fails gracefully' '
407 mkdir test-bin &&
408 write_script test-bin/git-remote-foo <<-EOF &&
409 read capabilities
410 echo import
411 echo refspec ${SQ}*:*${SQ}
414 PATH="$PWD/test-bin:$PATH" &&
415 export PATH &&
416 test_must_fail nongit git ls-remote foo::bar
420 test_done