Sync with 2.38.4
[git.git] / t / t5512-ls-remote.sh
blob20d063fb9ae72c4df77a00dd5d5cb6da65640238
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-lib.sh
10 generate_references () {
11 for ref
13 oid=$(git rev-parse "$ref") &&
14 printf '%s\t%s\n' "$oid" "$ref" || return 1
15 done
18 test_expect_success 'dies when no remote found' '
19 test_must_fail git ls-remote
22 test_expect_success setup '
23 >file &&
24 git add file &&
25 test_tick &&
26 git commit -m initial &&
27 git tag mark &&
28 git tag mark1.1 &&
29 git tag mark1.2 &&
30 git tag mark1.10 &&
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 &&
37 git remote add self "$(pwd)/.git" &&
38 git remote add self2 "."
41 test_expect_success 'ls-remote --tags .git' '
42 git ls-remote --tags .git >actual &&
43 test_cmp expected.tag actual
46 test_expect_success 'ls-remote .git' '
47 git ls-remote .git >actual &&
48 test_cmp expected.all actual
51 test_expect_success 'ls-remote --tags self' '
52 git ls-remote --tags self >actual &&
53 test_cmp expected.tag actual
56 test_expect_success 'ls-remote self' '
57 git ls-remote self >actual &&
58 test_cmp expected.all actual
61 test_expect_success 'ls-remote --sort="version:refname" --tags self' '
62 generate_references \
63 refs/tags/mark \
64 refs/tags/mark1.1 \
65 refs/tags/mark1.2 \
66 refs/tags/mark1.10 >expect &&
67 git ls-remote --sort="version:refname" --tags self >actual &&
68 test_cmp expect actual
71 test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
72 generate_references \
73 refs/tags/mark1.10 \
74 refs/tags/mark1.2 \
75 refs/tags/mark1.1 \
76 refs/tags/mark >expect &&
77 git ls-remote --sort="-version:refname" --tags self >actual &&
78 test_cmp expect actual
81 test_expect_success 'ls-remote --sort="-refname" --tags self' '
82 generate_references \
83 refs/tags/mark1.2 \
84 refs/tags/mark1.10 \
85 refs/tags/mark1.1 \
86 refs/tags/mark >expect &&
87 git ls-remote --sort="-refname" --tags self >actual &&
88 test_cmp expect actual
91 test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' '
92 test_must_fail git ls-remote
95 test_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
101 test_expect_success 'use "origin" when no remote specified and multiple found' '
102 URL="$(pwd)/.git" &&
103 echo "From $URL" >exp_err &&
105 git remote add origin "$URL" &&
106 git ls-remote 2>actual_err >actual &&
108 test_cmp exp_err actual_err &&
109 test_cmp expected.all actual
112 test_expect_success 'suppress "From <url>" with -q' '
113 git ls-remote -q 2>actual_err &&
114 ! test_cmp exp_err actual_err
117 test_expect_success 'use branch.<name>.remote if possible' '
119 # Test that we are indeed using branch.<name>.remote, not "origin", even
120 # though the "origin" remote has been set.
123 # setup a new remote to differentiate from "origin"
124 git clone . other.git &&
126 cd other.git &&
127 echo "$(git rev-parse HEAD) HEAD" &&
128 git show-ref | sed -e "s/ / /"
129 ) >exp &&
131 URL="other.git" &&
132 echo "From $URL" >exp_err &&
134 git remote add other $URL &&
135 git config branch.main.remote other &&
137 git ls-remote 2>actual_err >actual &&
138 test_cmp exp_err actual_err &&
139 test_cmp exp actual
142 test_expect_success 'confuses pattern as remote when no remote specified' '
143 if test_have_prereq MINGW
144 then
145 # Windows does not like asterisks in pathname
146 does_not_exist=main
147 else
148 does_not_exist="refs*main"
149 fi &&
150 cat >exp <<-EOF &&
151 fatal: '\''$does_not_exist'\'' does not appear to be a git repository
152 fatal: Could not read from remote repository.
154 Please make sure you have the correct access rights
155 and the repository exists.
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>.
161 # We could just as easily have used "main"; the "*" emphasizes its
162 # role as a pattern.
163 test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
164 test_cmp exp actual
167 test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
169 git ls-remote --exit-code ./no-such-repository
170 status=$?
171 } &&
172 test $status != 2 && test $status != 0
175 test_expect_success 'Report success even when nothing matches' '
176 git ls-remote other.git "refs/nsn/*" >actual &&
177 test_must_be_empty actual
180 test_expect_success 'Report no-match with --exit-code' '
181 test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
182 test_must_be_empty actual
185 test_expect_success 'Report match with --exit-code' '
186 git ls-remote --exit-code other.git "refs/tags/*" >actual &&
187 git ls-remote . tags/mark* >expect &&
188 test_cmp expect actual
191 test_expect_success 'set up some extra tags for ref hiding' '
192 git tag magic/one &&
193 git tag magic/two
196 for configsection in transfer uploadpack
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 &&
202 git ls-remote . >expect.raw &&
203 sed -e "/ refs\/tags\//d" expect.raw >expect &&
204 test_cmp expect actual
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
217 done
219 test_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
226 test_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
232 test_expect_success 'ls-remote --symref' '
233 git fetch origin &&
234 echo "ref: refs/heads/main HEAD" >expect &&
235 generate_references \
236 HEAD \
237 refs/heads/main >>expect &&
238 oid=$(git rev-parse HEAD) &&
239 echo "$oid refs/remotes/origin/HEAD" >>expect &&
240 generate_references \
241 refs/remotes/origin/main \
242 refs/tags/mark \
243 refs/tags/mark1.1 \
244 refs/tags/mark1.10 \
245 refs/tags/mark1.2 >>expect &&
246 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
247 # protocol v0 here.
248 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref >actual &&
249 test_cmp expect actual
252 test_expect_success 'ls-remote with filtered symref (refname)' '
253 rev=$(git rev-parse HEAD) &&
254 cat >expect <<-EOF &&
255 ref: refs/heads/main HEAD
256 $rev HEAD
258 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
259 # protocol v0 here.
260 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . HEAD >actual &&
261 test_cmp expect actual
264 test_expect_failure 'ls-remote with filtered symref (--heads)' '
265 git symbolic-ref refs/heads/foo refs/tags/mark &&
266 cat >expect <<-EOF &&
267 ref: refs/tags/mark refs/heads/foo
268 $rev refs/heads/foo
269 $rev refs/heads/main
271 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
272 # protocol v0 here.
273 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
274 test_cmp expect actual
277 test_expect_success 'ls-remote --symref omits filtered-out matches' '
278 cat >expect <<-EOF &&
279 $rev refs/heads/foo
280 $rev refs/heads/main
282 # Protocol v2 supports sending symrefs for refs other than HEAD, so use
283 # protocol v0 here.
284 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref --heads . >actual &&
285 test_cmp expect actual &&
286 GIT_TEST_PROTOCOL_VERSION=0 git ls-remote --symref . "refs/heads/*" >actual &&
287 test_cmp expect actual
290 test_lazy_prereq GIT_DAEMON '
291 test_bool_env GIT_TEST_GIT_DAEMON true
294 # This test spawns a daemon, so run it only if the user would be OK with
295 # testing with git-daemon.
296 test_expect_success PIPE,JGIT,GIT_DAEMON 'indicate no refs in standards-compliant empty remote' '
297 test_set_port JGIT_DAEMON_PORT &&
298 JGIT_DAEMON_PID= &&
299 git init --bare empty.git &&
300 >empty.git/git-daemon-export-ok &&
301 mkfifo jgit_daemon_output &&
303 jgit daemon --port="$JGIT_DAEMON_PORT" . >jgit_daemon_output &
304 JGIT_DAEMON_PID=$!
305 } &&
306 test_when_finished kill "$JGIT_DAEMON_PID" &&
308 read line &&
309 case $line in
310 Exporting*)
313 echo "Expected: Exporting" &&
314 false;;
315 esac &&
316 read line &&
317 case $line in
318 "Listening on"*)
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
330 test_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
339 test_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
345 test_expect_success 'ls-remote patterns work with all protocol versions' '
346 git for-each-ref --format="%(objectname) %(refname)" \
347 refs/heads/main refs/remotes/origin/main >expect &&
348 git -c protocol.version=1 ls-remote . main >actual.v1 &&
349 test_cmp expect actual.v1 &&
350 git -c protocol.version=2 ls-remote . main >actual.v2 &&
351 test_cmp expect actual.v2
354 test_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
363 test_done