5
5
# @brief Hookah: An elegantly minimal solution for Git hooks
6
6
# @description Hookah streamlines the process of managing Git hooks. This file is a
7
7
# library of functions that can easily be used by hooks written in Bash. Use it by
8
- # prepending your script with the following
8
+ # prepending your hook script with the following
9
9
#
10
10
# ```bash
11
11
# #!/usr/bin/env bash
27
27
hookah.init () {
28
28
set -Eeo pipefail
29
29
shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose
30
- export LANG=' C' LC_CTYPE=' C' LC_NUMERIC=' C' LC_TIME=' C' LC_COLLATE=' C' LC_MONETARY= ' C ' \
31
- LC_MESSAGES =' C' LC_PAPER =' C' LC_NAME =' C' LC_ADDRESS =' C' LC_TELEPHONE =' C' \
32
- LC_MEASUREMENT=' C' LC_IDENTIFICATION=' C' LC_ALL=' C'
30
+ export LANG=' C' LC_CTYPE=' C' LC_NUMERIC=' C' LC_TIME=' C' LC_COLLATE=' C' \
31
+ LC_MONETARY =' C' LC_MESSAGES =' C' LC_PAPER =' C' LC_NAME =' C' LC_ADDRESS =' C' \
32
+ LC_TELEPHONE= ' C ' LC_MEASUREMENT=' C' LC_IDENTIFICATION=' C' LC_ALL=' C'
33
33
trap ' __hookah_trap_err' ' ERR'
34
34
35
35
while [ ! -d ' .git' ] && [ " $PWD " != / ]; do
36
36
if ! cd ..; then
37
- __hookah_die " Failed to cd to nearest Git repository"
37
+ __hookah_internal_die " Failed to cd to nearest Git repository"
38
38
fi
39
39
done
40
40
if [ " $PWD " = / ]; then
41
- __hookah_die " Failed to cd to nearest Git repository"
41
+ __hookah_internal_die " Failed to cd to nearest Git repository"
42
42
fi
43
43
44
44
# Prevent any possibilities of 'stdin in is not a tty'
45
45
if ! exec < /dev/tty; then
46
- __hookah_die " Failed to redirect tty to standard input"
46
+ __hookah_internal_warn " Failed to redirect tty to standard input"
47
47
fi
48
48
49
- __hookah_print " Running ${BASH_SOURCE[1]##*/ } "
50
-
49
+ __hookah_internal_info " Running ${BASH_SOURCE[1]##*/ } "
51
50
}
52
51
53
52
# @description Prints a command before running it
54
- # #args $@ Command to execute
53
+ # @arg $@ Command to execute
55
54
hookah.run () {
56
- printf ' %s\n ' " Hookah: Running command: ' $* ' "
55
+ __hookah_exec " $* "
57
56
" $@ "
58
57
}
59
58
60
59
# @description Prints a command before running it. But, if the command fails, do not abort execution
61
- # @args $@ Command to execute
60
+ # @arg $@ Command to execute
62
61
hookah.run_allow_fail () {
63
62
if ! hookah.run " $@ " ; then
64
- printf ' %s\n' " Hookah: Command failed"
63
+ hookah.die ' Command failed'
64
+ fi
65
+ }
66
+
67
+ # @description Prints `$1` formatted as an error and the stacktrace to standard error,
68
+ # then exits with code 1
69
+ # @arg $1 string Text to print
70
+ hookah.die () {
71
+ if [ -n " $1 " ]; then
72
+ __hookah_internal_error " $1 . Exiting" ' Hookah'
73
+ else
74
+ __hookah_internal_error ' Exiting' ' Hookah'
65
75
fi
76
+
77
+ exit 1
78
+ }
79
+
80
+ # @description Prints `$1` formatted as a warning to standard error
81
+ # @arg $1 string Text to print
82
+ hookah.warn () {
83
+ __hookah_internal_warn " $1 " ' Hookah'
84
+ }
85
+
86
+ # @description Prints `$1` formatted as information to standard output
87
+ # @arg $1 string Text to print
88
+ hookah.info () {
89
+ __hookah_internal_info " $1 " ' Hookah'
66
90
}
67
91
68
92
# @description Scans environment variables to determine if script is in a CI environment
@@ -72,6 +96,7 @@ hookah.run_allow_fail() {
72
96
hookah.is_ci () {
73
97
unset -v REPLY; REPLY=
74
98
99
+ # List from 'https://github.com/watson/ci-info/blob/master/vendors.json'
75
100
if [[ -v ' APPVEYOR' ]]; then
76
101
REPLY=' AppVeyor'
77
102
elif [[ -v ' SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' ]]; then
@@ -164,48 +189,58 @@ __hookah_is_color() {
164
189
}
165
190
166
191
# @internal
167
- __hookah_internal_error () {
168
- printf ' %s\n' " Internal Error: $1 " >&2
192
+ __hookah_exec () {
193
+ if __hookah_is_color; then
194
+ printf " \033[1mHookah \033[1m[exec]:\033[0m %s\n" " $* "
195
+ else
196
+ printf " Hookah [exec]: %s\n" " $* "
197
+ fi
169
198
}
170
199
171
200
# @internal
172
- __hookah_die () {
173
- __hookah_error " $1 "
201
+ __hookah_internal_die () {
202
+ __hookah_internal_error " $1 "
174
203
exit 1
175
204
}
176
205
177
206
# @internal
178
- __hookah_error () {
179
- printf ' %s\n' " Hookah: lib.sh: Error: $1 . Exiting"
180
- exit 1
181
- }
207
+ __hookah_internal_error () {
208
+ local str=" ${2:- " Hookah (internal)" } "
182
209
183
- # @internal
184
- __hookah_print () {
185
- printf ' %s\n' " Hookah: $1 "
186
- }
210
+ if __hookah_is_color; then
211
+ printf " \033[1;31m\033[1m$str \033[1m[error]:\033[0m %s\n" " $1 "
212
+ else
213
+ printf " $str [error]: %s\n" " $1 "
214
+ fi
215
+ } >&2
187
216
188
217
# @internal
189
- __hookah_trap_err () {
190
- local error_code= $?
218
+ __hookah_internal_warn () {
219
+ local str= " ${2 :- " Hookah (internal) " } "
191
220
192
- __hookah_internal_error " Your hook did not exit successfully"
193
- __hookah_print_stacktrace
194
-
195
- exit $error_code
221
+ if __hookah_is_color; then
222
+ printf " \033[1;33m\033[1m$str \033[1m[warn]:\033[0m %s\n" " $1 "
223
+ else
224
+ printf " $str [warn]: %s\n" " $1 "
225
+ fi
196
226
} >&2
197
227
198
228
# @internal
199
- __hookah_print_stacktrace () {
229
+ __hookah_internal_info () {
230
+ local str=" ${2:- " Hookah (internal)" } "
231
+
200
232
if __hookah_is_color; then
201
- printf ' \033[4m%s \033[0m\n ' ' Stacktrace: '
233
+ printf " \033[0;36m \033[1m $str \033[1m[info]:\033[0m %s\n " " $1 "
202
234
else
203
- printf ' %s\n' ' Stacktrace: '
235
+ printf " $str [info]: %s\n" " $1 "
204
236
fi
237
+ }
205
238
206
- local i=
207
- for (( i= 0 ; i< ${# FUNCNAME[@]} - 1 ; ++ i)) ; do
208
- local __bash_source=" ${BASH_SOURCE[$i]} " ; __bash_source=${__bash_source##*/ }
209
- printf ' %s\n' " in ${FUNCNAME[$i]} ($__bash_source :${BASH_LINENO[$i-1]} )"
210
- done ; unset -v i __bash_source
211
- } >&2
239
+ # @internal
240
+ __hookah_trap_err () {
241
+ local error_code=$?
242
+
243
+ __hookah_internal_error " Your hook did not exit successfully (exit code $error_code )"
244
+
245
+ exit $error_code
246
+ }
0 commit comments