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