forked from Autohook/Autohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautohook.sh
executable file
·100 lines (89 loc) · 2.49 KB
/
autohook.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
# Autohook
# A very, very small Git hook manager with focus on automation
# Author: Nik Kantar <http://nkantar.com>
# Version: 2.1.1
# Website: https://github.com/nkantar/Autohook
echo() {
# shellcheck disable=SC2145
builtin echo "[Autohook] $@";
}
install() {
hook_types=(
"applypatch-msg"
"commit-msg"
"post-applypatch"
"post-checkout"
"post-commit"
"post-merge"
"post-receive"
"post-rewrite"
"post-update"
"pre-applypatch"
"pre-auto-gc"
"pre-commit"
"pre-push"
"pre-rebase"
"pre-receive"
"prepare-commit-msg"
"update"
)
repo_root=$(git rev-parse --show-toplevel)
hooks_dir="$repo_root/.git/hooks"
autohook_linktarget="../../hooks/autohook.sh"
for hook_type in "${hook_types[@]}"
do
hook_symlink="$hooks_dir/$hook_type"
ln -s $autohook_linktarget "$hook_symlink"
mkdir -v "$repo_root/hooks/$hook_type"
mkdir -v "$repo_root/hooks/scripts"
ln -s "../scripts/default.sh" "hooks/$hook_type/00_default.sh"
done
}
main() {
calling_file=$(basename "$0")
if [[ $calling_file == "autohook.sh" ]]
then
command=$1
if [[ $command == "install" ]]
then
install
fi
else
repo_root=$(git rev-parse --show-toplevel)
hook_type=$calling_file
symlinks_dir="$repo_root/hooks/$hook_type"
files=("$symlinks_dir"/*)
number_of_symlinks="${#files[@]}"
if [[ $number_of_symlinks == 1 ]]
then
if [[ $(basename "${files[0]}") == "*" ]]
then
number_of_symlinks=0
fi
fi
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
if [[ $number_of_symlinks -gt 0 ]]
then
hook_exit_code=0
for file in "${files[@]}"
do
scriptname=$(basename "$file")
echo "BEGIN $scriptname"
eval "$file" &> /dev/null
script_exit_code=$?
if [[ $script_exit_code != 0 ]]
then
hook_exit_code=$script_exit_code
fi
echo "FINISH $scriptname"
done
if [[ $hook_exit_code != 0 ]]
then
echo "A $hook_type script yielded negative exit code $hook_exit_code"
exit $hook_exit_code
fi
fi
fi
}
main "$@"