Skip to content

Instantly share code, notes, and snippets.

@mar-wir
Forked from mjackson/multiple-git-hooks.sh
Created June 27, 2020 21:15
Show Gist options
  • Save mar-wir/f2f8d57d95a6cb6c77f7f0b9d650c928 to your computer and use it in GitHub Desktop.
Save mar-wir/f2f8d57d95a6cb6c77f7f0b9d650c928 to your computer and use it in GitHub Desktop.
Run multiple scripts for the same git hook
#!/bin/sh
# This script should be saved in a git repo as a hook file, e.g. .git/hooks/pre-receive.
# It looks for scripts in the .git/hooks/pre-receive.d directory and executes them in order,
# passing along stdin. If any script exits with a non-zero status, this script exits.
script_dir=$(dirname $0)
hook_name=$(basename $0)
hook_dir="$script_dir/$hook_name.d"
if [[ -d $hook_dir ]]; then
stdin=$(cat /dev/stdin)
for hook in $hook_dir/*; do
echo "Running $hook_name/$hook hook"
echo "$stdin" | $hook "$@"
exit_code=$?
if [ $exit_code != 0 ]; then
exit $exit_code
fi
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment