1
0
mirror of https://github.com/git/git synced 2024-07-05 00:58:49 +00:00
git/t/t5703-upload-pack-ref-in-want.sh

405 lines
10 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='upload-pack ref-in-want'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
tests: mark tests relying on the current default for `init.defaultBranch` In addition to the manual adjustment to let the `linux-gcc` CI job run the test suite with `master` and then with `main`, this patch makes sure that GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME is set in all test scripts that currently rely on the initial branch name being `master by default. To determine which test scripts to mark up, the first step was to force-set the default branch name to `master` in - all test scripts that contain the keyword `master`, - t4211, which expects `t/t4211/history.export` with a hard-coded ref to initialize the default branch, - t5560 because it sources `t/t556x_common` which uses `master`, - t8002 and t8012 because both source `t/annotate-tests.sh` which also uses `master`) This trick was performed by this command: $ sed -i '/^ *\. \.\/\(test-lib\|lib-\(bash\|cvs\|git-svn\)\|gitweb-lib\)\.sh$/i\ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\ ' $(git grep -l master t/t[0-9]*.sh) \ t/t4211*.sh t/t5560*.sh t/t8002*.sh t/t8012*.sh After that, careful, manual inspection revealed that some of the test scripts containing the needle `master` do not actually rely on a specific default branch name: either they mention `master` only in a comment, or they initialize that branch specificially, or they do not actually refer to the current default branch. Therefore, the aforementioned modification was undone in those test scripts thusly: $ git checkout HEAD -- \ t/t0027-auto-crlf.sh t/t0060-path-utils.sh \ t/t1011-read-tree-sparse-checkout.sh \ t/t1305-config-include.sh t/t1309-early-config.sh \ t/t1402-check-ref-format.sh t/t1450-fsck.sh \ t/t2024-checkout-dwim.sh \ t/t2106-update-index-assume-unchanged.sh \ t/t3040-subprojects-basic.sh t/t3301-notes.sh \ t/t3308-notes-merge.sh t/t3423-rebase-reword.sh \ t/t3436-rebase-more-options.sh \ t/t4015-diff-whitespace.sh t/t4257-am-interactive.sh \ t/t5323-pack-redundant.sh t/t5401-update-hooks.sh \ t/t5511-refspec.sh t/t5526-fetch-submodules.sh \ t/t5529-push-errors.sh t/t5530-upload-pack-error.sh \ t/t5548-push-porcelain.sh \ t/t5552-skipping-fetch-negotiator.sh \ t/t5572-pull-submodule.sh t/t5608-clone-2gb.sh \ t/t5614-clone-submodules-shallow.sh \ t/t7508-status.sh t/t7606-merge-custom.sh \ t/t9302-fast-import-unpack-limit.sh We excluded one set of test scripts in these commands, though: the range of `git p4` tests. The reason? `git p4` stores the (foreign) remote branch in the branch called `p4/master`, which is obviously not the default branch. Manual analysis revealed that only five of these tests actually require a specific default branch name to pass; They were modified thusly: $ sed -i '/^ *\. \.\/lib-git-p4\.sh$/i\ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\ ' t/t980[0167]*.sh t/t9811*.sh Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-11-18 23:44:19 +00:00
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
get_actual_refs () {
sed -n -e '/wanted-refs/,/0001/{
/wanted-refs/d
/0001/d
p
}' <out | test-tool pkt-line unpack >actual_refs
}
get_actual_commits () {
test-tool pkt-line unpack-sideband <out >o.pack &&
git index-pack o.pack &&
git verify-pack -v o.idx >objs &&
sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs >objs.sed &&
sort >actual_commits <objs.sed
}
check_output () {
get_actual_refs &&
test_cmp expected_refs actual_refs &&
get_actual_commits &&
sort expected_commits >sorted_commits &&
test_cmp sorted_commits actual_commits
}
write_command () {
echo "command=$1"
if test "$(test_oid algo)" != sha1
then
echo "object-format=$(test_oid algo)"
fi
}
# c(o/foo) d(o/bar)
# \ /
# b e(baz) f(main)
# \__ | __/
# \ | /
# a
test_expect_success 'setup repository' '
test_commit a &&
git branch -M main &&
git checkout -b o/foo &&
test_commit b &&
test_commit c &&
git checkout -b o/bar b &&
test_commit d &&
git checkout -b baz a &&
test_commit e &&
git checkout main &&
test_commit f
'
test_expect_success 'config controls ref-in-want advertisement' '
test-tool serve-v2 --advertise-capabilities >out &&
perl -ne "/ref-in-want/ and print" out >out.filter &&
test_must_be_empty out.filter &&
git config uploadpack.allowRefInWant false &&
test-tool serve-v2 --advertise-capabilities >out &&
perl -ne "/ref-in-want/ and print" out >out.filter &&
test_must_be_empty out.filter &&
git config uploadpack.allowRefInWant true &&
test-tool serve-v2 --advertise-capabilities >out &&
perl -ne "/ref-in-want/ and print" out >out.filter &&
test_file_not_empty out.filter
'
test_expect_success 'invalid want-ref line' '
test-tool pkt-line pack >in <<-EOF &&
$(write_command fetch)
0001
no-progress
want-ref refs/heads/non-existent
done
0000
EOF
test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
grep "unknown ref" out
'
test_expect_success 'basic want-ref' '
oid=$(git rev-parse f) &&
cat >expected_refs <<-EOF &&
$oid refs/heads/main
EOF
git rev-parse f >expected_commits &&
oid=$(git rev-parse a) &&
test-tool pkt-line pack >in <<-EOF &&
$(write_command fetch)
0001
no-progress
want-ref refs/heads/main
have $oid
done
0000
EOF
test-tool serve-v2 --stateless-rpc >out <in &&
check_output
'
test_expect_success 'multiple want-ref lines' '
oid_c=$(git rev-parse c) &&
oid_d=$(git rev-parse d) &&
cat >expected_refs <<-EOF &&
$oid_c refs/heads/o/foo
$oid_d refs/heads/o/bar
EOF
git rev-parse c d >expected_commits &&
oid=$(git rev-parse b) &&
test-tool pkt-line pack >in <<-EOF &&
$(write_command fetch)
0001
no-progress
want-ref refs/heads/o/foo
want-ref refs/heads/o/bar
have $oid
done
0000
EOF
test-tool serve-v2 --stateless-rpc >out <in &&
check_output
'
test_expect_success 'mix want and want-ref' '
oid=$(git rev-parse f) &&
cat >expected_refs <<-EOF &&
$oid refs/heads/main
EOF
git rev-parse e f >expected_commits &&
test-tool pkt-line pack >in <<-EOF &&
$(write_command fetch)
0001
no-progress
want-ref refs/heads/main
want $(git rev-parse e)
have $(git rev-parse a)
done
0000
EOF
test-tool serve-v2 --stateless-rpc >out <in &&
check_output
'
test_expect_success 'want-ref with ref we already have commit for' '
oid=$(git rev-parse c) &&
cat >expected_refs <<-EOF &&
$oid refs/heads/o/foo
EOF
>expected_commits &&
oid=$(git rev-parse c) &&
test-tool pkt-line pack >in <<-EOF &&
$(write_command fetch)
0001
no-progress
want-ref refs/heads/o/foo
have $oid
done
0000
EOF
test-tool serve-v2 --stateless-rpc >out <in &&
check_output
'
REPO="$(pwd)/repo"
LOCAL_PRISTINE="$(pwd)/local_pristine"
# $REPO
# c(o/foo) d(o/bar)
# \ /
# b e(baz) f(main)
# \__ | __/
# \ | /
# a
#
# $LOCAL_PRISTINE
# s32(side)
# |
# .
# .
# |
# a(main)
test_expect_success 'setup repos for fetching with ref-in-want tests' '
(
git init -b main "$REPO" &&
cd "$REPO" &&
test_commit a &&
# Local repo with many commits (so that negotiation will take
# more than 1 request/response pair)
rm -rf "$LOCAL_PRISTINE" &&
git clone "file://$REPO" "$LOCAL_PRISTINE" &&
cd "$LOCAL_PRISTINE" &&
git checkout -b side &&
test_commit_bulk --id=s 33 &&
# Add novel commits to upstream
git checkout main &&
cd "$REPO" &&
git checkout -b o/foo &&
test_commit b &&
test_commit c &&
git checkout -b o/bar b &&
test_commit d &&
git checkout -b baz a &&
test_commit e &&
git checkout main &&
test_commit f
) &&
git -C "$REPO" config uploadpack.allowRefInWant true &&
git -C "$LOCAL_PRISTINE" config protocol.version 2
'
test_expect_success 'fetching with exact OID' '
test_when_finished "rm -f log" &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
oid=$(git -C "$REPO" rev-parse d) &&
GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
"$oid":refs/heads/actual &&
git -C "$REPO" rev-parse "d" >expected &&
git -C local rev-parse refs/heads/actual >actual &&
test_cmp expected actual &&
grep "want $oid" log
'
test_expect_success 'fetching multiple refs' '
test_when_finished "rm -f log" &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
git -C "$REPO" rev-parse "main" "baz" >expected &&
git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
test_cmp expected actual &&
grep "want-ref refs/heads/main" log &&
grep "want-ref refs/heads/baz" log
'
test_expect_success 'fetching ref and exact OID' '
test_when_finished "rm -f log" &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
oid=$(git -C "$REPO" rev-parse b) &&
GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
main "$oid":refs/heads/actual &&
git -C "$REPO" rev-parse "main" "b" >expected &&
git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
test_cmp expected actual &&
grep "want $oid" log &&
grep "want-ref refs/heads/main" log
'
test_expect_success 'fetching with wildcard that does not match any refs' '
test_when_finished "rm -f log" &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
test_must_be_empty out
'
test_expect_success 'fetching with wildcard that matches multiple refs' '
test_when_finished "rm -f log" &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
git -C local rev-parse "o/foo" "o/bar" >actual &&
test_cmp expected actual &&
grep "want-ref refs/heads/o/foo" log &&
grep "want-ref refs/heads/o/bar" log
'
. "$TEST_DIRECTORY"/lib-httpd.sh
start_httpd
REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
LOCAL_PRISTINE="$(pwd)/local_pristine"
test_expect_success 'setup repos for change-while-negotiating test' '
(
git init -b main "$REPO" &&
cd "$REPO" &&
>.git/git-daemon-export-ok &&
test_commit m1 &&
git tag -d m1 &&
# Local repo with many commits (so that negotiation will take
# more than 1 request/response pair)
rm -rf "$LOCAL_PRISTINE" &&
git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
cd "$LOCAL_PRISTINE" &&
git checkout -b side &&
test_commit_bulk --id=s 33 &&
# Add novel commits to upstream
git checkout main &&
cd "$REPO" &&
test_commit m2 &&
test_commit m3 &&
git tag -d m2 m3
) &&
t/lib-httpd: avoid using macOS' sed Among other differences relative to GNU sed, macOS' sed always ends its output with a trailing newline, even if the input did not have such a trailing newline. Surprisingly, this makes three httpd-based tests fail on macOS: t5616, t5702 and t5703. ("Surprisingly" because those tests have been around for some time, but apparently nobody runs them on macOS with a working Apache2 setup.) The reason is that we use `sed` in those tests to filter the response of the web server. Apart from the fact that we use GNU constructs (such as using a space after the `c` command instead of a backslash and a newline), we have another problem: macOS' sed LF-only newlines while webservers are supposed to use CR/LF ones. Even worse, t5616 uses `sed` to replace a binary part of the response with a new binary part (kind of hoping that the replaced binary part does not contain a 0x0a byte which would be interpreted as a newline). To that end, it calls on Perl to read the binary pack file and hex-encode it, then calls on `sed` to prefix every hex digit pair with a `\x` in order to construct the text that the `c` statement of the `sed` invocation is supposed to insert. So we call Perl and sed to construct a sed statement. The final nail in the coffin is that macOS' sed does not even interpret those `\x<hex>` constructs. Let's just replace all of that by Perl snippets. With Perl, at least, we do not have to deal with GNU vs macOS semantics, we do not have to worry about unwanted trailing newlines, and we do not have to spawn commands to construct arguments for other commands to be spawned (i.e. we can avoid a whole lot of shell scripting complexity). The upshot is that this fixes t5616, t5702 and t5703 on macOS with Apache2. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-27 13:23:11 +00:00
git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
git -C "$LOCAL_PRISTINE" config protocol.version 2
'
inconsistency () {
# Simulate that the server initially reports $2 as the ref
# corresponding to $1, and after that, $1 as the ref corresponding to
# $1. This corresponds to the real-life situation where the server's
# repository appears to change during negotiation, for example, when
# different servers in a load-balancing arrangement serve (stateless)
# RPCs during a single negotiation.
oid1=$(git -C "$REPO" rev-parse $1) &&
oid2=$(git -C "$REPO" rev-parse $2) &&
t/lib-httpd: avoid using macOS' sed Among other differences relative to GNU sed, macOS' sed always ends its output with a trailing newline, even if the input did not have such a trailing newline. Surprisingly, this makes three httpd-based tests fail on macOS: t5616, t5702 and t5703. ("Surprisingly" because those tests have been around for some time, but apparently nobody runs them on macOS with a working Apache2 setup.) The reason is that we use `sed` in those tests to filter the response of the web server. Apart from the fact that we use GNU constructs (such as using a space after the `c` command instead of a backslash and a newline), we have another problem: macOS' sed LF-only newlines while webservers are supposed to use CR/LF ones. Even worse, t5616 uses `sed` to replace a binary part of the response with a new binary part (kind of hoping that the replaced binary part does not contain a 0x0a byte which would be interpreted as a newline). To that end, it calls on Perl to read the binary pack file and hex-encode it, then calls on `sed` to prefix every hex digit pair with a `\x` in order to construct the text that the `c` statement of the `sed` invocation is supposed to insert. So we call Perl and sed to construct a sed statement. The final nail in the coffin is that macOS' sed does not even interpret those `\x<hex>` constructs. Let's just replace all of that by Perl snippets. With Perl, at least, we do not have to deal with GNU vs macOS semantics, we do not have to worry about unwanted trailing newlines, and we do not have to spawn commands to construct arguments for other commands to be spawned (i.e. we can avoid a whole lot of shell scripting complexity). The upshot is that this fixes t5616, t5702 and t5703 on macOS with Apache2. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-02-27 13:23:11 +00:00
echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
}
test_expect_success 'server is initially ahead - no ref in want' '
git -C "$REPO" config uploadpack.allowRefInWant false &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
inconsistency main $(test_oid numeric) &&
test_must_fail git -C local fetch 2>err &&
test_i18ngrep "fatal: remote error: upload-pack: not our ref" err
'
test_expect_success 'server is initially ahead - ref in want' '
git -C "$REPO" config uploadpack.allowRefInWant true &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
inconsistency main $(test_oid numeric) &&
git -C local fetch &&
git -C "$REPO" rev-parse --verify main >expected &&
git -C local rev-parse --verify refs/remotes/origin/main >actual &&
test_cmp expected actual
'
test_expect_success 'server is initially behind - no ref in want' '
git -C "$REPO" config uploadpack.allowRefInWant false &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
inconsistency main "main^" &&
git -C local fetch &&
git -C "$REPO" rev-parse --verify "main^" >expected &&
git -C local rev-parse --verify refs/remotes/origin/main >actual &&
test_cmp expected actual
'
test_expect_success 'server is initially behind - ref in want' '
git -C "$REPO" config uploadpack.allowRefInWant true &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
inconsistency main "main^" &&
git -C local fetch &&
git -C "$REPO" rev-parse --verify "main" >expected &&
git -C local rev-parse --verify refs/remotes/origin/main >actual &&
test_cmp expected actual
'
test_expect_success 'server loses a ref - ref in want' '
git -C "$REPO" config uploadpack.allowRefInWant true &&
rm -rf local &&
cp -r "$LOCAL_PRISTINE" local &&
echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
test_must_fail git -C local fetch 2>err &&
test_i18ngrep "fatal: remote error: unknown ref refs/heads/rain" err
'
# DO NOT add non-httpd-specific tests here, because the last part of this
# test script is only executed when httpd is available and enabled.
test_done