git/t/t5545-push-options.sh

292 lines
8.9 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='pushing to a repository using push options'
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
GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
. ./test-lib.sh
mk_repo_pair () {
rm -rf workbench upstream &&
test_create_repo upstream &&
test_create_repo workbench &&
(
cd upstream &&
git config receive.denyCurrentBranch warn &&
mkdir -p .git/hooks &&
cat >.git/hooks/pre-receive <<-'EOF' &&
#!/bin/sh
if test -n "$GIT_PUSH_OPTION_COUNT"; then
i=0
>hooks/pre-receive.push_options
while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
eval "value=\$GIT_PUSH_OPTION_$i"
echo $value >>hooks/pre-receive.push_options
i=$((i + 1))
done
fi
EOF
chmod u+x .git/hooks/pre-receive
cat >.git/hooks/post-receive <<-'EOF' &&
#!/bin/sh
if test -n "$GIT_PUSH_OPTION_COUNT"; then
i=0
>hooks/post-receive.push_options
while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
eval "value=\$GIT_PUSH_OPTION_$i"
echo $value >>hooks/post-receive.push_options
i=$((i + 1))
done
fi
EOF
chmod u+x .git/hooks/post-receive
) &&
(
cd workbench &&
git remote add up ../upstream
)
}
# Compare the ref ($1) in upstream with a ref value from workbench ($2)
# i.e. test_refs second HEAD@{2}
test_refs () {
test $# = 2 &&
git -C upstream rev-parse --verify "$1" >expect &&
git -C workbench rev-parse --verify "$2" >actual &&
test_cmp expect actual
}
test_expect_success 'one push option works for a single branch' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git push --push-option=asdf up main
) &&
test_refs main main &&
echo "asdf" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'push option denied by remote' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions false &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
test_must_fail git push --push-option=asdf up main
) &&
test_refs main HEAD@{1}
'
test_expect_success 'two push options work' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git push --push-option=asdf --push-option="more structured text" up main
) &&
test_refs main main &&
printf "asdf\nmore structured text\n" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'push options and submodules' '
test_when_finished "rm -rf parent" &&
test_when_finished "rm -rf parent_upstream" &&
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
cp -r upstream parent_upstream &&
test_commit -C upstream one &&
test_create_repo parent &&
git -C parent remote add up ../parent_upstream &&
test_commit -C parent one &&
git -C parent push --mirror up &&
test_config_global protocol.file.allow always &&
git -C parent submodule add ../upstream workbench &&
git -C parent/workbench remote add up ../../upstream &&
git -C parent commit -m "add submodule" &&
test_commit -C parent/workbench two &&
git -C parent add workbench &&
git -C parent commit -m "update workbench" &&
git -C parent push \
--push-option=asdf --push-option="more structured text" \
--recurse-submodules=on-demand up main &&
git -C upstream rev-parse --verify main >expect &&
git -C parent/workbench rev-parse --verify main >actual &&
test_cmp expect actual &&
git -C parent_upstream rev-parse --verify main >expect &&
git -C parent rev-parse --verify main >actual &&
test_cmp expect actual &&
printf "asdf\nmore structured text\n" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options &&
test_cmp expect parent_upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect parent_upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'default push option' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git -c push.pushOption=default push up main
) &&
test_refs main main &&
echo "default" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'two default push options' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git -c push.pushOption=default1 -c push.pushOption=default2 push up main
) &&
test_refs main main &&
printf "default1\ndefault2\n" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'push option from command line overrides from-config push option' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git -c push.pushOption=default push --push-option=manual up main
) &&
test_refs main main &&
echo "manual" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'empty value of push.pushOption in config clears the list' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
git -c push.pushOption=default1 -c push.pushOption= -c push.pushOption=default2 push up main
) &&
test_refs main main &&
echo "default2" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options
'
test_expect_success 'invalid push option in config' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
(
cd workbench &&
test_commit one &&
git push --mirror up &&
test_commit two &&
test_must_fail git -c push.pushOption push up main
) &&
test_refs main HEAD@{1}
'
test_expect_success 'push options keep quoted characters intact (direct)' '
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
test_commit -C workbench one &&
git -C workbench push --push-option="\"embedded quotes\"" up main &&
echo "\"embedded quotes\"" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options
'
. "$TEST_DIRECTORY"/lib-httpd.sh
start_httpd
# set up http repository for fetching/pushing, with push options config
# bool set to $1
mk_http_pair () {
test_when_finished "rm -rf test_http_clone" &&
test_when_finished 'rm -rf "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git' &&
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions "$1" &&
git -C upstream config http.receivepack true &&
cp -R upstream/.git "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git &&
git clone "$HTTPD_URL"/smart/upstream test_http_clone
}
test_expect_success 'push option denied properly by http server' '
mk_http_pair false &&
test_commit -C test_http_clone one &&
test_must_fail git -C test_http_clone push --push-option=asdf origin main 2>actual &&
test_grep "the receiving end does not support push options" actual &&
git -C test_http_clone push origin main
'
test_expect_success 'push options work properly across http' '
mk_http_pair true &&
test_commit -C test_http_clone one &&
git -C test_http_clone push origin main &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
git -C test_http_clone rev-parse --verify main >actual &&
test_cmp expect actual &&
test_commit -C test_http_clone two &&
git -C test_http_clone push --push-option=asdf --push-option="more structured text" origin main &&
printf "asdf\nmore structured text\n" >expect &&
test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options &&
test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/post-receive.push_options &&
git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
git -C test_http_clone rev-parse --verify main >actual &&
test_cmp expect actual
'
test_expect_success 'push options keep quoted characters intact (http)' '
mk_http_pair true &&
test_commit -C test_http_clone one &&
git -C test_http_clone push --push-option="\"embedded quotes\"" origin main &&
echo "\"embedded quotes\"" >expect &&
test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options
'
# 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