perl: make Git::IndexInfo work with SHA-256
[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         test_config -C client extensions.partialClone "$SERVER" &&
353
354         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
355                 fetch --filter=blob:none "$SERVER" master:refs/heads/other &&
356         grep "version 2" trace &&
357
358         # Ensure that the old version of the file is missing
359         git -C client rev-list --quiet --objects --missing=print other \
360                 >observed.oids &&
361         grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
362
363         # Ensure that client passes fsck
364         git -C client fsck
365 '
366
367 test_expect_success 'do not advertise filter if not configured to do so' '
368         SERVER="file://$(pwd)/server" &&
369
370         rm "$(pwd)/trace" &&
371         git -C server config uploadpack.allowfilter 1 &&
372         GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
373                 ls-remote "$SERVER" &&
374         grep "fetch=.*filter" trace &&
375
376         rm "$(pwd)/trace" &&
377         git -C server config uploadpack.allowfilter 0 &&
378         GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
379                 ls-remote "$SERVER" &&
380         grep "fetch=" trace >fetch_capabilities &&
381         ! grep filter fetch_capabilities
382 '
383
384 test_expect_success 'partial clone warns if filter is not advertised' '
385         rm -rf client &&
386         git -C server config uploadpack.allowfilter 0 &&
387         git -c protocol.version=2 \
388                 clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
389         test_i18ngrep "filtering not recognized by server, ignoring" err
390 '
391
392 test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
393         git -C server config uploadpack.allowfilter 0 &&
394
395         # Custom request that tries to filter even though it is not advertised.
396         test-tool pkt-line pack >in <<-EOF &&
397         command=fetch
398         object-format=$(test_oid algo)
399         0001
400         want $(git -C server rev-parse master)
401         filter blob:none
402         0000
403         EOF
404
405         test_must_fail test-tool -C server serve-v2 --stateless-rpc \
406                 <in >/dev/null 2>err &&
407         grep "unexpected line: .filter blob:none." err &&
408
409         # Exercise to ensure that if advertised, filter works
410         git -C server config uploadpack.allowfilter 1 &&
411         test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
412 '
413
414 test_expect_success 'default refspec is used to filter ref when fetchcing' '
415         test_when_finished "rm -f log" &&
416
417         GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
418                 fetch origin &&
419
420         git -C file_child log -1 --format=%s three >actual &&
421         git -C file_parent log -1 --format=%s three >expect &&
422         test_cmp expect actual &&
423
424         grep "ref-prefix refs/heads/" log &&
425         grep "ref-prefix refs/tags/" log
426 '
427
428 test_expect_success 'fetch supports various ways of have lines' '
429         rm -rf server client trace &&
430         git init server &&
431         test_commit -C server dwim &&
432         TREE=$(git -C server rev-parse HEAD^{tree}) &&
433         git -C server tag exact \
434                 $(git -C server commit-tree -m a "$TREE") &&
435         git -C server tag dwim-unwanted \
436                 $(git -C server commit-tree -m b "$TREE") &&
437         git -C server tag exact-unwanted \
438                 $(git -C server commit-tree -m c "$TREE") &&
439         git -C server tag prefix1 \
440                 $(git -C server commit-tree -m d "$TREE") &&
441         git -C server tag prefix2 \
442                 $(git -C server commit-tree -m e "$TREE") &&
443         git -C server tag fetch-by-sha1 \
444                 $(git -C server commit-tree -m f "$TREE") &&
445         git -C server tag completely-unrelated \
446                 $(git -C server commit-tree -m g "$TREE") &&
447
448         git init client &&
449         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
450                 fetch "file://$(pwd)/server" \
451                 dwim \
452                 refs/tags/exact \
453                 refs/tags/prefix*:refs/tags/prefix* \
454                 "$(git -C server rev-parse fetch-by-sha1)" &&
455
456         # Ensure that the appropriate prefixes are sent (using a sample)
457         grep "fetch> ref-prefix dwim" trace &&
458         grep "fetch> ref-prefix refs/heads/dwim" trace &&
459         grep "fetch> ref-prefix refs/tags/prefix" trace &&
460
461         # Ensure that the correct objects are returned
462         git -C client cat-file -e $(git -C server rev-parse dwim) &&
463         git -C client cat-file -e $(git -C server rev-parse exact) &&
464         git -C client cat-file -e $(git -C server rev-parse prefix1) &&
465         git -C client cat-file -e $(git -C server rev-parse prefix2) &&
466         git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
467         test_must_fail git -C client cat-file -e \
468                 $(git -C server rev-parse dwim-unwanted) &&
469         test_must_fail git -C client cat-file -e \
470                 $(git -C server rev-parse exact-unwanted) &&
471         test_must_fail git -C client cat-file -e \
472                 $(git -C server rev-parse completely-unrelated)
473 '
474
475 test_expect_success 'fetch supports include-tag and tag following' '
476         rm -rf server client trace &&
477         git init server &&
478
479         test_commit -C server to_fetch &&
480         git -C server tag -a annotated_tag -m message &&
481
482         git init client &&
483         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
484                 fetch "$(pwd)/server" to_fetch:to_fetch &&
485
486         grep "fetch> ref-prefix to_fetch" trace &&
487         grep "fetch> ref-prefix refs/tags/" trace &&
488         grep "fetch> include-tag" trace &&
489
490         git -C client cat-file -e $(git -C client rev-parse annotated_tag)
491 '
492
493 test_expect_success 'upload-pack respects client shallows' '
494         rm -rf server client trace &&
495
496         git init server &&
497         test_commit -C server base &&
498         test_commit -C server client_has &&
499
500         git clone --depth=1 "file://$(pwd)/server" client &&
501
502         # Add extra commits to the client so that the whole fetch takes more
503         # than 1 request (due to negotiation)
504         test_commit_bulk -C client --id=c 32 &&
505
506         git -C server checkout -b newbranch base &&
507         test_commit -C server client_wants &&
508
509         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
510                 fetch origin newbranch &&
511         # Ensure that protocol v2 is used
512         grep "fetch< version 2" trace
513 '
514
515 test_expect_success 'ensure that multiple fetches in same process from a shallow repo works' '
516         rm -rf server client trace &&
517
518         test_create_repo server &&
519         test_commit -C server one &&
520         test_commit -C server two &&
521         test_commit -C server three &&
522         git clone --shallow-exclude two "file://$(pwd)/server" client &&
523
524         git -C server tag -a -m "an annotated tag" twotag two &&
525
526         # Triggers tag following (thus, 2 fetches in one process)
527         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
528                 fetch --shallow-exclude one origin &&
529         # Ensure that protocol v2 is used
530         grep "fetch< version 2" trace
531 '
532
533 test_expect_success 'deepen-relative' '
534         rm -rf server client trace &&
535
536         test_create_repo server &&
537         test_commit -C server one &&
538         test_commit -C server two &&
539         test_commit -C server three &&
540         git clone --depth 1 "file://$(pwd)/server" client &&
541         test_commit -C server four &&
542
543         # Sanity check that only "three" is downloaded
544         git -C client log --pretty=tformat:%s master >actual &&
545         echo three >expected &&
546         test_cmp expected actual &&
547
548         GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
549                 fetch --deepen=1 origin &&
550         # Ensure that protocol v2 is used
551         grep "fetch< version 2" trace &&
552
553         git -C client log --pretty=tformat:%s origin/master >actual &&
554         cat >expected <<-\EOF &&
555         four
556         three
557         two
558         EOF
559         test_cmp expected actual
560 '
561
562 # Test protocol v2 with 'http://' transport
563 #
564 . "$TEST_DIRECTORY"/lib-httpd.sh
565 start_httpd
566
567 test_expect_success 'create repo to be served by http:// transport' '
568         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
569         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
570         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
571 '
572
573 test_expect_success 'clone with http:// using protocol v2' '
574         test_when_finished "rm -f log" &&
575
576         GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
577                 clone "$HTTPD_URL/smart/http_parent" http_child &&
578
579         git -C http_child log -1 --format=%s >actual &&
580         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
581         test_cmp expect actual &&
582
583         # Client requested to use protocol v2
584         grep "Git-Protocol: version=2" log &&
585         # Server responded using protocol v2
586         grep "git< version 2" log &&
587         # Verify that the chunked encoding sending codepath is NOT exercised
588         ! grep "Send header: Transfer-Encoding: chunked" log
589 '
590
591 test_expect_success 'clone big repository with http:// using protocol v2' '
592         test_when_finished "rm -f log" &&
593
594         git init "$HTTPD_DOCUMENT_ROOT_PATH/big" &&
595         # Ensure that the list of wants is greater than http.postbuffer below
596         for i in $(test_seq 1 1500)
597         do
598                 # do not use here-doc, because it requires a process
599                 # per loop iteration
600                 echo "commit refs/heads/too-many-refs-$i" &&
601                 echo "committer git <git@example.com> $i +0000" &&
602                 echo "data 0" &&
603                 echo "M 644 inline bla.txt" &&
604                 echo "data 4" &&
605                 echo "bla"
606         done | git -C "$HTTPD_DOCUMENT_ROOT_PATH/big" fast-import &&
607
608         GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git \
609                 -c protocol.version=2 -c http.postbuffer=65536 \
610                 clone "$HTTPD_URL/smart/big" big_child &&
611
612         # Client requested to use protocol v2
613         grep "Git-Protocol: version=2" log &&
614         # Server responded using protocol v2
615         grep "git< version 2" log &&
616         # Verify that the chunked encoding sending codepath is exercised
617         grep "Send header: Transfer-Encoding: chunked" log
618 '
619
620 test_expect_success 'fetch with http:// using protocol v2' '
621         test_when_finished "rm -f log" &&
622
623         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
624
625         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
626                 fetch &&
627
628         git -C http_child log -1 --format=%s origin/master >actual &&
629         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
630         test_cmp expect actual &&
631
632         # Server responded using protocol v2
633         grep "git< version 2" log
634 '
635
636 test_expect_success 'fetch with http:// by hash without tag following with protocol v2 does not list refs' '
637         test_when_finished "rm -f log" &&
638
639         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two_a &&
640         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" rev-parse two_a >two_a_hash &&
641
642         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
643                 fetch --no-tags origin $(cat two_a_hash) &&
644
645         grep "fetch< version 2" log &&
646         ! grep "fetch> command=ls-refs" log
647 '
648
649 test_expect_success 'fetch from namespaced repo respects namespaces' '
650         test_when_finished "rm -f log" &&
651
652         git init "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" &&
653         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" one &&
654         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" two &&
655         git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" \
656                 update-ref refs/namespaces/ns/refs/heads/master one &&
657
658         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
659                 fetch "$HTTPD_URL/smart_namespace/nsrepo" \
660                 refs/heads/master:refs/heads/theirs &&
661
662         # Server responded using protocol v2
663         grep "fetch< version 2" log &&
664
665         git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" rev-parse one >expect &&
666         git -C http_child rev-parse theirs >actual &&
667         test_cmp expect actual
668 '
669
670 test_expect_success 'ls-remote with v2 http sends only one POST' '
671         test_when_finished "rm -f log" &&
672
673         git ls-remote "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" >expect &&
674         GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
675                 ls-remote "$HTTPD_URL/smart/http_parent" >actual &&
676         test_cmp expect actual &&
677
678         grep "Send header: POST" log >posts &&
679         test_line_count = 1 posts
680 '
681
682 test_expect_success 'push with http:// and a config of v2 does not request v2' '
683         test_when_finished "rm -f log" &&
684         # Till v2 for push is designed, make sure that if a client has
685         # protocol.version configured to use v2, that the client instead falls
686         # back and uses v0.
687
688         test_commit -C http_child three &&
689
690         # Push to another branch, as the target repository has the
691         # master branch checked out and we cannot push into it.
692         GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
693                 push origin HEAD:client_branch &&
694
695         git -C http_child log -1 --format=%s >actual &&
696         git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
697         test_cmp expect actual &&
698
699         # Client did not request to use protocol v2
700         ! grep "Git-Protocol: version=2" log &&
701         # Server did not respond using protocol v2
702         ! grep "git< version 2" log
703 '
704
705 test_expect_success 'when server sends "ready", expect DELIM' '
706         rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child &&
707
708         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
709         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
710
711         git clone "$HTTPD_URL/smart/http_parent" http_child &&
712
713         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
714
715         # After "ready" in the acknowledgments section, pretend that a FLUSH
716         # (0000) was sent instead of a DELIM (0001).
717         printf "\$ready = 1 if /ready/; \$ready && s/0001/0000/" \
718                 >"$HTTPD_ROOT_PATH/one-time-perl" &&
719
720         test_must_fail git -C http_child -c protocol.version=2 \
721                 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
722         test_i18ngrep "expected packfile to be sent after .ready." err
723 '
724
725 test_expect_success 'when server does not send "ready", expect FLUSH' '
726         rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child log &&
727
728         git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
729         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
730
731         git clone "$HTTPD_URL/smart/http_parent" http_child &&
732
733         test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
734
735         # Create many commits to extend the negotiation phase across multiple
736         # requests, so that the server does not send "ready" in the first
737         # request.
738         test_commit_bulk -C http_child --id=c 32 &&
739
740         # After the acknowledgments section, pretend that a DELIM
741         # (0001) was sent instead of a FLUSH (0000).
742         printf "\$ack = 1 if /acknowledgments/; \$ack && s/0000/0001/" \
743                 >"$HTTPD_ROOT_PATH/one-time-perl" &&
744
745         test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" git -C http_child \
746                 -c protocol.version=2 \
747                 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
748         grep "fetch< .*acknowledgments" log &&
749         ! grep "fetch< .*ready" log &&
750         test_i18ngrep "expected no other sections to be sent after no .ready." err
751 '
752
753 # DO NOT add non-httpd-specific tests here, because the last part of this
754 # test script is only executed when httpd is available and enabled.
755
756 test_done