-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·3521 lines (3208 loc) · 92.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·3521 lines (3208 loc) · 92.2 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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Installer for git hook runner toolkit. Creates shared runner, library, and
# stub dispatchers for the configured hook list using POSIX-compliant sh.
set -eu
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname "$0")" && pwd -P)
LIB_PATH="${SCRIPT_DIR}/lib/common.sh"
if [ ! -f "${LIB_PATH}" ]; then
printf '[hook-runner] ERROR: missing shared library at %s\n' "${LIB_PATH}" >&2
exit 1
fi
. "${LIB_PATH}"
CLI_ARGS_LIB="${SCRIPT_DIR}/lib/cli_args.sh"
if [ ! -f "${CLI_ARGS_LIB}" ]; then
printf '[hook-runner] ERROR: missing CLI helper at %s\n' "${CLI_ARGS_LIB}" >&2
exit 1
fi
. "${CLI_ARGS_LIB}"
EPHEMERAL_LIB="${SCRIPT_DIR}/lib/ephemeral_lifecycle.sh"
if [ ! -f "${EPHEMERAL_LIB}" ]; then
printf '[hook-runner] ERROR: missing ephemeral lifecycle library at %s\n' "${EPHEMERAL_LIB}" >&2
exit 1
fi
. "${EPHEMERAL_LIB}"
EPHEMERAL_OVERLAY_LIB="${SCRIPT_DIR}/lib/ephemeral_overlay.sh"
if [ ! -f "${EPHEMERAL_OVERLAY_LIB}" ]; then
printf '[hook-runner] ERROR: missing ephemeral overlay library at %s\n' "${EPHEMERAL_OVERLAY_LIB}" >&2
exit 1
fi
. "${EPHEMERAL_OVERLAY_LIB}"
CONFIGS_LIB="${SCRIPT_DIR}/lib/configs.sh"
if [ ! -f "${CONFIGS_LIB}" ]; then
printf '[hook-runner] ERROR: missing config library at %s\n' "${CONFIGS_LIB}" >&2
exit 1
fi
. "${CONFIGS_LIB}"
CONFIGS_REGISTRY_LIB="${SCRIPT_DIR}/lib/configs_registry.sh"
if [ ! -f "${CONFIGS_REGISTRY_LIB}" ]; then
printf '[hook-runner] ERROR: missing config registry at %s\n' "${CONFIGS_REGISTRY_LIB}" >&2
exit 1
fi
. "${CONFIGS_REGISTRY_LIB}"
print_usage() {
cat <<'HELP'
NAME
githooks - Manage shared Git hook runners and composable hook parts.
SYNOPSIS
githooks [GLOBAL OPTIONS] COMMAND [SUBCOMMAND] [ARGS]
DESCRIPTION
The toolkit installs a central runner under .githooks/, arranges Git hook
stubs to dispatch through it, and provides commands for staging, listing,
and removing hook parts. Commands accept --dry-run so you can inspect
planned actions before they modify the working tree.
Ephemeral Mode keeps toolkit assets inside the repository's .git
directory, letting you enable hooks without committing toolkit files.
Combine it with overlay controls to layer local automation alongside any
versioned hook directories. Use it when repository policy forbids tracked
tooling: all requirements are met with a writable Git worktree and a
POSIX-compliant shell. The companion guide at docs/ephemeral-mode.md covers
prerequisites, install steps, precedence rules, and uninstall workflows.
Use `githooks install --mode ephemeral --help` for CLI specifics and
`githooks uninstall --mode ephemeral --dry-run` to preview manifest-guided
cleanup before making changes.
TYPICAL FLOW
bootstrap (optional) -> install -> stage add -> configure hook configs -> hooks list (optional) -> update (as needed) -> uninstall (when retiring hooks)
For standard installs, commit .githooks/ after you are happy with the setup. For Ephemeral Mode, skip the commit step and edit configs under .git/.githooks/config instead.
GLOBAL OPTIONS
-h, --help
Show this overview or, when combined with a command, print its manual.
-V, --version
Display the toolkit version banner.
-n, --dry-run
Simulate filesystem changes, echoing the work that would be performed.
--mode MODE
Select installation mode. The default `standard` vendors files into
.githooks/. Use `ephemeral` to install under .git/.githooks/ while
leaving tracked files untouched; see docs/ephemeral-mode.md for details.
--repo PATH
Run commands against a target repository path instead of the current
working directory.
COMMAND OVERVIEW
bootstrap
Vendor the toolkit into the repository (for self-contained use).
install
Provision the runner, stubs, and default hook directories.
update
Refresh managed runner assets and staged hook parts without altering user configuration.
stage
Add, remove, or list staged hook parts sourced from directories.
hooks
Summarise hook coverage and staged parts across the repository.
config
Inspect Git configuration relevant to the runner, or update hooks-path.
uninstall
Remove managed stubs and shared runner artefacts.
help
Display contextual help for any command or subcommand.
LEGACY COMPATIBILITY
init Alias for install.
add Alias for stage add.
remove Alias for stage remove.
SEE ALSO
githooks help COMMAND
githooks COMMAND help
githooks COMMAND SUBCOMMAND --help
HELP
}
DEFAULT_HOOKS="post-merge post-rewrite post-checkout pre-commit prepare-commit-msg commit-msg post-commit pre-push"
ALL_HOOKS="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-merge-commit pre-rebase post-checkout post-merge post-rewrite pre-push pre-receive update post-receive post-update reference-transaction push-to-checkout proc-receive pre-auto-gc post-index-change sendemail-validate fsmonitor-watchman p4-changelist p4-prepare-changelist p4-post-changelist p4-pre-submit p4-post-submit"
MODE="install"
DRY_RUN=0
FORCE=0
HOOKS="${DEFAULT_HOOKS}"
USE_ALL=0
HOOKS_WERE_EXPLICIT=0
REQUEST_STAGE=0
REQUEST_UNINSTALL=0
CLI_INSTALL_MODE="standard"
REPO_OVERRIDE=""
BOOTSTRAP_ITEMS="install.sh _runner.sh lib examples hooks tui docs README.md CHANGELOG.md"
TOOLKIT_VERSION="${TOOLKIT_VERSION:-0.3.0}"
COMPAT_WARNED=0
STAGE_LIST_HEADER_SHOWN=0
STAGE_SOURCES_ORDERED=""
STAGE_HOOK_FILTERS_ORDERED=""
STAGE_NAME_FILTERS_ORDERED=""
STAGE_ORDER_STRATEGY="source"
STAGE_SUMMARY=0
STAGE_LEGACY_USED=0
STAGE_RUNNER_READY=0
STAGE_COPY_COUNT=0
STAGE_SKIP_IDENTICAL=0
STAGE_SKIPPED_FILES=0
STAGE_DETECTED_HOOKS=""
STAGE_DETECTED_SOURCE="none"
STAGE_PLAN_FILE=""
STAGE_PLAN_COUNT=0
STAGE_PLANNED_DESTS=""
UPDATE_REFRESHED_STUBS=0
UPDATE_PARTS_REFRESHED=0
UPDATE_PARTS_SKIPPED_IDENTICAL=0
UPDATE_PARTS_SOURCE_MISSING=0
UPDATE_SUPPORT_REFRESHED=0
UPDATE_REFRESH_CONFIGS=0
parse_hook_list() {
if [ "$#" -ne 1 ]; then
githooks_die "--hooks requires an argument"
fi
_raw_hooks=$1
HOOKS=""
_saved_ifs=$IFS
IFS=','
set -f
set -- ${_raw_hooks}
set +f
IFS=${_saved_ifs}
for _hook_item in "$@"; do
_trimmed=$(printf '%s' "${_hook_item}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${_trimmed}" ]; then
if [ -z "${HOOKS}" ]; then
HOOKS="${_trimmed}"
else
HOOKS="${HOOKS} ${_trimmed}"
fi
fi
done
if [ -z "${HOOKS}" ]; then
githooks_die "--hooks requires at least one hook name"
fi
}
log_action() {
githooks_log_info "$1"
}
maybe_run() {
maybe_description=$1
shift
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: ${maybe_description}"
return 0
fi
log_action "${maybe_description}"
"$@"
}
ensure_parent_dir() {
ensure_path=$1
ensure_dir=$(dirname "${ensure_path}")
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: ensure directory ${ensure_dir}"
return 0
fi
githooks_mkdir_p "${ensure_dir}"
}
install_runner() {
shared_root=$(githooks_shared_root)
runner_target="${shared_root%/}/_runner.sh"
lib_target="${shared_root%/}/lib/common.sh"
watch_lib_target="${shared_root%/}/lib/watch-configured-actions.sh"
hooks_lib_dir="${hooks_root%/}/lib"
hooks_lib_target="${hooks_lib_dir%/}/common.sh"
hooks_watch_lib_target="${hooks_lib_dir%/}/watch-configured-actions.sh"
if [ "${shared_root_is_ephemeral:-0}" -eq 1 ]; then
return 0
fi
runner_target_absolute=$(githooks_absolute_path "${runner_target}")
maybe_run "create shared directory ${shared_root}" githooks_mkdir_p "${shared_root}"
maybe_run "create library directory ${shared_root%/}/lib" githooks_mkdir_p "${shared_root%/}/lib"
maybe_run "create hooks library directory ${hooks_lib_dir}" githooks_mkdir_p "${hooks_lib_dir}"
maybe_run "copy runner to .git/hooks/_runner.sh" githooks_copy_file "${SCRIPT_DIR}/_runner.sh" "${hooks_root%/}/_runner.sh"
maybe_run "copy shared library to ${lib_target}" githooks_copy_file "${SCRIPT_DIR}/lib/common.sh" "${lib_target}"
maybe_run "copy watch-configured-actions library to ${watch_lib_target}" githooks_copy_file "${SCRIPT_DIR}/lib/watch-configured-actions.sh" "${watch_lib_target}"
maybe_run "copy shared library to ${hooks_lib_target}" githooks_copy_file "${SCRIPT_DIR}/lib/common.sh" "${hooks_lib_target}"
maybe_run "copy watch-configured-actions library to ${hooks_watch_lib_target}" githooks_copy_file "${SCRIPT_DIR}/lib/watch-configured-actions.sh" "${hooks_watch_lib_target}"
maybe_run "chmod runner ${hooks_root%/}/_runner.sh" githooks_chmod 755 "${hooks_root%/}/_runner.sh"
}
write_stub() {
stub_hook=$1
stub_path="${hooks_root%/}/${stub_hook}"
runner_target_absolute="${hooks_root%/}/_runner.sh"
if [ -e "${stub_path}" ] && [ "${FORCE}" -eq 0 ]; then
githooks_log_warn "stub exists for ${stub_hook}; use --force to overwrite"
return 0
fi
ensure_parent_dir "${stub_path}"
if [ "${DRY_RUN}" -eq 1 ]; then
return 0
fi
tmp_stub="${stub_path}.tmp"
githooks_stub_body "${runner_target_absolute}" "${stub_hook}" >"${tmp_stub}"
mv "${tmp_stub}" "${stub_path}"
githooks_chmod 755 "${stub_path}"
}
uninstall_stub() {
unstub_hook=$1
stub_path="${hooks_root%/}/${unstub_hook}"
if [ ! -f "${stub_path}" ]; then
return 0
fi
if ! grep -q 'generated by .githooks-runner' "${stub_path}" 2>/dev/null; then
githooks_log_warn "skipping ${unstub_hook}; not managed by runner"
return 0
fi
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove stub ${stub_path}"
return 0
fi
rm -f "${stub_path}"
githooks_log_info "removed ${stub_path}"
}
create_parts_dir() {
parts_hook=$1
parts_dir="${shared_root%/}/${parts_hook}.d"
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: ensure parts directory ${parts_dir}"
return 0
fi
githooks_mkdir_p "${parts_dir}"
}
bootstrap_copy_item() {
bootstrap_item=$1
bootstrap_src="${BOOTSTRAP_SOURCE%/}/${bootstrap_item}"
bootstrap_dst="${BOOTSTRAP_TARGET%/}/${bootstrap_item}"
if [ ! -e "${bootstrap_src}" ]; then
githooks_log_warn "bootstrap skip missing ${bootstrap_item}"
return 0
fi
if [ -d "${bootstrap_src}" ]; then
maybe_run "copy ${bootstrap_item} directory" cp -R "${bootstrap_src}" "${bootstrap_dst}"
else
maybe_run "copy ${bootstrap_item}" githooks_copy_file "${bootstrap_src}" "${bootstrap_dst}"
fi
}
bootstrap_chmod_if_exists() {
bootstrap_path=$1
bootstrap_mode=$2
if [ -f "${bootstrap_path}" ]; then
maybe_run "chmod ${bootstrap_mode} ${bootstrap_path}" githooks_chmod "${bootstrap_mode}" "${bootstrap_path}"
fi
}
remove_runner_files() {
if [ "${shared_root_is_ephemeral:-0}" -eq 1 ]; then
if [ "${DRY_RUN}" -eq 1 ]; then
githooks_log_info "DRY-RUN: no managed runner artefacts to remove (ephemeral mode)"
else
githooks_log_info "no managed runner artefacts removed (ephemeral mode uses .git/.githooks)"
fi
return 0
fi
runner_source="${SCRIPT_DIR%/}/_runner.sh"
lib_source="${SCRIPT_DIR%/}/lib/common.sh"
watch_lib_source="${SCRIPT_DIR%/}/lib/watch-configured-actions.sh"
hooks_lib_source_target="${hooks_lib_target}"
hooks_watch_lib_source_target="${hooks_watch_lib_target}"
planned_removal=0
performed_removal=0
if [ -f "${runner_target}" ]; then
if [ -f "${runner_source}" ] && [ "${runner_target}" -ef "${runner_source}" ]; then
githooks_log_info "skip removing ${runner_target}; shared with installer source"
else
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove ${runner_target}"
planned_removal=1
else
rm -f "${runner_target}"
performed_removal=1
githooks_log_info "removed ${runner_target}"
fi
fi
fi
if [ -f "${lib_target}" ]; then
if [ -f "${lib_source}" ] && [ "${lib_target}" -ef "${lib_source}" ]; then
githooks_log_info "skip removing ${lib_target}; shared with installer source"
else
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove ${lib_target}"
planned_removal=1
else
rm -f "${lib_target}"
performed_removal=1
githooks_log_info "removed ${lib_target}"
fi
fi
fi
if [ -f "${watch_lib_target}" ]; then
if [ -f "${watch_lib_source}" ] && [ "${watch_lib_target}" -ef "${watch_lib_source}" ]; then
githooks_log_info "skip removing ${watch_lib_target}; shared with installer source"
else
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove ${watch_lib_target}"
planned_removal=1
else
rm -f "${watch_lib_target}"
performed_removal=1
githooks_log_info "removed ${watch_lib_target}"
fi
fi
fi
if [ -f "${hooks_lib_source_target}" ]; then
if [ -f "${lib_source}" ] && [ "${hooks_lib_source_target}" -ef "${lib_source}" ]; then
githooks_log_info "skip removing ${hooks_lib_source_target}; shared with installer source"
else
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove ${hooks_lib_source_target}"
planned_removal=1
else
rm -f "${hooks_lib_source_target}"
performed_removal=1
githooks_log_info "removed ${hooks_lib_source_target}"
fi
fi
fi
if [ -f "${hooks_watch_lib_source_target}" ]; then
if [ -f "${watch_lib_source}" ] && [ "${hooks_watch_lib_source_target}" -ef "${watch_lib_source}" ]; then
githooks_log_info "skip removing ${hooks_watch_lib_source_target}; shared with installer source"
else
if [ "${DRY_RUN}" -eq 1 ]; then
log_action "DRY-RUN: remove ${hooks_watch_lib_source_target}"
planned_removal=1
else
rm -f "${hooks_watch_lib_source_target}"
performed_removal=1
githooks_log_info "removed ${hooks_watch_lib_source_target}"
fi
fi
fi
if [ "${DRY_RUN}" -eq 1 ]; then
if [ "${planned_removal}" -eq 0 ]; then
githooks_log_info "DRY-RUN: no managed runner artefacts to remove"
fi
return 0
fi
if [ "${performed_removal}" -eq 1 ]; then
githooks_log_info "removed runner artefacts from ${shared_root}"
else
githooks_log_info "no runner artefacts removed"
fi
}
stage_append_unique() {
stage_var=$1
stage_value=$2
if [ -z "${stage_value}" ]; then
return 0
fi
eval "stage_current=\${${stage_var}}"
for stage_item in ${stage_current}; do
if [ "${stage_item}" = "${stage_value}" ]; then
return 0
fi
done
if [ -z "${stage_current}" ]; then
eval "${stage_var}='${stage_value}'"
else
eval "${stage_var}='${stage_current} ${stage_value}'"
fi
}
stage_list_contains() {
stage_list=$1
stage_seek=$2
stage_saved_ifs=$IFS
IFS=$' \t\n'
set -f
set -- ${stage_list}
set +f
IFS=${stage_saved_ifs}
for stage_item in "$@"; do
if [ "${stage_item}" = "${stage_seek}" ]; then
return 0
fi
done
return 1
}
stage_resolve_source_dir() {
stage_raw=$1
if [ -z "${stage_raw}" ]; then
printf '%s' "${SCRIPT_DIR}"
return 0
fi
case "${stage_raw}" in
/*)
printf '%s' "${stage_raw}"
;;
~*)
stage_rest=${stage_raw#~}
printf '%s/%s' "${HOME}" "${stage_rest#/}"
;;
*)
printf '%s/%s' "${SCRIPT_DIR%/}" "${stage_raw}"
;;
esac
}
stage_append_ordered() {
stage_var=$1
stage_value=$2
if [ -z "${stage_value}" ]; then
return 0
fi
eval "stage_current=\${${stage_var}}"
for stage_item in ${stage_current}; do
if [ "${stage_item}" = "${stage_value}" ]; then
return 0
fi
done
if [ -z "${stage_current}" ]; then
eval "${stage_var}='${stage_value}'"
else
eval "${stage_var}='${stage_current} ${stage_value}'"
fi
}
stage_add_source() {
stage_raw=$1
case "${stage_raw}" in
''|all)
stage_add_source examples
stage_add_source hooks
return 0
;;
examples)
stage_path="${SCRIPT_DIR%/}/examples"
;;
hooks)
stage_path="${SCRIPT_DIR%/}/hooks"
;;
*)
stage_path=$(stage_resolve_source_dir "${stage_raw}")
;;
esac
stage_append_ordered STAGE_SOURCES_ORDERED "${stage_path}"
}
stage_add_hook() {
stage_hook=$1
if [ -z "${stage_hook}" ]; then
return 0
fi
case "${stage_hook}" in
*,*)
stage_saved_ifs=$IFS
IFS=','
set -f
set -- ${stage_hook}
set +f
IFS=${stage_saved_ifs}
for stage_piece in "$@"; do
stage_trim=$(printf '%s' "${stage_piece}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_trim}" ]; then
stage_add_hook "${stage_trim}"
fi
done
return 0
;;
esac
stage_trim=$(printf '%s' "${stage_hook}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_trim}" ]; then
stage_append_ordered STAGE_HOOK_FILTERS_ORDERED "${stage_trim}"
fi
}
stage_add_name() {
stage_name=$1
if [ -z "${stage_name}" ]; then
return 0
fi
case "${stage_name}" in
*,*)
stage_saved_ifs=$IFS
IFS=','
set -f
set -- ${stage_name}
set +f
IFS=${stage_saved_ifs}
for stage_piece in "$@"; do
stage_trim=$(printf '%s' "${stage_piece}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_trim}" ]; then
stage_add_name "${stage_trim}"
fi
done
return 0
;;
esac
stage_trim=$(printf '%s' "${stage_name}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_trim}" ]; then
stage_append_ordered STAGE_NAME_FILTERS_ORDERED "${stage_trim}"
fi
}
stage_parse_legacy_selector() {
stage_token=$1
stage_trimmed=$(printf '%s' "${stage_token}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -z "${stage_trimmed}" ]; then
return 0
fi
STAGE_LEGACY_USED=1
case "${stage_trimmed}" in
all|examples|hooks)
stage_add_source "${stage_trimmed}"
;;
source:*)
stage_add_source "${stage_trimmed#source:}"
;;
hook:*)
stage_add_hook "${stage_trimmed#hook:}"
;;
name:*)
stage_add_name "${stage_trimmed#name:}"
;;
*)
stage_add_hook "${stage_trimmed}"
;;
esac
}
stage_handle_legacy_list() {
stage_list=$1
if [ -z "${stage_list}" ]; then
stage_parse_legacy_selector "all"
return 0
fi
stage_saved_ifs=$IFS
IFS=','
set -f
set -- ${stage_list}
set +f
IFS=${stage_saved_ifs}
if [ $# -eq 0 ]; then
stage_parse_legacy_selector "${stage_list}"
return 0
fi
for stage_item in "$@"; do
stage_parse_legacy_selector "${stage_item}"
done
}
stage_ensure_default_sources() {
if [ -n "${STAGE_SOURCES_ORDERED}" ]; then
return 0
fi
stage_add_source examples
stage_add_source hooks
return 0
}
stage_display_source() {
stage_source_path=$1
case "${stage_source_path}" in
"${SCRIPT_DIR%/}/examples")
printf '%s' 'examples'
;;
"${SCRIPT_DIR%/}/hooks")
printf '%s' 'hooks'
;;
*)
printf '%s' "${stage_source_path}"
;;
esac
}
stage_prepare_plan_file() {
if [ -n "${STAGE_PLAN_FILE}" ] && [ -f "${STAGE_PLAN_FILE}" ]; then
:
else
STAGE_PLAN_FILE=$(mktemp "${TMPDIR:-/tmp}/githooks-stage-plan.XXXXXX") || githooks_die 'failed to create stage plan file'
fi
STAGE_PLAN_COUNT=0
STAGE_PLANNED_DESTS=""
}
stage_cleanup_plan() {
if [ -n "${STAGE_PLAN_FILE}" ] && [ -f "${STAGE_PLAN_FILE}" ]; then
rm -f "${STAGE_PLAN_FILE}"
fi
STAGE_PLAN_FILE=""
}
stage_register_destination() {
stage_dest=$1
if [ -z "${stage_dest}" ]; then
return 1
fi
stage_found=0
stage_new=""
for stage_item in ${STAGE_PLANNED_DESTS}; do
if [ "${stage_item}" = "${stage_dest}" ]; then
stage_found=1
if [ "${FORCE}" -eq 1 ]; then
continue
fi
fi
if [ -z "${stage_item}" ]; then
continue
fi
if [ -z "${stage_new}" ]; then
stage_new="${stage_item}"
else
stage_new="${stage_new} ${stage_item}"
fi
done
if [ "${stage_found}" -eq 1 ] && [ "${FORCE}" -eq 0 ]; then
STAGE_PLANNED_DESTS="${stage_new}"
return 1
fi
if [ -z "${stage_new}" ]; then
STAGE_PLANNED_DESTS="${stage_dest}"
else
STAGE_PLANNED_DESTS="${stage_new} ${stage_dest}"
fi
return 0
}
stage_relative_path() {
stage_file=$1
stage_source=$2
case "${stage_file}" in
"${stage_source%/}/"*)
printf '%s' "${stage_file#${stage_source%/}/}"
;;
*)
printf '%s' "$(basename "${stage_file}")"
;;
esac
}
stage_add_plan_entry() {
stage_hook=$1
stage_name=$2
stage_source=$3
stage_file=$4
stage_rel=$(stage_relative_path "${stage_file}" "${stage_source}")
parts_dir=$(githooks_parts_dir "${stage_hook}")
stage_dest="${parts_dir%/}/${stage_name}"
if ! stage_register_destination "${stage_dest}"; then
githooks_log_info "stage skip ${stage_name}: duplicate destination for ${stage_hook}"
return 0
fi
stage_prepare_plan_file
printf '%s|%s|%s|%s|%s\n' "${stage_hook}" "${stage_name}" "${stage_source}" "${stage_rel}" "${stage_file}" >>"${STAGE_PLAN_FILE}" || githooks_die "failed to write stage plan"
STAGE_PLAN_COUNT=$((STAGE_PLAN_COUNT + 1))
}
stage_register_support_assets() {
if [ "$#" -ne 2 ]; then
return 0
fi
support_name=$1
githooks_config_require_for_part "${support_name}"
}
stage_install_required_configs() {
githooks_config_install_required
}
stage_summary_requested() {
if [ "${STAGE_SUMMARY}" -eq 1 ] || [ "${DRY_RUN}" -eq 1 ]; then
return 0
fi
return 1
}
stage_sort_plan_if_needed() {
if [ -z "${STAGE_PLAN_FILE}" ] || [ ! -f "${STAGE_PLAN_FILE}" ]; then
return 0
fi
case "${STAGE_ORDER_STRATEGY}" in
hook)
stage_tmp=$(mktemp "${TMPDIR:-/tmp}/githooks-stage-plan-sort.XXXXXX") || githooks_die 'failed to create sort scratch'
LC_ALL=C sort -t '|' -k1,1 -k2,2 "${STAGE_PLAN_FILE}" >"${stage_tmp}"
mv "${stage_tmp}" "${STAGE_PLAN_FILE}"
;;
name)
stage_tmp=$(mktemp "${TMPDIR:-/tmp}/githooks-stage-plan-sort.XXXXXX") || githooks_die 'failed to create sort scratch'
LC_ALL=C sort -t '|' -k2,2 -k1,1 "${STAGE_PLAN_FILE}" >"${stage_tmp}"
mv "${stage_tmp}" "${STAGE_PLAN_FILE}"
;;
*)
:
;;
esac
}
stage_emit_plan_summary() {
if stage_summary_requested; then
:
else
return 0
fi
if [ -z "${STAGE_PLAN_FILE}" ] || [ ! -f "${STAGE_PLAN_FILE}" ]; then
return 0
fi
while IFS='|' read -r stage_hook stage_name stage_source stage_rel stage_file; do
[ -n "${stage_hook}" ] || continue
stage_label=$(stage_display_source "${stage_source}")
githooks_log_info "PLAN: hook=${stage_hook} name=${stage_name} source=${stage_label} rel=${stage_rel}"
done <"${STAGE_PLAN_FILE}"
}
stage_is_hook_like() {
if [ "$#" -ne 1 ]; then
return 1
fi
stage_candidate=$1
case "${stage_candidate}" in
*-*)
;;
*)
return 1
;;
esac
case "${stage_candidate}" in
*[!A-Za-z0-9-]*)
return 1
;;
esac
return 0
}
stage_hooks_from_directory() {
stage_file=$1
stage_root=$2
STAGE_TMP_DIR_HOOKS=""
stage_norm_root=${stage_root%/}
stage_rel=${stage_file}
case "${stage_file}" in
"${stage_norm_root}"/*)
stage_rel=${stage_file#${stage_norm_root}/}
;;
esac
stage_rel_dir=$(dirname "${stage_rel}")
if [ "${stage_rel_dir}" = "." ]; then
stage_rel_dir=""
fi
stage_parent=""
if [ -n "${stage_rel_dir}" ]; then
stage_parent=$(basename "${stage_rel_dir}")
fi
stage_grand_dir=""
if [ -n "${stage_rel_dir}" ]; then
stage_grand_dir=$(dirname "${stage_rel_dir}")
if [ "${stage_grand_dir}" = "." ]; then
stage_grand_dir=""
fi
fi
stage_grandparent=""
if [ -n "${stage_grand_dir}" ]; then
stage_grandparent=$(basename "${stage_grand_dir}")
fi
if [ -n "${stage_parent}" ]; then
case "${stage_parent}" in
hooks|examples)
;;
*.d)
stage_candidate=${stage_parent%.d}
if [ -n "${stage_candidate}" ]; then
stage_append_unique STAGE_TMP_DIR_HOOKS "${stage_candidate}"
fi
;;
*)
if stage_is_hook_like "${stage_parent}"; then
stage_append_unique STAGE_TMP_DIR_HOOKS "${stage_parent}"
fi
;;
esac
fi
if [ -n "${stage_grandparent}" ]; then
case "${stage_grandparent}" in
hooks|examples)
;;
*.d)
stage_candidate=${stage_grandparent%.d}
if [ -n "${stage_candidate}" ]; then
stage_append_unique STAGE_TMP_DIR_HOOKS "${stage_candidate}"
fi
;;
*)
if stage_is_hook_like "${stage_grandparent}"; then
stage_append_unique STAGE_TMP_DIR_HOOKS "${stage_grandparent}"
fi
;;
esac
fi
stage_result=${STAGE_TMP_DIR_HOOKS}
STAGE_TMP_DIR_HOOKS=""
printf '%s' "${stage_result}"
}
stage_detect_hooks_for_file() {
stage_file=$1
stage_root=$2
STAGE_DETECTED_HOOKS=""
STAGE_DETECTED_SOURCE="none"
stage_metadata_hooks=""
stage_metadata_found=0
stage_nonempty=0
stage_line_total=0
while IFS= read -r stage_line || [ -n "${stage_line}" ]; do
stage_line_total=$((stage_line_total + 1))
stage_trimmed=$(printf '%s' "${stage_line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_trimmed}" ]; then
stage_nonempty=$((stage_nonempty + 1))
fi
case "${stage_trimmed}" in
'# githooks-stage:'*)
stage_values=${stage_trimmed#\# githooks-stage:}
stage_values=$(printf '%s' "${stage_values}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr ',' ' ')
for stage_value in ${stage_values}; do
stage_token=$(printf '%s' "${stage_value}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${stage_token}" ]; then
stage_append_unique stage_metadata_hooks "${stage_token}"
fi
done
stage_metadata_found=1
break
;;
esac
if [ "${stage_nonempty}" -ge 20 ]; then
break
fi
if [ "${stage_line_total}" -ge 200 ]; then
break
fi
done < "${stage_file}"
if [ "${stage_metadata_found}" -eq 1 ] && [ -n "${stage_metadata_hooks}" ]; then
STAGE_DETECTED_HOOKS="${stage_metadata_hooks}"
STAGE_DETECTED_SOURCE="metadata"
return 0
fi
stage_dir_hooks=$(stage_hooks_from_directory "${stage_file}" "${stage_root}")
if [ -n "${stage_dir_hooks}" ]; then
STAGE_DETECTED_HOOKS="${stage_dir_hooks}"
STAGE_DETECTED_SOURCE="directory"
return 0
fi
STAGE_DETECTED_HOOKS=""
STAGE_DETECTED_SOURCE="none"
return 1
}
stage_normalise_hooks() {
stage_input=$1
STAGE_TMP_UNIQUE=""
for stage_item in ${stage_input}; do
stage_append_unique STAGE_TMP_UNIQUE "${stage_item}"
done
stage_result=${STAGE_TMP_UNIQUE}
STAGE_TMP_UNIQUE=""
printf '%s' "${stage_result}"
}
stage_hook_allowed() {
stage_hook=$1
if [ -n "${STAGE_HOOK_FILTERS_ORDERED}" ]; then
if stage_list_contains "${STAGE_HOOK_FILTERS_ORDERED}" "${stage_hook}"; then
:
else
return 1
fi
fi
if [ "${HOOKS_WERE_EXPLICIT}" -eq 1 ]; then
if stage_list_contains "${HOOKS}" "${stage_hook}"; then
:
else
return 1
fi
fi
return 0
}
stage_name_matches_filter() {
stage_candidate=$1
stage_filter=$2
case "${stage_candidate}" in
${stage_filter})
return 0
;;
esac
stage_candidate_noext=${stage_candidate%.sh}
if [ "${stage_candidate_noext}" != "${stage_candidate}" ]; then
case "${stage_candidate_noext}" in
${stage_filter})
return 0
;;
esac
fi
return 1
}
stage_name_allowed() {
stage_basename=$1
if [ -z "${STAGE_NAME_FILTERS_ORDERED}" ]; then
return 0
fi
for stage_name_filter in ${STAGE_NAME_FILTERS_ORDERED}; do
if stage_name_matches_filter "${stage_basename}" "${stage_name_filter}"; then
return 0
fi
done
return 1
}
stage_ensure_runner() {
if [ "${shared_root_is_ephemeral:-0}" -eq 1 ]; then
return 0
fi
if [ "${STAGE_RUNNER_READY}" -eq 0 ]; then
install_runner
STAGE_RUNNER_READY=1
fi
}