-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooka
executable file
·457 lines (342 loc) · 10.4 KB
/
hooka
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env bash
HOOKA_DEBUG=${HOOKA_DEBUG:-0}
[[ "$HOOKA_DEBUG" == 1 ]] && set -x
HOOKA=$(basename "$0")
HOOKA_DIR=$(dirname "$(realpath "$0")")
HOOKA_MOCK_HOOK="$HOOKA_DIR/lib/hooks"
# relative path setting where to store hooka settings and hooks
HOOKA_PATH=${HOOKA_PATH:-".hooka"}
# relative path setting where to store hookahs (hook scripts)
HOOKAH_PATH=${HOOKAH_PATH:-"hookah"}
HOOKAH_OPTS=("silent" "phony" "reticulate")
HOOKA_FORCE=${HOOKA_FORCE:-0}
HOOKA_GIT_CORE_HOOKS=${HOOKA_GIT_CORE_HOOKS:-0}
HOOKA_GIT_CORE_HOOKS_PATH=${HOOKA_GIT_CORE_HOOKS_PATH}
HOOKA_OPTS_IFS=${HOOKA_OPTS_IFS:-' '}
_ARGS=()
# no-parse args
_NP_ARGS=()
GIT_HOOKS=(
"applypatch-msg"
"commit-msg"
"fsmonitor-watchman"
"post-update"
"pre-applypatch"
"pre-commit"
"pre-push"
"pre-rebase"
"pre-receive"
"prepare-commit-msg"
"update"
)
# shellcheck source=lib/io.sh
source "$HOOKA_DIR/lib/io.sh"
# shellcheck source=lib/trap.sh
source "$HOOKA_DIR/lib/trap.sh"
# shellcheck source=lib/version.sh
source "$HOOKA_DIR/lib/version.sh"
# shellcheck source=lib/builtin.sh
source "$HOOKA_DIR/lib/builtin.sh"
function usage {
cat << EOF
.===. (
| | )
| | (
| | )
| \\*/
,' //.
:~~~~~//~~;
\`. // .'
sc\`-------'
$HOOKA: just add water
Usage: $HOOKA action [options...]
Options:
-V, --verbose echo every command that gets executed
-h, --help display this help
Commands:
init init hooka
- [git < 2.9] into .git/hooks
- [git ≥ 2.9] using git.core.hooksPath
update [git < 2.9] update hooka in .git/hooks
destroy remove hooka from your repo
- [git < 2.9] remove .git/hooks
- [git ≥ 2.9] unset git.core.hooksPath
add [hook] [name] add [name] hookah to [hook]
add [hook] [file] add (link) [file] into [hook]
edit [hook] edit [hook] index
edit [name] edit [name] hookah
run [hook] run [hook] hookahs
run [hook] [name] run [hook] [name] hookah only
help display this help
EOF
}
function parse_args {
# Do not parse a starting --help|-h as an action
# let it fail later. HOOKA --foo means "no action"
! [[ $1 =~ ^- ]] && ACTION=$1 && shift
while [[ $# -gt 0 ]]; do
case $1 in
-V|--verbose)
set -x
HOOKA_DEBUG=1
;;
-h|--help)
usage && exit
;;
-f|--force)
HOOKA_FORCE=1
;;
-)
_ARGS+=("$(cat "$2")")
shift
;;
--)
shift
_NP_ARGS+=("$@")
break
;;
*)
_ARGS+=("$1")
;;
esac
shift
done
}
function each_hook {
for hook in "${GIT_HOOKS[@]}"; do
"$@" "$HOOKA_GIT_DIR/hooks/$hook"
done
}
function install_hook {
local force=$1
local hook_file=$2
[[ -f $hook_file ]] && [[ $force == 0 ]] && \
warn "[-] $hook_file exists, ignoring" && return
ln -sF "$HOOKA_MOCK_HOOK/_hook" "$hook_file"
chmod u+x "$hook_file"
inf "[+] $hook_file"
}
function index_hookah {
local hook=$1
local hookah=$2
set -- "${@:3:$#}"
[[ -f "$HOOKA_PATH/$hook.index" ]] || > "$HOOKA_PATH/$hook.index" cat << EOF
#
# .===. (
# | | )
# | | ( $hook.index
# | | )
# | \\*/
# ,' //.
# :~~~~~//~~;
# \`. // .'
# sc\`-------'
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove or comment out a line here, that step WILL NOT BE RUN
# However, if you remove everything, changes will be aborted.
EOF
local line; line="$(IFS=$HOOKA_OPTS_IFS ; set -- "$hookah" "$@" ; echo "$*")"
lint_hookah "$line" || return 1
# XXX something better ? ?
printf "%s\n" "$line" >> "$HOOKA_PATH/$hook.index"
inf "[+] ${name}@${hook} $(vtfmt dim)$*$(vtfmt reset)"
}
function add_hookah { # 1: path, 2: file
[[ -f $2 ]] && ! force \
&& err! "[!] $2 exists, use $HOOKA edit $1, or $HOOKA add $1 --force"
# hookah is a real path to a file
if [[ -f $1 ]]; then
! [[ -x $1 ]] && err! "[!] $1 is a file, but is not executable"
ln -sF "$(realpath "$1")" "$2"
inf "[+] $1 -> $2 $(force && echo "(--force)")"
return
fi
local tmp
tmp=$(mktemp "/tmp/hookah.XXX")
on_exit "rm -f $tmp"
> "$tmp" cat << EOF
#!/usr/bin/env bash
# Write your own code here
# XXX TODO Helpers available are:
# ...
EOF
edit "$tmp" && empty "$tmp" && err! "[!] aborting due to empty file"
cp -f "$tmp" "$2" && chmod u+x "$2"
inf "[+] $2 $(force && echo "(--force)")"
}
function err_lint {
local err=$1; shift
local errvarn=$1; shift
local errvar=${!errvarn}
local pre="${*:1:(errvarn - 1)}"
local post="${*:(errvarn + 1):$#}"
local err_m=()
[[ -n $pre ]] && err_m+=("$pre")
err_m+=("$(vtfmt underline fg:red)$errvar$(vtfmt reset)")
[[ -n $post ]] && err_m+=("$post")
local spacing=''
[[ $errvarn -gt 1 ]] && spacing=$(printf '%*s' $((${#pre}+1)) '')
echo -e "${err_m[*]}"
echo -e "$spacing$(vtfmt fg:red)↳ $(vtfmt reset)$err$(vtfmt reset)"
}
function lint_hookah {
# shellcheck disable=SC2086
set -- $(_IFS=$IFS; IFS=$HOOKA_OPTS_IFS; set -- $1 ; IFS=$_IFS; echo "$*")
{ [[ -f $HOOKAH_PATH/$1 ]] && [[ -x $HOOKAH_PATH/$1 ]]; } || \
[[ -x "$1" ]] || hash "$1" &> /dev/null || {
err "$(err_lint "not something I can execute" 1 "$@")"
return 1
}
local i=2
while [[ $i -le $# ]]; do
# stop parsing if "--" and accept anything after
[[ ${!i} == "--" ]] && return 0
is_in ${!i} "${HOOKAH_OPTS[@]}" || {
err "$(err_lint "valid options: ${HOOKAH_OPTS[*]}" $i "$@")"
return 1
}
((i=i+1))
done
return 0
}
function edit_hooka {
local file="$1"
edit "$file" && empty "$file" && err! "[!] aborting due to empty file"
local err
while read -r line; do
# comment
if [[ "$line" =~ ^[[:space:]]*# ]]; then
continue
# whitespace
elif [[ "$line" =~ ^[[:space:]]*$ ]]; then
continue
else
lint_hookah "$line" || err=1
fi
done < "$file"
if [[ -n $err ]]; then
if confirm "[?] Want to go back to fix these"; then
edit_hooka "$file"
return $?
fi
confirm "[?] Want to save as it is" && err=0
fi
return $err
}
function hooked {
# XXX git < 2.9 has no core.hooksPath
if git_version_lt 2.9; then
return 1
fi
[[ "$(git config core.hooksPath)" == "$HOOKA_MOCK_HOOK" ]]
}
function main {
parse_args "$@"
set -- "${_ARGS[@]}"
case $ACTION in
help)
usage && return
;;
esac
HOOKA_GIT_DIR=$(git rev-parse --git-dir 2> /dev/null)
[[ -z $HOOKA_GIT_DIR ]] && err! "[!] $PWD is not a git repo"
HOOKA_GIT_CORE_HOOKS_PATH=${HOOKA_GIT_CORE_HOOKS_PATH:-$HOOKA_GIT_DIR/hooks}
# Convert relative path settings to absolute
HOOKA_PATH=$(git rev-parse --show-toplevel 3> /dev/null)/$HOOKA_PATH
HOOKAH_PATH=${HOOKA_PATH}/$HOOKAH_PATH
mkdir -p "$HOOKA_PATH" "$HOOKAH_PATH"
case $ACTION in
init)
# git < 2.9 has no core.hooksPath
if git_version_lt 2.9; then
warn "[!] git version < 2.9, overriding local hooks"
each_hook install_hook $HOOKA_FORCE
else
git config core.hooksPath "$HOOKA_MOCK_HOOK"
inf "[+] git config core.hooksPath -> $HOOKA_MOCK_HOOK"
fi
return
;;
esac
! hooked && warn "[!] hooka hookahs not hooked, try '$HOOKA init'"
case $ACTION in
destroy)
# git < 2.9 has no core.hooksPath
if git_version_lt 2.9; then
warn "[!] git version < 2.9"
confirm "This will remove hooks under .git/hooks, are you sure" \
&& each_hook rm -f
return
fi
git config --unset core.hooksPath
inf "[+] git config --unset core.hooksPath"
;;
add)
[[ -z $1 ]] && usage && exit 1
if [[ -z $2 ]]; then
add_hookah "$1" "$HOOKAH_PATH/$(basename "$1")"
return
fi
local name; name=$(basename "$2")
local file=$HOOKAH_PATH/$name
! [[ -f $file ]] && add_hookah "$2" "$file"
local index="$HOOKA_PATH/$1.index"
[[ -f $index ]] && grep -E "^$name(\w*|$)" "$index" &> /dev/null \
&& ! force \
&& err! "[!] $name already in $1 (use --force to add anyway)"
index_hookah "$1" "$name" "${@:3:$#}"
;;
edit)
if [[ -f "$HOOKA_PATH/$1.index" ]]; then
local tmp; tmp=$(mktemp "/tmp/hookah.XXX"); on_exit "rm -f $tmp"
cp "$HOOKA_PATH/$1.index" "$tmp"
edit_hooka "$tmp" && cp "$tmp" "$HOOKA_PATH/$1.index"
elif [[ -f "$HOOKAH_PATH/$1" ]]; then
edit "$HOOKAH_PATH/$1"
else
err! "[!] $1 or $1.index not found"
fi
;;
ls)
# XXX proper list, not this
[[ -z $1 ]] && ls -1 "$HOOKA_PATH"/*.index && return
cat "$HOOKA_PATH/$1.index"
;;
run)
if [[ -f $HOOKAH_PATH/$1 ]]; then
inf "[+] running $1"
"$HOOKAH_PATH/$1" "${_NP_ARGS[@]}" || err! "[!] $1 failed"
return
fi
[[ -f $HOOKA_PATH/$1.index ]] && while read -r line; do
[[ -z "$line" ]] && continue
IFS=$HOOKA_OPTS_IFS read -ra line <<< "$line"
local hookah=${line[0]}
local optsend=$(index "--" "${line[@]}" || echo ${#line})
local opts=("${line[@]:1:$optsend-1}")
local args=("${line[@]:$optsend+1:${#line}}")
# Run accepts a "hook step" second parameter, this filters it
[[ -n $2 ]] && [[ "$2" != "$hookah" ]] \
&& warn "[-] skipping $hookah" \
&& continue
inf "[+] running $hookah"
"$HOOKAH_PATH/$hookah" "${args[@]}" "${_NP_ARGS[@]}" || err! "[!] $hookah failed"
done < <(sed '/^#/d' "$HOOKA_PATH/$1.index")
! [[ $HOOKA_GIT_CORE_HOOKS == 1 ]] \
|| ! [[ -x $HOOKA_GIT_CORE_HOOKS_PATH/$1 ]] \
&& return
# Also run git core hook, whatever they are
inf "[+] running $HOOKA_GIT_CORE_HOOKS_PATH/$1"
"$HOOKA_GIT_CORE_HOOKS_PATH/$1" "${_NP_ARGS[@]}" \
|| err! "[!] $HOOKA_GIT_CORE_HOOKS_PATH/$1 failed"
;;
*)
usage
exit 1
;;
esac
}
# Allow sourcing of this script
if [[ $(basename "$(realpath "$0")") == "hooka" ]]; then
main "$@"
fi