rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t5702-protocol-v2.sh
blob00ce9aec234685bca8d16018862282290aa64201
1 #!/bin/sh
3 test_description='test git wire-protocol version 2'
5 TEST_NO_CREATE_REPO=1
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 . ./test-lib.sh
12 # Test protocol v2 with 'git://' transport
14 . "$TEST_DIRECTORY"/lib-git-daemon.sh
15 start_git_daemon --export-all --enable=receive-pack
16 daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
18 test_expect_success 'create repo to be served by git-daemon' '
19 git init "$daemon_parent" &&
20 test_commit -C "$daemon_parent" one
23 test_expect_success 'list refs with git:// using protocol v2' '
24 test_when_finished "rm -f log" &&
26 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
27 ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
29 # Client requested to use protocol v2
30 grep "ls-remote> .*\\\0\\\0version=2\\\0$" log &&
31 # Server responded using protocol v2
32 grep "ls-remote< version 2" log &&
34 git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
35 test_cmp expect actual
38 test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
39 test_when_finished "rm -f log" &&
41 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
42 ls-remote "$GIT_DAEMON_URL/parent" main >actual &&
44 cat >expect <<-EOF &&
45 $(git -C "$daemon_parent" rev-parse refs/heads/main)$(printf "\t")refs/heads/main
46 EOF
48 test_cmp expect actual
51 test_expect_success 'clone with git:// using protocol v2' '
52 test_when_finished "rm -f log" &&
54 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
55 clone "$GIT_DAEMON_URL/parent" daemon_child &&
57 git -C daemon_child log -1 --format=%s >actual &&
58 git -C "$daemon_parent" log -1 --format=%s >expect &&
59 test_cmp expect actual &&
61 # Client requested to use protocol v2
62 grep "clone> .*\\\0\\\0version=2\\\0$" log &&
63 # Server responded using protocol v2
64 grep "clone< version 2" log
67 test_expect_success 'fetch with git:// using protocol v2' '
68 test_when_finished "rm -f log" &&
70 test_commit -C "$daemon_parent" two &&
72 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
73 fetch &&
75 git -C daemon_child log -1 --format=%s origin/main >actual &&
76 git -C "$daemon_parent" log -1 --format=%s >expect &&
77 test_cmp expect actual &&
79 # Client requested to use protocol v2
80 grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
81 # Server responded using protocol v2
82 grep "fetch< version 2" log
85 test_expect_success 'fetch by hash without tag following with protocol v2 does not list refs' '
86 test_when_finished "rm -f log" &&
88 test_commit -C "$daemon_parent" two_a &&
89 git -C "$daemon_parent" rev-parse two_a >two_a_hash &&
91 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
92 fetch --no-tags origin $(cat two_a_hash) &&
94 grep "fetch< version 2" log &&
95 ! grep "fetch> command=ls-refs" log
98 test_expect_success 'pull with git:// using protocol v2' '
99 test_when_finished "rm -f log" &&
101 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
102 pull &&
104 git -C daemon_child log -1 --format=%s >actual &&
105 git -C "$daemon_parent" log -1 --format=%s >expect &&
106 test_cmp expect actual &&
108 # Client requested to use protocol v2
109 grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
110 # Server responded using protocol v2
111 grep "fetch< version 2" log
114 test_expect_success 'push with git:// and a config of v2 does not request v2' '
115 test_when_finished "rm -f log" &&
117 # Till v2 for push is designed, make sure that if a client has
118 # protocol.version configured to use v2, that the client instead falls
119 # back and uses v0.
121 test_commit -C daemon_child three &&
123 # Push to another branch, as the target repository has the
124 # main branch checked out and we cannot push into it.
125 GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
126 push origin HEAD:client_branch &&
128 git -C daemon_child log -1 --format=%s >actual &&
129 git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
130 test_cmp expect actual &&
132 # Client requested to use protocol v2
133 ! grep "push> .*\\\0\\\0version=2\\\0$" log &&
134 # Server responded using protocol v2
135 ! grep "push< version 2" log
138 stop_git_daemon
140 # Test protocol v2 with 'file://' transport
142 test_expect_success 'create repo to be served by file:// transport' '
143 git init file_parent &&
144 test_commit -C file_parent one
147 test_expect_success 'list refs with file:// using protocol v2' '
148 test_when_finished "rm -f log" &&
150 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
151 ls-remote --symref "file://$(pwd)/file_parent" >actual &&
153 # Server responded using protocol v2
154 grep "ls-remote< version 2" log &&
156 git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
157 test_cmp expect actual
160 test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
161 test_when_finished "rm -f log" &&
163 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
164 ls-remote "file://$(pwd)/file_parent" main >actual &&
166 cat >expect <<-EOF &&
167 $(git -C file_parent rev-parse refs/heads/main)$(printf "\t")refs/heads/main
170 test_cmp expect actual
173 test_expect_success 'server-options are sent when using ls-remote' '
174 test_when_finished "rm -f log" &&
176 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
177 ls-remote -o hello -o world "file://$(pwd)/file_parent" main >actual &&
179 cat >expect <<-EOF &&
180 $(git -C file_parent rev-parse refs/heads/main)$(printf "\t")refs/heads/main
183 test_cmp expect actual &&
184 grep "server-option=hello" log &&
185 grep "server-option=world" log
188 test_expect_success 'warn if using server-option with ls-remote with legacy protocol' '
189 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
190 ls-remote -o hello -o world "file://$(pwd)/file_parent" main 2>err &&
192 test_i18ngrep "see protocol.version in" err &&
193 test_i18ngrep "server options require protocol version 2 or later" err
196 test_expect_success 'clone with file:// using protocol v2' '
197 test_when_finished "rm -f log" &&
199 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
200 clone "file://$(pwd)/file_parent" file_child &&
202 git -C file_child log -1 --format=%s >actual &&
203 git -C file_parent log -1 --format=%s >expect &&
204 test_cmp expect actual &&
206 # Server responded using protocol v2
207 grep "clone< version 2" log &&
209 # Client sent ref-prefixes to filter the ref-advertisement
210 grep "ref-prefix HEAD" log &&
211 grep "ref-prefix refs/heads/" log &&
212 grep "ref-prefix refs/tags/" log
215 test_expect_success 'clone of empty repo propagates name of default branch' '
216 test_when_finished "rm -rf file_empty_parent file_empty_child" &&
218 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
219 git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
221 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
222 git -c init.defaultBranch=main -c protocol.version=2 \
223 clone "file://$(pwd)/file_empty_parent" file_empty_child &&
224 grep "refs/heads/mydefaultbranch" file_empty_child/.git/HEAD
227 test_expect_success '...but not if explicitly forbidden by config' '
228 test_when_finished "rm -rf file_empty_parent file_empty_child" &&
230 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
231 git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
232 test_config -C file_empty_parent lsrefs.unborn ignore &&
234 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
235 git -c init.defaultBranch=main -c protocol.version=2 \
236 clone "file://$(pwd)/file_empty_parent" file_empty_child &&
237 ! grep "refs/heads/mydefaultbranch" file_empty_child/.git/HEAD
240 test_expect_success 'bare clone propagates empty default branch' '
241 test_when_finished "rm -rf file_empty_parent file_empty_child.git" &&
243 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
244 git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
246 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
247 git -c init.defaultBranch=main -c protocol.version=2 \
248 clone --bare \
249 "file://$(pwd)/file_empty_parent" file_empty_child.git &&
250 grep "refs/heads/mydefaultbranch" file_empty_child.git/HEAD
253 test_expect_success 'fetch with file:// using protocol v2' '
254 test_when_finished "rm -f log" &&
256 test_commit -C file_parent two &&
258 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
259 fetch origin &&
261 git -C file_child log -1 --format=%s origin/main >actual &&
262 git -C file_parent log -1 --format=%s >expect &&
263 test_cmp expect actual &&
265 # Server responded using protocol v2
266 grep "fetch< version 2" log
269 test_expect_success 'ref advertisement is filtered during fetch using protocol v2' '
270 test_when_finished "rm -f log" &&
272 test_commit -C file_parent three &&
273 git -C file_parent branch unwanted-branch three &&
275 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
276 fetch origin main &&
278 git -C file_child log -1 --format=%s origin/main >actual &&
279 git -C file_parent log -1 --format=%s >expect &&
280 test_cmp expect actual &&
282 grep "refs/heads/main" log &&
283 ! grep "refs/heads/unwanted-branch" log
286 test_expect_success 'server-options are sent when fetching' '
287 test_when_finished "rm -f log" &&
289 test_commit -C file_parent four &&
291 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
292 fetch -o hello -o world origin main &&
294 git -C file_child log -1 --format=%s origin/main >actual &&
295 git -C file_parent log -1 --format=%s >expect &&
296 test_cmp expect actual &&
298 grep "server-option=hello" log &&
299 grep "server-option=world" log
302 test_expect_success 'warn if using server-option with fetch with legacy protocol' '
303 test_when_finished "rm -rf temp_child" &&
305 git init temp_child &&
307 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C temp_child -c protocol.version=0 \
308 fetch -o hello -o world "file://$(pwd)/file_parent" main 2>err &&
310 test_i18ngrep "see protocol.version in" err &&
311 test_i18ngrep "server options require protocol version 2 or later" err
314 test_expect_success 'server-options are sent when cloning' '
315 test_when_finished "rm -rf log myclone" &&
317 GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
318 clone --server-option=hello --server-option=world \
319 "file://$(pwd)/file_parent" myclone &&
321 grep "server-option=hello" log &&
322 grep "server-option=world" log
325 test_expect_success 'warn if using server-option with clone with legacy protocol' '
326 test_when_finished "rm -rf myclone" &&
328 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
329 clone --server-option=hello --server-option=world \
330 "file://$(pwd)/file_parent" myclone 2>err &&
332 test_i18ngrep "see protocol.version in" err &&
333 test_i18ngrep "server options require protocol version 2 or later" err
336 test_expect_success 'upload-pack respects config using protocol v2' '
337 git init server &&
338 write_script server/.git/hook <<-\EOF &&
339 touch hookout
340 "$@"
342 test_commit -C server one &&
344 test_config_global uploadpack.packobjectshook ./hook &&
345 test_path_is_missing server/.git/hookout &&
346 git -c protocol.version=2 clone "file://$(pwd)/server" client &&
347 test_path_is_file server/.git/hookout
350 test_expect_success 'setup filter tests' '
351 rm -rf server client &&
352 git init server &&
354 # 1 commit to create a file, and 1 commit to modify it
355 test_commit -C server message1 a.txt &&
356 test_commit -C server message2 a.txt &&
357 git -C server config protocol.version 2 &&
358 git -C server config uploadpack.allowfilter 1 &&
359 git -C server config uploadpack.allowanysha1inwant 1 &&
360 git -C server config protocol.version 2
363 test_expect_success 'partial clone' '
364 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
365 clone --filter=blob:none "file://$(pwd)/server" client &&
366 grep "version 2" trace &&
368 # Ensure that the old version of the file is missing
369 git -C client rev-list --quiet --objects --missing=print main \
370 >observed.oids &&
371 grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
373 # Ensure that client passes fsck
374 git -C client fsck
377 test_expect_success 'dynamically fetch missing object' '
378 rm "$(pwd)/trace" &&
379 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
380 cat-file -p $(git -C server rev-parse message1:a.txt) &&
381 grep "version 2" trace
384 test_expect_success 'when dynamically fetching missing object, do not list refs' '
385 ! grep "git> command=ls-refs" trace
388 test_expect_success 'partial fetch' '
389 rm -rf client "$(pwd)/trace" &&
390 git init client &&
391 SERVER="file://$(pwd)/server" &&
393 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
394 fetch --filter=blob:none "$SERVER" main:refs/heads/other &&
395 grep "version 2" trace &&
397 # Ensure that the old version of the file is missing
398 git -C client rev-list --quiet --objects --missing=print other \
399 >observed.oids &&
400 grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
402 # Ensure that client passes fsck
403 git -C client fsck
406 test_expect_success 'do not advertise filter if not configured to do so' '
407 SERVER="file://$(pwd)/server" &&
409 rm "$(pwd)/trace" &&
410 git -C server config uploadpack.allowfilter 1 &&
411 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
412 ls-remote "$SERVER" &&
413 grep "fetch=.*filter" trace &&
415 rm "$(pwd)/trace" &&
416 git -C server config uploadpack.allowfilter 0 &&
417 GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
418 ls-remote "$SERVER" &&
419 grep "fetch=" trace >fetch_capabilities &&
420 ! grep filter fetch_capabilities
423 test_expect_success 'partial clone warns if filter is not advertised' '
424 rm -rf client &&
425 git -C server config uploadpack.allowfilter 0 &&
426 git -c protocol.version=2 \
427 clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
428 test_i18ngrep "filtering not recognized by server, ignoring" err
431 test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
432 git -C server config uploadpack.allowfilter 0 &&
434 # Custom request that tries to filter even though it is not advertised.
435 test-tool pkt-line pack >in <<-EOF &&
436 command=fetch
437 object-format=$(test_oid algo)
438 0001
439 want $(git -C server rev-parse main)
440 filter blob:none
441 0000
444 test_must_fail test-tool -C server serve-v2 --stateless-rpc \
445 <in >/dev/null 2>err &&
446 grep "unexpected line: .filter blob:none." err &&
448 # Exercise to ensure that if advertised, filter works
449 git -C server config uploadpack.allowfilter 1 &&
450 test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
453 test_expect_success 'default refspec is used to filter ref when fetchcing' '
454 test_when_finished "rm -f log" &&
456 GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
457 fetch origin &&
459 git -C file_child log -1 --format=%s three >actual &&
460 git -C file_parent log -1 --format=%s three >expect &&
461 test_cmp expect actual &&
463 grep "ref-prefix refs/heads/" log &&
464 grep "ref-prefix refs/tags/" log
467 test_expect_success 'fetch supports various ways of have lines' '
468 rm -rf server client trace &&
469 git init server &&
470 test_commit -C server dwim &&
471 TREE=$(git -C server rev-parse HEAD^{tree}) &&
472 git -C server tag exact \
473 $(git -C server commit-tree -m a "$TREE") &&
474 git -C server tag dwim-unwanted \
475 $(git -C server commit-tree -m b "$TREE") &&
476 git -C server tag exact-unwanted \
477 $(git -C server commit-tree -m c "$TREE") &&
478 git -C server tag prefix1 \
479 $(git -C server commit-tree -m d "$TREE") &&
480 git -C server tag prefix2 \
481 $(git -C server commit-tree -m e "$TREE") &&
482 git -C server tag fetch-by-sha1 \
483 $(git -C server commit-tree -m f "$TREE") &&
484 git -C server tag completely-unrelated \
485 $(git -C server commit-tree -m g "$TREE") &&
487 git init client &&
488 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
489 fetch "file://$(pwd)/server" \
490 dwim \
491 refs/tags/exact \
492 refs/tags/prefix*:refs/tags/prefix* \
493 "$(git -C server rev-parse fetch-by-sha1)" &&
495 # Ensure that the appropriate prefixes are sent (using a sample)
496 grep "fetch> ref-prefix dwim" trace &&
497 grep "fetch> ref-prefix refs/heads/dwim" trace &&
498 grep "fetch> ref-prefix refs/tags/prefix" trace &&
500 # Ensure that the correct objects are returned
501 git -C client cat-file -e $(git -C server rev-parse dwim) &&
502 git -C client cat-file -e $(git -C server rev-parse exact) &&
503 git -C client cat-file -e $(git -C server rev-parse prefix1) &&
504 git -C client cat-file -e $(git -C server rev-parse prefix2) &&
505 git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
506 test_must_fail git -C client cat-file -e \
507 $(git -C server rev-parse dwim-unwanted) &&
508 test_must_fail git -C client cat-file -e \
509 $(git -C server rev-parse exact-unwanted) &&
510 test_must_fail git -C client cat-file -e \
511 $(git -C server rev-parse completely-unrelated)
514 test_expect_success 'fetch supports include-tag and tag following' '
515 rm -rf server client trace &&
516 git init server &&
518 test_commit -C server to_fetch &&
519 git -C server tag -a annotated_tag -m message &&
521 git init client &&
522 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
523 fetch "$(pwd)/server" to_fetch:to_fetch &&
525 grep "fetch> ref-prefix to_fetch" trace &&
526 grep "fetch> ref-prefix refs/tags/" trace &&
527 grep "fetch> include-tag" trace &&
529 git -C client cat-file -e $(git -C client rev-parse annotated_tag)
532 test_expect_success 'upload-pack respects client shallows' '
533 rm -rf server client trace &&
535 git init server &&
536 test_commit -C server base &&
537 test_commit -C server client_has &&
539 git clone --depth=1 "file://$(pwd)/server" client &&
541 # Add extra commits to the client so that the whole fetch takes more
542 # than 1 request (due to negotiation)
543 test_commit_bulk -C client --id=c 32 &&
545 git -C server checkout -b newbranch base &&
546 test_commit -C server client_wants &&
548 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
549 fetch origin newbranch &&
550 # Ensure that protocol v2 is used
551 grep "fetch< version 2" trace
554 test_expect_success 'ensure that multiple fetches in same process from a shallow repo works' '
555 rm -rf server client trace &&
557 test_create_repo server &&
558 test_commit -C server one &&
559 test_commit -C server two &&
560 test_commit -C server three &&
561 git clone --shallow-exclude two "file://$(pwd)/server" client &&
563 git -C server tag -a -m "an annotated tag" twotag two &&
565 # Triggers tag following (thus, 2 fetches in one process)
566 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
567 fetch --shallow-exclude one origin &&
568 # Ensure that protocol v2 is used
569 grep "fetch< version 2" trace
572 test_expect_success 'deepen-relative' '
573 rm -rf server client trace &&
575 test_create_repo server &&
576 test_commit -C server one &&
577 test_commit -C server two &&
578 test_commit -C server three &&
579 git clone --depth 1 "file://$(pwd)/server" client &&
580 test_commit -C server four &&
582 # Sanity check that only "three" is downloaded
583 git -C client log --pretty=tformat:%s main >actual &&
584 echo three >expected &&
585 test_cmp expected actual &&
587 GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
588 fetch --deepen=1 origin &&
589 # Ensure that protocol v2 is used
590 grep "fetch< version 2" trace &&
592 git -C client log --pretty=tformat:%s origin/main >actual &&
593 cat >expected <<-\EOF &&
594 four
595 three
598 test_cmp expected actual
601 setup_negotiate_only () {
602 SERVER="$1"
603 URI="$2"
605 rm -rf "$SERVER" client
607 git init "$SERVER"
608 test_commit -C "$SERVER" one
609 test_commit -C "$SERVER" two
611 git clone "$URI" client
612 test_commit -C client three
615 test_expect_success 'usage: --negotiate-only without --negotiation-tip' '
616 SERVER="server" &&
617 URI="file://$(pwd)/server" &&
619 setup_negotiate_only "$SERVER" "$URI" &&
621 cat >err.expect <<-\EOF &&
622 fatal: --negotiate-only needs one or more --negotiation-tip=*
625 test_must_fail git -c protocol.version=2 -C client fetch \
626 --negotiate-only \
627 origin 2>err.actual &&
628 test_cmp err.expect err.actual
631 test_expect_success 'usage: --negotiate-only with --recurse-submodules' '
632 cat >err.expect <<-\EOF &&
633 fatal: options '\''--negotiate-only'\'' and '\''--recurse-submodules'\'' cannot be used together
636 test_must_fail git -c protocol.version=2 -C client fetch \
637 --negotiate-only \
638 --recurse-submodules \
639 origin 2>err.actual &&
640 test_cmp err.expect err.actual
643 test_expect_success 'file:// --negotiate-only' '
644 SERVER="server" &&
645 URI="file://$(pwd)/server" &&
647 setup_negotiate_only "$SERVER" "$URI" &&
649 git -c protocol.version=2 -C client fetch \
650 --no-tags \
651 --negotiate-only \
652 --negotiation-tip=$(git -C client rev-parse HEAD) \
653 origin >out &&
654 COMMON=$(git -C "$SERVER" rev-parse two) &&
655 grep "$COMMON" out
658 test_expect_success 'file:// --negotiate-only with protocol v0' '
659 SERVER="server" &&
660 URI="file://$(pwd)/server" &&
662 setup_negotiate_only "$SERVER" "$URI" &&
664 test_must_fail git -c protocol.version=0 -C client fetch \
665 --no-tags \
666 --negotiate-only \
667 --negotiation-tip=$(git -C client rev-parse HEAD) \
668 origin 2>err &&
669 test_i18ngrep "negotiate-only requires protocol v2" err
672 # Test protocol v2 with 'http://' transport
674 . "$TEST_DIRECTORY"/lib-httpd.sh
675 start_httpd
677 test_expect_success 'create repo to be served by http:// transport' '
678 git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
679 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
680 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
683 test_expect_success 'clone with http:// using protocol v2' '
684 test_when_finished "rm -f log" &&
686 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
687 clone "$HTTPD_URL/smart/http_parent" http_child &&
689 git -C http_child log -1 --format=%s >actual &&
690 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
691 test_cmp expect actual &&
693 # Client requested to use protocol v2
694 grep "Git-Protocol: version=2" log &&
695 # Server responded using protocol v2
696 grep "git< version 2" log &&
697 # Verify that the chunked encoding sending codepath is NOT exercised
698 ! grep "Send header: Transfer-Encoding: chunked" log
701 test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline length' '
702 test_when_finished "rm -f log" &&
704 git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" &&
705 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" file &&
707 test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
708 clone "$HTTPD_URL/smart/incomplete_length" incomplete_length_child 2>err &&
710 # Client requested to use protocol v2
711 grep "Git-Protocol: version=2" log &&
712 # Server responded using protocol v2
713 grep "git< version 2" log &&
714 # Client reported appropriate failure
715 test_i18ngrep "bytes of length header were received" err
718 test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline body' '
719 test_when_finished "rm -f log" &&
721 git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" &&
722 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" file &&
724 test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
725 clone "$HTTPD_URL/smart/incomplete_body" incomplete_body_child 2>err &&
727 # Client requested to use protocol v2
728 grep "Git-Protocol: version=2" log &&
729 # Server responded using protocol v2
730 grep "git< version 2" log &&
731 # Client reported appropriate failure
732 test_i18ngrep "bytes of body are still expected" err
735 test_expect_success 'clone with http:// using protocol v2 and invalid parameters' '
736 test_when_finished "rm -f log" &&
738 test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" \
739 git -c protocol.version=2 \
740 clone --shallow-since=20151012 "$HTTPD_URL/smart/http_parent" http_child_invalid &&
742 # Client requested to use protocol v2
743 grep "Git-Protocol: version=2" log &&
744 # Server responded using protocol v2
745 grep "git< version 2" log
748 test_expect_success 'clone big repository with http:// using protocol v2' '
749 test_when_finished "rm -f log" &&
751 git init "$HTTPD_DOCUMENT_ROOT_PATH/big" &&
752 # Ensure that the list of wants is greater than http.postbuffer below
753 for i in $(test_seq 1 1500)
755 # do not use here-doc, because it requires a process
756 # per loop iteration
757 echo "commit refs/heads/too-many-refs-$i" &&
758 echo "committer git <git@example.com> $i +0000" &&
759 echo "data 0" &&
760 echo "M 644 inline bla.txt" &&
761 echo "data 4" &&
762 echo "bla" || return 1
763 done | git -C "$HTTPD_DOCUMENT_ROOT_PATH/big" fast-import &&
765 GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git \
766 -c protocol.version=2 -c http.postbuffer=65536 \
767 clone "$HTTPD_URL/smart/big" big_child &&
769 # Client requested to use protocol v2
770 grep "Git-Protocol: version=2" log &&
771 # Server responded using protocol v2
772 grep "git< version 2" log &&
773 # Verify that the chunked encoding sending codepath is exercised
774 grep "Send header: Transfer-Encoding: chunked" log
777 test_expect_success 'fetch with http:// using protocol v2' '
778 test_when_finished "rm -f log" &&
780 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
782 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
783 fetch &&
785 git -C http_child log -1 --format=%s origin/main >actual &&
786 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
787 test_cmp expect actual &&
789 # Server responded using protocol v2
790 grep "git< version 2" log
793 test_expect_success 'fetch with http:// by hash without tag following with protocol v2 does not list refs' '
794 test_when_finished "rm -f log" &&
796 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two_a &&
797 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" rev-parse two_a >two_a_hash &&
799 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
800 fetch --no-tags origin $(cat two_a_hash) &&
802 grep "fetch< version 2" log &&
803 ! grep "fetch> command=ls-refs" log
806 test_expect_success 'fetch from namespaced repo respects namespaces' '
807 test_when_finished "rm -f log" &&
809 git init "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" &&
810 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" one &&
811 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" two &&
812 git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" \
813 update-ref refs/namespaces/ns/refs/heads/main one &&
815 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
816 fetch "$HTTPD_URL/smart_namespace/nsrepo" \
817 refs/heads/main:refs/heads/theirs &&
819 # Server responded using protocol v2
820 grep "fetch< version 2" log &&
822 git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" rev-parse one >expect &&
823 git -C http_child rev-parse theirs >actual &&
824 test_cmp expect actual
827 test_expect_success 'ls-remote with v2 http sends only one POST' '
828 test_when_finished "rm -f log" &&
830 git ls-remote "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" >expect &&
831 GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
832 ls-remote "$HTTPD_URL/smart/http_parent" >actual &&
833 test_cmp expect actual &&
835 grep "Send header: POST" log >posts &&
836 test_line_count = 1 posts
839 test_expect_success 'push with http:// and a config of v2 does not request v2' '
840 test_when_finished "rm -f log" &&
841 # Till v2 for push is designed, make sure that if a client has
842 # protocol.version configured to use v2, that the client instead falls
843 # back and uses v0.
845 test_commit -C http_child three &&
847 # Push to another branch, as the target repository has the
848 # main branch checked out and we cannot push into it.
849 GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
850 push origin HEAD:client_branch &&
852 git -C http_child log -1 --format=%s >actual &&
853 git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
854 test_cmp expect actual &&
856 # Client did not request to use protocol v2
857 ! grep "Git-Protocol: version=2" log &&
858 # Server did not respond using protocol v2
859 ! grep "git< version 2" log
862 test_expect_success 'when server sends "ready", expect DELIM' '
863 rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child &&
865 git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
866 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
868 git clone "$HTTPD_URL/smart/http_parent" http_child &&
870 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
872 # After "ready" in the acknowledgments section, pretend that a FLUSH
873 # (0000) was sent instead of a DELIM (0001).
874 printf "\$ready = 1 if /ready/; \$ready && s/0001/0000/" \
875 >"$HTTPD_ROOT_PATH/one-time-perl" &&
877 test_must_fail git -C http_child -c protocol.version=2 \
878 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
879 test_i18ngrep "expected packfile to be sent after .ready." err
882 test_expect_success 'when server does not send "ready", expect FLUSH' '
883 rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child log &&
885 git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
886 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
888 git clone "$HTTPD_URL/smart/http_parent" http_child &&
890 test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
892 # Create many commits to extend the negotiation phase across multiple
893 # requests, so that the server does not send "ready" in the first
894 # request.
895 test_commit_bulk -C http_child --id=c 32 &&
897 # After the acknowledgments section, pretend that a DELIM
898 # (0001) was sent instead of a FLUSH (0000).
899 printf "\$ack = 1 if /acknowledgments/; \$ack && s/0000/0001/" \
900 >"$HTTPD_ROOT_PATH/one-time-perl" &&
902 test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" git -C http_child \
903 -c protocol.version=2 \
904 fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
905 grep "fetch< .*acknowledgments" log &&
906 ! grep "fetch< .*ready" log &&
907 test_i18ngrep "expected no other sections to be sent after no .ready." err
910 configure_exclusion () {
911 git -C "$1" hash-object "$2" >objh &&
912 git -C "$1" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
913 git -C "$1" config --add \
914 "uploadpack.blobpackfileuri" \
915 "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
916 cat objh
919 test_expect_success 'part of packfile response provided as URI' '
920 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
921 rm -rf "$P" http_child log &&
923 git init "$P" &&
924 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
926 echo my-blob >"$P/my-blob" &&
927 git -C "$P" add my-blob &&
928 echo other-blob >"$P/other-blob" &&
929 git -C "$P" add other-blob &&
930 git -C "$P" commit -m x &&
932 configure_exclusion "$P" my-blob >h &&
933 configure_exclusion "$P" other-blob >h2 &&
935 GIT_TRACE=1 GIT_TRACE_PACKET="$(pwd)/log" GIT_TEST_SIDEBAND_ALL=1 \
936 git -c protocol.version=2 \
937 -c fetch.uriprotocols=http,https \
938 clone "$HTTPD_URL/smart/http_parent" http_child &&
940 # Ensure that my-blob and other-blob are in separate packfiles.
941 for idx in http_child/.git/objects/pack/*.idx
943 git verify-pack --object-format=$(test_oid algo) --verbose $idx >out &&
945 grep "^[0-9a-f]\{16,\} " out || :
946 } >out.objectlist &&
947 if test_line_count = 1 out.objectlist
948 then
949 if grep $(cat h) out
950 then
951 >hfound
952 fi &&
953 if grep $(cat h2) out
954 then
955 >h2found
957 fi || return 1
958 done &&
959 test -f hfound &&
960 test -f h2found &&
962 # Ensure that there are exactly 3 packfiles with associated .idx
963 ls http_child/.git/objects/pack/*.pack \
964 http_child/.git/objects/pack/*.idx >filelist &&
965 test_line_count = 6 filelist
968 test_expect_success 'packfile URIs with fetch instead of clone' '
969 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
970 rm -rf "$P" http_child log &&
972 git init "$P" &&
973 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
975 echo my-blob >"$P/my-blob" &&
976 git -C "$P" add my-blob &&
977 git -C "$P" commit -m x &&
979 configure_exclusion "$P" my-blob >h &&
981 git init http_child &&
983 GIT_TEST_SIDEBAND_ALL=1 \
984 git -C http_child -c protocol.version=2 \
985 -c fetch.uriprotocols=http,https \
986 fetch "$HTTPD_URL/smart/http_parent"
989 test_expect_success 'fetching with valid packfile URI but invalid hash fails' '
990 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
991 rm -rf "$P" http_child log &&
993 git init "$P" &&
994 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
996 echo my-blob >"$P/my-blob" &&
997 git -C "$P" add my-blob &&
998 echo other-blob >"$P/other-blob" &&
999 git -C "$P" add other-blob &&
1000 git -C "$P" commit -m x &&
1002 configure_exclusion "$P" my-blob >h &&
1003 # Configure a URL for other-blob. Just reuse the hash of the object as
1004 # the hash of the packfile, since the hash does not matter for this
1005 # test as long as it is not the hash of the pack, and it is of the
1006 # expected length.
1007 git -C "$P" hash-object other-blob >objh &&
1008 git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
1009 git -C "$P" config --add \
1010 "uploadpack.blobpackfileuri" \
1011 "$(cat objh) $(cat objh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
1013 test_must_fail env GIT_TEST_SIDEBAND_ALL=1 \
1014 git -c protocol.version=2 \
1015 -c fetch.uriprotocols=http,https \
1016 clone "$HTTPD_URL/smart/http_parent" http_child 2>err &&
1017 test_i18ngrep "pack downloaded from.*does not match expected hash" err
1020 test_expect_success 'packfile-uri with transfer.fsckobjects' '
1021 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1022 rm -rf "$P" http_child log &&
1024 git init "$P" &&
1025 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1027 echo my-blob >"$P/my-blob" &&
1028 git -C "$P" add my-blob &&
1029 git -C "$P" commit -m x &&
1031 configure_exclusion "$P" my-blob >h &&
1033 sane_unset GIT_TEST_SIDEBAND_ALL &&
1034 git -c protocol.version=2 -c transfer.fsckobjects=1 \
1035 -c fetch.uriprotocols=http,https \
1036 clone "$HTTPD_URL/smart/http_parent" http_child &&
1038 # Ensure that there are exactly 2 packfiles with associated .idx
1039 ls http_child/.git/objects/pack/*.pack \
1040 http_child/.git/objects/pack/*.idx >filelist &&
1041 test_line_count = 4 filelist
1044 test_expect_success 'packfile-uri with transfer.fsckobjects fails on bad object' '
1045 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1046 rm -rf "$P" http_child log &&
1048 git init "$P" &&
1049 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1051 cat >bogus-commit <<-EOF &&
1052 tree $EMPTY_TREE
1053 author Bugs Bunny 1234567890 +0000
1054 committer Bugs Bunny <bugs@bun.ni> 1234567890 +0000
1056 This commit object intentionally broken
1058 BOGUS=$(git -C "$P" hash-object -t commit -w --stdin <bogus-commit) &&
1059 git -C "$P" branch bogus-branch "$BOGUS" &&
1061 echo my-blob >"$P/my-blob" &&
1062 git -C "$P" add my-blob &&
1063 git -C "$P" commit -m x &&
1065 configure_exclusion "$P" my-blob >h &&
1067 sane_unset GIT_TEST_SIDEBAND_ALL &&
1068 test_must_fail git -c protocol.version=2 -c transfer.fsckobjects=1 \
1069 -c fetch.uriprotocols=http,https \
1070 clone "$HTTPD_URL/smart/http_parent" http_child 2>error &&
1071 test_i18ngrep "invalid author/committer line - missing email" error
1074 test_expect_success 'packfile-uri with transfer.fsckobjects succeeds when .gitmodules is separate from tree' '
1075 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1076 rm -rf "$P" http_child &&
1078 git init "$P" &&
1079 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1081 echo "[submodule libfoo]" >"$P/.gitmodules" &&
1082 echo "path = include/foo" >>"$P/.gitmodules" &&
1083 echo "url = git://example.com/git/lib.git" >>"$P/.gitmodules" &&
1084 git -C "$P" add .gitmodules &&
1085 git -C "$P" commit -m x &&
1087 configure_exclusion "$P" .gitmodules >h &&
1089 sane_unset GIT_TEST_SIDEBAND_ALL &&
1090 git -c protocol.version=2 -c transfer.fsckobjects=1 \
1091 -c fetch.uriprotocols=http,https \
1092 clone "$HTTPD_URL/smart/http_parent" http_child &&
1094 # Ensure that there are exactly 2 packfiles with associated .idx
1095 ls http_child/.git/objects/pack/*.pack \
1096 http_child/.git/objects/pack/*.idx >filelist &&
1097 test_line_count = 4 filelist
1100 test_expect_success 'packfile-uri with transfer.fsckobjects fails when .gitmodules separate from tree is invalid' '
1101 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1102 rm -rf "$P" http_child err &&
1104 git init "$P" &&
1105 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1107 echo "[submodule \"..\"]" >"$P/.gitmodules" &&
1108 echo "path = include/foo" >>"$P/.gitmodules" &&
1109 echo "url = git://example.com/git/lib.git" >>"$P/.gitmodules" &&
1110 git -C "$P" add .gitmodules &&
1111 git -C "$P" commit -m x &&
1113 configure_exclusion "$P" .gitmodules >h &&
1115 sane_unset GIT_TEST_SIDEBAND_ALL &&
1116 test_must_fail git -c protocol.version=2 -c transfer.fsckobjects=1 \
1117 -c fetch.uriprotocols=http,https \
1118 clone "$HTTPD_URL/smart/http_parent" http_child 2>err &&
1119 test_i18ngrep "disallowed submodule name" err
1122 test_expect_success 'packfile-uri path redacted in trace' '
1123 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1124 rm -rf "$P" http_child log &&
1126 git init "$P" &&
1127 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1129 echo my-blob >"$P/my-blob" &&
1130 git -C "$P" add my-blob &&
1131 git -C "$P" commit -m x &&
1133 git -C "$P" hash-object my-blob >objh &&
1134 git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
1135 git -C "$P" config --add \
1136 "uploadpack.blobpackfileuri" \
1137 "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
1139 GIT_TRACE_PACKET="$(pwd)/log" \
1140 git -c protocol.version=2 \
1141 -c fetch.uriprotocols=http,https \
1142 clone "$HTTPD_URL/smart/http_parent" http_child &&
1144 grep -F "clone< \\1$(cat packh) $HTTPD_URL/<redacted>" log
1147 test_expect_success 'packfile-uri path not redacted in trace when GIT_TRACE_REDACT=0' '
1148 P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
1149 rm -rf "$P" http_child log &&
1151 git init "$P" &&
1152 git -C "$P" config "uploadpack.allowsidebandall" "true" &&
1154 echo my-blob >"$P/my-blob" &&
1155 git -C "$P" add my-blob &&
1156 git -C "$P" commit -m x &&
1158 git -C "$P" hash-object my-blob >objh &&
1159 git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
1160 git -C "$P" config --add \
1161 "uploadpack.blobpackfileuri" \
1162 "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
1164 GIT_TRACE_PACKET="$(pwd)/log" \
1165 GIT_TRACE_REDACT=0 \
1166 git -c protocol.version=2 \
1167 -c fetch.uriprotocols=http,https \
1168 clone "$HTTPD_URL/smart/http_parent" http_child &&
1170 grep -F "clone< \\1$(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" log
1173 test_expect_success 'http:// --negotiate-only' '
1174 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
1175 URI="$HTTPD_URL/smart/server" &&
1177 setup_negotiate_only "$SERVER" "$URI" &&
1179 git -c protocol.version=2 -C client fetch \
1180 --no-tags \
1181 --negotiate-only \
1182 --negotiation-tip=$(git -C client rev-parse HEAD) \
1183 origin >out &&
1184 COMMON=$(git -C "$SERVER" rev-parse two) &&
1185 grep "$COMMON" out
1188 test_expect_success 'http:// --negotiate-only without wait-for-done support' '
1189 SERVER="server" &&
1190 URI="$HTTPD_URL/one_time_perl/server" &&
1192 setup_negotiate_only "$SERVER" "$URI" &&
1194 echo "s/ wait-for-done/ xxxx-xxx-xxxx/" \
1195 >"$HTTPD_ROOT_PATH/one-time-perl" &&
1197 test_must_fail git -c protocol.version=2 -C client fetch \
1198 --no-tags \
1199 --negotiate-only \
1200 --negotiation-tip=$(git -C client rev-parse HEAD) \
1201 origin 2>err &&
1202 test_i18ngrep "server does not support wait-for-done" err
1205 test_expect_success 'http:// --negotiate-only with protocol v0' '
1206 SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
1207 URI="$HTTPD_URL/smart/server" &&
1209 setup_negotiate_only "$SERVER" "$URI" &&
1211 test_must_fail git -c protocol.version=0 -C client fetch \
1212 --no-tags \
1213 --negotiate-only \
1214 --negotiation-tip=$(git -C client rev-parse HEAD) \
1215 origin 2>err &&
1216 test_i18ngrep "negotiate-only requires protocol v2" err
1219 # DO NOT add non-httpd-specific tests here, because the last part of this
1220 # test script is only executed when httpd is available and enabled.
1222 test_done