forked from Autohook/Autohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autohook.sh
executable file
·86 lines (75 loc) · 1.97 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
#!/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.0
# Website: https://github.com/nkantar/Autohook
echo() {
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_filename="$repo_root/hooks/autohook.sh"
autohook_path=$(realpath $autohook_filename)
for hook_type in "${hook_types[@]}"
do
hook_symlink="$hooks_dir/$hook_type"
ln -s $autohook_path $hook_symlink
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
for file in "${files[@]}"
do
scriptname=$(basename $file)
echo "BEGIN $scriptname"
eval $file &> /dev/null
echo "FINISH $scriptname"
done
fi
fi
}
main "$@"