2007-12-08 13:29:47 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_description='pre-commit and pre-merge-commit hooks'
|
2007-12-08 13:29:47 +01:00
|
|
|
|
2020-11-19 00:44:40 +01:00
|
|
|
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-19 00:44:19 +01:00
|
|
|
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
|
|
|
|
2022-08-02 17:33:13 +02:00
|
|
|
TEST_PASSES_SANITIZE_LEAK=true
|
2007-12-08 13:29:47 +01:00
|
|
|
. ./test-lib.sh
|
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_expect_success 'root commit' '
|
|
|
|
echo "root" >file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m "zeroth" &&
|
|
|
|
git checkout -b side &&
|
|
|
|
echo "foo" >foo &&
|
|
|
|
git add foo &&
|
|
|
|
git commit -m "make it non-ff" &&
|
|
|
|
git branch side-orig side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git checkout main
|
2019-08-07 20:57:07 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'setup conflicting branches' '
|
2020-11-19 00:44:40 +01:00
|
|
|
test_when_finished "git checkout main" &&
|
|
|
|
git checkout -b conflicting-a main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
echo a >conflicting &&
|
|
|
|
git add conflicting &&
|
|
|
|
git commit -m conflicting-a &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git checkout -b conflicting-b main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
echo b >conflicting &&
|
|
|
|
git add conflicting &&
|
|
|
|
git commit -m conflicting-b
|
|
|
|
'
|
|
|
|
|
2019-08-07 20:57:05 +02:00
|
|
|
test_expect_success 'with no hook' '
|
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
echo "foo" >file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit -m "first" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_expect_success 'with no hook (merge)' '
|
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2007-12-10 08:42:45 +01:00
|
|
|
test_expect_success '--no-verify with no hook' '
|
2019-08-07 20:57:05 +02:00
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
echo "bar" >file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit --no-verify -m "bar" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
2007-12-08 13:29:47 +01:00
|
|
|
|
2019-08-07 20:57:08 +02:00
|
|
|
test_expect_success '--no-verify with no hook (merge)' '
|
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge --no-verify -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:08 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook () {
|
|
|
|
test_when_finished "rm -f actual_hooks expected_hooks" &&
|
|
|
|
echo "$1" >expected_hooks &&
|
|
|
|
test_hook "$1" <<-EOF
|
|
|
|
echo $1 >>actual_hooks
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2007-12-10 08:42:45 +01:00
|
|
|
test_expect_success 'with succeeding hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook "pre-commit" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "more" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit -m "more" &&
|
|
|
|
test_cmp expected_hooks actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_expect_success 'with succeeding hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook "pre-merge-commit" &&
|
2019-08-07 20:57:07 +02:00
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
test_cmp expected_hooks actual_hooks
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'automatic merge fails; both hooks are available' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook "pre-commit" &&
|
|
|
|
setup_success_hook "pre-merge-commit" &&
|
2019-08-07 20:57:07 +02:00
|
|
|
|
|
|
|
git checkout conflicting-a &&
|
|
|
|
test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
|
|
|
|
test_path_is_missing actual_hooks &&
|
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
echo "pre-commit" >expected_hooks &&
|
2019-08-07 20:57:07 +02:00
|
|
|
echo a+b >conflicting &&
|
|
|
|
git add conflicting &&
|
|
|
|
git commit -m "resolve conflict" &&
|
|
|
|
test_cmp expected_hooks actual_hooks
|
|
|
|
'
|
|
|
|
|
2007-12-10 08:42:45 +01:00
|
|
|
test_expect_success '--no-verify with succeeding hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook "pre-commit" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "even more" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit --no-verify -m "even more" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
2007-12-08 13:29:47 +01:00
|
|
|
|
2019-08-07 20:57:08 +02:00
|
|
|
test_expect_success '--no-verify with succeeding hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_success_hook "pre-merge-commit" &&
|
2019-08-07 20:57:08 +02:00
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge --no-verify -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:08 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_failing_hook () {
|
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
test_hook "$1" <<-EOF
|
|
|
|
echo $1-failing-hook >>actual_hooks
|
|
|
|
exit 1
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2008-02-01 10:50:53 +01:00
|
|
|
test_expect_success 'with failing hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_failing_hook "pre-commit" &&
|
|
|
|
test_when_finished "rm -f expected_hooks" &&
|
|
|
|
echo "pre-commit-failing-hook" >expected_hooks &&
|
|
|
|
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "another" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
test_must_fail git commit -m "another" &&
|
|
|
|
test_cmp expected_hooks actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '--no-verify with failing hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_failing_hook "pre-commit" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "stuff" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit --no-verify -m "stuff" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
2007-12-08 13:29:47 +01:00
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_expect_success 'with failing hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_failing_hook "pre-merge-commit" &&
|
|
|
|
echo "pre-merge-commit-failing-hook" >expected_hooks &&
|
2019-08-07 20:57:07 +02:00
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
test_must_fail git merge -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
test_cmp expected_hooks actual_hooks
|
|
|
|
'
|
|
|
|
|
2019-08-07 20:57:08 +02:00
|
|
|
test_expect_success '--no-verify with failing hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_failing_hook "pre-merge-commit" &&
|
|
|
|
|
2019-08-07 20:57:08 +02:00
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge --no-verify -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:08 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_non_exec_hook () {
|
|
|
|
test_when_finished "rm -f actual_hooks" &&
|
|
|
|
test_hook "$1" <<-\EOF &&
|
|
|
|
echo non-exec >>actual_hooks
|
|
|
|
exit 1
|
|
|
|
EOF
|
|
|
|
test_hook --disable "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 22:55:27 +01:00
|
|
|
test_expect_success POSIXPERM 'with non-executable hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_non_exec_hook "pre-commit" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "content" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit -m "content" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
|
|
|
|
2009-03-13 22:55:27 +01:00
|
|
|
test_expect_success POSIXPERM '--no-verify with non-executable hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_non_exec_hook "pre-commit" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "more content" >>file &&
|
2007-12-10 08:42:45 +01:00
|
|
|
git add file &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git commit --no-verify -m "more content" &&
|
|
|
|
test_path_is_missing actual_hooks
|
2007-12-10 08:42:45 +01:00
|
|
|
'
|
2011-06-02 11:26:25 +02:00
|
|
|
|
2019-08-07 20:57:07 +02:00
|
|
|
test_expect_success POSIXPERM 'with non-executable hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_non_exec_hook "pre-merge" &&
|
2019-08-07 20:57:07 +02:00
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:07 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2019-08-07 20:57:08 +02:00
|
|
|
test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_non_exec_hook "pre-merge" &&
|
2019-08-07 20:57:08 +02:00
|
|
|
git branch -f side side-orig &&
|
|
|
|
git checkout side &&
|
2020-11-19 00:44:40 +01:00
|
|
|
git merge --no-verify -m "merge main" main &&
|
|
|
|
git checkout main &&
|
2019-08-07 20:57:08 +02:00
|
|
|
test_path_is_missing actual_hooks
|
|
|
|
'
|
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_require_prefix_hook () {
|
|
|
|
test_when_finished "rm -f expected_hooks" &&
|
|
|
|
echo require-prefix >expected_hooks &&
|
|
|
|
test_hook pre-commit <<-\EOF
|
|
|
|
echo require-prefix >>actual_hooks
|
|
|
|
test $GIT_PREFIX = "success/"
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2011-06-02 11:26:25 +02:00
|
|
|
test_expect_success 'with hook requiring GIT_PREFIX' '
|
2022-03-17 11:13:16 +01:00
|
|
|
test_when_finished "rm -rf actual_hooks success" &&
|
|
|
|
setup_require_prefix_hook &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "more content" >>file &&
|
2011-06-02 11:26:25 +02:00
|
|
|
git add file &&
|
|
|
|
mkdir success &&
|
|
|
|
(
|
|
|
|
cd success &&
|
|
|
|
git commit -m "hook requires GIT_PREFIX = success/"
|
|
|
|
) &&
|
2019-08-07 20:57:05 +02:00
|
|
|
test_cmp expected_hooks actual_hooks
|
2011-06-02 11:26:25 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with failing hook requiring GIT_PREFIX' '
|
2022-03-17 11:13:16 +01:00
|
|
|
test_when_finished "rm -rf actual_hooks fail" &&
|
|
|
|
setup_require_prefix_hook &&
|
2019-08-07 20:57:05 +02:00
|
|
|
echo "more content" >>file &&
|
2011-06-02 11:26:25 +02:00
|
|
|
git add file &&
|
|
|
|
mkdir fail &&
|
|
|
|
(
|
|
|
|
cd fail &&
|
|
|
|
test_must_fail git commit -m "hook must fail"
|
|
|
|
) &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git checkout -- file &&
|
|
|
|
test_cmp expected_hooks actual_hooks
|
2011-06-02 11:26:25 +02:00
|
|
|
'
|
2007-12-08 13:29:47 +01:00
|
|
|
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_require_author_hook () {
|
|
|
|
test_when_finished "rm -f expected_hooks actual_hooks" &&
|
|
|
|
echo check-author >expected_hooks &&
|
|
|
|
test_hook pre-commit <<-\EOF
|
|
|
|
echo check-author >>actual_hooks
|
|
|
|
test "$GIT_AUTHOR_NAME" = "New Author" &&
|
|
|
|
test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-11 11:12:10 +01:00
|
|
|
test_expect_success 'check the author in hook' '
|
2022-03-17 11:13:16 +01:00
|
|
|
setup_require_author_hook &&
|
2019-08-07 20:57:05 +02:00
|
|
|
cat >expected_hooks <<-EOF &&
|
2022-03-17 11:13:16 +01:00
|
|
|
check-author
|
|
|
|
check-author
|
|
|
|
check-author
|
2012-03-11 11:51:32 +01:00
|
|
|
EOF
|
|
|
|
test_must_fail git commit --allow-empty -m "by a.u.thor" &&
|
|
|
|
(
|
|
|
|
GIT_AUTHOR_NAME="New Author" &&
|
|
|
|
GIT_AUTHOR_EMAIL="newauthor@example.com" &&
|
|
|
|
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
|
|
|
|
git commit --allow-empty -m "by new.author via env" &&
|
|
|
|
git show -s
|
|
|
|
) &&
|
|
|
|
git commit --author="New Author <newauthor@example.com>" \
|
|
|
|
--allow-empty -m "by new.author via command line" &&
|
2019-08-07 20:57:05 +02:00
|
|
|
git show -s &&
|
|
|
|
test_cmp expected_hooks actual_hooks
|
2012-03-11 11:51:32 +01:00
|
|
|
'
|
|
|
|
|
2007-12-08 13:29:47 +01:00
|
|
|
test_done
|