]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5702-protocol-v2.sh
upload-pack: clear flags before each v2 request
[thirdparty/git.git] / t / t5702-protocol-v2.sh
CommitLineData
e52449b6
BW
1#!/bin/sh
2
3test_description='test git wire-protocol version 2'
4
5TEST_NO_CREATE_REPO=1
6
7. ./test-lib.sh
8
9# Test protocol v2 with 'git://' transport
10#
11. "$TEST_DIRECTORY"/lib-git-daemon.sh
12start_git_daemon --export-all --enable=receive-pack
13daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
14
15test_expect_success 'create repo to be served by git-daemon' '
16 git init "$daemon_parent" &&
17 test_commit -C "$daemon_parent" one
18'
19
20test_expect_success 'list refs with git:// using protocol v2' '
21 test_when_finished "rm -f log" &&
22
23 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
24 ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
25
26 # Client requested to use protocol v2
27 grep "git> .*\\\0\\\0version=2\\\0$" log &&
28 # Server responded using protocol v2
29 grep "git< version 2" log &&
30
31 git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
dcbaa0b3 32 test_cmp expect actual
e52449b6
BW
33'
34
b4be7410
BW
35test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
36 test_when_finished "rm -f log" &&
37
38 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
39 ls-remote "$GIT_DAEMON_URL/parent" master >actual &&
40
41 cat >expect <<-EOF &&
42 $(git -C "$daemon_parent" rev-parse refs/heads/master)$(printf "\t")refs/heads/master
43 EOF
44
dcbaa0b3 45 test_cmp expect actual
b4be7410
BW
46'
47
685fbd32
BW
48test_expect_success 'clone with git:// using protocol v2' '
49 test_when_finished "rm -f log" &&
50
51 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
52 clone "$GIT_DAEMON_URL/parent" daemon_child &&
53
54 git -C daemon_child log -1 --format=%s >actual &&
55 git -C "$daemon_parent" log -1 --format=%s >expect &&
56 test_cmp expect actual &&
57
58 # Client requested to use protocol v2
59 grep "clone> .*\\\0\\\0version=2\\\0$" log &&
60 # Server responded using protocol v2
61 grep "clone< version 2" log
62'
63
64test_expect_success 'fetch with git:// using protocol v2' '
65 test_when_finished "rm -f log" &&
66
67 test_commit -C "$daemon_parent" two &&
68
69 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
70 fetch &&
71
72 git -C daemon_child log -1 --format=%s origin/master >actual &&
73 git -C "$daemon_parent" log -1 --format=%s >expect &&
74 test_cmp expect actual &&
75
76 # Client requested to use protocol v2
77 grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
78 # Server responded using protocol v2
79 grep "fetch< version 2" log
80'
81
82test_expect_success 'pull with git:// using protocol v2' '
83 test_when_finished "rm -f log" &&
84
85 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
86 pull &&
87
88 git -C daemon_child log -1 --format=%s >actual &&
89 git -C "$daemon_parent" log -1 --format=%s >expect &&
90 test_cmp expect actual &&
91
92 # Client requested to use protocol v2
93 grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
94 # Server responded using protocol v2
95 grep "fetch< version 2" log
96'
97
1aa8dded
BW
98test_expect_success 'push with git:// and a config of v2 does not request v2' '
99 test_when_finished "rm -f log" &&
100
101 # Till v2 for push is designed, make sure that if a client has
102 # protocol.version configured to use v2, that the client instead falls
103 # back and uses v0.
104
105 test_commit -C daemon_child three &&
106
107 # Push to another branch, as the target repository has the
108 # master branch checked out and we cannot push into it.
109 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
110 push origin HEAD:client_branch &&
111
112 git -C daemon_child log -1 --format=%s >actual &&
113 git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
114 test_cmp expect actual &&
115
116 # Client requested to use protocol v2
117 ! grep "push> .*\\\0\\\0version=2\\\0$" log &&
118 # Server responded using protocol v2
119 ! grep "push< version 2" log
120'
121
e52449b6
BW
122stop_git_daemon
123
124# Test protocol v2 with 'file://' transport
125#
126test_expect_success 'create repo to be served by file:// transport' '
127 git init file_parent &&
128 test_commit -C file_parent one
129'
130
131test_expect_success 'list refs with file:// using protocol v2' '
132 test_when_finished "rm -f log" &&
133
134 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
135 ls-remote --symref "file://$(pwd)/file_parent" >actual &&
136
137 # Server responded using protocol v2
138 grep "git< version 2" log &&
139
140 git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
dcbaa0b3 141 test_cmp expect actual
e52449b6
BW
142'
143
b4be7410
BW
144test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
145 test_when_finished "rm -f log" &&
146
147 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
148 ls-remote "file://$(pwd)/file_parent" master >actual &&
149
150 cat >expect <<-EOF &&
151 $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
152 EOF
153
dcbaa0b3 154 test_cmp expect actual
b4be7410
BW
155'
156
ff473221
BW
157test_expect_success 'server-options are sent when using ls-remote' '
158 test_when_finished "rm -f log" &&
159
160 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
161 ls-remote -o hello -o world "file://$(pwd)/file_parent" master >actual &&
162
163 cat >expect <<-EOF &&
164 $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
165 EOF
166
dcbaa0b3 167 test_cmp expect actual &&
ff473221
BW
168 grep "server-option=hello" log &&
169 grep "server-option=world" log
170'
171
172
685fbd32
BW
173test_expect_success 'clone with file:// using protocol v2' '
174 test_when_finished "rm -f log" &&
175
176 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
177 clone "file://$(pwd)/file_parent" file_child &&
178
179 git -C file_child log -1 --format=%s >actual &&
180 git -C file_parent log -1 --format=%s >expect &&
181 test_cmp expect actual &&
182
183 # Server responded using protocol v2
402c47d9
BW
184 grep "clone< version 2" log &&
185
186 # Client sent ref-prefixes to filter the ref-advertisement
187 grep "ref-prefix HEAD" log &&
188 grep "ref-prefix refs/heads/" log &&
189 grep "ref-prefix refs/tags/" log
685fbd32
BW
190'
191
192test_expect_success 'fetch with file:// using protocol v2' '
193 test_when_finished "rm -f log" &&
194
195 test_commit -C file_parent two &&
196
197 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
198 fetch origin &&
199
200 git -C file_child log -1 --format=%s origin/master >actual &&
201 git -C file_parent log -1 --format=%s >expect &&
202 test_cmp expect actual &&
203
204 # Server responded using protocol v2
205 grep "fetch< version 2" log
206'
207
208test_expect_success 'ref advertisment is filtered during fetch using protocol v2' '
209 test_when_finished "rm -f log" &&
210
211 test_commit -C file_parent three &&
2b554353 212 git -C file_parent branch unwanted-branch three &&
685fbd32
BW
213
214 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
215 fetch origin master &&
216
217 git -C file_child log -1 --format=%s origin/master >actual &&
218 git -C file_parent log -1 --format=%s >expect &&
219 test_cmp expect actual &&
220
2b554353
JT
221 grep "refs/heads/master" log &&
222 ! grep "refs/heads/unwanted-branch" log
685fbd32
BW
223'
224
5e3548ef
BW
225test_expect_success 'server-options are sent when fetching' '
226 test_when_finished "rm -f log" &&
227
228 test_commit -C file_parent four &&
229
230 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
231 fetch -o hello -o world origin master &&
232
233 git -C file_child log -1 --format=%s origin/master >actual &&
234 git -C file_parent log -1 --format=%s >expect &&
235 test_cmp expect actual &&
236
237 grep "server-option=hello" log &&
238 grep "server-option=world" log
239'
240
54592687
JT
241test_expect_success 'upload-pack respects config using protocol v2' '
242 git init server &&
243 write_script server/.git/hook <<-\EOF &&
244 touch hookout
245 "$@"
246 EOF
247 test_commit -C server one &&
248
249 test_config_global uploadpack.packobjectshook ./hook &&
250 test_path_is_missing server/.git/hookout &&
251 git -c protocol.version=2 clone "file://$(pwd)/server" client &&
252 test_path_is_file server/.git/hookout
253'
254
ba95710a
JT
255test_expect_success 'setup filter tests' '
256 rm -rf server client &&
257 git init server &&
258
259 # 1 commit to create a file, and 1 commit to modify it
260 test_commit -C server message1 a.txt &&
261 test_commit -C server message2 a.txt &&
262 git -C server config protocol.version 2 &&
263 git -C server config uploadpack.allowfilter 1 &&
264 git -C server config uploadpack.allowanysha1inwant 1 &&
265 git -C server config protocol.version 2
266'
267
268test_expect_success 'partial clone' '
269 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
270 clone --filter=blob:none "file://$(pwd)/server" client &&
271 grep "version 2" trace &&
272
273 # Ensure that the old version of the file is missing
8d6ba495 274 git -C client rev-list --quiet --objects --missing=print master \
ba95710a
JT
275 >observed.oids &&
276 grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
277
278 # Ensure that client passes fsck
279 git -C client fsck
280'
281
282test_expect_success 'dynamically fetch missing object' '
283 rm "$(pwd)/trace" &&
284 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
285 cat-file -p $(git -C server rev-parse message1:a.txt) &&
286 grep "version 2" trace
287'
288
289test_expect_success 'partial fetch' '
290 rm -rf client "$(pwd)/trace" &&
291 git init client &&
292 SERVER="file://$(pwd)/server" &&
293 test_config -C client extensions.partialClone "$SERVER" &&
294
295 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
296 fetch --filter=blob:none "$SERVER" master:refs/heads/other &&
297 grep "version 2" trace &&
298
299 # Ensure that the old version of the file is missing
8d6ba495 300 git -C client rev-list --quiet --objects --missing=print other \
ba95710a
JT
301 >observed.oids &&
302 grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
303
304 # Ensure that client passes fsck
305 git -C client fsck
306'
307
308test_expect_success 'do not advertise filter if not configured to do so' '
309 SERVER="file://$(pwd)/server" &&
310
311 rm "$(pwd)/trace" &&
312 git -C server config uploadpack.allowfilter 1 &&
313 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
314 ls-remote "$SERVER" &&
315 grep "fetch=.*filter" trace &&
316
317 rm "$(pwd)/trace" &&
318 git -C server config uploadpack.allowfilter 0 &&
319 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
320 ls-remote "$SERVER" &&
321 grep "fetch=" trace >fetch_capabilities &&
322 ! grep filter fetch_capabilities
323'
324
325test_expect_success 'partial clone warns if filter is not advertised' '
326 rm -rf client &&
327 git -C server config uploadpack.allowfilter 0 &&
328 git -c protocol.version=2 \
329 clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
330 test_i18ngrep "filtering not recognized by server, ignoring" err
331'
332
333test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
334 git -C server config uploadpack.allowfilter 0 &&
335
336 # Custom request that tries to filter even though it is not advertised.
8ea40cc5 337 test-tool pkt-line pack >in <<-EOF &&
ba95710a
JT
338 command=fetch
339 0001
340 want $(git -C server rev-parse master)
341 filter blob:none
342 0000
343 EOF
344
345 test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
346 grep "unexpected line: .filter blob:none." err &&
347
348 # Exercise to ensure that if advertised, filter works
349 git -C server config uploadpack.allowfilter 1 &&
350 git -C server serve --stateless-rpc <in >/dev/null
351'
352
dcc73cf7
BW
353test_expect_success 'default refspec is used to filter ref when fetchcing' '
354 test_when_finished "rm -f log" &&
355
356 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
357 fetch origin &&
358
359 git -C file_child log -1 --format=%s three >actual &&
360 git -C file_parent log -1 --format=%s three >expect &&
361 test_cmp expect actual &&
362
363 grep "ref-prefix refs/heads/" log &&
364 grep "ref-prefix refs/tags/" log
365'
366
15cfc985
JT
367test_expect_success 'fetch supports various ways of have lines' '
368 rm -rf server client trace &&
369 git init server &&
370 test_commit -C server dwim &&
371 TREE=$(git -C server rev-parse HEAD^{tree}) &&
372 git -C server tag exact \
373 $(git -C server commit-tree -m a "$TREE") &&
374 git -C server tag dwim-unwanted \
375 $(git -C server commit-tree -m b "$TREE") &&
376 git -C server tag exact-unwanted \
377 $(git -C server commit-tree -m c "$TREE") &&
378 git -C server tag prefix1 \
379 $(git -C server commit-tree -m d "$TREE") &&
380 git -C server tag prefix2 \
381 $(git -C server commit-tree -m e "$TREE") &&
382 git -C server tag fetch-by-sha1 \
383 $(git -C server commit-tree -m f "$TREE") &&
384 git -C server tag completely-unrelated \
385 $(git -C server commit-tree -m g "$TREE") &&
386
387 git init client &&
388 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
389 fetch "file://$(pwd)/server" \
390 dwim \
391 refs/tags/exact \
392 refs/tags/prefix*:refs/tags/prefix* \
393 "$(git -C server rev-parse fetch-by-sha1)" &&
394
395 # Ensure that the appropriate prefixes are sent (using a sample)
396 grep "fetch> ref-prefix dwim" trace &&
397 grep "fetch> ref-prefix refs/heads/dwim" trace &&
398 grep "fetch> ref-prefix refs/tags/prefix" trace &&
399
400 # Ensure that the correct objects are returned
401 git -C client cat-file -e $(git -C server rev-parse dwim) &&
402 git -C client cat-file -e $(git -C server rev-parse exact) &&
403 git -C client cat-file -e $(git -C server rev-parse prefix1) &&
404 git -C client cat-file -e $(git -C server rev-parse prefix2) &&
405 git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
406 test_must_fail git -C client cat-file -e \
407 $(git -C server rev-parse dwim-unwanted) &&
408 test_must_fail git -C client cat-file -e \
409 $(git -C server rev-parse exact-unwanted) &&
410 test_must_fail git -C client cat-file -e \
411 $(git -C server rev-parse completely-unrelated)
412'
413
2b554353
JT
414test_expect_success 'fetch supports include-tag and tag following' '
415 rm -rf server client trace &&
416 git init server &&
417
418 test_commit -C server to_fetch &&
419 git -C server tag -a annotated_tag -m message &&
420
421 git init client &&
422 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
423 fetch "$(pwd)/server" to_fetch:to_fetch &&
424
425 grep "fetch> ref-prefix to_fetch" trace &&
426 grep "fetch> ref-prefix refs/tags/" trace &&
427 grep "fetch> include-tag" trace &&
428
429 git -C client cat-file -e $(git -C client rev-parse annotated_tag)
430'
431
d1035cac
JT
432test_expect_success 'upload-pack respects client shallows' '
433 rm -rf server client trace &&
434
435 git init server &&
436 test_commit -C server base &&
437 test_commit -C server client_has &&
438
439 git clone --depth=1 "file://$(pwd)/server" client &&
440
441 # Add extra commits to the client so that the whole fetch takes more
442 # than 1 request (due to negotiation)
443 for i in $(test_seq 1 32)
444 do
445 test_commit -C client c$i
446 done &&
447
448 git -C server checkout -b newbranch base &&
449 test_commit -C server client_wants &&
450
451 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
452 fetch origin newbranch &&
453 # Ensure that protocol v2 is used
454 grep "fetch< version 2" trace
455'
456
0f1dc53f
BW
457# Test protocol v2 with 'http://' transport
458#
459. "$TEST_DIRECTORY"/lib-httpd.sh
460start_httpd
461
462test_expect_success 'create repo to be served by http:// transport' '
463 git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
464 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
465 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
466'
467
468test_expect_success 'clone with http:// using protocol v2' '
469 test_when_finished "rm -f log" &&
470
471 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
472 clone "$HTTPD_URL/smart/http_parent" http_child &&
473
474 git -C http_child log -1 --format=%s >actual &&
475 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
476 test_cmp expect actual &&
477
478 # Client requested to use protocol v2
479 grep "Git-Protocol: version=2" log &&
480 # Server responded using protocol v2
481 grep "git< version 2" log
482'
483
484test_expect_success 'fetch with http:// using protocol v2' '
485 test_when_finished "rm -f log" &&
486
487 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
488
489 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
490 fetch &&
491
492 git -C http_child log -1 --format=%s origin/master >actual &&
493 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
494 test_cmp expect actual &&
495
496 # Server responded using protocol v2
497 grep "git< version 2" log
498'
499
a4d78ce2
BW
500test_expect_success 'push with http:// and a config of v2 does not request v2' '
501 test_when_finished "rm -f log" &&
502 # Till v2 for push is designed, make sure that if a client has
503 # protocol.version configured to use v2, that the client instead falls
504 # back and uses v0.
505
506 test_commit -C http_child three &&
507
508 # Push to another branch, as the target repository has the
509 # master branch checked out and we cannot push into it.
510 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
511 push origin HEAD:client_branch &&
512
513 git -C http_child log -1 --format=%s >actual &&
514 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
515 test_cmp expect actual &&
516
517 # Client didnt request to use protocol v2
518 ! grep "Git-Protocol: version=2" log &&
519 # Server didnt respond using protocol v2
520 ! grep "git< version 2" log
521'
522
523
0f1dc53f
BW
524stop_httpd
525
e52449b6 526test_done