55# @brief Hookah: An elegantly minimal solution for Git hooks
66# @description Hookah streamlines the process of managing Git hooks. This file is a
77# 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
99#
1010# ```bash
1111# #!/usr/bin/env bash
2727hookah.init () {
2828 set -Eeo pipefail
2929 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'
3333 trap ' __hookah_trap_err' ' ERR'
3434
3535 while [ ! -d ' .git' ] && [ " $PWD " != / ]; do
3636 if ! cd ..; then
37- __hookah_die " Failed to cd to nearest Git repository"
37+ __hookah_internal_die " Failed to cd to nearest Git repository"
3838 fi
3939 done
4040 if [ " $PWD " = / ]; then
41- __hookah_die " Failed to cd to nearest Git repository"
41+ __hookah_internal_die " Failed to cd to nearest Git repository"
4242 fi
4343
4444 # Prevent any possibilities of 'stdin in is not a tty'
4545 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"
4747 fi
4848
49- __hookah_print " Running ${BASH_SOURCE[1]##*/ } "
50-
49+ __hookah_internal_info " Running ${BASH_SOURCE[1]##*/ } "
5150}
5251
5352# @description Prints a command before running it
54- # #args $@ Command to execute
53+ # @arg $@ Command to execute
5554hookah.run () {
56- printf ' %s\n ' " Hookah: Running command: ' $* ' "
55+ __hookah_exec " $* "
5756 " $@ "
5857}
5958
6059# @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
6261hookah.run_allow_fail () {
6362 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'
6575 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'
6690}
6791
6892# @description Scans environment variables to determine if script is in a CI environment
@@ -72,6 +96,7 @@ hookah.run_allow_fail() {
7296hookah.is_ci () {
7397 unset -v REPLY; REPLY=
7498
99+ # List from 'https://github.com/watson/ci-info/blob/master/vendors.json'
75100 if [[ -v ' APPVEYOR' ]]; then
76101 REPLY=' AppVeyor'
77102 elif [[ -v ' SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' ]]; then
@@ -164,48 +189,58 @@ __hookah_is_color() {
164189}
165190
166191# @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
169198}
170199
171200# @internal
172- __hookah_die () {
173- __hookah_error " $1 "
201+ __hookah_internal_die () {
202+ __hookah_internal_error " $1 "
174203 exit 1
175204}
176205
177206# @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)" } "
182209
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
187216
188217# @internal
189- __hookah_trap_err () {
190- local error_code= $?
218+ __hookah_internal_warn () {
219+ local str= " ${2 :- " Hookah (internal) " } "
191220
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
196226} >&2
197227
198228# @internal
199- __hookah_print_stacktrace () {
229+ __hookah_internal_info () {
230+ local str=" ${2:- " Hookah (internal)" } "
231+
200232 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 "
202234 else
203- printf ' %s\n' ' Stacktrace: '
235+ printf " $str [info]: %s\n" " $1 "
204236 fi
237+ }
205238
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