git-commit-vandalism/t/t5702-protocol-v2.sh
Jonathan Tan d1035cac09 upload-pack: clear flags before each v2 request
Suppose a server has the following commit graph:

 A   B
  \ /
   O

We create a client by cloning A from the server with depth 1, and add
many commits to it (so that future fetches span multiple requests due to
lengthy negotiation). If it then fetches B using protocol v2, the fetch
spanning multiple requests, the resulting packfile does not contain O
even though the client did report that A is shallow.

This is because upload_pack_v2() can be called multiple times while
processing the same session. During the 2nd and all subsequent
invocations, some object flags remain from the previous invocations. In
particular, CLIENT_SHALLOW remains, preventing process_shallow() from
adding client-reported shallows to the "shallows" array, and hence
pack-objects not knowing about these client-reported shallows.

Therefore, teach upload_pack_v2() to clear object flags at the start of
each invocation. This has some other results:

 - THEY_HAVE gates addition of objects to have_obj in process_haves().
   Previously in upload_pack_v2(), have_obj needed to be static because
   once an object is added to have_obj, it is never readded and thus we
   needed to retain the contents of have_obj between invocations. Now
   that flags are cleared, this is no longer necessary. This patch does
   not change the behavior of ok_to_give_up() (THEY_HAVE is still set on
   each "have") and got_oid() (used only in non-v2)); THEY_HAVE is not
   used in any other function.

 - WANTED gates addition of objects to want_obj in parse_want() and
   parse_want_ref(). It is also used in receive_needs(), but that is
   only used in non-v2. For the same reasons as THEY_HAVE, want_obj no
   longer needs to be static in upload_pack_v2().

 - CLIENT_SHALLOW is changed as discussed above.

Clearing of the other 5 flags does not affect functionality in v2. (Note
that in non-v2, upload_pack() is only called once per process, so each
invocation starts with blank flags anyway.)

 - OUR_REF is only used in non-v2.

 - COMMON_KNOWN is only used as a scratch flag in ok_to_give_up().

 - SHALLOW is passed to invocations in deepen() and
   deepen_by_rev_list(), but upload-pack doesn't use it.

 - NOT_SHALLOW is used by send_shallow() and send_unshallow(), but
   invocations of those functions are always preceded by code that sets
   NOT_SHALLOW on the appropriate objects.

 - HIDDEN_REF is only used in non-v2.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-10-19 12:04:53 +09:00

527 lines
16 KiB
Bash
Executable File

#!/bin/sh
test_description='test git wire-protocol version 2'
TEST_NO_CREATE_REPO=1
. ./test-lib.sh
# Test protocol v2 with 'git://' transport
#
. "$TEST_DIRECTORY"/lib-git-daemon.sh
start_git_daemon --export-all --enable=receive-pack
daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
test_expect_success 'create repo to be served by git-daemon' '
git init "$daemon_parent" &&
test_commit -C "$daemon_parent" one
'
test_expect_success 'list refs with git:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
# Client requested to use protocol v2
grep "git> .*\\\0\\\0version=2\\\0$" log &&
# Server responded using protocol v2
grep "git< version 2" log &&
git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
test_cmp expect actual
'
test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
ls-remote "$GIT_DAEMON_URL/parent" master >actual &&
cat >expect <<-EOF &&
$(git -C "$daemon_parent" rev-parse refs/heads/master)$(printf "\t")refs/heads/master
EOF
test_cmp expect actual
'
test_expect_success 'clone with git:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
clone "$GIT_DAEMON_URL/parent" daemon_child &&
git -C daemon_child log -1 --format=%s >actual &&
git -C "$daemon_parent" log -1 --format=%s >expect &&
test_cmp expect actual &&
# Client requested to use protocol v2
grep "clone> .*\\\0\\\0version=2\\\0$" log &&
# Server responded using protocol v2
grep "clone< version 2" log
'
test_expect_success 'fetch with git:// using protocol v2' '
test_when_finished "rm -f log" &&
test_commit -C "$daemon_parent" two &&
GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
fetch &&
git -C daemon_child log -1 --format=%s origin/master >actual &&
git -C "$daemon_parent" log -1 --format=%s >expect &&
test_cmp expect actual &&
# Client requested to use protocol v2
grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
# Server responded using protocol v2
grep "fetch< version 2" log
'
test_expect_success 'pull with git:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
pull &&
git -C daemon_child log -1 --format=%s >actual &&
git -C "$daemon_parent" log -1 --format=%s >expect &&
test_cmp expect actual &&
# Client requested to use protocol v2
grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
# Server responded using protocol v2
grep "fetch< version 2" log
'
test_expect_success 'push with git:// and a config of v2 does not request v2' '
test_when_finished "rm -f log" &&
# Till v2 for push is designed, make sure that if a client has
# protocol.version configured to use v2, that the client instead falls
# back and uses v0.
test_commit -C daemon_child three &&
# Push to another branch, as the target repository has the
# master branch checked out and we cannot push into it.
GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
push origin HEAD:client_branch &&
git -C daemon_child log -1 --format=%s >actual &&
git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
test_cmp expect actual &&
# Client requested to use protocol v2
! grep "push> .*\\\0\\\0version=2\\\0$" log &&
# Server responded using protocol v2
! grep "push< version 2" log
'
stop_git_daemon
# Test protocol v2 with 'file://' transport
#
test_expect_success 'create repo to be served by file:// transport' '
git init file_parent &&
test_commit -C file_parent one
'
test_expect_success 'list refs with file:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
ls-remote --symref "file://$(pwd)/file_parent" >actual &&
# Server responded using protocol v2
grep "git< version 2" log &&
git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
test_cmp expect actual
'
test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
ls-remote "file://$(pwd)/file_parent" master >actual &&
cat >expect <<-EOF &&
$(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
EOF
test_cmp expect actual
'
test_expect_success 'server-options are sent when using ls-remote' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
ls-remote -o hello -o world "file://$(pwd)/file_parent" master >actual &&
cat >expect <<-EOF &&
$(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
EOF
test_cmp expect actual &&
grep "server-option=hello" log &&
grep "server-option=world" log
'
test_expect_success 'clone with file:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
clone "file://$(pwd)/file_parent" file_child &&
git -C file_child log -1 --format=%s >actual &&
git -C file_parent log -1 --format=%s >expect &&
test_cmp expect actual &&
# Server responded using protocol v2
grep "clone< version 2" log &&
# Client sent ref-prefixes to filter the ref-advertisement
grep "ref-prefix HEAD" log &&
grep "ref-prefix refs/heads/" log &&
grep "ref-prefix refs/tags/" log
'
test_expect_success 'fetch with file:// using protocol v2' '
test_when_finished "rm -f log" &&
test_commit -C file_parent two &&
GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
fetch origin &&
git -C file_child log -1 --format=%s origin/master >actual &&
git -C file_parent log -1 --format=%s >expect &&
test_cmp expect actual &&
# Server responded using protocol v2
grep "fetch< version 2" log
'
test_expect_success 'ref advertisment is filtered during fetch using protocol v2' '
test_when_finished "rm -f log" &&
test_commit -C file_parent three &&
git -C file_parent branch unwanted-branch three &&
GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
fetch origin master &&
git -C file_child log -1 --format=%s origin/master >actual &&
git -C file_parent log -1 --format=%s >expect &&
test_cmp expect actual &&
grep "refs/heads/master" log &&
! grep "refs/heads/unwanted-branch" log
'
test_expect_success 'server-options are sent when fetching' '
test_when_finished "rm -f log" &&
test_commit -C file_parent four &&
GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
fetch -o hello -o world origin master &&
git -C file_child log -1 --format=%s origin/master >actual &&
git -C file_parent log -1 --format=%s >expect &&
test_cmp expect actual &&
grep "server-option=hello" log &&
grep "server-option=world" log
'
test_expect_success 'upload-pack respects config using protocol v2' '
git init server &&
write_script server/.git/hook <<-\EOF &&
touch hookout
"$@"
EOF
test_commit -C server one &&
test_config_global uploadpack.packobjectshook ./hook &&
test_path_is_missing server/.git/hookout &&
git -c protocol.version=2 clone "file://$(pwd)/server" client &&
test_path_is_file server/.git/hookout
'
test_expect_success 'setup filter tests' '
rm -rf server client &&
git init server &&
# 1 commit to create a file, and 1 commit to modify it
test_commit -C server message1 a.txt &&
test_commit -C server message2 a.txt &&
git -C server config protocol.version 2 &&
git -C server config uploadpack.allowfilter 1 &&
git -C server config uploadpack.allowanysha1inwant 1 &&
git -C server config protocol.version 2
'
test_expect_success 'partial clone' '
GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
clone --filter=blob:none "file://$(pwd)/server" client &&
grep "version 2" trace &&
# Ensure that the old version of the file is missing
git -C client rev-list --quiet --objects --missing=print master \
>observed.oids &&
grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
# Ensure that client passes fsck
git -C client fsck
'
test_expect_success 'dynamically fetch missing object' '
rm "$(pwd)/trace" &&
GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
cat-file -p $(git -C server rev-parse message1:a.txt) &&
grep "version 2" trace
'
test_expect_success 'partial fetch' '
rm -rf client "$(pwd)/trace" &&
git init client &&
SERVER="file://$(pwd)/server" &&
test_config -C client extensions.partialClone "$SERVER" &&
GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
fetch --filter=blob:none "$SERVER" master:refs/heads/other &&
grep "version 2" trace &&
# Ensure that the old version of the file is missing
git -C client rev-list --quiet --objects --missing=print other \
>observed.oids &&
grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
# Ensure that client passes fsck
git -C client fsck
'
test_expect_success 'do not advertise filter if not configured to do so' '
SERVER="file://$(pwd)/server" &&
rm "$(pwd)/trace" &&
git -C server config uploadpack.allowfilter 1 &&
GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
ls-remote "$SERVER" &&
grep "fetch=.*filter" trace &&
rm "$(pwd)/trace" &&
git -C server config uploadpack.allowfilter 0 &&
GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
ls-remote "$SERVER" &&
grep "fetch=" trace >fetch_capabilities &&
! grep filter fetch_capabilities
'
test_expect_success 'partial clone warns if filter is not advertised' '
rm -rf client &&
git -C server config uploadpack.allowfilter 0 &&
git -c protocol.version=2 \
clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
test_i18ngrep "filtering not recognized by server, ignoring" err
'
test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
git -C server config uploadpack.allowfilter 0 &&
# Custom request that tries to filter even though it is not advertised.
test-tool pkt-line pack >in <<-EOF &&
command=fetch
0001
want $(git -C server rev-parse master)
filter blob:none
0000
EOF
test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
grep "unexpected line: .filter blob:none." err &&
# Exercise to ensure that if advertised, filter works
git -C server config uploadpack.allowfilter 1 &&
git -C server serve --stateless-rpc <in >/dev/null
'
test_expect_success 'default refspec is used to filter ref when fetchcing' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
fetch origin &&
git -C file_child log -1 --format=%s three >actual &&
git -C file_parent log -1 --format=%s three >expect &&
test_cmp expect actual &&
grep "ref-prefix refs/heads/" log &&
grep "ref-prefix refs/tags/" log
'
test_expect_success 'fetch supports various ways of have lines' '
rm -rf server client trace &&
git init server &&
test_commit -C server dwim &&
TREE=$(git -C server rev-parse HEAD^{tree}) &&
git -C server tag exact \
$(git -C server commit-tree -m a "$TREE") &&
git -C server tag dwim-unwanted \
$(git -C server commit-tree -m b "$TREE") &&
git -C server tag exact-unwanted \
$(git -C server commit-tree -m c "$TREE") &&
git -C server tag prefix1 \
$(git -C server commit-tree -m d "$TREE") &&
git -C server tag prefix2 \
$(git -C server commit-tree -m e "$TREE") &&
git -C server tag fetch-by-sha1 \
$(git -C server commit-tree -m f "$TREE") &&
git -C server tag completely-unrelated \
$(git -C server commit-tree -m g "$TREE") &&
git init client &&
GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
fetch "file://$(pwd)/server" \
dwim \
refs/tags/exact \
refs/tags/prefix*:refs/tags/prefix* \
"$(git -C server rev-parse fetch-by-sha1)" &&
# Ensure that the appropriate prefixes are sent (using a sample)
grep "fetch> ref-prefix dwim" trace &&
grep "fetch> ref-prefix refs/heads/dwim" trace &&
grep "fetch> ref-prefix refs/tags/prefix" trace &&
# Ensure that the correct objects are returned
git -C client cat-file -e $(git -C server rev-parse dwim) &&
git -C client cat-file -e $(git -C server rev-parse exact) &&
git -C client cat-file -e $(git -C server rev-parse prefix1) &&
git -C client cat-file -e $(git -C server rev-parse prefix2) &&
git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
test_must_fail git -C client cat-file -e \
$(git -C server rev-parse dwim-unwanted) &&
test_must_fail git -C client cat-file -e \
$(git -C server rev-parse exact-unwanted) &&
test_must_fail git -C client cat-file -e \
$(git -C server rev-parse completely-unrelated)
'
test_expect_success 'fetch supports include-tag and tag following' '
rm -rf server client trace &&
git init server &&
test_commit -C server to_fetch &&
git -C server tag -a annotated_tag -m message &&
git init client &&
GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
fetch "$(pwd)/server" to_fetch:to_fetch &&
grep "fetch> ref-prefix to_fetch" trace &&
grep "fetch> ref-prefix refs/tags/" trace &&
grep "fetch> include-tag" trace &&
git -C client cat-file -e $(git -C client rev-parse annotated_tag)
'
test_expect_success 'upload-pack respects client shallows' '
rm -rf server client trace &&
git init server &&
test_commit -C server base &&
test_commit -C server client_has &&
git clone --depth=1 "file://$(pwd)/server" client &&
# Add extra commits to the client so that the whole fetch takes more
# than 1 request (due to negotiation)
for i in $(test_seq 1 32)
do
test_commit -C client c$i
done &&
git -C server checkout -b newbranch base &&
test_commit -C server client_wants &&
GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
fetch origin newbranch &&
# Ensure that protocol v2 is used
grep "fetch< version 2" trace
'
# Test protocol v2 with 'http://' transport
#
. "$TEST_DIRECTORY"/lib-httpd.sh
start_httpd
test_expect_success 'create repo to be served by http:// transport' '
git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
'
test_expect_success 'clone with http:// using protocol v2' '
test_when_finished "rm -f log" &&
GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
clone "$HTTPD_URL/smart/http_parent" http_child &&
git -C http_child log -1 --format=%s >actual &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
test_cmp expect actual &&
# Client requested to use protocol v2
grep "Git-Protocol: version=2" log &&
# Server responded using protocol v2
grep "git< version 2" log
'
test_expect_success 'fetch with http:// using protocol v2' '
test_when_finished "rm -f log" &&
test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
fetch &&
git -C http_child log -1 --format=%s origin/master >actual &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
test_cmp expect actual &&
# Server responded using protocol v2
grep "git< version 2" log
'
test_expect_success 'push with http:// and a config of v2 does not request v2' '
test_when_finished "rm -f log" &&
# Till v2 for push is designed, make sure that if a client has
# protocol.version configured to use v2, that the client instead falls
# back and uses v0.
test_commit -C http_child three &&
# Push to another branch, as the target repository has the
# master branch checked out and we cannot push into it.
GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
push origin HEAD:client_branch &&
git -C http_child log -1 --format=%s >actual &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
test_cmp expect actual &&
# Client didnt request to use protocol v2
! grep "Git-Protocol: version=2" log &&
# Server didnt respond using protocol v2
! grep "git< version 2" log
'
stop_httpd
test_done