-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ssh-install-hooks
More file actions
executable file
·205 lines (173 loc) · 3.77 KB
/
git-ssh-install-hooks
File metadata and controls
executable file
·205 lines (173 loc) · 3.77 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
readonly BASENAME="git-ssh"
readonly NAME="${BASENAME}-install-hooks"
readonly VERSION="0.0.1"
readonly HOOK_SOURCE="${HOOK_SOURCE:-/usr/share/${BASENAME}/hooks}"
readonly GIT_DIR="${GIT_DIR:-.git}"
_log() {
_msg="$1"
shift
printf -- "[%s] ${_msg}\n" "${NAME}" "$@"
unset _msg
return 0
}
##
# Error Log
#
# Accepts arguments in the same format as printf
# $1 message
# $@ arguments
##
_elog() {
_log "$@" 1>&2 || return 1
return 0
}
_collect_hooks() {
_hooks=""
for _h in "${HOOK_SOURCE}"/*; do
if [ -r "${_h}" ]; then
if [ -z "${_hooks}" ]; then
_hooks="${_h}"
else
_hooks="${_hooks}
${_h}"
fi
fi
unset _h
done
unset _h
printf -- '%s\n' "${_hooks}"
unset _hooks
return 0
}
_inform_hooks() {
printf -- 'The following files will be installed:\n'
_collect_hooks || return 1
return 0
}
_usage() {
printf -- '%s\n' "$(
cat <<EOF
${NAME} [${VERSION}] <command>
This script will install/overwrite your git repository prepare-commit-msg hooks
to check and enforce that all committers have a signed SSH key present.
[Commands]
install [--force] Install hooks (--force will overwrite existing hooks)
list List hooks
[Hooks]
$(_inform_hooks)
EOF
)"
}
_do_install() {
_install_target="$1"
_log 'Installing %s -> %s' "${_install_target}" "${GIT_DIR}/hooks"
cp -f "${_install_target}" "${GIT_DIR}/hooks" || {
_elog 'Failed to install %s' "${_install_target}"
unset _install_target
return 1
}
unset _install_target
return 0
}
_is_git_project() {
[ -d "${GIT_DIR}" ] && [ -d "${GIT_DIR}/hooks" ]
}
_install() {
_forced="$1"
for _hook in $(_collect_hooks); do
case "${_hook}" in
*.prepare-commit-msg)
_do_install "${_hook}" || {
_elog 'Failed to install hook %s' "$(basename "${_hook}")"
unset _hook
unset _forced
return 1
}
;;
*)
if [ -z "${_forced}" ] || [ "${_forced}" -eq 0 ]; then
# If the file already exists, prompt user
if [ -r "${GIT_DIR}/hooks/$(basename "${_hook}")" ]; then
# Don't use _log so we don't force a newline
printf -- '[%s] Are you sure you want to overwrite the existing hook %s? (y/N) ' "${NAME}" "$(basename "${_hook}")"
IFS= read -r _yes
case "${_yes}" in
y | Y | yes | YES)
_do_install "${_hook}" || {
_elog 'Failed to install hook %s' "$(basename "${_hook}")"
unset _hook
unset _forced
return 1
}
;;
*)
_log 'Skipping install of hook %s' "$(basename "${_hook}")"
continue
;;
esac
else
_do_install "${_hook}" || {
_elog 'Failed to install hook %s' "$(basename "${_hook}")"
unset _hook
unset _forced
return 1
}
fi
else
_do_install "${_hook}" || {
_elog 'Failed to install hook %s' "$(basename "${_hook}")"
unset _hook
unset _forced
return 1
}
fi
;;
esac
unset _hook
done
unset _hook
unset _forced
return 0
}
_list() {
_inform_hooks || return 1
return 0
}
main() {
_cmd=""
_force_install=0
while [ -n "$1" ]; do
case "$1" in
install)
_cmd="install"
;;
list)
_cmd="list"
;;
--force)
_force_install=1
;;
esac
shift
done
case "${_cmd}" in
install)
if ! _is_git_project; then
_elog 'Cannot install hook. This is not a git project?'
return 1
fi
_install "${_force_install}" || return 1
;;
list)
_list || return 1
;;
*)
_usage || return 1
return 1
;;
esac
return 0
}
main "$@" || exit 1
exit 0