-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple-git-hooks.sh
executable file
·48 lines (39 loc) · 1.2 KB
/
multiple-git-hooks.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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.
blue=$(tput setaf 247)
invert=$(tput rev)
red=$(tput setaf 1)
reset=$(tput sgr0)
script_dir=$(dirname "$0")
hook_name=$(basename "$0")
hook_dir="$script_dir/$hook_name.d"
if [ ! -d "$hook_dir" ]; then
print "${invert}$hook_dir not found${reset}"
exit 0
fi
found=0
tmp=$(mktemp)
trap 'rm $tmp' EXIT
find "$hook_dir" -perm -u=x -not -type d >"$tmp"
# see https://github.com/koalaman/shellcheck/wiki/SC2044 for the strange loop
while IFS= read -r hook; do
found=1
hook_cmd="$hook_name.d/$(basename "$hook")"
printf %s "Running ${blue}${hook_cmd}${reset} hook"
ts=$(date +%s%N)
$hook "$@" </dev/tty
exit_code=$?
if [ $exit_code != 0 ]; then
printf "\n%s\n" "${hook_cmd} hook returned ${red}${exit_code}${reset}"
exit $exit_code
fi
tt=$((($(date +%s%N) - ts) / 1000000))
printf " %s\n" "${blue}${tt} ms${reset}"
done <"$tmp"
if [ "$found" -eq 0 ]; then
printf "%s\n" "${red}no hook found in ${hook_dir}${reset}"
exit 0
fi
exit 0