-
-
Save heoh/cf80a3204122faed0c530de60a222e15 to your computer and use it in GitHub Desktop.
Run multiple scripts for the same git hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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