forked from Deducteam/lambdapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_hook_helper.sh
executable file
·52 lines (45 loc) · 1.1 KB
/
git_hook_helper.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
49
50
51
52
#!/bin/sh
set -euf -o noclobber
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
usage="Usage: $(basename "$0") [-b]
Set up git pre-commit hook.
Arguments:
-b Include compilation in hook.
"
sanity_only=true
while getopts 'bh' arg; do
case "${arg}" in
b) sanity_only='';;
h) printf '%s' "${usage}"
exit 0
;;
*) printf 'Invalid option\\n'
printf '%s' "${usage}"
exit 1
;;
esac
done
git_root="$(realpath "${dir}/../.git")"
hook_path="${git_root}/hooks/pre-commit"
hook_cmd=''
## Set hook command depending on cli arguments
if [ -n "${sanity_only}" ]; then
hook_cmd='if [ ! -z "$(make sanity_check)" ]; then
echo "Sanity check failed."
exit 1
fi'
else
hook_cmd='if [ ! -z "$(make sanity_check)" ] || ! make bin; then
echo "Sanity check or compilation failed."
exit 1
fi'
fi
if [ -f "${hook_path}" ]; then
printf 'Pre-commit hook [%s] found.
Remove it or add the command
%s
' "${hook_path}" "${hook_cmd}"
exit 1
fi
printf '#!/bin/sh\n%s\n' "${hook_cmd}" > "${hook_path}"
chmod 755 "${hook_path}"