diff --git a/husky b/husky index bf7c89640..cef870192 100644 --- a/husky +++ b/husky @@ -2,6 +2,7 @@ [ "$HUSKY" = "2" ] && set -x n=$(basename "$0") s=$(dirname "$(dirname "$0")")/$n +h="$(git rev-parse --git-dir)/hooks/$n" [ ! -f "$s" ] && exit 0 @@ -19,4 +20,13 @@ c=$? [ $c != 0 ] && echo "husky - $n script failed (code $c)" [ $c = 127 ] && echo "husky - command not found in PATH=$PATH" -exit $c +[ $c != 0 ] && exit $c + +if [ -x "$h" ]; then + "$h" "$@" + c=$? + [ $c != 0 ] && echo "husky - $h hook failed (code $c)" + [ $c != 0 ] && exit $c +fi + +exit 0 diff --git a/test/13_chain_git_hooks.sh b/test/13_chain_git_hooks.sh new file mode 100755 index 000000000..ae27d54c0 --- /dev/null +++ b/test/13_chain_git_hooks.sh @@ -0,0 +1,24 @@ +#!/bin/sh +. test/functions.sh +setup +install + +npx --no-install husky + +# Create a custom .git/hooks/pre-commit +mkdir -p .git/hooks +cat > .git/hooks/pre-commit << 'EOF' +#!/bin/sh +echo "custom git hook executed" +exit 0 +EOF +chmod +x .git/hooks/pre-commit + +# Should execute both husky and original Git hook +git add package.json +echo "echo \"husky hook executed\"" > .husky/pre-commit +output=$(git commit -m "test" 2>&1) +echo "$output" | grep -q "husky hook executed" || error "husky hook should run" +echo "$output" | grep -q "custom git hook executed" || error "custom .git/hooks/pre-commit should run" + +rm .git/hooks/pre-commit