Merge branch 'ab/hook-tests-updates'

Update tests around the use of hook scripts.

* ab/hook-tests-updates:
  http tests: use "test_hook" for "smart" and "dumb" http tests
  proc-receive hook tests: use "test_hook" instead of "write_script"
  tests: extend "test_hook" for "rm" and "chmod -x", convert "$HOOK"
  tests: use "test_hook" for misc "mkdir -p" and "chmod" cases
  tests: change "mkdir -p && write_script" to use "test_hook"
  tests: change "cat && chmod +x" to use "test_hook"
  gc + p4 tests: use "test_hook", remove sub-shells
  fetch+push tests: use "test_hook" and "test_when_finished" pattern
  bugreport tests: tighten up "git bugreport -s hooks" test
  tests: assume the hooks are disabled by default
  http tests: don't rely on "hook/post-update.sample"
  hook tests: turn exit code assertions into a loop
  test-lib-functions: add and use a "test_hook" wrapper
This commit is contained in:
Junio C Hamano 2022-03-30 18:01:11 -07:00
commit a5bf611cc4
67 changed files with 573 additions and 604 deletions

View File

@ -12,8 +12,7 @@ then
fi fi
test_expect_success 'setup' ' test_expect_success 'setup' '
mkdir -p "$TRASH_DIRECTORY/.git/hooks" && test_hook --setup pre-commit <<-\EOF
write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF
echo $HOBBES >&2 echo $HOBBES >&2
EOF EOF
' '

View File

@ -60,18 +60,22 @@ test_expect_success 'can create leading directories outside of a git dir' '
test_expect_success 'indicates populated hooks' ' test_expect_success 'indicates populated hooks' '
test_when_finished rm git-bugreport-hooks.txt && test_when_finished rm git-bugreport-hooks.txt &&
test_when_finished rm -fr .git/hooks &&
rm -fr .git/hooks && test_hook applypatch-msg <<-\EOF &&
mkdir .git/hooks && true
for hook in applypatch-msg prepare-commit-msg.sample EOF
do test_hook unknown-hook <<-\EOF &&
write_script ".git/hooks/$hook" <<-EOF || return 1 true
echo "hook $hook exists" EOF
EOF
done &&
git bugreport -s hooks && git bugreport -s hooks &&
grep applypatch-msg git-bugreport-hooks.txt &&
! grep prepare-commit-msg git-bugreport-hooks.txt sort >expect <<-\EOF &&
[Enabled Hooks]
applypatch-msg
EOF
sed -ne "/^\[Enabled Hooks\]$/,/^$/p" <git-bugreport-hooks.txt >actual &&
test_cmp expect actual
' '
test_done test_done

View File

@ -6,11 +6,11 @@ test_description='Test the core.hooksPath configuration variable'
test_expect_success 'set up a pre-commit hook in core.hooksPath' ' test_expect_success 'set up a pre-commit hook in core.hooksPath' '
>actual && >actual &&
mkdir -p .git/custom-hooks .git/hooks && mkdir -p .git/custom-hooks &&
write_script .git/custom-hooks/pre-commit <<-\EOF && write_script .git/custom-hooks/pre-commit <<-\EOF &&
echo CUSTOM >>actual echo CUSTOM >>actual
EOF EOF
write_script .git/hooks/pre-commit <<-\EOF test_hook --setup pre-commit <<-\EOF
echo NORMAL >>actual echo NORMAL >>actual
EOF EOF
' '

View File

@ -8,7 +8,6 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '
mkdir -p .git/hooks &&
test_commit PRE && test_commit PRE &&
PRE_OID=$(git rev-parse PRE) && PRE_OID=$(git rev-parse PRE) &&
test_commit POST && test_commit POST &&
@ -16,9 +15,8 @@ test_expect_success setup '
' '
test_expect_success 'hook allows updating ref if successful' ' test_expect_success 'hook allows updating ref if successful' '
test_when_finished "rm .git/hooks/reference-transaction" &&
git reset --hard PRE && git reset --hard PRE &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
echo "$*" >>actual echo "$*" >>actual
EOF EOF
cat >expect <<-EOF && cat >expect <<-EOF &&
@ -30,9 +28,8 @@ test_expect_success 'hook allows updating ref if successful' '
' '
test_expect_success 'hook aborts updating ref in prepared state' ' test_expect_success 'hook aborts updating ref in prepared state' '
test_when_finished "rm .git/hooks/reference-transaction" &&
git reset --hard PRE && git reset --hard PRE &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
if test "$1" = prepared if test "$1" = prepared
then then
exit 1 exit 1
@ -43,9 +40,9 @@ test_expect_success 'hook aborts updating ref in prepared state' '
' '
test_expect_success 'hook gets all queued updates in prepared state' ' test_expect_success 'hook gets all queued updates in prepared state' '
test_when_finished "rm .git/hooks/reference-transaction actual" && test_when_finished "rm actual" &&
git reset --hard PRE && git reset --hard PRE &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
if test "$1" = prepared if test "$1" = prepared
then then
while read -r line while read -r line
@ -66,9 +63,9 @@ test_expect_success 'hook gets all queued updates in prepared state' '
' '
test_expect_success 'hook gets all queued updates in committed state' ' test_expect_success 'hook gets all queued updates in committed state' '
test_when_finished "rm .git/hooks/reference-transaction actual" && test_when_finished "rm actual" &&
git reset --hard PRE && git reset --hard PRE &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
if test "$1" = committed if test "$1" = committed
then then
while read -r line while read -r line
@ -86,9 +83,9 @@ test_expect_success 'hook gets all queued updates in committed state' '
' '
test_expect_success 'hook gets all queued updates in aborted state' ' test_expect_success 'hook gets all queued updates in aborted state' '
test_when_finished "rm .git/hooks/reference-transaction actual" && test_when_finished "rm actual" &&
git reset --hard PRE && git reset --hard PRE &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
if test "$1" = aborted if test "$1" = aborted
then then
while read -r line while read -r line
@ -115,11 +112,11 @@ test_expect_success 'interleaving hook calls succeed' '
git init --bare target-repo.git && git init --bare target-repo.git &&
write_script target-repo.git/hooks/reference-transaction <<-\EOF && test_hook -C target-repo.git reference-transaction <<-\EOF &&
echo $0 "$@" >>actual echo $0 "$@" >>actual
EOF EOF
write_script target-repo.git/hooks/update <<-\EOF && test_hook -C target-repo.git update <<-\EOF &&
echo $0 "$@" >>actual echo $0 "$@" >>actual
EOF EOF
@ -140,7 +137,7 @@ test_expect_success 'hook does not get called on packing refs' '
# Pack references first such that we are in a known state. # Pack references first such that we are in a known state.
git pack-refs --all && git pack-refs --all &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
echo "$@" >>actual echo "$@" >>actual
cat >>actual cat >>actual
EOF EOF
@ -166,7 +163,7 @@ test_expect_success 'deleting packed ref calls hook once' '
git update-ref refs/heads/to-be-deleted $POST_OID && git update-ref refs/heads/to-be-deleted $POST_OID &&
git pack-refs --all && git pack-refs --all &&
write_script .git/hooks/reference-transaction <<-\EOF && test_hook reference-transaction <<-\EOF &&
echo "$@" >>actual echo "$@" >>actual
cat >>actual cat >>actual
EOF EOF

View File

@ -27,7 +27,7 @@ test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
' '
test_expect_success 'git hook run: basic' ' test_expect_success 'git hook run: basic' '
write_script .git/hooks/test-hook <<-EOF && test_hook test-hook <<-EOF &&
echo Test hook echo Test hook
EOF EOF
@ -39,7 +39,7 @@ test_expect_success 'git hook run: basic' '
' '
test_expect_success 'git hook run: stdout and stderr both write to our stderr' ' test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
write_script .git/hooks/test-hook <<-EOF && test_hook test-hook <<-EOF &&
echo >&1 Will end up on stderr echo >&1 Will end up on stderr
echo >&2 Will end up on stderr echo >&2 Will end up on stderr
EOF EOF
@ -53,38 +53,23 @@ test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
test_must_be_empty stdout.actual test_must_be_empty stdout.actual
' '
test_expect_success 'git hook run: exit codes are passed along' ' for code in 1 2 128 129
write_script .git/hooks/test-hook <<-EOF && do
exit 1 test_expect_success "git hook run: exit code $code is passed along" '
EOF test_hook test-hook <<-EOF &&
exit $code
EOF
test_expect_code 1 git hook run test-hook && test_expect_code $code git hook run test-hook
'
write_script .git/hooks/test-hook <<-EOF && done
exit 2
EOF
test_expect_code 2 git hook run test-hook &&
write_script .git/hooks/test-hook <<-EOF &&
exit 128
EOF
test_expect_code 128 git hook run test-hook &&
write_script .git/hooks/test-hook <<-EOF &&
exit 129
EOF
test_expect_code 129 git hook run test-hook
'
test_expect_success 'git hook run arg u ments without -- is not allowed' ' test_expect_success 'git hook run arg u ments without -- is not allowed' '
test_expect_code 129 git hook run test-hook arg u ments test_expect_code 129 git hook run test-hook arg u ments
' '
test_expect_success 'git hook run -- pass arguments' ' test_expect_success 'git hook run -- pass arguments' '
write_script .git/hooks/test-hook <<-\EOF && test_hook test-hook <<-\EOF &&
echo $1 echo $1
echo $2 echo $2
EOF EOF
@ -99,7 +84,7 @@ test_expect_success 'git hook run -- pass arguments' '
' '
test_expect_success 'git hook run -- out-of-repo runs excluded' ' test_expect_success 'git hook run -- out-of-repo runs excluded' '
write_script .git/hooks/test-hook <<-EOF && test_hook test-hook <<-EOF &&
echo Test hook echo Test hook
EOF EOF
@ -120,6 +105,10 @@ test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
Hook ran four Hook ran four
EOF EOF
test_hook test-hook <<-EOF &&
echo Test hook
EOF
# Test various ways of specifying the path. See also # Test various ways of specifying the path. See also
# t1350-config-hooks-path.sh # t1350-config-hooks-path.sh
>actual && >actual &&

View File

@ -559,10 +559,7 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' '
' '
post_checkout_hook () { post_checkout_hook () {
gitdir=${1:-.git} test_hook -C "$1" post-checkout <<-\EOF
test_when_finished "rm -f $gitdir/hooks/post-checkout" &&
mkdir -p $gitdir/hooks &&
write_script $gitdir/hooks/post-checkout <<-\EOF
{ {
echo $* echo $*
git rev-parse --git-dir --show-toplevel git rev-parse --git-dir --show-toplevel

View File

@ -619,9 +619,7 @@ test_expect_success 'rebase a detached HEAD' '
' '
test_expect_success 'rebase a commit violating pre-commit' ' test_expect_success 'rebase a commit violating pre-commit' '
test_hook pre-commit <<-\EOF &&
mkdir -p .git/hooks &&
write_script .git/hooks/pre-commit <<-\EOF &&
test -z "$(git diff --cached --check)" test -z "$(git diff --cached --check)"
EOF EOF
echo "monde! " >> file1 && echo "monde! " >> file1 &&
@ -636,8 +634,6 @@ test_expect_success 'rebase a commit violating pre-commit' '
' '
test_expect_success 'rebase with a file named HEAD in worktree' ' test_expect_success 'rebase with a file named HEAD in worktree' '
rm -fr .git/hooks &&
git reset --hard && git reset --hard &&
git checkout -b branch3 A && git checkout -b branch3 A &&
@ -1688,10 +1684,8 @@ test_expect_success 'valid author header when author contains single quote' '
' '
test_expect_success 'post-commit hook is called' ' test_expect_success 'post-commit hook is called' '
test_when_finished "rm -f .git/hooks/post-commit" &&
>actual && >actual &&
mkdir -p .git/hooks && test_hook post-commit <<-\EOS &&
write_script .git/hooks/post-commit <<-\EOS &&
git rev-parse HEAD >>actual git rev-parse HEAD >>actual
EOS EOS
( (

View File

@ -31,12 +31,9 @@ test_expect_success 'rebase --root fails with too many args' '
' '
test_expect_success 'setup pre-rebase hook' ' test_expect_success 'setup pre-rebase hook' '
mkdir -p .git/hooks && test_hook --setup pre-rebase <<-\EOF
cat >.git/hooks/pre-rebase <<EOF && echo "$1,$2" >.git/PRE-REBASE-INPUT
#!$SHELL_PATH EOF
echo "\$1,\$2" >.git/PRE-REBASE-INPUT
EOF
chmod +x .git/hooks/pre-rebase
' '
cat > expect <<EOF cat > expect <<EOF
4 4
@ -141,12 +138,9 @@ commit work7~5
EOF EOF
test_expect_success 'setup pre-rebase hook that fails' ' test_expect_success 'setup pre-rebase hook that fails' '
mkdir -p .git/hooks && test_hook --setup --clobber pre-rebase <<-\EOF
cat >.git/hooks/pre-rebase <<EOF && false
#!$SHELL_PATH EOF
false
EOF
chmod +x .git/hooks/pre-rebase
' '
test_expect_success 'pre-rebase hook stops rebase' ' test_expect_success 'pre-rebase hook stops rebase' '

View File

@ -41,12 +41,9 @@ test_expect_success 'rebase -i' '
' '
test_expect_success 'setup pre-rebase hook' ' test_expect_success 'setup pre-rebase hook' '
mkdir -p .git/hooks && test_hook --setup pre-rebase <<-\EOF
cat >.git/hooks/pre-rebase <<EOF && echo "$1,$2" >.git/PRE-REBASE-INPUT
#!$SHELL_PATH EOF
echo "\$1,\$2" >.git/PRE-REBASE-INPUT
EOF
chmod +x .git/hooks/pre-rebase
' '
test_expect_success 'pre-rebase hook gets correct input (1)' ' test_expect_success 'pre-rebase hook gets correct input (1)' '
@ -102,12 +99,9 @@ test_expect_success 'pre-rebase hook gets correct input (6)' '
' '
test_expect_success 'setup pre-rebase hook that fails' ' test_expect_success 'setup pre-rebase hook that fails' '
mkdir -p .git/hooks && test_hook --setup --clobber pre-rebase <<-\EOF
cat >.git/hooks/pre-rebase <<EOF && false
#!$SHELL_PATH EOF
false
EOF
chmod +x .git/hooks/pre-rebase
' '
test_expect_success 'pre-rebase hook stops rebase (1)' ' test_expect_success 'pre-rebase hook stops rebase (1)' '

View File

@ -292,9 +292,9 @@ test_expect_success 'post-rewrite hook and fixups work for merges' '
git commit --fixup HEAD same2.t && git commit --fixup HEAD same2.t &&
fixup="$(git rev-parse HEAD)" && fixup="$(git rev-parse HEAD)" &&
mkdir -p .git/hooks && test_hook post-rewrite <<-\EOF &&
test_when_finished "rm .git/hooks/post-rewrite" && cat >actual
echo "cat >actual" | write_script .git/hooks/post-rewrite && EOF
test_tick && test_tick &&
git rebase -i --autosquash -r HEAD^^^ && git rebase -i --autosquash -r HEAD^^^ &&

View File

@ -315,12 +315,10 @@ test_expect_success 'am --patch-format=hg applies hg patch' '
' '
test_expect_success 'am with applypatch-msg hook' ' test_expect_success 'am with applypatch-msg hook' '
test_when_finished "rm -f .git/hooks/applypatch-msg" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook applypatch-msg <<-\EOF &&
write_script .git/hooks/applypatch-msg <<-\EOF &&
cat "$1" >actual-msg && cat "$1" >actual-msg &&
echo hook-message >"$1" echo hook-message >"$1"
EOF EOF
@ -335,12 +333,10 @@ test_expect_success 'am with applypatch-msg hook' '
' '
test_expect_success 'am with failing applypatch-msg hook' ' test_expect_success 'am with failing applypatch-msg hook' '
test_when_finished "rm -f .git/hooks/applypatch-msg" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook applypatch-msg <<-\EOF &&
write_script .git/hooks/applypatch-msg <<-\EOF &&
exit 1 exit 1
EOF EOF
test_must_fail git am patch1 && test_must_fail git am patch1 &&
@ -350,12 +346,10 @@ test_expect_success 'am with failing applypatch-msg hook' '
' '
test_expect_success 'am with pre-applypatch hook' ' test_expect_success 'am with pre-applypatch hook' '
test_when_finished "rm -f .git/hooks/pre-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook pre-applypatch <<-\EOF &&
write_script .git/hooks/pre-applypatch <<-\EOF &&
git diff first >diff.actual git diff first >diff.actual
exit 0 exit 0
EOF EOF
@ -368,12 +362,10 @@ test_expect_success 'am with pre-applypatch hook' '
' '
test_expect_success 'am with failing pre-applypatch hook' ' test_expect_success 'am with failing pre-applypatch hook' '
test_when_finished "rm -f .git/hooks/pre-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook pre-applypatch <<-\EOF &&
write_script .git/hooks/pre-applypatch <<-\EOF &&
exit 1 exit 1
EOF EOF
test_must_fail git am patch1 && test_must_fail git am patch1 &&
@ -383,12 +375,10 @@ test_expect_success 'am with failing pre-applypatch hook' '
' '
test_expect_success 'am with post-applypatch hook' ' test_expect_success 'am with post-applypatch hook' '
test_when_finished "rm -f .git/hooks/post-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook post-applypatch <<-\EOF &&
write_script .git/hooks/post-applypatch <<-\EOF &&
git rev-parse HEAD >head.actual git rev-parse HEAD >head.actual
git diff second >diff.actual git diff second >diff.actual
exit 0 exit 0
@ -403,12 +393,10 @@ test_expect_success 'am with post-applypatch hook' '
' '
test_expect_success 'am with failing post-applypatch hook' ' test_expect_success 'am with failing post-applypatch hook' '
test_when_finished "rm -f .git/hooks/post-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook post-applypatch <<-\EOF &&
write_script .git/hooks/post-applypatch <<-\EOF &&
git rev-parse HEAD >head.actual git rev-parse HEAD >head.actual
exit 1 exit 1
EOF EOF

View File

@ -20,46 +20,38 @@ test_expect_success setup '
git clone --bare ./. victim.git && git clone --bare ./. victim.git &&
GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 && GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
git update-ref refs/heads/main $commit1 && git update-ref refs/heads/main $commit1 &&
git update-ref refs/heads/tofail $commit0 git update-ref refs/heads/tofail $commit0 &&
test_hook --setup -C victim.git pre-receive <<-\EOF &&
printf %s "$@" >>$GIT_DIR/pre-receive.args
cat - >$GIT_DIR/pre-receive.stdin
echo STDOUT pre-receive
echo STDERR pre-receive >&2
EOF
test_hook --setup -C victim.git update <<-\EOF &&
echo "$@" >>$GIT_DIR/update.args
read x; printf %s "$x" >$GIT_DIR/update.stdin
echo STDOUT update $1
echo STDERR update $1 >&2
test "$1" = refs/heads/main || exit
EOF
test_hook --setup -C victim.git post-receive <<-\EOF &&
printf %s "$@" >>$GIT_DIR/post-receive.args
cat - >$GIT_DIR/post-receive.stdin
echo STDOUT post-receive
echo STDERR post-receive >&2
EOF
test_hook --setup -C victim.git post-update <<-\EOF
echo "$@" >>$GIT_DIR/post-update.args
read x; printf %s "$x" >$GIT_DIR/post-update.stdin
echo STDOUT post-update
echo STDERR post-update >&2
EOF
' '
cat >victim.git/hooks/pre-receive <<'EOF'
#!/bin/sh
printf %s "$@" >>$GIT_DIR/pre-receive.args
cat - >$GIT_DIR/pre-receive.stdin
echo STDOUT pre-receive
echo STDERR pre-receive >&2
EOF
chmod u+x victim.git/hooks/pre-receive
cat >victim.git/hooks/update <<'EOF'
#!/bin/sh
echo "$@" >>$GIT_DIR/update.args
read x; printf %s "$x" >$GIT_DIR/update.stdin
echo STDOUT update $1
echo STDERR update $1 >&2
test "$1" = refs/heads/main || exit
EOF
chmod u+x victim.git/hooks/update
cat >victim.git/hooks/post-receive <<'EOF'
#!/bin/sh
printf %s "$@" >>$GIT_DIR/post-receive.args
cat - >$GIT_DIR/post-receive.stdin
echo STDOUT post-receive
echo STDERR post-receive >&2
EOF
chmod u+x victim.git/hooks/post-receive
cat >victim.git/hooks/post-update <<'EOF'
#!/bin/sh
echo "$@" >>$GIT_DIR/post-update.args
read x; printf %s "$x" >$GIT_DIR/post-update.stdin
echo STDOUT post-update
echo STDERR post-update >&2
EOF
chmod u+x victim.git/hooks/post-update
test_expect_success push ' test_expect_success push '
test_must_fail git send-pack --force ./victim.git \ test_must_fail git send-pack --force ./victim.git \
main tofail >send.out 2>send.err main tofail >send.out 2>send.err
@ -136,7 +128,7 @@ test_expect_success 'send-pack stderr contains hook messages' '
' '
test_expect_success 'pre-receive hook that forgets to read its input' ' test_expect_success 'pre-receive hook that forgets to read its input' '
write_script victim.git/hooks/pre-receive <<-\EOF && test_hook --clobber -C victim.git pre-receive <<-\EOF &&
exit 0 exit 0
EOF EOF
rm -f victim.git/hooks/update victim.git/hooks/post-update && rm -f victim.git/hooks/update victim.git/hooks/post-update &&

View File

@ -25,13 +25,15 @@ test_expect_success setup '
GIT_DIR=clone2/.git git update-index --add a GIT_DIR=clone2/.git git update-index --add a
' '
for clone in 1 2; do test_expect_success 'setup clone hooks' '
cat >clone${clone}/.git/hooks/post-merge <<'EOF' test_when_finished "rm -f hook" &&
#!/bin/sh cat >hook <<-\EOF &&
echo $@ >> $GIT_DIR/post-merge.args echo $@ >>$GIT_DIR/post-merge.args
EOF EOF
chmod u+x clone${clone}/.git/hooks/post-merge
done test_hook --setup -C clone1 post-merge <hook &&
test_hook --setup -C clone2 post-merge <hook
'
test_expect_success 'post-merge does not run for up-to-date ' ' test_expect_success 'post-merge does not run for up-to-date ' '
GIT_DIR=clone1/.git git merge $commit0 && GIT_DIR=clone1/.git git merge $commit0 &&

View File

@ -10,8 +10,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '
mkdir -p .git/hooks && test_hook --setup post-checkout <<-\EOF &&
write_script .git/hooks/post-checkout <<-\EOF &&
echo "$@" >.git/post-checkout.args echo "$@" >.git/post-checkout.args
EOF EOF
test_commit one && test_commit one &&

View File

@ -5,7 +5,7 @@ test_description='remote push rejects are reported by client'
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'setup' ' test_expect_success 'setup' '
write_script .git/hooks/update <<-\EOF && test_hook update <<-\EOF &&
exit 1 exit 1
EOF EOF
echo 1 >file && echo 1 >file &&

View File

@ -17,15 +17,13 @@ test_expect_success 'setup' '
git checkout A^0 && git checkout A^0 &&
test_commit E bar E && test_commit E bar E &&
test_commit F foo F && test_commit F foo F &&
git checkout main git checkout main &&
'
cat >.git/hooks/post-rewrite <<EOF test_hook --setup post-rewrite <<-EOF
#!/bin/sh echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args cat > "$TRASH_DIRECTORY"/post-rewrite.data
cat > "$TRASH_DIRECTORY"/post-rewrite.data EOF
EOF '
chmod u+x .git/hooks/post-rewrite
clear_hook_input () { clear_hook_input () {
rm -f post-rewrite.args post-rewrite.data rm -f post-rewrite.args post-rewrite.data

View File

@ -5,7 +5,7 @@ test_description='remote messages are colorized on the client'
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'setup' ' test_expect_success 'setup' '
write_script .git/hooks/update <<-\EOF && test_hook --setup update <<-\EOF &&
echo error: error echo error: error
echo ERROR: also highlighted echo ERROR: also highlighted
echo hint: hint echo hint: hint

View File

@ -36,7 +36,7 @@ setup_upstream_and_workbench () {
TAG=$(git -C workbench rev-parse v123) && TAG=$(git -C workbench rev-parse v123) &&
# setup pre-receive hook # setup pre-receive hook
write_script upstream.git/hooks/pre-receive <<-\EOF && test_hook --setup -C upstream.git pre-receive <<-\EOF &&
exec >&2 exec >&2
echo "# pre-receive hook" echo "# pre-receive hook"
while read old new ref while read old new ref
@ -46,7 +46,7 @@ setup_upstream_and_workbench () {
EOF EOF
# setup post-receive hook # setup post-receive hook
write_script upstream.git/hooks/post-receive <<-\EOF && test_hook --setup -C upstream.git post-receive <<-\EOF &&
exec >&2 exec >&2
echo "# post-receive hook" echo "# post-receive hook"
while read old new ref while read old new ref

View File

@ -3,7 +3,7 @@ test_expect_success "setup receive.procReceiveRefs" '
' '
test_expect_success "setup proc-receive hook" ' test_expect_success "setup proc-receive hook" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic1" \ -r "ok refs/for/main/topic1" \

View File

@ -1,6 +1,6 @@
test_expect_success "setup pre-receive hook ($PROTOCOL)" ' test_expect_success "setup pre-receive hook ($PROTOCOL)" '
mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" && mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" &&
write_script "$upstream/hooks/pre-receive" <<-EOF test_hook -C "$upstream" --clobber pre-receive <<-\EOF
exit 1 exit 1
EOF EOF
' '
@ -21,7 +21,7 @@ test_expect_success "git-push is declined ($PROTOCOL)" '
EOF EOF
test_cmp expect actual && test_cmp expect actual &&
test_cmp_refs -C "$upstream" <<-EOF test_cmp_refs -C "$upstream" <<-\EOF
<COMMIT-A> refs/heads/main <COMMIT-A> refs/heads/main
EOF EOF
' '

View File

@ -1,6 +1,6 @@
test_expect_success "setup pre-receive hook ($PROTOCOL/porcelain)" ' test_expect_success "setup pre-receive hook ($PROTOCOL/porcelain)" '
mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" && mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" &&
write_script "$upstream/hooks/pre-receive" <<-EOF test_hook -C "$upstream" --clobber pre-receive <<-\EOF
exit 1 exit 1
EOF EOF
' '

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --version 2 test-tool proc-receive -v --version 2
EOF EOF
@ -40,7 +40,7 @@ test_expect_success "proc-receive: bad protocol (unknown version, $PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-version test-tool proc-receive -v --die-read-version
EOF EOF
@ -65,13 +65,13 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-version, $PROTO
grep "remote: fatal: die with the --die-read-version option" out-$test_count && grep "remote: fatal: die with the --die-read-version option" out-$test_count &&
grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count && grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count &&
test_cmp_refs -C "$upstream" <<-EOF test_cmp_refs -C "$upstream" <<-\EOF
<COMMIT-A> refs/heads/main <COMMIT-A> refs/heads/main
EOF EOF
' '
test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-write-version test-tool proc-receive -v --die-write-version
EOF EOF
@ -102,7 +102,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-write-version, $PROT
' '
test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-commands test-tool proc-receive -v --die-read-commands
EOF EOF
@ -132,7 +132,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-commands, $PROT
' '
test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-push-options test-tool proc-receive -v --die-read-push-options
EOF EOF
@ -164,7 +164,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-push-options, $
' '
test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-write-report test-tool proc-receive -v --die-write-report
EOF EOF
@ -194,7 +194,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-write-report, $PROTO
' '
test_expect_success "setup proc-receive hook (no report, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (no report, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v test-tool proc-receive -v
EOF EOF
@ -236,7 +236,7 @@ test_expect_success "cleanup ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (no ref, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (no ref, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok" -r "ok"
@ -269,7 +269,7 @@ test_expect_success "proc-receive: bad protocol (no ref, $PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "xx refs/for/main/topic" -r "xx refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --version 2 test-tool proc-receive -v --version 2
EOF EOF
@ -40,7 +40,7 @@ test_expect_success "proc-receive: bad protocol (unknown version, $PROTOCOL/porc
' '
test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-version test-tool proc-receive -v --die-read-version
EOF EOF
@ -71,7 +71,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-version, $PROTO
' '
test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-write-version test-tool proc-receive -v --die-write-version
EOF EOF
@ -102,7 +102,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-write-version, $PROT
' '
test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-commands test-tool proc-receive -v --die-read-commands
EOF EOF
@ -132,7 +132,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-commands, $PROT
' '
test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-read-push-options test-tool proc-receive -v --die-read-push-options
EOF EOF
@ -164,7 +164,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-read-push-options, $
' '
test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v --die-write-report test-tool proc-receive -v --die-write-report
EOF EOF
@ -194,7 +194,7 @@ test_expect_success "proc-receive: bad protocol (hook --die-write-report, $PROTO
' '
test_expect_success "setup proc-receive hook (no report, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (no report, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v test-tool proc-receive -v
EOF EOF
@ -236,7 +236,7 @@ test_expect_success "cleanup ($PROTOCOL/porcelain)" '
' '
test_expect_success "setup proc-receive hook (no ref, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (no ref, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok" -r "ok"
@ -270,7 +270,7 @@ test_expect_success "proc-receive: bad protocol (no ref, $PROTOCOL/porcelain)" '
' '
test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "xx refs/for/main/topic" -r "xx refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ng refs/for/main/topic" -r "ng refs/for/main/topic"
@ -31,7 +31,7 @@ test_expect_success "proc-receive: fail to update (ng, no message, $PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (ng message, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (ng message, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ng refs/for/main/topic error msg" -r "ng refs/for/main/topic error msg"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ng refs/for/main/topic" -r "ng refs/for/main/topic"
@ -32,7 +32,7 @@ test_expect_success "proc-receive: fail to update (ng, no message, $PROTOCOL/por
' '
test_expect_success "setup proc-receive hook (ng message, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (ng message, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ng refs/for/main/topic error msg" -r "ng refs/for/main/topic error msg"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" -r "ok refs/heads/main"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" -r "ok refs/heads/main"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,6 +1,6 @@
test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL)" ' test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL)" '
git -C "$upstream" config receive.advertisePushOptions false && git -C "$upstream" config receive.advertisePushOptions false &&
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"
@ -31,7 +31,7 @@ test_expect_success "enable push options ($PROTOCOL)" '
' '
test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL)" ' test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
--version 0 \ --version 0 \
@ -75,7 +75,7 @@ test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL)
' '
test_expect_success "restore proc-receive hook ($PROTOCOL)" ' test_expect_success "restore proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,6 +1,6 @@
test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL/porcelain)" '
git -C "$upstream" config receive.advertisePushOptions false && git -C "$upstream" config receive.advertisePushOptions false &&
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"
@ -32,7 +32,7 @@ test_expect_success "enable push options ($PROTOCOL/porcelain)" '
' '
test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL/porcelain)" ' test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
--version 0 \ --version 0 \
@ -78,7 +78,7 @@ test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL/
' '
test_expect_success "restore proc-receive hook ($PROTOCOL/porcelain)" ' test_expect_success "restore proc-receive hook ($PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (ok, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (ok, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (ok, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (ok, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" -r "ok refs/for/main/topic"

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "option refname refs/pull/123/head" \ -r "option refname refs/pull/123/head" \
@ -30,7 +30,7 @@ test_expect_success "proc-receive: report option without matching ok ($PROTOCOL)
' '
test_expect_success "setup proc-receive hook (option refname, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option refname, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -62,7 +62,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -95,7 +95,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
' '
test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -129,7 +129,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL)
' '
test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -161,7 +161,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -195,7 +195,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL)
' '
test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/a/b/c/topic" \ -r "ok refs/for/a/b/c/topic" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "option refname refs/pull/123/head" \ -r "option refname refs/pull/123/head" \
@ -31,7 +31,7 @@ test_expect_success "proc-receive: report option without matching ok ($PROTOCOL/
' '
test_expect_success "setup proc-receive hook (option refname, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option refname, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -64,7 +64,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL/porcelain)"
' '
test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -99,7 +99,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
' '
test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -134,7 +134,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL/
' '
test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -167,7 +167,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL/porcelain)"
' '
test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -202,7 +202,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL/
' '
test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/a/b/c/topic" \ -r "ok refs/for/a/b/c/topic" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (ft, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (ft, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (fall-through, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (fall-through, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-\EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \

View File

@ -14,7 +14,7 @@ test_expect_success "setup git config for remote-tracking of special refs" '
' '
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -87,7 +87,7 @@ test_expect_success "proc-receive: check remote-tracking #1 ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -162,7 +162,7 @@ test_expect_success "proc-receive: check remote-tracking #2 ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -58,7 +58,7 @@ test_expect_success "proc-receive: multiple rewrite for one ref, no refname for
' '
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \
@ -119,7 +119,7 @@ test_expect_success "proc-receive: multiple rewrites for one ref, no refname for
' '
test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/main/topic" \ -r "ok refs/for/main/topic" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook ($PROTOCOL)" ' test_expect_success "setup proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/next/topic2" \ -r "ok refs/for/next/topic2" \

View File

@ -1,5 +1,5 @@
test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/for/next/topic2" \ -r "ok refs/for/next/topic2" \

View File

@ -17,7 +17,7 @@ test_expect_success "setup upstream branches ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook ($PROTOCOL)" ' test_expect_success "setup proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" \ -r "ok refs/heads/main" \

View File

@ -17,7 +17,7 @@ test_expect_success "setup upstream branches ($PROTOCOL/porcelain)" '
' '
test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" ' test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" \ -r "ok refs/heads/main" \

View File

@ -9,7 +9,7 @@ test_expect_success "config receive.procReceiveRefs with modifiers ($PROTOCOL)"
' '
test_expect_success "setup proc-receive hook ($PROTOCOL)" ' test_expect_success "setup proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" \ -r "ok refs/heads/main" \
@ -70,7 +70,7 @@ test_expect_success "setup upstream: create tags/v123 ($PROTOCOL)" '
' '
test_expect_success "setup proc-receive hook ($PROTOCOL)" ' test_expect_success "setup proc-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/proc-receive" <<-EOF test_hook -C "$upstream" --clobber proc-receive <<-EOF
printf >&2 "# proc-receive hook\n" printf >&2 "# proc-receive hook\n"
test-tool proc-receive -v \ test-tool proc-receive -v \
-r "ok refs/heads/main" \ -r "ok refs/heads/main" \

View File

@ -169,7 +169,7 @@ test_expect_success 'atomic fetch with failing backfill' '
# one of both fails to update correctly. # one of both fails to update correctly.
# #
# To trigger failure we simply abort when backfilling a tag. # To trigger failure we simply abort when backfilling a tag.
write_script clone3/.git/hooks/reference-transaction <<-\EOF && test_hook -C clone3 reference-transaction <<-\EOF &&
while read oldrev newrev reference while read oldrev newrev reference
do do
if test "$reference" = refs/tags/tag1 if test "$reference" = refs/tags/tag1
@ -201,7 +201,7 @@ test_expect_success 'atomic fetch with backfill should use single transaction' '
$ZERO_OID $T refs/tags/tag1 $ZERO_OID $T refs/tags/tag1
EOF EOF
write_script clone4/.git/hooks/reference-transaction <<-\EOF && test_hook -C clone4 reference-transaction <<-\EOF &&
( echo "$*" && cat ) >>actual ( echo "$*" && cat ) >>actual
EOF EOF

View File

@ -273,7 +273,7 @@ test_expect_success 'fetch --atomic executes a single reference transaction only
EOF EOF
rm -f atomic/actual && rm -f atomic/actual &&
write_script atomic/.git/hooks/reference-transaction <<-\EOF && test_hook -C atomic reference-transaction <<-\EOF &&
( echo "$*" && cat ) >>actual ( echo "$*" && cat ) >>actual
EOF EOF
@ -306,7 +306,7 @@ test_expect_success 'fetch --atomic aborts all reference updates if hook aborts'
EOF EOF
rm -f atomic/actual && rm -f atomic/actual &&
write_script atomic/.git/hooks/reference-transaction <<-\EOF && test_hook -C atomic/.git reference-transaction <<-\EOF &&
( echo "$*" && cat ) >>actual ( echo "$*" && cat ) >>actual
exit 1 exit 1
EOF EOF
@ -334,7 +334,7 @@ test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
test_line_count = 2 atomic/.git/FETCH_HEAD && test_line_count = 2 atomic/.git/FETCH_HEAD &&
cp atomic/.git/FETCH_HEAD expected && cp atomic/.git/FETCH_HEAD expected &&
write_script atomic/.git/hooks/reference-transaction <<-\EOF && test_hook -C atomic reference-transaction <<-\EOF &&
exit 1 exit 1
EOF EOF
@ -364,7 +364,7 @@ test_expect_success 'fetch --atomic --prune executes a single reference transact
$ZERO_OID $head_oid refs/remotes/origin/new-branch $ZERO_OID $head_oid refs/remotes/origin/new-branch
EOF EOF
write_script atomic/.git/hooks/reference-transaction <<-\EOF && test_hook -C atomic reference-transaction <<-\EOF &&
( echo "$*" && cat ) >>actual ( echo "$*" && cat ) >>actual
EOF EOF

View File

@ -23,14 +23,10 @@ D=$(pwd)
mk_empty () { mk_empty () {
repo_name="$1" repo_name="$1"
rm -fr "$repo_name" && test_when_finished "rm -rf \"$repo_name\"" &&
mkdir "$repo_name" && test_path_is_missing "$repo_name" &&
( git init "$repo_name" &&
cd "$repo_name" && git -C "$repo_name" config receive.denyCurrentBranch warn
git init &&
git config receive.denyCurrentBranch warn &&
mv .git/hooks .git/hooks-disabled
)
} }
mk_test () { mk_test () {
@ -59,40 +55,28 @@ mk_test () {
mk_test_with_hooks() { mk_test_with_hooks() {
repo_name=$1 repo_name=$1
mk_test "$@" && mk_test "$@" &&
( test_hook -C "$repo_name" pre-receive <<-'EOF' &&
cd "$repo_name" && cat - >>pre-receive.actual
mkdir .git/hooks && EOF
cd .git/hooks &&
cat >pre-receive <<-'EOF' && test_hook -C "$repo_name" update <<-'EOF' &&
#!/bin/sh printf "%s %s %s\n" "$@" >>update.actual
cat - >>pre-receive.actual EOF
EOF
cat >update <<-'EOF' && test_hook -C "$repo_name" post-receive <<-'EOF' &&
#!/bin/sh cat - >>post-receive.actual
printf "%s %s %s\n" "$@" >>update.actual EOF
EOF
cat >post-receive <<-'EOF' && test_hook -C "$repo_name" post-update <<-'EOF'
#!/bin/sh for ref in "$@"
cat - >>post-receive.actual do
EOF printf "%s\n" "$ref" >>post-update.actual
done
cat >post-update <<-'EOF' && EOF
#!/bin/sh
for ref in "$@"
do
printf "%s\n" "$ref" >>post-update.actual
done
EOF
chmod +x pre-receive update post-receive post-update
)
} }
mk_child() { mk_child() {
rm -rf "$2" && test_when_finished "rm -rf \"$2\"" &&
git clone "$1" "$2" git clone "$1" "$2"
} }
@ -197,32 +181,32 @@ grep_wrote () {
grep 'write_pack_file/wrote.*"value":"'$1'"' $2 grep 'write_pack_file/wrote.*"value":"'$1'"' $2
} }
test_expect_success 'push with negotiation' ' test_expect_success 'push without negotiation' '
# Without negotiation
mk_empty testrepo && mk_empty testrepo &&
git push testrepo $the_first_commit:refs/remotes/origin/first_commit && git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
test_commit -C testrepo unrelated_commit && test_commit -C testrepo unrelated_commit &&
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit && git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
echo now pushing without negotiation && test_when_finished "rm event" &&
GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main && GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
grep_wrote 5 event && # 2 commits, 2 trees, 1 blob grep_wrote 5 event # 2 commits, 2 trees, 1 blob
'
# Same commands, but with negotiation test_expect_success 'push with negotiation' '
rm event &&
mk_empty testrepo && mk_empty testrepo &&
git push testrepo $the_first_commit:refs/remotes/origin/first_commit && git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
test_commit -C testrepo unrelated_commit && test_commit -C testrepo unrelated_commit &&
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit && git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
test_when_finished "rm event" &&
GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main && GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
grep_wrote 2 event # 1 commit, 1 tree grep_wrote 2 event # 1 commit, 1 tree
' '
test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' ' test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
rm event &&
mk_empty testrepo && mk_empty testrepo &&
git push testrepo $the_first_commit:refs/remotes/origin/first_commit && git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
test_commit -C testrepo unrelated_commit && test_commit -C testrepo unrelated_commit &&
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit && git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
test_when_finished "rm event" &&
GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \ GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err && git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
grep_wrote 5 event && # 2 commits, 2 trees, 1 blob grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
@ -668,7 +652,6 @@ test_expect_success 'push does not update local refs on failure' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
mk_child testrepo child && mk_child testrepo child &&
mkdir testrepo/.git/hooks &&
echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive && echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
chmod +x testrepo/.git/hooks/pre-receive && chmod +x testrepo/.git/hooks/pre-receive &&
( (
@ -1330,7 +1313,7 @@ done
test_expect_success 'fetch follows tags by default' ' test_expect_success 'fetch follows tags by default' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
( (
cd src && cd src &&
@ -1340,6 +1323,7 @@ test_expect_success 'fetch follows tags by default' '
sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 | sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
sort -k 3 >../expect sort -k 3 >../expect
) && ) &&
test_when_finished "rm -rf dst" &&
git init dst && git init dst &&
( (
cd dst && cd dst &&
@ -1365,8 +1349,9 @@ test_expect_success 'peeled advertisements are not considered ref tips' '
test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' ' test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
test_when_finished "rm -rf dst" &&
git init --bare dst && git init --bare dst &&
( (
cd src && cd src &&
@ -1389,8 +1374,9 @@ test_expect_success 'pushing a specific ref applies remote.$name.push as refmap'
test_expect_success 'with no remote.$name.push, it is not used as refmap' ' test_expect_success 'with no remote.$name.push, it is not used as refmap' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
test_when_finished "rm -rf dst" &&
git init --bare dst && git init --bare dst &&
( (
cd src && cd src &&
@ -1411,8 +1397,9 @@ test_expect_success 'with no remote.$name.push, it is not used as refmap' '
test_expect_success 'with no remote.$name.push, upstream mapping is used' ' test_expect_success 'with no remote.$name.push, upstream mapping is used' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
test_when_finished "rm -rf dst" &&
git init --bare dst && git init --bare dst &&
( (
cd src && cd src &&
@ -1440,8 +1427,9 @@ test_expect_success 'with no remote.$name.push, upstream mapping is used' '
test_expect_success 'push does not follow tags by default' ' test_expect_success 'push does not follow tags by default' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
test_when_finished "rm -rf dst" &&
git init --bare dst && git init --bare dst &&
( (
cd src && cd src &&
@ -1463,8 +1451,9 @@ test_expect_success 'push does not follow tags by default' '
test_expect_success 'push --follow-tags only pushes relevant tags' ' test_expect_success 'push --follow-tags only pushes relevant tags' '
mk_test testrepo heads/main && mk_test testrepo heads/main &&
rm -fr src dst && test_when_finished "rm -rf src" &&
git init src && git init src &&
test_when_finished "rm -rf dst" &&
git init --bare dst && git init --bare dst &&
( (
cd src && cd src &&
@ -1502,9 +1491,9 @@ EOF
' '
test_expect_success 'pushing a tag pushes the tagged object' ' test_expect_success 'pushing a tag pushes the tagged object' '
rm -rf dst.git &&
blob=$(echo unreferenced | git hash-object -w --stdin) && blob=$(echo unreferenced | git hash-object -w --stdin) &&
git tag -m foo tag-of-blob $blob && git tag -m foo tag-of-blob $blob &&
test_when_finished "rm -rf dst.git" &&
git init --bare dst.git && git init --bare dst.git &&
git push dst.git tag-of-blob && git push dst.git tag-of-blob &&
# the receiving index-pack should have noticed # the receiving index-pack should have noticed
@ -1515,7 +1504,7 @@ test_expect_success 'pushing a tag pushes the tagged object' '
' '
test_expect_success 'push into bare respects core.logallrefupdates' ' test_expect_success 'push into bare respects core.logallrefupdates' '
rm -rf dst.git && test_when_finished "rm -rf dst.git" &&
git init --bare dst.git && git init --bare dst.git &&
git -C dst.git config core.logallrefupdates true && git -C dst.git config core.logallrefupdates true &&
@ -1533,7 +1522,7 @@ test_expect_success 'push into bare respects core.logallrefupdates' '
' '
test_expect_success 'fetch into bare respects core.logallrefupdates' ' test_expect_success 'fetch into bare respects core.logallrefupdates' '
rm -rf dst.git && test_when_finished "rm -rf dst.git" &&
git init --bare dst.git && git init --bare dst.git &&
( (
cd dst.git && cd dst.git &&
@ -1554,6 +1543,7 @@ test_expect_success 'fetch into bare respects core.logallrefupdates' '
' '
test_expect_success 'receive.denyCurrentBranch = updateInstead' ' test_expect_success 'receive.denyCurrentBranch = updateInstead' '
mk_empty testrepo &&
git push testrepo main && git push testrepo main &&
( (
cd testrepo && cd testrepo &&
@ -1656,7 +1646,7 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
) && ) &&
# (5) push into void # (5) push into void
rm -fr void && test_when_finished "rm -rf void" &&
git init void && git init void &&
( (
cd void && cd void &&
@ -1678,26 +1668,23 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
' '
test_expect_success 'updateInstead with push-to-checkout hook' ' test_expect_success 'updateInstead with push-to-checkout hook' '
rm -fr testrepo && test_when_finished "rm -rf testrepo" &&
git init testrepo && git init testrepo &&
( git -C testrepo pull .. main &&
cd testrepo && git -C testrepo reset --hard HEAD^^ &&
git pull .. main && git -C testrepo tag initial &&
git reset --hard HEAD^^ && git -C testrepo config receive.denyCurrentBranch updateInstead &&
git tag initial && test_hook -C testrepo push-to-checkout <<-\EOF &&
git config receive.denyCurrentBranch updateInstead && echo >&2 updating from $(git rev-parse HEAD)
write_script .git/hooks/push-to-checkout <<-\EOF echo >&2 updating to "$1"
echo >&2 updating from $(git rev-parse HEAD)
echo >&2 updating to "$1"
git update-index -q --refresh && git update-index -q --refresh &&
git read-tree -u -m HEAD "$1" || { git read-tree -u -m HEAD "$1" || {
status=$? status=$?
echo >&2 read-tree failed echo >&2 read-tree failed
exit $status exit $status
} }
EOF EOF
) &&
# Try pushing into a pristine # Try pushing into a pristine
git push testrepo main && git push testrepo main &&
@ -1740,35 +1727,32 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
) && ) &&
# push into void # push into void
rm -fr void && test_when_finished "rm -rf void" &&
git init void && git init void &&
( git -C void config receive.denyCurrentBranch updateInstead &&
cd void && test_hook -C void push-to-checkout <<-\EOF &&
git config receive.denyCurrentBranch updateInstead && if git rev-parse --quiet --verify HEAD
write_script .git/hooks/push-to-checkout <<-\EOF then
if git rev-parse --quiet --verify HEAD has_head=yes
then echo >&2 updating from $(git rev-parse HEAD)
has_head=yes else
echo >&2 updating from $(git rev-parse HEAD) has_head=no
else echo >&2 pushing into void
has_head=no fi
echo >&2 pushing into void echo >&2 updating to "$1"
fi
echo >&2 updating to "$1"
git update-index -q --refresh && git update-index -q --refresh &&
case "$has_head" in case "$has_head" in
yes) yes)
git read-tree -u -m HEAD "$1" ;; git read-tree -u -m HEAD "$1" ;;
no) no)
git read-tree -u -m "$1" ;; git read-tree -u -m "$1" ;;
esac || { esac || {
status=$? status=$?
echo >&2 read-tree failed echo >&2 read-tree failed
exit $status exit $status
} }
EOF EOF
) &&
git push void main && git push void main &&
( (

View File

@ -233,7 +233,7 @@ test_expect_success 'git pull --no-verify flag passed to merge' '
git init src && git init src &&
test_commit -C src one && test_commit -C src one &&
git clone src dst && git clone src dst &&
write_script dst/.git/hooks/commit-msg <<-\EOF && test_hook -C dst commit-msg <<-\EOF &&
false false
EOF EOF
test_commit -C src two && test_commit -C src two &&
@ -245,7 +245,7 @@ test_expect_success 'git pull --no-verify --verify passed to merge' '
git init src && git init src &&
test_commit -C src one && test_commit -C src one &&
git clone src dst && git clone src dst &&
write_script dst/.git/hooks/commit-msg <<-\EOF && test_hook -C dst commit-msg <<-\EOF &&
false false
EOF EOF
test_commit -C src two && test_commit -C src two &&

View File

@ -35,8 +35,7 @@ test_expect_success setup '
test_expect_success 'unsigned push does not send push certificate' ' test_expect_success 'unsigned push does not send push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -52,8 +51,7 @@ test_expect_success 'unsigned push does not send push certificate' '
test_expect_success 'talking with a receiver without push certificate support' ' test_expect_success 'talking with a receiver without push certificate support' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -69,22 +67,19 @@ test_expect_success 'talking with a receiver without push certificate support' '
test_expect_success 'push --signed fails with a receiver without push certificate support' ' test_expect_success 'push --signed fails with a receiver without push certificate support' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
test_must_fail git push --signed dst noop ff +noff 2>err && test_must_fail git push --signed dst noop ff +noff 2>err &&
test_i18ngrep "the receiving end does not support" err test_i18ngrep "the receiving end does not support" err
' '
test_expect_success 'push --signed=1 is accepted' ' test_expect_success 'push --signed=1 is accepted' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
test_must_fail git push --signed=1 dst noop ff +noff 2>err && test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
test_i18ngrep "the receiving end does not support" err test_i18ngrep "the receiving end does not support" err
' '
test_expect_success GPG 'no certificate for a signed push with no update' ' test_expect_success GPG 'no certificate for a signed push with no update' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
if test -n "${GIT_PUSH_CERT-}" if test -n "${GIT_PUSH_CERT-}"
then then
git cat-file blob $GIT_PUSH_CERT >../push-cert git cat-file blob $GIT_PUSH_CERT >../push-cert
@ -96,9 +91,8 @@ test_expect_success GPG 'no certificate for a signed push with no update' '
test_expect_success GPG 'signed push sends push certificate' ' test_expect_success GPG 'signed push sends push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && git -C dst config receive.certnonceseed sekrit &&
write_script dst/.git/hooks/post-receive <<-\EOF && test_hook -C dst post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -139,10 +133,9 @@ test_expect_success GPG 'signed push sends push certificate' '
test_expect_success GPGSSH 'ssh signed push sends push certificate' ' test_expect_success GPGSSH 'ssh signed push sends push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" && git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
git -C dst config receive.certnonceseed sekrit && git -C dst config receive.certnonceseed sekrit &&
write_script dst/.git/hooks/post-receive <<-\EOF && test_hook -C dst post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -223,9 +216,8 @@ test_expect_success GPG 'inconsistent push options in signed push not allowed' '
test_expect_success GPG 'fail without key and heed user.signingkey' ' test_expect_success GPG 'fail without key and heed user.signingkey' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && git -C dst config receive.certnonceseed sekrit &&
write_script dst/.git/hooks/post-receive <<-\EOF && test_hook -C dst post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -273,9 +265,8 @@ test_expect_success GPG 'fail without key and heed user.signingkey' '
test_expect_success GPGSM 'fail without key and heed user.signingkey x509' ' test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
test_config gpg.format x509 && test_config gpg.format x509 &&
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && git -C dst config receive.certnonceseed sekrit &&
write_script dst/.git/hooks/post-receive <<-\EOF && test_hook -C dst post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -326,10 +317,9 @@ test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
test_expect_success GPGSSH 'fail without key and heed user.signingkey ssh' ' test_expect_success GPGSSH 'fail without key and heed user.signingkey ssh' '
test_config gpg.format ssh && test_config gpg.format ssh &&
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" && git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
git -C dst config receive.certnonceseed sekrit && git -C dst config receive.certnonceseed sekrit &&
write_script dst/.git/hooks/post-receive <<-\EOF && test_hook -C dst post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate

View File

@ -42,7 +42,9 @@ test_expect_success 'setup remote repository' '
git clone --bare test_repo test_repo.git && git clone --bare test_repo test_repo.git &&
cd test_repo.git && cd test_repo.git &&
git --bare update-server-info && git --bare update-server-info &&
mv hooks/post-update.sample hooks/post-update && test_hook --setup post-update <<-\EOF &&
exec git update-server-info
EOF
ORIG_HEAD=$(git rev-parse --verify HEAD) && ORIG_HEAD=$(git rev-parse --verify HEAD) &&
cd - && cd - &&
mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH" mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"

View File

@ -96,18 +96,18 @@ test_expect_success 'create and delete remote branch' '
test_must_fail git show-ref --verify refs/remotes/origin/dev test_must_fail git show-ref --verify refs/remotes/origin/dev
' '
cat >"$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update" <<EOF test_expect_success 'setup rejected update hook' '
#!/bin/sh test_hook --setup -C "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" update <<-\EOF &&
exit 1 exit 1
EOF EOF
chmod a+x "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
cat >exp <<EOF cat >exp <<-EOF
remote: error: hook declined to update refs/heads/dev2 remote: error: hook declined to update refs/heads/dev2
To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
! [remote rejected] dev2 -> dev2 (hook declined) ! [remote rejected] dev2 -> dev2 (hook declined)
error: failed to push some refs to 'http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git' error: failed to push some refs to '\''http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'\''
EOF EOF
'
test_expect_success 'rejected update prints status' ' test_expect_success 'rejected update prints status' '
cd "$ROOT_PATH"/test_repo_clone && cd "$ROOT_PATH"/test_repo_clone &&
@ -419,10 +419,7 @@ test_expect_success CMDLINE_LIMIT 'push 2000 tags over http' '
' '
test_expect_success GPG 'push with post-receive to inspect certificate' ' test_expect_success GPG 'push with post-receive to inspect certificate' '
( test_hook -C "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git post-receive <<-\EOF &&
cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
mkdir -p hooks &&
write_script hooks/post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate
@ -437,8 +434,9 @@ test_expect_success GPG 'push with post-receive to inspect certificate' '
NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus} NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
NONCE=${GIT_PUSH_CERT_NONCE-nononce} NONCE=${GIT_PUSH_CERT_NONCE-nononce}
E_O_F E_O_F
EOF EOF
(
cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
git config receive.certnonceseed sekrit && git config receive.certnonceseed sekrit &&
git config receive.certnonceslop 30 git config receive.certnonceslop 30
) && ) &&

View File

@ -162,16 +162,10 @@ test_expect_success 'atomic push obeys update hook preventing a branch to be pus
test_commit two && test_commit two &&
git push --mirror up git push --mirror up
) && ) &&
( test_hook -C upstream update <<-\EOF &&
cd upstream && # only allow update to main from now on
HOOKDIR="$(git rev-parse --git-dir)/hooks" && test "$1" = "refs/heads/main"
HOOK="$HOOKDIR/update" && EOF
mkdir -p "$HOOKDIR" &&
write_script "$HOOK" <<-\EOF
# only allow update to main from now on
test "$1" = "refs/heads/main"
EOF
) &&
( (
cd workbench && cd workbench &&
git checkout main && git checkout main &&

View File

@ -5,7 +5,7 @@ test_description='check quarantine of objects during push'
test_expect_success 'create picky dest repo' ' test_expect_success 'create picky dest repo' '
git init --bare dest.git && git init --bare dest.git &&
write_script dest.git/hooks/pre-receive <<-\EOF test_hook --setup -C dest.git pre-receive <<-\EOF
while read old new ref; do while read old new ref; do
test "$(git log -1 --format=%s $new)" = reject && exit 1 test "$(git log -1 --format=%s $new)" = reject && exit 1
done done
@ -60,7 +60,7 @@ test_expect_success 'push to repo path with path separator (colon)' '
test_expect_success 'updating a ref from quarantine is forbidden' ' test_expect_success 'updating a ref from quarantine is forbidden' '
git init --bare update.git && git init --bare update.git &&
write_script update.git/hooks/pre-receive <<-\EOF && test_hook -C update.git pre-receive <<-\EOF &&
read old new refname read old new refname
git update-ref refs/heads/unrelated $new git update-ref refs/heads/unrelated $new
exit 1 exit 1

View File

@ -168,7 +168,7 @@ run_git_push_porcelain_output_test() {
' '
test_expect_success "prepare pre-receive hook ($PROTOCOL)" ' test_expect_success "prepare pre-receive hook ($PROTOCOL)" '
write_script "$upstream/hooks/pre-receive" <<-EOF test_hook --setup -C "$upstream" pre-receive <<-EOF
exit 1 exit 1
EOF EOF
' '

View File

@ -25,16 +25,17 @@ test_expect_success 'setup repository' '
git commit -m two git commit -m two
' '
setup_post_update_server_info_hook () {
test_hook --setup -C "$1" post-update <<-\EOF &&
exec git update-server-info
EOF
git -C "$1" update-server-info
}
test_expect_success 'create http-accessible bare repository with loose objects' ' test_expect_success 'create http-accessible bare repository with loose objects' '
cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
(cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config core.bare true &&
git config core.bare true && setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
mkdir -p hooks &&
write_script "hooks/post-update" <<-\EOF &&
exec git update-server-info
EOF
hooks/post-update
) &&
git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" && git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
git push public main:main git push public main:main
' '
@ -62,13 +63,7 @@ test_expect_success 'create password-protected repository' '
test_expect_success 'create empty remote repository' ' test_expect_success 'create empty remote repository' '
git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" && git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
(cd "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" && setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
mkdir -p hooks &&
write_script "hooks/post-update" <<-\EOF &&
exec git update-server-info
EOF
hooks/post-update
)
' '
test_expect_success 'empty dumb HTTP repository has default hash algorithm' ' test_expect_success 'empty dumb HTTP repository has default hash algorithm' '

View File

@ -6,16 +6,11 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh . ./test-lib.sh
# Setup hook that always succeeds
HOOKDIR="$(git rev-parse --git-dir)/hooks"
HOOK="$HOOKDIR/pre-push"
mkdir -p "$HOOKDIR"
write_script "$HOOK" <<EOF
cat >actual
exit 0
EOF
test_expect_success 'setup' ' test_expect_success 'setup' '
test_hook pre-push <<-\EOF &&
cat >actual
EOF
git config push.default upstream && git config push.default upstream &&
git init --bare repo1 && git init --bare repo1 &&
git remote add parent1 repo1 && git remote add parent1 repo1 &&
@ -28,15 +23,16 @@ test_expect_success 'setup' '
git push parent1 HEAD:foreign && git push parent1 HEAD:foreign &&
test_cmp expect actual test_cmp expect actual
' '
write_script "$HOOK" <<EOF
cat >actual
exit 1
EOF
COMMIT1="$(git rev-parse HEAD)" COMMIT1="$(git rev-parse HEAD)"
export COMMIT1 export COMMIT1
test_expect_success 'push with failing hook' ' test_expect_success 'push with failing hook' '
test_hook pre-push <<-\EOF &&
cat >actual &&
exit 1
EOF
test_commit two && test_commit two &&
cat >expect <<-EOF && cat >expect <<-EOF &&
HEAD $(git rev-parse HEAD) refs/heads/main $(test_oid zero) HEAD $(git rev-parse HEAD) refs/heads/main $(test_oid zero)
@ -55,13 +51,13 @@ test_expect_success '--no-verify bypasses hook' '
COMMIT2="$(git rev-parse HEAD)" COMMIT2="$(git rev-parse HEAD)"
export COMMIT2 export COMMIT2
write_script "$HOOK" <<'EOF'
echo "$1" >actual
echo "$2" >>actual
cat >>actual
EOF
test_expect_success 'push with hook' ' test_expect_success 'push with hook' '
test_hook --setup pre-push <<-\EOF &&
echo "$1" >actual
echo "$2" >>actual
cat >>actual
EOF
cat >expect <<-EOF && cat >expect <<-EOF &&
parent1 parent1
repo1 repo1
@ -136,7 +132,9 @@ test_expect_success 'set up many-ref tests' '
' '
test_expect_success 'sigpipe does not cause pre-push hook failure' ' test_expect_success 'sigpipe does not cause pre-push hook failure' '
echo "exit 0" | write_script "$HOOK" && test_hook --clobber pre-push <<-\EOF &&
exit 0
EOF
git push parent1 "refs/heads/b/*:refs/heads/b/*" git push parent1 "refs/heads/b/*:refs/heads/b/*"
' '

View File

@ -79,12 +79,10 @@ test_expect_success 'clone from hooks' '
cd .. && cd .. &&
git init r1 && git init r1 &&
cd r1 && cd r1 &&
cat >.git/hooks/pre-commit <<-\EOF && test_hook pre-commit <<-\EOF &&
#!/bin/sh
git clone ../r0 ../r2 git clone ../r0 ../r2
exit 1 exit 1
EOF EOF
chmod u+x .git/hooks/pre-commit &&
: >file && : >file &&
git add file && git add file &&
test_must_fail git commit -m invoke-hook && test_must_fail git commit -m invoke-hook &&

View File

@ -101,12 +101,12 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' '
EOF EOF
git init pre-auto-gc-hook && git init pre-auto-gc-hook &&
test_hook -C pre-auto-gc-hook pre-auto-gc <<-\EOF &&
echo >&2 no gc for you &&
exit 1
EOF
( (
cd pre-auto-gc-hook && cd pre-auto-gc-hook &&
write_script ".git/hooks/pre-auto-gc" <<-\EOF &&
echo >&2 no gc for you &&
exit 1
EOF
git config gc.auto 3 && git config gc.auto 3 &&
git config gc.autoDetach false && git config gc.autoDetach false &&
@ -128,14 +128,12 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' '
See "git help gc" for manual housekeeping. See "git help gc" for manual housekeeping.
EOF EOF
( test_hook -C pre-auto-gc-hook --clobber pre-auto-gc <<-\EOF &&
cd pre-auto-gc-hook && echo >&2 will gc for you &&
write_script ".git/hooks/pre-auto-gc" <<-\EOF && exit 0
echo >&2 will gc for you && EOF
exit 0
EOF git -C pre-auto-gc-hook gc --auto >out.actual 2>err.actual &&
git gc --auto >../out.actual 2>../err.actual
) &&
test_must_be_empty out.actual && test_must_be_empty out.actual &&
test_cmp err.expect err.actual test_cmp err.expect err.actual

View File

@ -17,8 +17,7 @@ test_expect_success 'setup' '
' '
test_expect_success 'test status, add, commit, others trigger hook without flags set' ' test_expect_success 'test status, add, commit, others trigger hook without flags set' '
mkdir -p .git/hooks && test_hook post-index-change <<-\EOF &&
write_script .git/hooks/post-index-change <<-\EOF &&
if test "$1" -eq 1; then if test "$1" -eq 1; then
echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
exit 1 exit 1
@ -63,7 +62,7 @@ test_expect_success 'test status, add, commit, others trigger hook without flags
' '
test_expect_success 'test checkout and reset trigger the hook' ' test_expect_success 'test checkout and reset trigger the hook' '
write_script .git/hooks/post-index-change <<-\EOF && test_hook post-index-change <<-\EOF &&
if test "$1" -eq 1 && test "$2" -eq 1; then if test "$1" -eq 1 && test "$2" -eq 1; then
echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
exit 1 exit 1
@ -106,7 +105,7 @@ test_expect_success 'test checkout and reset trigger the hook' '
' '
test_expect_success 'test reset --mixed and update-index triggers the hook' ' test_expect_success 'test reset --mixed and update-index triggers the hook' '
write_script .git/hooks/post-index-change <<-\EOF && test_hook post-index-change <<-\EOF &&
if test "$1" -eq 1 && test "$2" -eq 1; then if test "$1" -eq 1 && test "$2" -eq 1; then
echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
exit 1 exit 1

View File

@ -7,37 +7,6 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh . ./test-lib.sh
HOOKDIR="$(git rev-parse --git-dir)/hooks"
PRECOMMIT="$HOOKDIR/pre-commit"
PREMERGE="$HOOKDIR/pre-merge-commit"
# Prepare sample scripts that write their $0 to actual_hooks
test_expect_success 'sample script setup' '
mkdir -p "$HOOKDIR" &&
write_script "$HOOKDIR/success.sample" <<-\EOF &&
echo $0 >>actual_hooks
exit 0
EOF
write_script "$HOOKDIR/fail.sample" <<-\EOF &&
echo $0 >>actual_hooks
exit 1
EOF
write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
echo $0 >>actual_hooks
exit 1
EOF
chmod -x "$HOOKDIR/non-exec.sample" &&
write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
echo $0 >>actual_hooks
test $GIT_PREFIX = "success/"
EOF
write_script "$HOOKDIR/check-author.sample" <<-\EOF
echo $0 >>actual_hooks
test "$GIT_AUTHOR_NAME" = "New Author" &&
test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
EOF
'
test_expect_success 'root commit' ' test_expect_success 'root commit' '
echo "root" >file && echo "root" >file &&
git add file && git add file &&
@ -96,10 +65,16 @@ test_expect_success '--no-verify with no hook (merge)' '
test_path_is_missing actual_hooks test_path_is_missing actual_hooks
' '
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
}
test_expect_success 'with succeeding hook' ' test_expect_success 'with succeeding hook' '
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && setup_success_hook "pre-commit" &&
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
echo "$PRECOMMIT" >expected_hooks &&
echo "more" >>file && echo "more" >>file &&
git add file && git add file &&
git commit -m "more" && git commit -m "more" &&
@ -107,9 +82,7 @@ test_expect_success 'with succeeding hook' '
' '
test_expect_success 'with succeeding hook (merge)' ' test_expect_success 'with succeeding hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && setup_success_hook "pre-merge-commit" &&
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
echo "$PREMERGE" >expected_hooks &&
git checkout side && git checkout side &&
git merge -m "merge main" main && git merge -m "merge main" main &&
git checkout main && git checkout main &&
@ -117,17 +90,14 @@ test_expect_success 'with succeeding hook (merge)' '
' '
test_expect_success 'automatic merge fails; both hooks are available' ' test_expect_success 'automatic merge fails; both hooks are available' '
test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" && setup_success_hook "pre-commit" &&
test_when_finished "rm -f expected_hooks actual_hooks" && setup_success_hook "pre-merge-commit" &&
test_when_finished "git checkout main" &&
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
git checkout conflicting-a && git checkout conflicting-a &&
test_must_fail git merge -m "merge conflicting-b" conflicting-b && test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
test_path_is_missing actual_hooks && test_path_is_missing actual_hooks &&
echo "$PRECOMMIT" >expected_hooks && echo "pre-commit" >expected_hooks &&
echo a+b >conflicting && echo a+b >conflicting &&
git add conflicting && git add conflicting &&
git commit -m "resolve conflict" && git commit -m "resolve conflict" &&
@ -135,8 +105,7 @@ test_expect_success 'automatic merge fails; both hooks are available' '
' '
test_expect_success '--no-verify with succeeding hook' ' test_expect_success '--no-verify with succeeding hook' '
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && setup_success_hook "pre-commit" &&
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
echo "even more" >>file && echo "even more" >>file &&
git add file && git add file &&
git commit --no-verify -m "even more" && git commit --no-verify -m "even more" &&
@ -144,8 +113,7 @@ test_expect_success '--no-verify with succeeding hook' '
' '
test_expect_success '--no-verify with succeeding hook (merge)' ' test_expect_success '--no-verify with succeeding hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && setup_success_hook "pre-merge-commit" &&
cp "$HOOKDIR/success.sample" "$PREMERGE" &&
git branch -f side side-orig && git branch -f side side-orig &&
git checkout side && git checkout side &&
git merge --no-verify -m "merge main" main && git merge --no-verify -m "merge main" main &&
@ -153,10 +121,19 @@ test_expect_success '--no-verify with succeeding hook (merge)' '
test_path_is_missing actual_hooks test_path_is_missing actual_hooks
' '
setup_failing_hook () {
test_when_finished "rm -f actual_hooks" &&
test_hook "$1" <<-EOF
echo $1-failing-hook >>actual_hooks
exit 1
EOF
}
test_expect_success 'with failing hook' ' test_expect_success 'with failing hook' '
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && setup_failing_hook "pre-commit" &&
cp "$HOOKDIR/fail.sample" "$PRECOMMIT" && test_when_finished "rm -f expected_hooks" &&
echo "$PRECOMMIT" >expected_hooks && echo "pre-commit-failing-hook" >expected_hooks &&
echo "another" >>file && echo "another" >>file &&
git add file && git add file &&
test_must_fail git commit -m "another" && test_must_fail git commit -m "another" &&
@ -164,8 +141,7 @@ test_expect_success 'with failing hook' '
' '
test_expect_success '--no-verify with failing hook' ' test_expect_success '--no-verify with failing hook' '
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && setup_failing_hook "pre-commit" &&
cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
echo "stuff" >>file && echo "stuff" >>file &&
git add file && git add file &&
git commit --no-verify -m "stuff" && git commit --no-verify -m "stuff" &&
@ -173,9 +149,8 @@ test_expect_success '--no-verify with failing hook' '
' '
test_expect_success 'with failing hook (merge)' ' test_expect_success 'with failing hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" && setup_failing_hook "pre-merge-commit" &&
cp "$HOOKDIR/fail.sample" "$PREMERGE" && echo "pre-merge-commit-failing-hook" >expected_hooks &&
echo "$PREMERGE" >expected_hooks &&
git checkout side && git checkout side &&
test_must_fail git merge -m "merge main" main && test_must_fail git merge -m "merge main" main &&
git checkout main && git checkout main &&
@ -183,8 +158,8 @@ test_expect_success 'with failing hook (merge)' '
' '
test_expect_success '--no-verify with failing hook (merge)' ' test_expect_success '--no-verify with failing hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && setup_failing_hook "pre-merge-commit" &&
cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
git branch -f side side-orig && git branch -f side side-orig &&
git checkout side && git checkout side &&
git merge --no-verify -m "merge main" main && git merge --no-verify -m "merge main" main &&
@ -192,9 +167,18 @@ test_expect_success '--no-verify with failing hook (merge)' '
test_path_is_missing actual_hooks test_path_is_missing actual_hooks
' '
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"
}
test_expect_success POSIXPERM 'with non-executable hook' ' test_expect_success POSIXPERM 'with non-executable hook' '
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && setup_non_exec_hook "pre-commit" &&
cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
echo "content" >>file && echo "content" >>file &&
git add file && git add file &&
git commit -m "content" && git commit -m "content" &&
@ -202,8 +186,7 @@ test_expect_success POSIXPERM 'with non-executable hook' '
' '
test_expect_success POSIXPERM '--no-verify with non-executable hook' ' test_expect_success POSIXPERM '--no-verify with non-executable hook' '
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" && setup_non_exec_hook "pre-commit" &&
cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
echo "more content" >>file && echo "more content" >>file &&
git add file && git add file &&
git commit --no-verify -m "more content" && git commit --no-verify -m "more content" &&
@ -211,8 +194,7 @@ test_expect_success POSIXPERM '--no-verify with non-executable hook' '
' '
test_expect_success POSIXPERM 'with non-executable hook (merge)' ' test_expect_success POSIXPERM 'with non-executable hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && setup_non_exec_hook "pre-merge" &&
cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
git branch -f side side-orig && git branch -f side side-orig &&
git checkout side && git checkout side &&
git merge -m "merge main" main && git merge -m "merge main" main &&
@ -221,8 +203,7 @@ test_expect_success POSIXPERM 'with non-executable hook (merge)' '
' '
test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' ' test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
test_when_finished "rm -f \"$PREMERGE\" actual_hooks" && setup_non_exec_hook "pre-merge" &&
cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
git branch -f side side-orig && git branch -f side side-orig &&
git checkout side && git checkout side &&
git merge --no-verify -m "merge main" main && git merge --no-verify -m "merge main" main &&
@ -230,10 +211,18 @@ test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
test_path_is_missing actual_hooks test_path_is_missing actual_hooks
' '
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
}
test_expect_success 'with hook requiring GIT_PREFIX' ' test_expect_success 'with hook requiring GIT_PREFIX' '
test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" && test_when_finished "rm -rf actual_hooks success" &&
cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" && setup_require_prefix_hook &&
echo "$PRECOMMIT" >expected_hooks &&
echo "more content" >>file && echo "more content" >>file &&
git add file && git add file &&
mkdir success && mkdir success &&
@ -245,9 +234,8 @@ test_expect_success 'with hook requiring GIT_PREFIX' '
' '
test_expect_success 'with failing hook requiring GIT_PREFIX' ' test_expect_success 'with failing hook requiring GIT_PREFIX' '
test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" && test_when_finished "rm -rf actual_hooks fail" &&
cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" && setup_require_prefix_hook &&
echo "$PRECOMMIT" >expected_hooks &&
echo "more content" >>file && echo "more content" >>file &&
git add file && git add file &&
mkdir fail && mkdir fail &&
@ -259,13 +247,23 @@ test_expect_success 'with failing hook requiring GIT_PREFIX' '
test_cmp expected_hooks actual_hooks test_cmp expected_hooks actual_hooks
' '
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
}
test_expect_success 'check the author in hook' ' test_expect_success 'check the author in hook' '
test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" && setup_require_author_hook &&
cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
cat >expected_hooks <<-EOF && cat >expected_hooks <<-EOF &&
$PRECOMMIT check-author
$PRECOMMIT check-author
$PRECOMMIT check-author
EOF EOF
test_must_fail git commit --allow-empty -m "by a.u.thor" && test_must_fail git commit --allow-empty -m "by a.u.thor" &&
( (

View File

@ -54,15 +54,11 @@ test_expect_success '--no-verify with no hook (editor)' '
' '
# now install hook that always succeeds test_expect_success 'setup: commit-msg hook that always succeeds' '
HOOKDIR="$(git rev-parse --git-dir)/hooks" test_hook --setup commit-msg <<-\EOF
HOOK="$HOOKDIR/commit-msg" exit 0
mkdir -p "$HOOKDIR" EOF
cat > "$HOOK" <<EOF '
#!/bin/sh
exit 0
EOF
chmod +x "$HOOK"
test_expect_success 'with succeeding hook' ' test_expect_success 'with succeeding hook' '
@ -98,11 +94,11 @@ test_expect_success '--no-verify with succeeding hook (editor)' '
' '
# now a hook that fails test_expect_success 'setup: commit-msg hook that always fails' '
cat > "$HOOK" <<EOF test_hook --clobber commit-msg <<-\EOF
#!/bin/sh exit 1
exit 1 EOF
EOF '
commit_msg_is () { commit_msg_is () {
test "$(git log --pretty=format:%s%b -1)" = "$1" test "$(git log --pretty=format:%s%b -1)" = "$1"
@ -176,8 +172,12 @@ test_expect_success 'merge bypasses failing hook with --no-verify' '
commit_msg_is "Merge branch '\''main'\'' into newbranch" commit_msg_is "Merge branch '\''main'\'' into newbranch"
' '
test_expect_success 'setup: commit-msg hook made non-executable' '
git_dir="$(git rev-parse --git-dir)" &&
chmod -x "$git_dir/hooks/commit-msg"
'
chmod -x "$HOOK"
test_expect_success POSIXPERM 'with non-executable hook' ' test_expect_success POSIXPERM 'with non-executable hook' '
echo "content" >file && echo "content" >file &&
@ -212,13 +212,12 @@ test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
' '
# now a hook that edits the commit message test_expect_success 'setup: commit-msg hook that edits the commit message' '
cat > "$HOOK" <<'EOF' test_hook --clobber commit-msg <<-\EOF
#!/bin/sh echo "new message" >"$1"
echo "new message" > "$1" exit 0
exit 0 EOF
EOF '
chmod +x "$HOOK"
test_expect_success 'hook edits commit message' ' test_expect_success 'hook edits commit message' '

View File

@ -47,25 +47,19 @@ test_expect_success 'with no hook' '
' '
# set up fake editor for interactive editing test_expect_success 'setup fake editor for interactive editing' '
cat > fake-editor <<'EOF' write_script fake-editor <<-\EOF &&
#!/bin/sh exit 0
exit 0 EOF
EOF
chmod +x fake-editor
## Not using test_set_editor here so we can easily ensure the editor variable ## Not using test_set_editor here so we can easily ensure the editor variable
## is only set for the editor tests ## is only set for the editor tests
FAKE_EDITOR="$(pwd)/fake-editor" FAKE_EDITOR="$(pwd)/fake-editor" &&
export FAKE_EDITOR export FAKE_EDITOR
'
# now install hook that always succeeds and adds a message
HOOKDIR="$(git rev-parse --git-dir)/hooks"
HOOK="$HOOKDIR/prepare-commit-msg"
mkdir -p "$HOOKDIR"
echo "#!$SHELL_PATH" > "$HOOK"
cat >> "$HOOK" <<'EOF'
test_expect_success 'setup prepare-commit-msg hook' '
test_hook --setup prepare-commit-msg <<\EOF
GIT_DIR=$(git rev-parse --git-dir) GIT_DIR=$(git rev-parse --git-dir)
if test -d "$GIT_DIR/rebase-merge" if test -d "$GIT_DIR/rebase-merge"
then then
@ -103,7 +97,7 @@ else
fi fi
exit 0 exit 0
EOF EOF
chmod +x "$HOOK" '
echo dummy template > "$(git rev-parse --git-dir)/template" echo dummy template > "$(git rev-parse --git-dir)/template"
@ -265,10 +259,11 @@ test_expect_success 'with hook and editor (cherry-pick)' '
test "$(git log -1 --pretty=format:%s)" = merge test "$(git log -1 --pretty=format:%s)" = merge
' '
cat > "$HOOK" <<'EOF' test_expect_success 'setup: commit-msg hook that always fails' '
#!/bin/sh test_hook --setup --clobber prepare-commit-msg <<-\EOF
exit 1 exit 1
EOF EOF
'
test_expect_success 'with failing hook' ' test_expect_success 'with failing hook' '
@ -296,9 +291,9 @@ test_expect_success 'with failing hook (merge)' '
git checkout -B other HEAD@{1} && git checkout -B other HEAD@{1} &&
echo "more" >> file && echo "more" >> file &&
git add file && git add file &&
rm -f "$HOOK" && test_hook --remove prepare-commit-msg &&
git commit -m other && git commit -m other &&
write_script "$HOOK" <<-EOF && test_hook --setup prepare-commit-msg <<-\EOF &&
exit 1 exit 1
EOF EOF
git checkout - && git checkout - &&

View File

@ -26,7 +26,7 @@ dirty_repo () {
} }
write_integration_script () { write_integration_script () {
write_script .git/hooks/fsmonitor-test<<-\EOF test_hook --setup --clobber fsmonitor-test<<-\EOF
if test "$#" -ne 2 if test "$#" -ne 2
then then
echo "$0: exactly 2 arguments expected" echo "$0: exactly 2 arguments expected"
@ -56,7 +56,6 @@ test_lazy_prereq UNTRACKED_CACHE '
' '
test_expect_success 'setup' ' test_expect_success 'setup' '
mkdir -p .git/hooks &&
: >tracked && : >tracked &&
: >modified && : >modified &&
mkdir dir1 && mkdir dir1 &&
@ -108,7 +107,7 @@ EOF
# test that "update-index --fsmonitor-valid" sets the fsmonitor valid bit # test that "update-index --fsmonitor-valid" sets the fsmonitor valid bit
test_expect_success 'update-index --fsmonitor-valid" sets the fsmonitor valid bit' ' test_expect_success 'update-index --fsmonitor-valid" sets the fsmonitor valid bit' '
write_script .git/hooks/fsmonitor-test<<-\EOF && test_hook fsmonitor-test<<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
EOF EOF
git update-index --fsmonitor && git update-index --fsmonitor &&
@ -169,7 +168,7 @@ EOF
# test that newly added files are marked valid # test that newly added files are marked valid
test_expect_success 'newly added files are marked valid' ' test_expect_success 'newly added files are marked valid' '
write_script .git/hooks/fsmonitor-test<<-\EOF && test_hook --setup --clobber fsmonitor-test<<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
EOF EOF
git add new && git add new &&
@ -210,7 +209,7 @@ EOF
# test that *only* files returned by the integration script get flagged as invalid # test that *only* files returned by the integration script get flagged as invalid
test_expect_success '*only* files returned by the integration script get flagged as invalid' ' test_expect_success '*only* files returned by the integration script get flagged as invalid' '
write_script .git/hooks/fsmonitor-test<<-\EOF && test_hook --clobber fsmonitor-test<<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
printf "dir1/modified\0" printf "dir1/modified\0"
EOF EOF
@ -231,7 +230,7 @@ test_expect_success 'refresh_index() invalidates fsmonitor cache' '
dirty_repo && dirty_repo &&
write_integration_script && write_integration_script &&
git add . && git add . &&
write_script .git/hooks/fsmonitor-test<<-\EOF && test_hook --clobber fsmonitor-test<<-\EOF &&
EOF EOF
git commit -m "to reset" && git commit -m "to reset" &&
git reset HEAD~1 && git reset HEAD~1 &&
@ -280,7 +279,7 @@ do
# Make sure it's actually skipping the check for modified and untracked # Make sure it's actually skipping the check for modified and untracked
# (if enabled) files unless it is told about them. # (if enabled) files unless it is told about them.
test_expect_success "status doesn't detect unreported modifications" ' test_expect_success "status doesn't detect unreported modifications" '
write_script .git/hooks/fsmonitor-test<<-\EOF && test_hook --clobber fsmonitor-test<<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
:>marker :>marker
EOF EOF
@ -322,7 +321,6 @@ test_expect_success UNTRACKED_CACHE 'ignore .git changes when invalidating UNTR'
test_create_repo dot-git && test_create_repo dot-git &&
( (
cd dot-git && cd dot-git &&
mkdir -p .git/hooks &&
: >tracked && : >tracked &&
test-tool chmtime =-60 tracked && test-tool chmtime =-60 tracked &&
: >modified && : >modified &&
@ -414,14 +412,14 @@ test_expect_success 'status succeeds with sparse index' '
git -C sparse sparse-checkout init --cone --sparse-index && git -C sparse sparse-checkout init --cone --sparse-index &&
git -C sparse sparse-checkout set dir1 dir2 && git -C sparse sparse-checkout set dir1 dir2 &&
write_script .git/hooks/fsmonitor-test <<-\EOF && test_hook --clobber fsmonitor-test <<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
EOF EOF
git -C full config core.fsmonitor ../.git/hooks/fsmonitor-test && git -C full config core.fsmonitor ../.git/hooks/fsmonitor-test &&
git -C sparse config core.fsmonitor ../.git/hooks/fsmonitor-test && git -C sparse config core.fsmonitor ../.git/hooks/fsmonitor-test &&
check_sparse_index_behavior ! && check_sparse_index_behavior ! &&
write_script .git/hooks/fsmonitor-test <<-\EOF && test_hook --clobber fsmonitor-test <<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
printf "dir1/modified\0" printf "dir1/modified\0"
EOF EOF
@ -439,7 +437,7 @@ test_expect_success 'status succeeds with sparse index' '
# This one modifies outside the sparse-checkout definition # This one modifies outside the sparse-checkout definition
# and hence we expect to expand the sparse-index. # and hence we expect to expand the sparse-index.
write_script .git/hooks/fsmonitor-test <<-\EOF && test_hook --clobber fsmonitor-test <<-\EOF &&
printf "last_update_token\0" printf "last_update_token\0"
printf "dir1a/modified\0" printf "dir1a/modified\0"
EOF EOF

View File

@ -5,10 +5,7 @@ test_description='ignored hook warning'
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '
hookdir="$(git rev-parse --git-dir)/hooks" && test_hook --setup pre-commit <<-\EOF
hook="$hookdir/pre-commit" &&
mkdir -p "$hookdir" &&
write_script "$hook" <<-\EOF
exit 0 exit 0
EOF EOF
' '
@ -19,20 +16,20 @@ test_expect_success 'no warning if hook is not ignored' '
' '
test_expect_success POSIXPERM 'warning if hook is ignored' ' test_expect_success POSIXPERM 'warning if hook is ignored' '
chmod -x "$hook" && test_hook --disable pre-commit &&
git commit --allow-empty -m "even more" 2>message && git commit --allow-empty -m "even more" 2>message &&
test_i18ngrep -e "hook was ignored" message test_i18ngrep -e "hook was ignored" message
' '
test_expect_success POSIXPERM 'no warning if advice.ignoredHook set to false' ' test_expect_success POSIXPERM 'no warning if advice.ignoredHook set to false' '
test_config advice.ignoredHook false && test_config advice.ignoredHook false &&
chmod -x "$hook" && test_hook --disable pre-commit &&
git commit --allow-empty -m "even more" 2>message && git commit --allow-empty -m "even more" 2>message &&
test_i18ngrep ! -e "hook was ignored" message test_i18ngrep ! -e "hook was ignored" message
' '
test_expect_success 'no warning if unset advice.ignoredHook and hook removed' ' test_expect_success 'no warning if unset advice.ignoredHook and hook removed' '
rm -f "$hook" && test_hook --remove pre-commit &&
test_unconfig advice.ignoredHook && test_unconfig advice.ignoredHook &&
git commit --allow-empty -m "even more" 2>message && git commit --allow-empty -m "even more" 2>message &&
test_i18ngrep ! -e "hook was ignored" message test_i18ngrep ! -e "hook was ignored" message

View File

@ -2288,9 +2288,7 @@ test_expect_success $PREREQ 'cmdline in-reply-to used with --no-thread' '
' '
test_expect_success $PREREQ 'invoke hook' ' test_expect_success $PREREQ 'invoke hook' '
mkdir -p .git/hooks && test_hook sendemail-validate <<-\EOF &&
write_script .git/hooks/sendemail-validate <<-\EOF &&
# test that we have the correct environment variable, pwd, and # test that we have the correct environment variable, pwd, and
# argument # argument
case "$GIT_DIR" in case "$GIT_DIR" in

View File

@ -277,16 +277,21 @@ test_expect_success 'run hook p4-pre-submit before submit' '
git commit -m "add hello.txt" && git commit -m "add hello.txt" &&
git config git-p4.skipSubmitEdit true && git config git-p4.skipSubmitEdit true &&
git p4 submit --dry-run >out && git p4 submit --dry-run >out &&
grep "Would apply" out && grep "Would apply" out
mkdir -p .git/hooks && ) &&
write_script .git/hooks/p4-pre-submit <<-\EOF && test_hook -C "$git" p4-pre-submit <<-\EOF &&
exit 0 exit 0
EOF EOF
(
cd "$git" &&
git p4 submit --dry-run >out && git p4 submit --dry-run >out &&
grep "Would apply" out && grep "Would apply" out
write_script .git/hooks/p4-pre-submit <<-\EOF && ) &&
exit 1 test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
EOF exit 1
EOF
(
cd "$git" &&
test_must_fail git p4 submit --dry-run >errs 2>&1 && test_must_fail git p4 submit --dry-run >errs 2>&1 &&
! grep "Would apply" errs ! grep "Would apply" errs
) )

View File

@ -551,6 +551,82 @@ write_script () {
chmod +x "$1" chmod +x "$1"
} }
# Usage: test_hook [options] <hook-name> <<-\EOF
#
# -C <dir>:
# Run all git commands in directory <dir>
# --setup
# Setup a hook for subsequent tests, i.e. don't remove it in a
# "test_when_finished"
# --clobber
# Overwrite an existing <hook-name>, if it exists. Implies
# --setup (i.e. the "test_when_finished" is assumed to have been
# set up already).
# --disable
# Disable (chmod -x) an existing <hook-name>, which must exist.
# --remove
# Remove (rm -f) an existing <hook-name>, which must exist.
test_hook () {
setup= &&
clobber= &&
disable= &&
remove= &&
indir= &&
while test $# != 0
do
case "$1" in
-C)
indir="$2" &&
shift
;;
--setup)
setup=t
;;
--clobber)
clobber=t
;;
--disable)
disable=t
;;
--remove)
remove=t
;;
-*)
BUG "invalid argument: $1"
;;
*)
break
;;
esac &&
shift
done &&
git_dir=$(git -C "$indir" rev-parse --absolute-git-dir) &&
hook_dir="$git_dir/hooks" &&
hook_file="$hook_dir/$1" &&
if test -n "$disable$remove"
then
test_path_is_file "$hook_file" &&
if test -n "$disable"
then
chmod -x "$hook_file"
elif test -n "$remove"
then
rm -f "$hook_file"
fi &&
return 0
fi &&
if test -z "$clobber"
then
test_path_is_missing "$hook_file"
fi &&
if test -z "$setup$clobber"
then
test_when_finished "rm \"$hook_file\""
fi &&
write_script "$hook_file"
}
# Use test_set_prereq to tell that a particular prerequisite is available. # Use test_set_prereq to tell that a particular prerequisite is available.
# The prerequisite can later be checked for in two ways: # The prerequisite can later be checked for in two ways:
# #