-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcli.sh
More file actions
3118 lines (2664 loc) · 97.5 KB
/
Copy pathcli.sh
File metadata and controls
3118 lines (2664 loc) · 97.5 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
#
# Command line helper for https://github.com/rycus86/githooks
#
# This tool provides a convenience utility to manage
# Githooks configuration, hook files and other
# related functionality.
# This script should be an alias for `git hooks`, done by
# git config --global alias.hooks "!${SCRIPT_DIR}/githooks"
#
# See the documentation in the project README for more information,
# or run the `git hooks help` command for available options.
#
# Legacy version number. Not used anymore, but old installs read it.
# Version: 9912.310000-000000
#####################################################
# Prints the command line help for usage and
# available commands.
#####################################################
print_help() {
print_help_header
echo "
Available commands:
disable Disables a hook in the current repository
enable Enables a previously disabled hook in the current repository
accept Accepts the pending changes of a new or modified hook
exec Executes a hook script on demand
trust Manages settings related to trusted repositories
list Lists the active hooks in the current repository
shared Manages the shared hook repositories
install Installs the latest Githooks hooks
uninstall Uninstalls the Githooks hooks
update Performs an update check
readme Manages the Githooks README in the current repository
ignore Manages Githooks ignore files in the current repository
config Manages various Githooks configuration
tools Manages script folders for tools
version Prints the version number of this script
help Prints this help message
You can also execute \`git hooks <cmd> help\` for more information on the individual commands.
"
}
#####################################################
# Prints a general header to be included
# as the first few lines of every help message.
#####################################################
print_help_header() {
echo
echo "Githooks - https://github.com/rycus86/githooks"
echo "----------------------------------------------"
}
#####################################################
# Sets the ${INSTALL_DIR} variable.
#
# Returns: None
#####################################################
load_install_dir() {
INSTALL_DIR=$(git config --global --get githooks.installDir)
if [ -z "${INSTALL_DIR}" ]; then
# install dir not defined, use default
INSTALL_DIR=~/".githooks"
elif [ ! -d "$INSTALL_DIR" ]; then
echo "! Githooks installation is corrupt! " >&2
echo " Install directory at ${INSTALL_DIR} is missing." >&2
INSTALL_DIR=~/".githooks"
echo " Falling back to default directory at ${INSTALL_DIR}" >&2
echo " Please run the Githooks install script again to fix it." >&2
fi
GITHOOKS_CLONE_DIR="$INSTALL_DIR/release"
}
#####################################################
# Set up the main variables that
# we will throughout the hook.
#
# Sets the ${CURRENT_GIT_DIR} variable
#
# Returns: None
#####################################################
set_main_variables() {
CURRENT_GIT_DIR=$(git rev-parse --git-common-dir 2>/dev/null)
if [ "${CURRENT_GIT_DIR}" = "--git-common-dir" ]; then
CURRENT_GIT_DIR=".git"
fi
load_install_dir
# Global IFS for loops
IFS_NEWLINE="
"
}
#####################################################
# Checks if the current directory is
# a Git repository or not.
# Returns:
# 0 if it is likely a Git repository,
# 1 otherwise
#####################################################
is_running_in_git_repo_root() {
git rev-parse >/dev/null 2>&1 || return 1
[ -d "${CURRENT_GIT_DIR}" ] || return 1
}
#####################################################
# Echo if the current repository is non-bare.
#
# Returns: 0
#####################################################
echo_if_non_bare_repo() {
if [ "$(git rev-parse --is-bare-repository 2>/dev/null)" = "false" ]; then
echo "$@"
fi
return 0
}
#####################################################
# Finds a hook file path based on trigger name,
# file name, relative or absolute path, or
# some combination of these, intended to
# enable or disable exactly one hook found.
#
# Sets the ${HOOK_PATH} environment variable.
#
# Returns:
# 0 on success, 1 when no hooks found
#####################################################
find_hook_path_to_enable_or_disable() {
if [ "$1" = "--shared" ]; then
shift
if [ -z "$1" ]; then
echo "! For shared repositories, either the trigger type" >&2
echo " the hook name or both needs to be given" >&2
return 1
fi
if [ ! -d "$INSTALL_DIR/shared" ]; then
echo "! No shared repositories found" >&2
return 1
fi
IFS="$IFS_NEWLINE"
for SHARED_ROOT in "$INSTALL_DIR/shared/"*; do
unset IFS
if [ ! -d "$SHARED_ROOT" ]; then
continue
fi
REMOTE_URL=$(git -C "$SHARED_ROOT" config --get remote.origin.url)
if [ -f "$(pwd)/.githooks/.shared" ]; then
SHARED_LOCAL_REPOS_LIST=$(grep -E "^[^#\n\r ].*$" <"$(pwd)/.githooks/.shared")
ACTIVE_LOCAL_REPO=$(echo "$SHARED_LOCAL_REPOS_LIST" | grep -F -o "$REMOTE_URL")
else
ACTIVE_LOCAL_REPO=""
fi
ACTIVE_GLOBAL_REPO=$(git config --global --get-all githooks.shared | grep -o "$REMOTE_URL")
if [ "$ACTIVE_LOCAL_REPO" != "$REMOTE_URL" ] && [ "$ACTIVE_GLOBAL_REPO" != "$REMOTE_URL" ]; then
continue
fi
if [ -n "$1" ] && [ -n "$2" ]; then
if [ -f "$SHARED_ROOT/.githooks/$1/$2" ]; then
HOOK_PATH="$SHARED_ROOT/.githooks/$1/$2"
return
elif [ -f "$SHARED_ROOT/$1/$2" ]; then
HOOK_PATH="$SHARED_ROOT/$1/$2"
return
fi
elif [ -d "$SHARED_ROOT/.githooks" ]; then
HOOK_PATH=$(find "$SHARED_ROOT/.githooks" -name "$1" | head -1)
[ -n "$HOOK_PATH" ] && return 0 || return 1
else
HOOK_PATH=$(find "$SHARED_ROOT" -name "$1" | head -1)
[ -n "$HOOK_PATH" ] && return 0 || return 1
fi
IFS="$IFS_NEWLINE"
done
echo "! Sorry, cannot find any shared hooks that would match that" >&2
return 1
fi
if [ -z "$1" ]; then
HOOK_PATH=$(cd .githooks && pwd)
elif [ -n "$1" ] && [ -n "$2" ]; then
HOOK_TARGET="$(pwd)/.githooks/$1/$2"
if [ -e "$HOOK_TARGET" ]; then
HOOK_PATH="$HOOK_TARGET"
fi
elif [ -n "$1" ]; then
if [ -e "$1" ]; then
HOOK_DIR=$(dirname "$1")
HOOK_NAME=$(basename "$1")
if [ "$HOOK_NAME" = "." ]; then
HOOK_PATH=$(cd "$HOOK_DIR" && pwd)
else
HOOK_PATH=$(cd "$HOOK_DIR" && pwd)/"$HOOK_NAME"
fi
elif [ -f ".githooks/$1" ]; then
HOOK_PATH=$(cd .githooks && pwd)/"$1"
else
for HOOK_DIR in .githooks/*; do
HOOK_ITEM=$(basename "$HOOK_DIR")
if [ "$HOOK_ITEM" = "$1" ]; then
HOOK_PATH=$(cd "$HOOK_DIR" && pwd)
fi
if [ ! -d "$HOOK_DIR" ]; then
continue
fi
HOOK_DIR=$(cd "$HOOK_DIR" && pwd)
IFS="$IFS_NEWLINE"
for HOOK_FILE in "$HOOK_DIR"/*; do
unset IFS
HOOK_ITEM=$(basename "$HOOK_FILE")
if [ "$HOOK_ITEM" = "$1" ]; then
HOOK_PATH="$HOOK_FILE"
fi
IFS="$IFS_NEWLINE"
done
done
fi
fi
if [ -z "$HOOK_PATH" ]; then
echo "! Sorry, cannot find any hooks that would match that" >&2
return 1
elif echo "$HOOK_PATH" | grep -F -qv "/.githooks"; then
if [ -d "$HOOK_PATH/.githooks" ]; then
HOOK_PATH="$HOOK_PATH/.githooks"
else
echo "! Sorry, cannot find any hooks that would match that" >&2
return 1
fi
fi
}
#####################################################
# Finds a hook file path based on trigger name,
# file name, relative or absolute path, or
# some combination of these, intended to
# loosely match for executing the ones found.
#
# Sets the ${HOOK_PATHS} environment variable,
# with the list of matching hook paths.
#
# Returns:
# 0 on success, 1 when no hooks found
#####################################################
find_hook_path_to_execute() {
if [ -z "$1" ]; then
echo "! Either the trigger type, the hook name or both needs to be given" >&2
return 1
elif [ -n "$1" ] && [ -n "$2" ]; then
if [ -n "$EXACT_MATCH" ]; then
SEARCH_PATTERN="$1/$2\$"
else
SEARCH_PATTERN="$1/.*$2.*"
fi
else
TRIGGER_TYPES="(applypatch-msg|pre-applypatch|post-applypatch|pre-commit|prepare-commit-msg|commit-msg|post-commit"
TRIGGER_TYPES="${TRIGGER_TYPES}|pre-rebase|post-checkout|post-merge|pre-push|pre-receive|update|post-receive"
TRIGGER_TYPES="${TRIGGER_TYPES}|post-update|push-to-checkout|pre-auto-gc|post-rewrite|sendemail-validate)"
if echo "$1" | grep -qE "^${TRIGGER_TYPES}$"; then
SEARCH_PATTERN="$1/.*"
else
if [ -n "$EXACT_MATCH" ]; then
SEARCH_PATTERN="${TRIGGER_TYPES}/$1\$"
else
SEARCH_PATTERN="${TRIGGER_TYPES}/.*$1.*"
fi
fi
fi
HOOK_PATHS=""
IFS="$IFS_NEWLINE"
for TARGET_HOOK in $(find "$(pwd)/.githooks" -type f | grep -E "/\.githooks/$SEARCH_PATTERN" | sort); do
if [ -z "$HOOK_PATHS" ]; then
HOOK_PATHS="$TARGET_HOOK"
else
HOOK_PATHS="$HOOK_PATHS
$TARGET_HOOK"
fi
done
unset IFS
if [ ! -d "$INSTALL_DIR/shared" ]; then
# No shared repositories found
return
fi
IFS="$IFS_NEWLINE"
for SHARED_ROOT in "$INSTALL_DIR/shared/"*; do
unset IFS
if [ ! -d "$SHARED_ROOT" ]; then
continue
fi
REMOTE_URL=$(git -C "$SHARED_ROOT" config --get remote.origin.url)
if [ -f "$(pwd)/.githooks/.shared" ]; then
SHARED_LOCAL_REPOS_LIST=$(grep -E "^[^#\n\r ].*$" <"$(pwd)/.githooks/.shared")
ACTIVE_LOCAL_REPO=$(echo "$SHARED_LOCAL_REPOS_LIST" | grep -F -o "$REMOTE_URL")
else
ACTIVE_LOCAL_REPO=""
fi
ACTIVE_GLOBAL_REPO=$(git config --global --get-all githooks.shared | grep -o "$REMOTE_URL")
if [ "$ACTIVE_LOCAL_REPO" != "$REMOTE_URL" ] && [ "$ACTIVE_GLOBAL_REPO" != "$REMOTE_URL" ]; then
continue
fi
if [ -d "$SHARED_ROOT/.githooks" ]; then
SHARED_ROOT_SEARCH_START="$SHARED_ROOT/.githooks"
else
SHARED_ROOT_SEARCH_START="$SHARED_ROOT"
fi
IFS="$IFS_NEWLINE"
for TARGET_HOOK in $(find "$SHARED_ROOT_SEARCH_START" -type f | grep -E "${SHARED_ROOT_SEARCH_START}/${SEARCH_PATTERN}" | sort); do
if [ -z "$HOOK_PATHS" ]; then
HOOK_PATHS="$TARGET_HOOK"
else
HOOK_PATHS="$HOOK_PATHS
$TARGET_HOOK"
fi
done
unset IFS
IFS="$IFS_NEWLINE"
done
}
#####################################################
# Creates the Githooks checksum file
# for the repository if it does not exist yet.
#####################################################
ensure_checksum_file_exists() {
touch "${CURRENT_GIT_DIR}/.githooks.checksum"
}
#####################################################
# Disables one or more hook files
# in the current repository.
#
# Returns:
# 1 if the current directory is not a Git repo,
# 0 otherwise
#####################################################
disable_hook() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks disable [--shared] [trigger] [hook-script]
git hooks disable [--shared] [hook-script]
git hooks disable [--shared] [trigger]
git hooks disable [-a|--all]
git hooks disable [-r|--reset]
Disables a hook in the current repository.
The \`trigger\` parameter should be the name of the Git event if given.
The \`hook-script\` can be the name of the file to disable, or its
relative path, or an absolute path, we will try to find it.
If the \`--shared\` parameter is given as the first argument,
hooks in the shared repositories will be disabled,
otherwise they are looked up in the current local repository.
The \`--all\` parameter on its own will disable running any Githooks
in the current repository, both existing ones and any future hooks.
The \`--reset\` parameter is used to undo this, and let hooks run again.
"
return
fi
if ! is_running_in_git_repo_root; then
echo "! The current directory \`$(pwd)\` does not seem" >&2
echo " to be the root of a Git repository!" >&2
exit 1
fi
if [ "$1" = "-a" ] || [ "$1" = "--all" ]; then
git config githooks.disable true &&
echo "All existing and future hooks are disabled in the current repository" &&
return
echo "! Failed to disable hooks in the current repository" >&2
exit 1
elif [ "$1" = "-r" ] || [ "$1" = "--reset" ]; then
git config --unset githooks.disable
if ! git config --get githooks.disable; then
echo "Githooks hook files are not disabled anymore by default" && return
else
echo "! Failed to re-enable Githooks hook files" >&2
exit 1
fi
fi
if ! find_hook_path_to_enable_or_disable "$@"; then
if [ "$1" = "update" ]; then
echo " Did you mean \`git hooks update disable\` ?"
fi
exit 1
fi
ensure_checksum_file_exists
find "$HOOK_PATH" -type f -path "*/.githooks/*" | while IFS= read -r HOOK_FILE; do
if grep -q "disabled> $HOOK_FILE" "${CURRENT_GIT_DIR}/.githooks.checksum" 2>/dev/null; then
echo "Hook file is already disabled at $HOOK_FILE"
continue
fi
echo "disabled> $HOOK_FILE" >>"${CURRENT_GIT_DIR}/.githooks.checksum"
echo "Hook file disabled at $HOOK_FILE"
done
}
#####################################################
# Enables one or more hook files
# in the current repository.
#
# Returns:
# 1 if the current directory is not a Git repo,
# 0 otherwise
#####################################################
enable_hook() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks enable [--shared] [trigger] [hook-script]
git hooks enable [--shared] [hook-script]
git hooks enable [--shared] [trigger]
Enables a hook or hooks in the current repository.
The \`trigger\` parameter should be the name of the Git event if given.
The \`hook-script\` can be the name of the file to enable, or its
relative path, or an absolute path, we will try to find it.
If the \`--shared\` parameter is given as the first argument,
hooks in the shared repositories will be enabled,
otherwise they are looked up in the current local repository.
"
return
fi
if ! is_running_in_git_repo_root; then
echo "The current directory \`$(pwd)\` does not seem to be the root of a Git repository!"
exit 1
fi
if ! find_hook_path_to_enable_or_disable "$@"; then
if [ "$1" = "update" ]; then
echo " Did you mean \`git hooks update enable\` ?"
fi
exit 1
fi
ensure_checksum_file_exists
sed "\\|disabled> $HOOK_PATH|d" "${CURRENT_GIT_DIR}/.githooks.checksum" >"${CURRENT_GIT_DIR}/.githooks.checksum.tmp" &&
mv "${CURRENT_GIT_DIR}/.githooks.checksum.tmp" "${CURRENT_GIT_DIR}/.githooks.checksum" &&
echo "Hook file(s) enabled at $HOOK_PATH"
}
#####################################################
# Accept changes to a new or existing but changed
# hook file by recording its checksum as accepted.
#
# Returns:
# 1 if the current directory is not a Git repo,
# 0 otherwise
#####################################################
accept_changes() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks accept [--shared] [trigger] [hook-script]
git hooks accept [--shared] [hook-script]
git hooks accept [--shared] [trigger]
Accepts a new hook or changes to an existing hook.
The \`trigger\` parameter should be the name of the Git event if given.
The \`hook-script\` can be the name of the file to enable, or its
relative path, or an absolute path, we will try to find it.
If the \`--shared\` parameter is given as the first argument,
hooks in the shared repositories will be accepted,
otherwise they are looked up in the current local repository.
"
return
fi
if ! is_running_in_git_repo_root; then
echo "The current directory \`$(pwd)\` does not seem to be the root of a Git repository!"
exit 1
fi
find_hook_path_to_enable_or_disable "$@" || exit 1
ensure_checksum_file_exists
find "$HOOK_PATH" -type f -path "*/.githooks/*" | while IFS= read -r HOOK_FILE; do
if grep -q "disabled> $HOOK_FILE" "${CURRENT_GIT_DIR}/.githooks.checksum"; then
echo "Hook file is currently disabled at $HOOK_FILE"
continue
fi
CHECKSUM=$(get_hook_checksum "$HOOK_FILE")
echo "$CHECKSUM $HOOK_FILE" >>"${CURRENT_GIT_DIR}/.githooks.checksum" &&
echo "Changes accepted for $HOOK_FILE"
done
}
#####################################################
# Execute a specific hook on demand.
#
# Returns:
# 1 if the current directory is not a Git repo,
# 0 otherwise
#####################################################
execute_hook() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks exec [--exact] [trigger] [hook-script]
git hooks exec [--exact] [hook-script]
git hooks exec [--exact] [trigger]
Executes a hook script on demand.
During these executions, the \`GITHOOKS_ON_DEMAND_EXEC\` environment
variable will be set, hook scripts can use that for conditional logic.
The \`trigger\` parameter should be the name of the Git event if given.
The \`hook-script\` can be the name of the file to execute, or its
relative path, or an absolute path, we will try to find it.
If the \`--exact\` flag is given, only hook scripts matching exactly
will be returned, otherwise all hook scripts matching the substring.
"
return
fi
if ! is_running_in_git_repo_root; then
echo "! The current directory \`$(pwd)\` does not seem to be the root of a Git repository!" >&2
exit 1
fi
if [ "$1" = "--exact" ]; then
EXACT_MATCH=1
shift
fi
if [ -z "$1" ]; then
echo "! Missing [hook-script] or [trigger] argument, see git hooks exec help" >&2
exit 1
elif [ -n "$2" ]; then
REQUESTED_HOOK_TYPE="$1"
ACCEPTED_HOOK_TYPES="
applypatch-msg pre-applypatch post-applypatch
pre-commit prepare-commit-msg commit-msg post-commit
pre-rebase post-checkout post-merge pre-push
pre-receive update post-receive post-update
push-to-checkout pre-auto-gc post-rewrite sendemail-validate"
if ! echo "$ACCEPTED_HOOK_TYPES" | grep -qE "\b$REQUESTED_HOOK_TYPE\b"; then
echo "! Unknown trigger type: $REQUESTED_HOOK_TYPE" >&2
echo " See git hooks exec help for usage" >&2
exit 1
fi
elif [ -n "$3" ]; then
echo "! Unexpected argument: $3" >&2
echo " See git hooks exec help for usage" >&2
exit 1
fi
find_hook_path_to_execute "$@" || exit 1
if [ -z "$HOOK_PATHS" ]; then
echo "! Sorry, cannot find any hooks that would match that" >&2
exit 1
fi
AGGREGATE_RESULT="0"
IFS="$IFS_NEWLINE"
for HOOK_FILE in ${HOOK_PATHS}; do
unset IFS
if [ -x "$HOOK_FILE" ]; then
# Run as an executable file
GITHOOKS_ON_DEMAND_EXEC=1 "$HOOK_FILE"
RUN_RESULT="$?"
elif [ -f "$HOOK_FILE" ]; then
# Run as a Shell script
GITHOOKS_ON_DEMAND_EXEC=1 sh "$HOOK_FILE"
RUN_RESULT="$?"
else
echo "! Unable to run hook file: $HOOK_FILE}" >&2
RUN_RESULT="1"
fi
[ "$RUN_RESULT" -gt "$AGGREGATE_RESULT" ] && AGGREGATE_RESULT="$RUN_RESULT"
IFS="$IFS_NEWLINE"
done
unset IFS
exit "$AGGREGATE_RESULT"
}
#####################################################
# Returns the SHA1 hash of the hook file
# passed in as the first argument.
#####################################################
get_hook_checksum() {
git hash-object "$1" 2>/dev/null
}
#####################################################
# Manage settings related to trusted repositories.
# It allows setting up and clearing marker
# files and Git configuration.
#
# Returns:
# 1 on failure, 0 otherwise
#####################################################
manage_trusted_repo() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks trust [--local|--global]
git hooks trust [revoke]
git hooks trust [delete]
git hooks trust [forget] [--local|--global]
Sets up, or reverts the trusted setting for the local/global repository.
When called without arguments, it marks the local repository as trusted.
The \`revoke\` argument resets the already accepted trust setting,
and the \`delete\` argument also deletes the trusted marker.
The \`forget\` option unsets the trust setting, asking for accepting
it again next time, if the repository is marked as trusted.
"
return
fi
if [ "$1" != "--global" ] && [ "$2" != "--global" ] && ! is_running_in_git_repo_root; then
echo "The current directory \`$(pwd)\` does not seem to be the root of a Git repository!"
exit 1
fi
if [ "$1" = "--local" ] || [ -z "$1" ]; then
mkdir -p .githooks &&
touch .githooks/trust-all &&
git config githooks.trust.all Y &&
echo "The current repository is now trusted." &&
echo_if_non_bare_repo " Do not forget to commit and push the trust marker!" &&
return
echo "! Failed to mark the current repository as trusted" >&2
exit 1
elif [ "$1" = "--global" ]; then
GLOBAL_TRUST=$(git config --global --get githooks.trust.all)
if [ -z "${GLOBAL_TRUST}" ] || [ "${GLOBAL_TRUST}" = "N" ]; then
git config --global githooks.trust.all Y
echo "Global hook are now trusted when they contains a .githooks/trust-all file"
return
elif [ "${GLOBAL_TRUST}" = "Y" ]; then
echo "Global hook are already trusted"
return
fi
elif [ "$1" = "forget" ]; then
if [ "$2" = "--local" ]; then
if [ -z "$(git config --local --get githooks.trust.all)" ]; then
echo "The current repository does not have trust settings."
return
elif git config --unset githooks.trust.all; then
echo "The current repository is no longer trusted."
return
else
echo "! Failed to revoke the trusted setting" >&2
exit 1
fi
elif [ "$2" = "--global" ]; then
if [ -z "$(git config --global --get githooks.trust.all)" ]; then
echo "Global hooks do not have trust settings."
return
elif git config --global --unset githooks.trust.all; then
echo "Global hooks are no longer trusted."
return
else
echo "! Failed to revoke the trusted setting for global hooks" >&2
exit 1
fi
else
echo "! Usage: \`git hooks trust forget [--local|--global] \`" >&2
exit 1
fi
elif [ "$1" = "revoke" ] || [ "$1" = "delete" ]; then
if git config githooks.trust.all N; then
echo "The current repository is no longer trusted."
else
echo "! Failed to revoke the trusted setting" >&2
exit 1
fi
if [ "$1" = "revoke" ]; then
return
fi
fi
if [ "$1" = "delete" ] || [ -f .githooks/trust-all ]; then
rm -rf .githooks/trust-all &&
echo "The trust marker is removed from the repository." &&
echo_if_non_bare_repo " Do not forget to commit and push the change!" &&
return
echo "! Failed to delete the trust marker" >&2
exit 1
fi
echo "! Unknown subcommand: $1" >&2
echo " Run \`git hooks trust help\` to see the available options." >&2
exit 1
}
#####################################################
# Checks if Githhoks is set up correctly,
# and that other git settings do not prevent it
# from executing scripts.
#####################################################
check_git_hooks_setup_is_correct() {
if [ -n "$(git config core.hooksPath)" ]; then
if [ "true" != "$(git config githooks.useCoreHooksPath)" ]; then
echo "! WARNING" >&2
echo " \`git config core.hooksPath\` is set to $(git config core.hooksPath)," >&2
echo " but Githooks is not configured to use that folder," >&2
echo " which could mean the hooks in this repository are not run by Githooks" >&2
echo >&2
fi
else
if [ "true" = "$(git config githooks.useCoreHooksPath)" ]; then
echo "! WARNING" >&2
echo " Githooks is configured to consider \`git config core.hooksPath\`," >&2
echo " but that git setting is not currently set," >&2
echo " which could mean the hooks in this repository are not run by Githooks" >&2
echo >&2
fi
fi
}
#####################################################
# Lists the hook files in the current
# repository along with their current state.
#
# Returns:
# 1 if the current directory is not a Git repo,
# 0 otherwise
#####################################################
list_hooks() {
if [ "$1" = "help" ]; then
print_help_header
echo "
git hooks list [type]
Lists the active hooks in the current repository along with their state.
If \`type\` is given, then it only lists the hooks for that trigger event.
This command needs to be run at the root of a repository.
"
return
fi
if ! is_running_in_git_repo_root; then
echo "The current directory \`$(pwd)\` does not seem to be the root of a Git repository!"
exit 1
fi
check_git_hooks_setup_is_correct
if [ -n "$*" ]; then
LIST_TYPES="$*"
WARN_NOT_FOUND="1"
else
LIST_TYPES="
applypatch-msg pre-applypatch post-applypatch
pre-commit prepare-commit-msg commit-msg post-commit
pre-rebase post-checkout post-merge pre-push
pre-receive update post-receive post-update
push-to-checkout pre-auto-gc post-rewrite sendemail-validate"
fi
for LIST_TYPE in $LIST_TYPES; do
LIST_OUTPUT=""
# non-Githooks hook file
if [ -x "${CURRENT_GIT_DIR}/hooks/${LIST_TYPE}.replaced.githook" ]; then
ITEM_STATE=$(get_hook_state "${CURRENT_GIT_DIR}/hooks/${LIST_TYPE}.replaced.githook")
LIST_OUTPUT="$LIST_OUTPUT
- $LIST_TYPE (previous / file / ${ITEM_STATE})"
fi
# global shared hooks
SHARED_REPOS_LIST=$(git config --global --get-all githooks.shared)
IFS="$IFS_NEWLINE"
for SHARED_ITEM in $(list_hooks_in_shared_repos "$LIST_TYPE"); do
unset IFS
if [ -d "$SHARED_ITEM" ]; then
for LIST_ITEM in "$SHARED_ITEM"/*; do
ITEM_NAME=$(basename "$LIST_ITEM")
ITEM_STATE=$(get_hook_state "$LIST_ITEM" "$@")
LIST_OUTPUT="$LIST_OUTPUT
- $ITEM_NAME (${ITEM_STATE} / shared:global)"
LIST_OUTPUT="$LIST_OUTPUT is from $LIST_ITEM"
done
elif [ -f "$SHARED_ITEM" ]; then
ITEM_STATE=$(get_hook_state "$SHARED_ITEM")
LIST_OUTPUT="$LIST_OUTPUT
- $LIST_TYPE (file / ${ITEM_STATE} / shared:global)"
fi
IFS="$IFS_NEWLINE"
done
# local shared hooks
if [ -f "$(pwd)/.githooks/.shared" ]; then
SHARED_REPOS_LIST=$(grep -E "^[^#\n\r ].*$" <"$(pwd)/.githooks/.shared")
IFS="$IFS_NEWLINE"
for SHARED_ITEM in $(list_hooks_in_shared_repos "$LIST_TYPE"); do
unset IFS
if [ -d "$SHARED_ITEM" ]; then
for LIST_ITEM in "$SHARED_ITEM"/*; do
ITEM_NAME=$(basename "$LIST_ITEM")
ITEM_STATE=$(get_hook_state "$LIST_ITEM")
LIST_OUTPUT="$LIST_OUTPUT
- $ITEM_NAME (${ITEM_STATE} / shared:local)"
done
elif [ -f "$SHARED_ITEM" ]; then
ITEM_STATE=$(get_hook_state "$SHARED_ITEM")
LIST_OUTPUT="$LIST_OUTPUT
- $LIST_TYPE (file / ${ITEM_STATE} / shared:local)"
fi
IFS="$IFS_NEWLINE"
done
fi
# in the current repository
if [ -d ".githooks/$LIST_TYPE" ]; then
IFS="$IFS_NEWLINE"
for LIST_ITEM in .githooks/"$LIST_TYPE"/*; do
unset IFS
ITEM_NAME=$(basename "$LIST_ITEM")
ITEM_STATE=$(get_hook_state "$(pwd)/.githooks/$LIST_TYPE/$ITEM_NAME")
LIST_OUTPUT="$LIST_OUTPUT
- $ITEM_NAME (${ITEM_STATE})"
IFS="$IFS_NEWLINE"
done
elif [ -f ".githooks/$LIST_TYPE" ]; then
ITEM_STATE=$(get_hook_state "$(pwd)/.githooks/$LIST_TYPE")
LIST_OUTPUT="$LIST_OUTPUT
- $LIST_TYPE (file / ${ITEM_STATE})"
fi
if [ -n "$LIST_OUTPUT" ]; then
echo "> ${LIST_TYPE}${LIST_OUTPUT}"
elif [ -n "$WARN_NOT_FOUND" ]; then
echo "> $LIST_TYPE"
echo " No active hooks found"
fi
done
}
#####################################################
# Returns the state of hook file
# in a human-readable format
# on the standard output.
#####################################################
get_hook_state() {
ITEM="$1"
shift
if is_repository_disabled; then
echo "disabled"
elif is_file_ignored "$ITEM"; then
echo "ignored"
elif is_trusted_repo "$ITEM"; then
echo "active / trusted"
else
get_hook_enabled_or_disabled_state "$ITEM"
fi
}
#####################################################
# Checks if Githooks is disabled in the
# current local repository.
#
# Returns:
# 0 if disabled, 1 otherwise
#####################################################
is_repository_disabled() {
GITHOOKS_CONFIG_DISABLE=$(git config --get githooks.disable)
if [ "$GITHOOKS_CONFIG_DISABLE" = "true" ] ||
[ "$GITHOOKS_CONFIG_DISABLE" = "y" ] || # Legacy
[ "$GITHOOKS_CONFIG_DISABLE" = "Y" ]; then # Legacy
return 0
else
return 1
fi
}
#####################################################
# Checks if the hook file at ${HOOK_PATH}
# is ignored and should not be executed.
#
# Returns:
# 0 if ignored, 1 otherwise
#####################################################
is_file_ignored() {
HOOK_NAME=$(basename "$1")
IS_IGNORED=""
# If there are .ignore files, read the list of patterns to exclude.
ALL_IGNORE_FILE=$(mktemp)
if [ -f ".githooks/.ignore" ]; then
cat ".githooks/.ignore" >"$ALL_IGNORE_FILE"
echo >>"$ALL_IGNORE_FILE"
fi
if [ -f ".githooks/${LIST_TYPE}/.ignore" ]; then
cat ".githooks/${LIST_TYPE}/.ignore" >>"$ALL_IGNORE_FILE"
echo >>"$ALL_IGNORE_FILE"
fi
# Check if the filename matches any of the ignored patterns
while IFS= read -r IGNORED; do
if [ -z "$IGNORED" ] || [ "$IGNORED" != "${IGNORED#\#}" ]; then
continue
fi
# shellcheck disable=SC2295
if [ -z "${HOOK_NAME##$IGNORED}" ]; then
IS_IGNORED="y"
break
fi
done <"$ALL_IGNORE_FILE"
# Remove the temporary file
rm -f "$ALL_IGNORE_FILE"
if [ -n "$IS_IGNORED" ]; then