Skip to content

Instantly share code, notes, and snippets.

@heoh
Forked from mjackson/multiple-git-hooks.sh
Last active December 11, 2019 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heoh/cf80a3204122faed0c530de60a222e15 to your computer and use it in GitHub Desktop.
Save heoh/cf80a3204122faed0c530de60a222e15 to your computer and use it in GitHub Desktop.
Run multiple scripts for the same git hook
#!/bin/bash
# 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.
local_script_dir=".git/hooks"
script_dir=$(dirname $0)
hook_name=$(basename $0)
stdin=$(cat /dev/stdin)
hooks=()
function add_file_to_hooks {
file=$1
if [ -f $file ]; then
hooks+=($file)
fi
}
function add_files_to_hooks {
dir=$1
if [ -d $dir ] && [ $(ls -A $dir) ]; then
for hook in $dir/*; do
hooks+=($hook)
done
fi
}
if [ ! "$local_script_dir" -ef "$script_dir" ]; then
add_file_to_hooks "$local_script_dir/$hook_name"
add_files_to_hooks "$local_script_dir/$hook_name.d"
fi
add_files_to_hooks "$script_dir/$hook_name.d"
for hook in ${hooks[@]}; do
echo "$stdin" | $hook "$@"
exit_code=$?
if [ $exit_code != 0 ]; then
exit $exit_code
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment