3 test_description
='git ls-remote'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 generate_references
() {
13 oid
=$
(git rev-parse
"$ref") &&
14 printf '%s\t%s\n' "$oid" "$ref" ||
return 1
18 test_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
23 write_script cat-input <<-\EOF
24 # send our initial advertisement/response
26 # soak up the flush packet from the client
31 test_expect_success
'dies when no remote found' '
32 test_must_fail git ls-remote
35 test_expect_success setup
'
39 git commit -m initial &&
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 &&
50 git remote add self "$(pwd)/.git" &&
51 git remote add self2 "."
54 test_expect_success
'ls-remote --tags .git' '
55 git ls-remote --tags .git >actual &&
56 test_cmp expected.tag actual
59 test_expect_success
'ls-remote .git' '
60 git ls-remote .git >actual &&
61 test_cmp expected.all actual
64 test_expect_success
'ls-remote --tags self' '
65 git ls-remote --tags self >actual &&
66 test_cmp expected.tag actual
69 test_expect_success
'ls-remote self' '
70 git ls-remote self >actual &&
71 test_cmp expected.all actual
74 test_expect_success
'ls-remote --sort="version:refname" --tags self' '
79 refs/tags/mark1.10 >expect &&
80 git ls-remote --sort="version:refname" --tags self >actual &&
81 test_cmp expect actual
84 test_expect_success
'ls-remote --sort="-version:refname" --tags self' '
89 refs/tags/mark >expect &&
90 git ls-remote --sort="-version:refname" --tags self >actual &&
91 test_cmp expect actual
94 test_expect_success
'ls-remote --sort="-refname" --tags self' '
99 refs/tags/mark >expect &&
100 git ls-remote --sort="-refname" --tags self >actual &&
101 test_cmp expect actual
104 test_expect_success
'dies when no remote specified, multiple remotes found, and no default specified' '
105 test_must_fail git ls-remote
108 test_expect_success
'succeeds when no remote specified but only one found' '
109 test_when_finished git remote add self2 "." &&
110 git remote remove self2 &&
114 test_expect_success
'use "origin" when no remote specified and multiple found' '
116 echo "From $URL" >exp_err &&
118 git remote add origin "$URL" &&
119 git ls-remote 2>actual_err >actual &&
121 test_cmp exp_err actual_err &&
122 test_cmp expected.all actual
125 test_expect_success
'suppress "From <url>" with -q' '
126 git ls-remote -q 2>actual_err &&
127 ! test_cmp exp_err actual_err
130 test_expect_success
'use branch.<name>.remote if possible' '
132 # Test that we are indeed using branch.<name>.remote, not "origin", even
133 # though the "origin" remote has been set.
136 # setup a new remote to differentiate from "origin"
137 git clone . other.git &&
140 echo "$(git rev-parse HEAD) HEAD" &&
141 git show-ref | sed -e "s/ / /"
145 echo "From $URL" >exp_err &&
147 git remote add other $URL &&
148 git config branch.main.remote other &&
150 git ls-remote 2>actual_err >actual &&
151 test_cmp exp_err actual_err &&
155 test_expect_success
'confuses pattern as remote when no remote specified' '
156 if test_have_prereq MINGW
158 # Windows does not like asterisks in pathname
161 does_not_exist="refs*main"
164 fatal: '\''$does_not_exist'\'' does not appear to be a git repository
165 fatal: Could not read from remote repository.
167 Please make sure you have the correct access rights
168 and the repository exists.
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
174 # We could just as easily have used "main"; the "*" emphasizes its
176 test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
180 test_expect_success
'die with non-2 for wrong repository even with --exit-code' '
182 git ls-remote --exit-code ./no-such-repository
185 test $status != 2 && test $status != 0
188 test_expect_success
'Report success even when nothing matches' '
189 git ls-remote other.git "refs/nsn/*" >actual &&
190 test_must_be_empty actual
193 test_expect_success
'Report no-match with --exit-code' '
194 test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
195 test_must_be_empty actual
198 test_expect_success
'Report match with --exit-code' '
199 git ls-remote --exit-code other.git "refs/tags/*" >actual &&
200 git ls-remote . tags/mark* >expect &&
201 test_cmp expect actual
204 test_expect_success
'set up some extra tags for ref hiding' '
209 for configsection
in transfer uploadpack
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 &&
215 git ls-remote . >expect.raw &&
216 sed -e "/ refs\/tags\//d" expect.raw >expect &&
217 test_cmp expect actual
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
232 test_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
239 test_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
245 test_expect_success
'ls-remote --symref' '
247 echo "ref: refs/heads/main HEAD" >expect &&
248 generate_references \
250 refs/heads/main >>expect &&
251 oid=$(git rev-parse HEAD) &&
252 echo "$oid refs/remotes/origin/HEAD" >>expect &&
253 generate_references \
254 refs/remotes/origin/main \
258 refs/tags/mark1.2 >>expect &&
259 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
261 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref >actual &&
262 test_cmp expect actual
265 test_expect_success
'ls-remote with filtered symref (refname)' '
266 rev=$(git rev-parse HEAD) &&
267 cat >expect <<-EOF &&
268 ref: refs/heads/main HEAD
271 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
273 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . HEAD >actual &&
274 test_cmp expect actual
277 test_expect_failure
'ls-remote with filtered symref (--heads)' '
278 git symbolic-ref refs/heads/foo refs/tags/mark &&
279 cat >expect <<-EOF &&
280 ref: refs/tags/mark refs/heads/foo
284 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
286 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
287 test_cmp expect actual
290 test_expect_success
'ls-remote --symref omits filtered-out matches' '
291 cat >expect <<-EOF &&
295 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
297 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
298 test_cmp expect actual &&
299 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . "refs/heads/*" >actual &&
300 test_cmp expect actual
303 test_expect_success
'indicate no refs in v0 standards-compliant empty remote' '
304 # Git does not produce an output like this, but it does match the
305 # standard and is produced by other implementations like JGit. So
306 # hard-code the case we care about.
308 # The actual capabilities do not matter; there are none that would
309 # change how ls-remote behaves.
310 oid=0000000000000000000000000000000000000000 &&
311 test-tool pkt-line pack >input.q <<-EOF &&
312 $oid capabilities^{}Qcaps-go-here
315 q_to_nul <input.q >input &&
317 # --exit-code asks the command to exit with 2 when no
318 # matching refs are found.
319 test_expect_code 2 git ls-remote --exit-code --upload-pack=./cat-input .
322 test_expect_success
'ls-remote works outside repository' '
323 # It is important for this repo to be inside the nongit
324 # area, as we want a repo name that does not include
325 # slashes (because those inhibit some of our configuration
327 nongit git init --bare dst.git &&
328 nongit git ls-remote dst.git
331 test_expect_success
'ls-remote --sort fails gracefully outside repository' '
332 # Use a sort key that requires access to the referenced objects.
333 nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
334 test_i18ngrep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
337 test_expect_success
'ls-remote patterns work with all protocol versions' '
338 git for-each-ref --format="%(objectname) %(refname)" \
339 refs/heads/main refs/remotes/origin/main >expect &&
340 git -c protocol.version=0 ls-remote . main >actual.v0 &&
341 test_cmp expect actual.v0 &&
342 git -c protocol.version=2 ls-remote . main >actual.v2 &&
343 test_cmp expect actual.v2
346 test_expect_success
'ls-remote prefixes work with all protocol versions' '
347 git for-each-ref --format="%(objectname) %(refname)" \
348 refs/heads/ refs/tags/ >expect &&
349 git -c protocol.version=0 ls-remote --heads --tags . >actual.v0 &&
350 test_cmp expect actual.v0 &&
351 git -c protocol.version=2 ls-remote --heads --tags . >actual.v2 &&
352 test_cmp expect actual.v2
355 test_expect_success
'v0 clients can handle multiple symrefs' '
356 # Modern versions of Git will not return multiple symref capabilities
357 # for v0, so we have to hard-code the response. Note that we will
358 # always use both v0 and object-format=sha1 here, as the hard-coded
359 # response reflects a server that only supports those.
360 oid=1234567890123456789012345678901234567890 &&
361 symrefs="symref=refs/remotes/origin/HEAD:refs/remotes/origin/main" &&
362 symrefs="$symrefs symref=HEAD:refs/heads/main" &&
364 # Likewise we want to make sure our parser is not fooled by the string
365 # "symref" appearing as part of an earlier cap. But there is no way to
366 # do that via upload-pack, as arbitrary strings can appear only in a
367 # "symref" value itself (where we skip past the values as a whole)
368 # and "agent" (which always appears after "symref", so putting our
369 # parser in a confused state is less interesting).
370 caps="some other caps including a-fake-symref-cap" &&
372 test-tool pkt-line pack >input.q <<-EOF &&
373 $oid HEADQ$caps $symrefs
375 $oid refs/remotes/origin/HEAD
376 $oid refs/remotes/origin/main
379 q_to_nul <input.q >input &&
381 cat >expect <<-EOF &&
382 ref: refs/heads/main HEAD
385 ref: refs/remotes/origin/main refs/remotes/origin/HEAD
386 $oid refs/remotes/origin/HEAD
387 $oid refs/remotes/origin/main
390 git ls-remote --symref --upload-pack=./cat-input . >actual &&
391 test_cmp expect actual