Merge branch 'rs/bisect-oid-to-hex-fix'
[git] / t / t5702-protocol-v2.sh
1 #!/bin/sh
2
3 test_description='test git wire-protocol version 2'
4
5 TEST_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
12 start_git_daemon --export-all --enable=receive-pack
13 daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
14
15 test_expect_success 'create repo to be served by git-daemon' '
16         test_oid_init &&
17         git init "$daemon_parent" &&
18         test_commit -C "$daemon_parent" one
19 '
20
21 test_expect_success 'list refs with git:// using protocol v2' '
22         test_when_finished "rm -f log" &&
23
24         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
25                 ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
26
27         # Client requested to use protocol v2
28         grep "git> .*\\\0\\\0version=2\\\0$" log &&
29         # Server responded using protocol v2
30         grep "git< version 2" log &&
31
32         git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
33         test_cmp expect actual
34 '
35
36 test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
37         test_when_finished "rm -f log" &&
38
39         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
40                 ls-remote "$GIT_DAEMON_URL/parent" master >actual &&
41
42         cat >expect <<-EOF &&
43         $(git -C "$daemon_parent" rev-parse refs/heads/master)$(printf "\t")refs/heads/master
44         EOF
45
46         test_cmp expect actual
47 '
48
49 test_expect_success 'clone with git:// using protocol v2' '
50         test_when_finished "rm -f log" &&
51
52         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
53                 clone "$GIT_DAEMON_URL/parent" daemon_child &&
54
55         git -C daemon_child log -1 --format=%s >actual &&
56         git -C "$daemon_parent" log -1 --format=%s >expect &&
57         test_cmp expect actual &&
58
59         # Client requested to use protocol v2
60         grep "clone> .*\\\0\\\0version=2\\\0$" log &&
61         # Server responded using protocol v2
62         grep "clone< version 2" log
63 '
64
65 test_expect_success 'fetch with git:// using protocol v2' '
66         test_when_finished "rm -f log" &&
67
68         test_commit -C "$daemon_parent" two &&
69
70         GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
71                 fetch &&
72
73         git -C daemon_child log -1 --format=%s origin/master >actual &&
74         git -C "$daemon_parent" log -1 --format=%s >expect &&
75         test_cmp expect actual &&
76
77         # Client requested to use protocol v2
78         grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
79         # Server responded using protocol v2
80         grep "fetch< version 2" log
81 '
82
83 test_expect_success 'fetch by hash without tag following with protocol v2 does not list refs' '
84         test_when_finished "rm -f log" &&
85
86         test_commit -C "$daemon_parent" two_a &&
87         git -C "$daemon_parent" rev-parse two_a >two_a_hash &&
88
89         GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
90                 fetch --no-tags origin $(cat two_a_hash) &&
91
92         grep "fetch< version 2" log &&
93         ! grep "fetch> command=ls-refs" log
94 '
95
96 test_expect_success 'pull with git:// using protocol v2' '
97         test_when_finished "rm -f log" &&
98
99         GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
100                 pull &&
101
102         git -C daemon_child log -1 --format=%s >actual &&
103         git -C "$daemon_parent" log -1 --format=%s >expect &&
104         test_cmp expect actual &&
105
106         # Client requested to use protocol v2
107         grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
108         # Server responded using protocol v2
109         grep "fetch< version 2" log
110 '
111
112 test_expect_success 'push with git:// and a config of v2 does not request v2' '
113         test_when_finished "rm -f log" &&
114
115         # Till v2 for push is designed, make sure that if a client has
116         # protocol.version configured to use v2, that the client instead falls
117         # back and uses v0.
118
119         test_commit -C daemon_child three &&
120
121         # Push to another branch, as the target repository has the
122         # master branch checked out and we cannot push into it.
123         GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
124                 push origin HEAD:client_branch &&
125
126         git -C daemon_child log -1 --format=%s >actual &&
127         git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
128         test_cmp expect actual &&
129
130         # Client requested to use protocol v2
131         ! grep "push> .*\\\0\\\0version=2\\\0$" log &&
132         # Server responded using protocol v2
133         ! grep "push< version 2" log
134 '
135
136 stop_git_daemon
137
138 # Test protocol v2 with 'file://' transport
139 #
140 test_expect_success 'create repo to be served by file:// transport' '
141         git init file_parent &&
142         test_commit -C file_parent one
143 '
144
145 test_expect_success 'list refs with file:// using protocol v2' '
146         test_when_finished "rm -f log" &&
147
148         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
149                 ls-remote --symref "file://$(pwd)/file_parent" >actual &&
150
151         # Server responded using protocol v2
152         grep "git< version 2" log &&
153
154         git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
155         test_cmp expect actual
156 '
157
158 test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
159         test_when_finished "rm -f log" &&
160
161         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
162                 ls-remote "file://$(pwd)/file_parent" master >actual &&
163
164         cat >expect <<-EOF &&
165         $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
166         EOF
167
168         test_cmp expect actual
169 '
170
171 test_expect_success 'server-options are sent when using ls-remote' '
172         test_when_finished "rm -f log" &&
173
174         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
175                 ls-remote -o hello -o world "file://$(pwd)/file_parent" master >actual &&
176
177         cat >expect <<-EOF &&
178         $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
179         EOF
180
181         test_cmp expect actual &&
182         grep "server-option=hello" log &&
183         grep "server-option=world" log
184 '
185
186 test_expect_success 'warn if using server-option with ls-remote with legacy protocol' '
187         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
188                 ls-remote -o hello -o world "file://$(pwd)/file_parent" master 2>err &&
189
190         test_i18ngrep "see protocol.version in" err &&
191         test_i18ngrep "server options require protocol version 2 or later" err
192 '
193
194 test_expect_success 'clone with file:// using protocol v2' '
195         test_when_finished "rm -f log" &&
196
197         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
198                 clone "file://$(pwd)/file_parent" file_child &&
199
200         git -C file_child log -1 --format=%s >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 "clone< version 2" log &&
206
207         # Client sent ref-prefixes to filter the ref-advertisement
208         grep "ref-prefix HEAD" log &&
209         grep "ref-prefix refs/heads/" log &&
210         grep "ref-prefix refs/tags/" log
211 '
212
213 test_expect_success 'fetch with file:// using protocol v2' '
214         test_when_finished "rm -f log" &&
215
216         test_commit -C file_parent two &&
217
218         GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
219                 fetch origin &&
220
221         git -C file_child log -1 --format=%s origin/master >actual &&
222         git -C file_parent log -1 --format=%s >expect &&
223         test_cmp expect actual &&
224
225         # Server responded using protocol v2
226         grep "fetch< version 2" log
227 '
228
229 test_expect_success 'ref advertisement is filtered during fetch using protocol v2' '
230         test_when_finished "rm -f log" &&
231
232         test_commit -C file_parent three &&
233         git -C file_parent branch unwanted-branch three &&
234
235         GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
236                 fetch origin master &&
237
238         git -C file_child log -1 --format=%s origin/master >actual &&
239         git -C file_parent log -1 --format=%s >expect &&
240         test_cmp expect actual &&
241
242         grep "refs/heads/master" log &&
243         ! grep "refs/heads/unwanted-branch" log
244 '
245
246 test_expect_success 'server-options are sent when fetching' '
247         test_when_finished "rm -f log" &&
248
249         test_commit -C file_parent four &&
250
251         GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
252                 fetch -o hello -o world origin master &&
253
254         git -C file_child log -1 --format=%s origin/master >actual &&
255         git -C file_parent log -1 --format=%s >expect &&
256         test_cmp expect actual &&
257
258         grep "server-option=hello" log &&
259         grep "server-option=world" log
260 '
261
262 test_expect_success 'warn if using server-option with fetch with legacy protocol' '
263         test_when_finished "rm -rf temp_child" &&
264
265         git init temp_child &&
266
267         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C temp_child -c protocol.version=0 \
268                 fetch -o hello -o world "file://$(pwd)/file_parent" master 2>err &&
269
270         test_i18ngrep "see protocol.version in" err &&
271         test_i18ngrep "server options require protocol version 2 or later" err
272 '
273
274 test_expect_success 'server-options are sent when cloning' '
275         test_when_finished "rm -rf log myclone" &&
276
277         GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
278                 clone --server-option=hello --server-option=world \
279                 "file://$(pwd)/file_parent" myclone &&
280
281         grep "server-option=hello" log &&
282         grep "server-option=world" log
283 '
284
285 test_expect_success 'warn if using server-option with clone with legacy protocol' '
286         test_when_finished "rm -rf myclone" &&
287
288         test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
289                 clone --server-option=hello --server-option=world \
290                 "file://$(pwd)/file_parent" myclone 2>err &&
291
292         test_i18ngrep "see protocol.version in" err &&
293         test_i18ngrep "server options require protocol version 2 or later" err
294 '
295
296 test_expect_success 'upload-pack respects config using protocol v2' '
297         git init server &&
298         write_script server/.git/hook <<-\EOF &&
299                 touch hookout
300                 "$@"
301         EOF
302         test_commit -C server one &&
303
304         test_config_global uploadpack.packobjectshook ./hook &&
305         test_path_is_missing server/.git/hookout &&
306         git -c protocol.version=2 clone "file://$(pwd)/server" client &&
307         test_path_is_file server/.git/hookout
308 '
309
310 test_expect_success 'setup filter tests' '
311         rm -rf server client &&
312         git init server &&
313
314         # 1 commit to create a file, and 1 commit to modify it
315         test_commit -C server message1 a.txt &&
316         test_commit -C server message2 a.txt &&
317         git -C server config protocol.version 2 &&
318         git -C server config uploadpack.allowfilter 1 &&
319         git -C server config uploadpack.allowanysha1inwant 1 &&
320         git -C server config protocol.version 2
321 '
322
323 test_expect_success 'partial clone' '
324         GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
325                 clone --filter=blob:none "file://$(pwd)/server" client &&
326         grep "version 2" trace &&
327
328         # Ensure that the old version of the file is missing
329         git -C client rev-list --quiet --objects --missing=print master \
330                 >observed.oids &&
331         grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
332
333         # Ensure that client passes fsck
334         git -C client fsck
335 '
336
337 test_expect_success 'dynamically fetch missing object' '
338         rm "$(pwd)/trace" &&
339         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
340                 cat-file -p $(git -C server rev-parse message1:a.txt) &&
341         grep "version 2" trace
342 '
343
344 test_expect_success 'when dynamically fetching missing object, do not list refs' '
345         ! grep "git> command=ls-refs" trace
346 '
347
348 test_expect_success 'partial fetch' '
349         rm -rf client "$(pwd)/trace" &&
350         git init client &&
351         SERVER="file://$(pwd)/server" &&
352
353         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
354                 fetch --filter=blob:none "$SERVER" master:refs/heads/other &&
355         grep "version 2" trace &&
356
357         # Ensure that the old version of the file is missing
358         git -C client rev-list --quiet --objects --missing=print other \
359                 >observed.oids &&
360         grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
361
362         # Ensure that client passes fsck
363         git -C client fsck
364 '
365
366 test_expect_success 'do not advertise filter if not configured to do so' '
367         SERVER="file://$(pwd)/server" &&
368
369         rm "$(pwd)/trace" &&
370         git -C server config uploadpack.allowfilter 1 &&
371         GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
372                 ls-remote "$SERVER" &&
373         grep "fetch=.*filter" trace &&
374
375         rm "$(pwd)/trace" &&
376         git -C server config uploadpack.allowfilter 0 &&
377         GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
378                 ls-remote "$SERVER" &&
379         grep "fetch=" trace >fetch_capabilities &&
380         ! grep filter fetch_capabilities
381 '
382
383 test_expect_success 'partial clone warns if filter is not advertised' '
384         rm -rf client &&
385         git -C server config uploadpack.allowfilter 0 &&
386         git -c protocol.version=2 \
387                 clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
388         test_i18ngrep "filtering not recognized by server, ignoring" err
389 '
390
391 test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
392         git -C server config uploadpack.allowfilter 0 &&
393
394         # Custom request that tries to filter even though it is not advertised.
395         test-tool pkt-line pack >in <<-EOF &&
396         command=fetch
397         object-format=$(test_oid algo)
398         0001
399         want $(git -C server rev-parse master)
400         filter blob:none
401         0000
402         EOF
403
404         test_must_fail test-tool -C server serve-v2 --stateless-rpc \
405                 <in >/dev/null 2>err &&
406         grep "unexpected line: .filter blob:none." err &&
407
408         # Exercise to ensure that if advertised, filter works
409         git -C server config uploadpack.allowfilter 1 &&
410         test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
411 '
412
413 test_expect_success 'default refspec is used to filter ref when fetchcing' '
414         test_when_finished "rm -f log" &&
415
416         GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
417                 fetch origin &&
418
419         git -C file_child log -1 --format=%s three >actual &&
420         git -C file_parent log -1 --format=%s three >expect &&
421         test_cmp expect actual &&
422
423         grep "ref-prefix refs/heads/" log &&
424         grep "ref-prefix refs/tags/" log
425 '
426
427 test_expect_success 'fetch supports various ways of have lines' '
428         rm -rf server client trace &&
429         git init server &&
430         test_commit -C server dwim &&
431         TREE=$(git -C server rev-parse HEAD^{tree}) &&
432         git -C server tag exact \
433                 $(git -C server commit-tree -m a "$TREE") &&
434         git -C server tag dwim-unwanted \
435                 $(git -C server commit-tree -m b "$TREE") &&
436         git -C server tag exact-unwanted \
437                 $(git -C server commit-tree -m c "$TREE") &&
438         git -C server tag prefix1 \
439                 $(git -C server commit-tree -m d "$TREE") &&
440         git -C server tag prefix2 \
441                 $(git -C server commit-tree -m e "$TREE") &&
442         git -C server tag fetch-by-sha1 \
443                 $(git -C server commit-tree -m f "$TREE") &&
444         git -C server tag completely-unrelated \
445                 $(git -C server commit-tree -m g "$TREE") &&
446
447         git init client &&
448         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
449                 fetch "file://$(pwd)/server" \
450                 dwim \
451                 refs/tags/exact \
452                 refs/tags/prefix*:refs/tags/prefix* \
453                 "$(git -C server rev-parse fetch-by-sha1)" &&
454
455         # Ensure that the appropriate prefixes are sent (using a sample)
456         grep "fetch> ref-prefix dwim" trace &&
457         grep "fetch> ref-prefix refs/heads/dwim" trace &&
458         grep "fetch> ref-prefix refs/tags/prefix" trace &&
459
460         # Ensure that the correct objects are returned
461         git -C client cat-file -e $(git -C server rev-parse dwim) &&
462         git -C client cat-file -e $(git -C server rev-parse exact) &&
463         git -C client cat-file -e $(git -C server rev-parse prefix1) &&
464         git -C client cat-file -e $(git -C server rev-parse prefix2) &&
465         git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
466         test_must_fail git -C client cat-file -e \
467                 $(git -C server rev-parse dwim-unwanted) &&
468         test_must_fail git -C client cat-file -e \
469                 $(git -C server rev-parse exact-unwanted) &&
470         test_must_fail git -C client cat-file -e \
471                 $(git -C server rev-parse completely-unrelated)
472 '
473
474 test_expect_success 'fetch supports include-tag and tag following' '
475         rm -rf server client trace &&
476         git init server &&
477
478         test_commit -C server to_fetch &&
479         git -C server tag -a annotated_tag -m message &&
480
481         git init client &&
482         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
483                 fetch "$(pwd)/server" to_fetch:to_fetch &&
484
485         grep "fetch> ref-prefix to_fetch" trace &&
486         grep "fetch> ref-prefix refs/tags/" trace &&
487         grep "fetch> include-tag" trace &&
488
489         git -C client cat-file -e $(git -C client rev-parse annotated_tag)
490 '
491
492 test_expect_success 'upload-pack respects client shallows' '
493         rm -rf server client trace &&
494
495         git init server &&
496         test_commit -C server base &&
497         test_commit -C server client_has &&
498
499         git clone --depth=1 "file://$(pwd)/server" client &&
500
501         # Add extra commits to the client so that the whole fetch takes more
502         # than 1 request (due to negotiation)
503         test_commit_bulk -C client --id=c 32 &&
504
505         git -C server checkout -b newbranch base &&
506         test_commit -C server client_wants &&
507
508         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
509                 fetch origin newbranch &&
510         # Ensure that protocol v2 is used
511         grep "fetch< version 2" trace
512 '
513
514 test_expect_success 'ensure that multiple fetches in same process from a shallow repo works' '
515         rm -rf server client trace &&
516
517         test_create_repo server &&
518         test_commit -C server one &&
519         test_commit -C server two &&
520         test_commit -C server three &&
521         git clone --shallow-exclude two "file://$(pwd)/server" client &&
522
523         git -C server tag -a -m "an annotated tag" twotag two &&
524
525         # Triggers tag following (thus, 2 fetches in one process)
526         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
527                 fetch --shallow-exclude one origin &&
528         # Ensure that protocol v2 is used
529         grep "fetch< version 2" trace
530 '
531
532 test_expect_success 'deepen-relative' '
533         rm -rf server client trace &&
534
535         test_create_repo server &&
536         test_commit -C server one &&
537         test_commit -C server two &&
538         test_commit -C server three &&
539         git clone --depth 1 "file://$(pwd)/server" client &&
540         test_commit -C server four &&
541
542         # Sanity check that only "three" is downloaded
543         git -C client log --pretty=tformat:%s master >actual &&
544         echo three >expected &&
545         test_cmp expected actual &&
546
547         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
548                 fetch --deepen=1 origin &&
549         # Ensure that protocol v2 is used
550         grep "fetch< version 2" trace &&
551
552         git -C client log --pretty=tformat:%s origin/master >actual &&
553         cat >expected <<-\EOF &&
554         four
555         three
556         two
557         EOF
558         test_cmp expected actual
559 '
560
561 # Test protocol v2 with 'http://' transport
562 #
563 . "$TEST_DIRECTORY"/lib-httpd.sh
564 start_httpd
565
566 test_expect_success 'create repo to be served by http:// transport' '
567         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
568         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
569         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
570 '
571
572 test_expect_success 'clone with http:// using protocol v2' '
573         test_when_finished "rm -f log" &&
574
575         GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
576                 clone "$HTTPD_URL/smart/http_parent" http_child &&
577
578         git -C http_child log -1 --format=%s >actual &&
579         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
580         test_cmp expect actual &&
581
582         # Client requested to use protocol v2
583         grep "Git-Protocol: version=2" log &&
584         # Server responded using protocol v2
585         grep "git< version 2" log &&
586         # Verify that the chunked encoding sending codepath is NOT exercised
587         ! grep "Send header: Transfer-Encoding: chunked" log
588 '
589
590 test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline length' '
591         test_when_finished "rm -f log" &&
592
593         git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" &&
594         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" file &&
595
596         test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
597                 clone "$HTTPD_URL/smart/incomplete_length" incomplete_length_child 2>err &&
598
599         # Client requested to use protocol v2
600         grep "Git-Protocol: version=2" log &&
601         # Server responded using protocol v2
602         grep "git< version 2" log &&
603         # Client reported appropriate failure
604         test_i18ngrep "bytes of length header were received" err
605 '
606
607 test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline body' '
608         test_when_finished "rm -f log" &&
609
610         git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" &&
611         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" file &&
612
613         test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
614                 clone "$HTTPD_URL/smart/incomplete_body" incomplete_body_child 2>err &&
615
616         # Client requested to use protocol v2
617         grep "Git-Protocol: version=2" log &&
618         # Server responded using protocol v2
619         grep "git< version 2" log &&
620         # Client reported appropriate failure
621         test_i18ngrep "bytes of body are still expected" err
622 '
623
624 test_expect_success 'clone with http:// using protocol v2 and invalid parameters' '
625         test_when_finished "rm -f log" &&
626
627         test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" \
628                 git -c protocol.version=2 \
629                 clone --shallow-since=20151012 "$HTTPD_URL/smart/http_parent" http_child_invalid &&
630
631         # Client requested to use protocol v2
632         grep "Git-Protocol: version=2" log &&
633         # Server responded using protocol v2
634         grep "git< version 2" log
635 '
636
637 test_expect_success 'clone big repository with http:// using protocol v2' '
638         test_when_finished "rm -f log" &&
639
640         git init "$HTTPD_DOCUMENT_ROOT_PATH/big" &&
641         # Ensure that the list of wants is greater than http.postbuffer below
642         for i in $(test_seq 1 1500)
643         do
644                 # do not use here-doc, because it requires a process
645                 # per loop iteration
646                 echo "commit refs/heads/too-many-refs-$i" &&
647                 echo "committer git <git@example.com> $i +0000" &&
648                 echo "data 0" &&
649                 echo "M 644 inline bla.txt" &&
650                 echo "data 4" &&
651                 echo "bla"
652         done | git -C "$HTTPD_DOCUMENT_ROOT_PATH/big" fast-import &&
653
654         GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git \
655                 -c protocol.version=2 -c http.postbuffer=65536 \
656                 clone "$HTTPD_URL/smart/big" big_child &&
657
658         # Client requested to use protocol v2
659         grep "Git-Protocol: version=2" log &&
660         # Server responded using protocol v2
661         grep "git< version 2" log &&
662         # Verify that the chunked encoding sending codepath is exercised
663         grep "Send header: Transfer-Encoding: chunked" log
664 '
665
666 test_expect_success 'fetch with http:// using protocol v2' '
667         test_when_finished "rm -f log" &&
668
669         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
670
671         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
672                 fetch &&
673
674         git -C http_child log -1 --format=%s origin/master >actual &&
675         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
676         test_cmp expect actual &&
677
678         # Server responded using protocol v2
679         grep "git< version 2" log
680 '
681
682 test_expect_success 'fetch with http:// by hash without tag following with protocol v2 does not list refs' '
683         test_when_finished "rm -f log" &&
684
685         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two_a &&
686         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" rev-parse two_a >two_a_hash &&
687
688         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
689                 fetch --no-tags origin $(cat two_a_hash) &&
690
691         grep "fetch< version 2" log &&
692         ! grep "fetch> command=ls-refs" log
693 '
694
695 test_expect_success 'fetch from namespaced repo respects namespaces' '
696         test_when_finished "rm -f log" &&
697
698         git init "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" &&
699         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" one &&
700         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" two &&
701         git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" \
702                 update-ref refs/namespaces/ns/refs/heads/master one &&
703
704         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
705                 fetch "$HTTPD_URL/smart_namespace/nsrepo" \
706                 refs/heads/master:refs/heads/theirs &&
707
708         # Server responded using protocol v2
709         grep "fetch< version 2" log &&
710
711         git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" rev-parse one >expect &&
712         git -C http_child rev-parse theirs >actual &&
713         test_cmp expect actual
714 '
715
716 test_expect_success 'ls-remote with v2 http sends only one POST' '
717         test_when_finished "rm -f log" &&
718
719         git ls-remote "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" >expect &&
720         GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
721                 ls-remote "$HTTPD_URL/smart/http_parent" >actual &&
722         test_cmp expect actual &&
723
724         grep "Send header: POST" log >posts &&
725         test_line_count = 1 posts
726 '
727
728 test_expect_success 'push with http:// and a config of v2 does not request v2' '
729         test_when_finished "rm -f log" &&
730         # Till v2 for push is designed, make sure that if a client has
731         # protocol.version configured to use v2, that the client instead falls
732         # back and uses v0.
733
734         test_commit -C http_child three &&
735
736         # Push to another branch, as the target repository has the
737         # master branch checked out and we cannot push into it.
738         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
739                 push origin HEAD:client_branch &&
740
741         git -C http_child log -1 --format=%s >actual &&
742         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
743         test_cmp expect actual &&
744
745         # Client did not request to use protocol v2
746         ! grep "Git-Protocol: version=2" log &&
747         # Server did not respond using protocol v2
748         ! grep "git< version 2" log
749 '
750
751 test_expect_success 'when server sends "ready", expect DELIM' '
752         rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child &&
753
754         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
755         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
756
757         git clone "$HTTPD_URL/smart/http_parent" http_child &&
758
759         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
760
761         # After "ready" in the acknowledgments section, pretend that a FLUSH
762         # (0000) was sent instead of a DELIM (0001).
763         printf "\$ready = 1 if /ready/; \$ready && s/0001/0000/" \
764                 >"$HTTPD_ROOT_PATH/one-time-perl" &&
765
766         test_must_fail git -C http_child -c protocol.version=2 \
767                 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
768         test_i18ngrep "expected packfile to be sent after .ready." err
769 '
770
771 test_expect_success 'when server does not send "ready", expect FLUSH' '
772         rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child log &&
773
774         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
775         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
776
777         git clone "$HTTPD_URL/smart/http_parent" http_child &&
778
779         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
780
781         # Create many commits to extend the negotiation phase across multiple
782         # requests, so that the server does not send "ready" in the first
783         # request.
784         test_commit_bulk -C http_child --id=c 32 &&
785
786         # After the acknowledgments section, pretend that a DELIM
787         # (0001) was sent instead of a FLUSH (0000).
788         printf "\$ack = 1 if /acknowledgments/; \$ack && s/0000/0001/" \
789                 >"$HTTPD_ROOT_PATH/one-time-perl" &&
790
791         test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" git -C http_child \
792                 -c protocol.version=2 \
793                 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
794         grep "fetch< .*acknowledgments" log &&
795         ! grep "fetch< .*ready" log &&
796         test_i18ngrep "expected no other sections to be sent after no .ready." err
797 '
798
799 configure_exclusion () {
800         git -C "$1" hash-object "$2" >objh &&
801         git -C "$1" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
802         git -C "$1" config --add \
803                 "uploadpack.blobpackfileuri" \
804                 "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
805         cat objh
806 }
807
808 test_expect_success 'part of packfile response provided as URI' '
809         P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
810         rm -rf "$P" http_child log &&
811
812         git init "$P" &&
813         git -C "$P" config "uploadpack.allowsidebandall" "true" &&
814
815         echo my-blob >"$P/my-blob" &&
816         git -C "$P" add my-blob &&
817         echo other-blob >"$P/other-blob" &&
818         git -C "$P" add other-blob &&
819         git -C "$P" commit -m x &&
820
821         configure_exclusion "$P" my-blob >h &&
822         configure_exclusion "$P" other-blob >h2 &&
823
824         GIT_TRACE=1 GIT_TRACE_PACKET="$(pwd)/log" GIT_TEST_SIDEBAND_ALL=1 \
825         git -c protocol.version=2 \
826                 -c fetch.uriprotocols=http,https \
827                 clone "$HTTPD_URL/smart/http_parent" http_child &&
828
829         # Ensure that my-blob and other-blob are in separate packfiles.
830         for idx in http_child/.git/objects/pack/*.idx
831         do
832                 git verify-pack --verbose $idx >out &&
833                 {
834                         grep "^[0-9a-f]\{16,\} " out || :
835                 } >out.objectlist &&
836                 if test_line_count = 1 out.objectlist
837                 then
838                         if grep $(cat h) out
839                         then
840                                 >hfound
841                         fi &&
842                         if grep $(cat h2) out
843                         then
844                                 >h2found
845                         fi
846                 fi
847         done &&
848         test -f hfound &&
849         test -f h2found &&
850
851         # Ensure that there are exactly 6 files (3 .pack and 3 .idx).
852         ls http_child/.git/objects/pack/* >filelist &&
853         test_line_count = 6 filelist
854 '
855
856 test_expect_success 'fetching with valid packfile URI but invalid hash fails' '
857         P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
858         rm -rf "$P" http_child log &&
859
860         git init "$P" &&
861         git -C "$P" config "uploadpack.allowsidebandall" "true" &&
862
863         echo my-blob >"$P/my-blob" &&
864         git -C "$P" add my-blob &&
865         echo other-blob >"$P/other-blob" &&
866         git -C "$P" add other-blob &&
867         git -C "$P" commit -m x &&
868
869         configure_exclusion "$P" my-blob >h &&
870         # Configure a URL for other-blob. Just reuse the hash of the object as
871         # the hash of the packfile, since the hash does not matter for this
872         # test as long as it is not the hash of the pack, and it is of the
873         # expected length.
874         git -C "$P" hash-object other-blob >objh &&
875         git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
876         git -C "$P" config --add \
877                 "uploadpack.blobpackfileuri" \
878                 "$(cat objh) $(cat objh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
879
880         test_must_fail env GIT_TEST_SIDEBAND_ALL=1 \
881                 git -c protocol.version=2 \
882                 -c fetch.uriprotocols=http,https \
883                 clone "$HTTPD_URL/smart/http_parent" http_child 2>err &&
884         test_i18ngrep "pack downloaded from.*does not match expected hash" err
885 '
886
887 # DO NOT add non-httpd-specific tests here, because the last part of this
888 # test script is only executed when httpd is available and enabled.
889
890 test_done