-
Notifications
You must be signed in to change notification settings - Fork 20
/
install.sh
executable file
·2053 lines (1735 loc) · 69.6 KB
/
install.sh
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
#
# Installs the base Git hook templates from https://github.com/rycus86/githooks
# and performs some optional setup for existing repositories.
# See the documentation in the project README for more information.
#
# Legacy version number. Not used anymore, but old installs read it.
# Version: 9912.310000-000000
# The list of hooks we can manage with this script
MANAGED_HOOK_NAMES="
applypatch-msg pre-applypatch post-applypatch
pre-commit pre-merge-commit prepare-commit-msg commit-msg post-commit
pre-rebase post-checkout post-merge pre-push
pre-receive update post-receive post-update reference-transaction
push-to-checkout pre-auto-gc post-rewrite sendemail-validate
post-index-change
"
MANAGED_SERVER_HOOK_NAMES="
pre-push pre-receive update post-receive post-update
reference-transaction push-to-checkout pre-auto-gc
"
############################################################
# Execute the full installation process.
#
# Returns:
# 0 when successfully finished, 1 if failed
############################################################
execute_installation() {
# Global IFS for loops
IFS_NEWLINE="
"
parse_command_line_arguments "$@" || return 1
load_install_dir || return 1
if ! is_postupdate; then
check_deprecation || return 1
legacy_transform_before_update || return 1
update_release_clone || return 1
if is_clone_updated || ! is_running_internal_install; then
# Either
# - we just updated the release clone -> dispatch to the new install
# - or we are running not from the release clone -> dispatch to the install
# in the clone.
run_internal_install --internal-postupdate "$@" || return 1
return 0
fi
fi
# From here starts the post update logic
# meaning the `--internal-postupdate` flag is set
# and we are running inside the release clone
# meaning the `--internal-install` flag is set.
if ! is_dry_run; then
legacy_transform_after_update || return 1
fi
if is_non_interactive; then
disable_tty_input
fi
# Find the directory to install to
if is_single_repo_install; then
get_cwd_git_dir || return 1
else
prepare_target_template_directory || return 1
fi
# Install the hook templates if needed
if ! is_single_repo_install; then
setup_hook_templates || return 1
echo # For visual separation
fi
# Install the command line helper tool
install_command_line_tool
echo # For visual separation
# Automatic updates
if ! is_autoupdate && setup_automatic_update_checks; then
echo # For visual separation
fi
if ! should_skip_install_into_existing_repositories; then
if is_single_repo_install; then
get_cwd_git_dir || return 1
install_hooks_into_repo "$CWD_GIT_DIR" || return 1
else
if ! is_autoupdate; then
install_into_existing_repositories
fi
install_into_registered_repositories
fi
fi
echo # For visual separation
# Set up shared hook repositories if needed
if ! is_autoupdate && ! is_non_interactive && ! is_single_repo_install; then
setup_shared_hook_repositories
echo # For visual separation
fi
# Legacy transformations
if ! is_dry_run && ! is_single_repo_install; then
legacy_transform_end || return 1
fi
thank_you
return 0
}
############################################################
# Checks if any deprecated features are used
#
# Returns:
# 1 if the install should be aborted, 0 otherwise
############################################################
check_deprecation() {
if is_single_repo_install || is_autoupdate; then
if ! check_not_deprecated_single_install; then
echo "! Install failed due to deprecated single install" >&2
return 1
fi
fi
return 0
}
############################################################
# Function to dispatch to all legacy transformations
# at the start.
# We are not yet deleting old values since the install
# could go wrong and dry-run could also be activated.
#
# Returns: 0 if successful, 1 otherwise
############################################################
legacy_transform_before_update() {
LEGACY_TRANSFORM_FAILURES="false"
# Variable transformations in global git config
# Can be applied to all versions without any problem
OLD_CONFIG_VALUE=$(git config --global githooks.autoupdate.updateCloneUrl)
if [ -n "$OLD_CONFIG_VALUE" ]; then
git config --global githooks.cloneUrl "$OLD_CONFIG_VALUE" || LEGACY_TRANSFORM_FAILURES="true"
fi
OLD_CONFIG_VALUE=$(git config --global githooks.autoupdate.updateCloneBranch)
if [ -n "$OLD_CONFIG_VALUE" ]; then
git config --global githooks.cloneBranch "$OLD_CONFIG_VALUE" || LEGACY_TRANSFORM_FAILURES="true"
fi
OLD_CONFIG_VALUE=$(git config --global githooks.previous.searchdir)
if [ -n "$OLD_CONFIG_VALUE" ]; then
git config --global githooks.previousSearchDir "$OLD_CONFIG_VALUE" || LEGACY_TRANSFORM_FAILURES="true"
fi
# Copy legacy file to new location
if [ -f "$INSTALL_DIR/autoupdate/registered" ]; then
cp "$INSTALL_DIR/autoupdate/registered" "$INSTALL_DIR/registered" || LEGACY_TRANSFORM_FAILURES="true"
fi
if [ "$LEGACY_TRANSFORM_FAILURES" = "true" ]; then
echo "! There were legacy transform errors: check stderr"
return 1
fi
return 0
}
############################################################
# Tests if the commit sha `$1`
# is before or equal of the commit sha `$2`.
#
# Returns: 0 if successful, 1 otherwise
############################################################
legacy_transform_is_ancestor() {
if [ -n "$1" ] && [ -n "$2" ] &&
execute_git "$GITHOOKS_CLONE_DIR" merge-base --is-ancestor \
"$1" "$2" >/dev/null 2>&1; then
# commit 1 <= commit 2
return 0
fi
return 1
}
############################################################
# Function to dispatch to all legacy transformations
# right after the update.
#
# Returns: 0 if successful, 1 otherwise
############################################################
legacy_transform_after_update() {
COMMIT_COUNT=$(execute_git "$GITHOOKS_CLONE_DIR" rev-list --count HEAD)
GITHOOKS_CLONE_CURRENT_COMMIT=$(execute_git "$GITHOOKS_CLONE_DIR" rev-parse HEAD)
if [ "$COMMIT_COUNT" != "1" ] && [ -z "${INTERNAL_UPDATED_FROM_COMMIT+set}" ]; then
# If the clone dir has been updated (commit count != 1) and
# we do not have the update commit yet (meaning we are updating from an old version )
# we set it to the last commit where this feature (INTERNAL_UPDATED_FROM_COMMIT)
# was not yet available
INTERNAL_UPDATED_FROM_COMMIT="ab86d2a529f58744a71e79227e434f19b84589e6"
fi
# Because of changes in PR #125 right after commit ab86d2a5:
# - Check if we need transforms for `--global githooks.shared`
# to be split into multiple config values
# - Show info that all hook hashes are differently computed now
# and every hooks needs to be trusted again.
# - Show info that `githooks.failOnNonExistingHooks` is enabled due to renaming
# internal cloned shared hooks repositories.
if legacy_transform_is_ancestor \
"$INTERNAL_UPDATED_FROM_COMMIT" \
"ab86d2a529f58744a71e79227e434f19b84589e6"; then
legacy_transform_split_global_shared_entries || LEGACY_TRANSFORM_FAILURES="true"
echo >&2
echo "! Info: Because the hash algorithm changed from" >&2
echo " \$(md5sum) to \$(git hash-object)," >&2
echo " you unfortunately need to retrust all hooks again." >&2
echo >&2
fi
}
############################################################
# Function to dispatch to all legacy transformations
# at the end
#
# Returns: 0
############################################################
legacy_transform_end() {
# Variable transformations in global git config
# Can be applied to all versions without any problem
git config --global --unset githooks.autoupdate.updateCloneUrl
git config --global --unset githooks.autoupdate.updateCloneBranch
git config --global --unset githooks.previous.searchdir
# Remove legacy registration file (we moved it to another location)
if [ -f "$INSTALL_DIR/autoupdate/registered" ]; then
rm -rf "$INSTALL_DIR/autoupdate" || LEGACY_TRANSFORM_FAILURES="true"
fi
legacy_transform_registered_repos || LEGACY_TRANSFORM_FAILURES="true"
if [ "$LEGACY_TRANSFORM_FAILURES" = "true" ]; then
echo "! There were legacy transform errors: check stderr"
return 1
fi
return 0
}
############################################################
# Transform all comma-delimited global \`githooks.shared\`
# values into multiple git config values.
#
# Returns:
# 1 when failed, 0 otherwise
############################################################
legacy_transform_split_global_shared_entries() {
CURRENT_LIST=$(git config --global --get githooks.shared)
FAILURE="false"
# If it contains a comma, split it...
if echo "$CURRENT_LIST" | grep -q ","; then
git config --global --unset githooks.shared
# Split it and add all back
IFS=",$IFS_NEWLINE"
for ITEM in $CURRENT_LIST; do
unset IFS
if [ -n "$ITEM" ]; then
git config --global --add githooks.shared "$ITEM" || FAILURE="true"
fi
IFS=",$IFS_NEWLINE"
done
unset IFS
fi
if [ "$FAILURE" = "true" ]; then
echo "! Warning: Could not migrate the global shared hook repositories setting:" >&2
echo "\`$CURRENT_LIST\`" >&2
echo " Please check \`githooks.shared\` and add all comma-separated" >&2
echo " values manually by running:" >&2
echo " \$ git config --global --add githooks.shared <value>" >&2
LEGACY_TRANSFORM_FAILURES="true"
fi
return 0
}
############################################################
# Function to dispatch to all legacy transformations
# for all registered repos.
#
# Returns:
# 1 when failed, 0 otherwise
############################################################
legacy_transform_registered_repos() {
# Check if we need transforms for `.shared` files:
# Put local paths into `--local githooks.shared`.
# which was introduced by PR #125 right after commit ab86d2a5:
PR_125="false"
if legacy_transform_is_ancestor \
"$INTERNAL_UPDATED_FROM_COMMIT" \
"ab86d2a529f58744a71e79227e434f19b84589e6"; then
PR_125="true"
fi
if [ "$(git config --global githooks.useCoreHooksPath)" = "true" ]; then
if [ "$PR_125" = "true" ]; then
echo >&2
echo "! DEPRECATION WARNING: Local paths for shared hook repositories" >&2
echo " configured with \`.githooks/.shared\` files per repository" >&2
echo " are no more supported and need" >&2
echo " to be moved manually to the local Git configuration variable" >&2
echo " \`githooks.shared\` by running:" >&2
echo " \$ git hooks shared add --local <local path>" >&2
echo >&2
echo >&2
echo "! DEPRECATION WARNING: Because of renaming of internal cloned shared" >&2
echo " hook repositories, you should update all shared hook repositories" >&2
echo " by running in all repositories using Githooks:" >&2
echo " \$ git hooks shared update" >&2
echo " The Git config variable \`githooks.failOnNonExistingSharedHooks\` has been" >&2
echo " enabled globally to safely fail if you forgot to update them." >&2
echo >&2
git config --global githooks.failOnNonExistingSharedHooks "true"
fi
else
LIST="$INSTALL_DIR/registered"
if [ ! -f "$LIST" ]; then
return 0
fi
IFS="$IFS_NEWLINE"
while read -r REGISTERED_REPO; do
unset IFS
if [ "$(git -C "$REGISTERED_REPO" rev-parse --is-inside-git-dir 2>/dev/null)" != "true" ]; then
# Not existing git dir -> skip.
true
else
WORKTREES=$(get_repo_worktrees "$REGISTERED_REPO")
IFS="$IFS_NEWLINE"
for TREE in $WORKTREES; do
unset IFS
# safe guard: a bit buggy get_repo_worktrees
if [ "$(git -C "$TREE" rev-parse --is-inside-git-dir 2>/dev/null)" = "true" ]; then
continue
fi
legacy_transform_remove_legacy_config "$TREE"
if [ "$PR_125" = "true" ]; then
legacy_transform_adjust_local_paths "$TREE" ||
LEGACY_TRANSFORM_FAILURES="true"
legacy_transform_update_shared_hooks "$TREE" ||
LEGACY_TRANSFORM_FAILURES="true"
fi
IFS="$IFS_NEWLINE"
done
fi
IFS="$IFS_NEWLINE"
done <"$LIST"
fi
return 0
}
############################################################
# Remove legacy config values in repo `$1`.
#
# Returns: None
############################################################
legacy_transform_remove_legacy_config() {
git -C "$REGISTERED_REPO" config --local --unset githooks.autoupdate.registered >/dev/null 2>&1
}
############################################################
# Function to adjust local paths in `.githooks/.shared`
# file which are forbidden.
#
# Returns:
# 1 when failed, 0 otherwise
############################################################
legacy_transform_adjust_local_paths() {
SHARED_FILE="$1/.githooks/.shared"
if [ -f "$SHARED_FILE" ]; then
NEW_SHARED_LIST=$(mktemp)
MOVED_URLS=$(mktemp)
MOVED="false"
IFS=",$IFS_NEWLINE" # legacy split also with comma -> put it on a new line
while read -r LINE || [ -n "$LINE" ]; do
unset IFS
if echo "$LINE" | grep -qE "^\s*(#.*)?$"; then
echo "$LINE" >>"$NEW_SHARED_LIST"
elif is_local_path "$LINE" || is_local_url "$LINE"; then
git -C "$1" config --local --add githooks.shared "$LINE"
echo "$LINE" >>"$MOVED_URLS"
MOVED="true"
else
echo "$LINE" >>"$NEW_SHARED_LIST"
fi
IFS=",$IFS_NEWLINE"
done <"$SHARED_FILE"
cp -f "$NEW_SHARED_LIST" "$SHARED_FILE" &&
rm -rf "$NEW_SHARED_LIST" >/dev/null 2>&1
if [ "$MOVED" = "true" ]; then
echo "! Warning: The shared hooks configuration in" >&2
echo " \`$SHARED_FILE\`" >&2
echo " contains local paths which are not supported" >&2
echo " any more:" >&2
sed -E "s/^/ - /" "$MOVED_URLS" >&2
echo " These paths are now moved to the local Git" >&2
echo " configuration value \`githooks.shared\`." >&2
echo " The file \`$SHARED_FILE\` has been changed and" >&2
echo " should be committed!" >&2
fi
rm -rf "$MOVED_URLS" >/dev/null 2>&1
fi
return 0
}
############################################################
# Function to update shared hooks repos in configured by
# `.shared` file in repo `$1`.
#
# Returns:
# 1 when failed, 0 otherwise
############################################################
legacy_transform_update_shared_hooks() {
# Could be more efficient if we have a "--shared,--local,--global"
# flag on this command.
if [ -d "$1" ]; then
out=$(cd "$1" 2>&1 && sh "$GITHOOKS_CLONE_DIR/cli.sh" shared update 2>&1)
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "! Could not execute shared update in" >&2
echo " \`$1\`" >&2
echo " Errors: \`$out\`" >&2
return 1
fi
fi
return 0
}
#####################################################
# Check if `$1` is not a supported git clone url and
# is treated as a local path to a repository.
# See `https://tools.ietf.org/html/rfc3986#appendix-B`
# Returns: 0 if it is a local path, 1 otherwise
#####################################################
is_local_path() {
if echo "$1" | grep -Eq "^[^:/?#]+://" || # it is a <scheme>://
echo "$1" | grep -Eq "^.+@.+:.+"; then # or it is a short scp syntax
return 1
fi
return 0
}
#####################################################
# Check if url `$1` is a local url, e.g `file://`.
#
# Returns: 0 if it is a local url, 1 otherwise
#####################################################
is_local_url() {
if echo "$1" | grep -iEq "^\s*file://"; then
return 0
fi
return 1
}
############################################################
# Sets the install directory.
#
# Returns:
# 1 when failed to configure the install directory,
# 0 otherwise
############################################################
load_install_dir() {
# First check if we already have
# an install directory set (from --prefix)
if [ -z "$INSTALL_DIR" ]; then
# load from config
INSTALL_DIR=$(git config --global githooks.installDir)
if [ -z "$INSTALL_DIR" ]; then
# if still empty, then set to default
INSTALL_DIR=~/".githooks"
elif [ ! -d "$INSTALL_DIR" ]; then
echo "! Configured install directory ${INSTALL_DIR} does not exist" >&2
INSTALL_DIR=~/".githooks"
fi
fi
GITHOOKS_CLONE_DIR="$INSTALL_DIR/release"
if is_dry_run; then
return 0
fi
if ! git config --global githooks.installDir "$INSTALL_DIR"; then
echo "! Could not set \`githooks.installDir\`"
return 1
fi
if ! git config --global githooks.runner "$INSTALL_DIR/release/base-template.sh"; then
echo "! Could not set \`githooks.runner\`"
return 1
fi
return 0
}
############################################################
# Checks if we are running an internal install
# from the release repository.
#
# Returns: 0 if `true`, 1 otherwise
############################################################
is_running_internal_install() {
if [ "$INTERNAL_INSTALL" = "true" ] ||
[ "$INTERNAL_INSTALL" = "yes" ]; then # Legacy over environment
return 0
fi
return 1
}
############################################################
# Set up variables based on command line arguments.
#
# Returns: 0 if all arguments are parsed correctly, 1 otherwise
############################################################
parse_command_line_arguments() {
TARGET_TEMPLATE_DIR=""
for p in "$@"; do
if [ "$p" = "--internal-autoupdate" ]; then
INTERNAL_AUTOUPDATE="true"
elif [ "$p" = "--internal-install" ]; then
INTERNAL_INSTALL="true"
elif [ "$p" = "--internal-postupdate" ]; then
INTERNAL_POSTUPDATE="true"
elif [ "$p" = "--internal-updated-from" ]; then
: # nothing to do here
elif [ "$prev_p" = "--internal-updated-from" ] && (echo "$p" | grep -qvE '^\-\-.*'); then
INTERNAL_UPDATED_FROM_COMMIT="$p"
elif [ "$p" = "--dry-run" ]; then
DRY_RUN="true"
elif [ "$p" = "--non-interactive" ]; then
NON_INTERACTIVE="true"
elif [ "$p" = "--single" ]; then
SINGLE_REPO_INSTALL="true"
elif [ "$p" = "--skip-install-into-existing" ]; then
SKIP_INSTALL_INTO_EXISTING="true"
elif [ "$p" = "--prefix" ]; then
: # nothing to do here
elif [ "$prev_p" = "--prefix" ] && (echo "$p" | grep -qvE '^\-\-.*'); then
# Allow user to pass preferred install prefix
INSTALL_DIR="$p"
# Try to see if the path is given with a tilde
TILDE_REPLACED=$(echo "$INSTALL_DIR" | awk 'gsub("~", "'"$HOME"'", $0)')
if [ -n "$TILDE_REPLACED" ]; then
INSTALL_DIR="$TILDE_REPLACED"
fi
INSTALL_DIR="$INSTALL_DIR/.githooks"
elif [ "$p" = "--template-dir" ]; then
: # nothing to do here
elif [ "$prev_p" = "--template-dir" ] && (echo "$p" | grep -qvE '^\-\-.*'); then
# Allow user to pass preferred template dir
TARGET_TEMPLATE_DIR="$p"
elif [ "$p" = "--only-server-hooks" ]; then
INSTALL_ONLY_SERVER_HOOKS="true"
elif [ "$p" = "--use-core-hookspath" ]; then
USE_CORE_HOOKSPATH="true"
# No point in installing into existing when using core.hooksPath
SKIP_INSTALL_INTO_EXISTING="true"
elif [ "$p" = "--clone-url" ]; then
: # nothing to do here
elif [ "$prev_p" = "--clone-url" ] && (echo "$p" | grep -qvE '^\-\-.*'); then
GITHOOKS_CLONE_URL="$p"
elif [ "$p" = "--clone-branch" ]; then
: # nothing to do here
elif [ "$prev_p" = "--clone-branch" ] && (echo "$p" | grep -qvE '^\-\-.*'); then
GITHOOKS_CLONE_BRANCH="$p"
else
echo "! Unknown argument \`$p\`" >&2
return 1
fi
prev_p="$p"
done
# Legacy flag over environment
if [ "$DO_UPDATE_ONLY" = "yes" ]; then
unset DO_UPDATE_ONLY
INTERNAL_AUTOUPDATE="true"
fi
# Using core.hooksPath implies it applies to all repo's
if is_single_repo_install && use_core_hookspath; then
echo "! Cannot use --single and --use-core-hookspath together" >&2
return 1
fi
}
############################################################
# Check if this repo is not a deprecated single install.
#
# Returns: 1 if deprecated single install, 0 otherwise
############################################################
check_not_deprecated_single_install() {
if is_git_repo "$(pwd)" && git config --local githooks.single.install >/dev/null 2>&1; then
echo >&2
echo "! DEPRECATION WARNING: Single install repositories are" >&2
echo " deprecated!" >&2
echo >&2
echo " The single installation feature" >&2
echo " with \`--single\` was changed to the following only" >&2
echo " behavior:" >&2
echo "" >&2
echo " - install Githooks hooks into the current repository" >&2
echo " - the installed hooks are not standalone anymore" >&2
echo " and behave exactly the same as current non-single" >&2
echo " installs" >&2
echo >&2
echo " You appear to have setup this repo as a single install." >&2
echo " The hooks in this repository are not supported anymore." >&2
echo >&2
echo " To install the latest hooks you need to reset this option" >&2
echo " by running" >&2
echo " \`git config --local --unset githooks.single.install\`" >&2
echo " in order to use this repository with githooks." >&2
echo >&2
return 1 # DeprecateSingleInstall
fi
return 0
}
############################################################
# Check if the install script is
# running in 'dry-run' mode.
#
# Returns:
# 0 in dry-run mode, 1 otherwise
############################################################
is_dry_run() {
[ "$DRY_RUN" = "true" ] || return 1
}
############################################################
# Check if the install script is
# running in non-interactive mode.
#
# Returns:
# 0 in non-interactive mode, 1 otherwise
############################################################
is_non_interactive() {
[ "$NON_INTERACTIVE" = "true" ] || return 1
}
############################################################
# Check if we should skip installing hooks
# into existing repositories.
#
# Returns:
# 0 if we should skip, 1 otherwise
############################################################
should_skip_install_into_existing_repositories() {
[ "$SKIP_INSTALL_INTO_EXISTING" = "true" ] ||
use_core_hookspath ||
[ "$(git config --global githooks.useCoreHooksPath)" = "true" ] || return 1
}
############################################################
# Check if the install script is
# running for a single repository only.
#
# Returns:
# 0 in single repository install mode, 1 otherwise
############################################################
is_single_repo_install() {
[ "$SINGLE_REPO_INSTALL" = "true" ] || return 1
}
############################################################
# Check if the install script is
# running with `--only-server-hooks` or if globally
# configured to run only server hooks.
#
# Returns:
# 0 if only server hooks should be installed, 1 otherwise
############################################################
install_only_server_hooks() {
[ "$INSTALL_ONLY_SERVER_HOOKS" = "true" ] ||
[ "$(git config --global githooks.maintainOnlyServerHooks)" = "true" ] ||
[ "$(git config --global githooks.maintainOnlyServerHooks)" = "Y" ] || # Legacy
return 1
}
############################################################
# Check if the install script is
# running with `--use-core-hookspath`.
#
# Returns:
# 0 if using `core.hooksPath`, 1 otherwise
############################################################
use_core_hookspath() {
[ "$USE_CORE_HOOKSPATH" = "true" ] || return 1
}
############################################################
# Check if the install script is an autoupdate.
#
# Returns:
# 0 if its an update, 1 otherwise
############################################################
is_autoupdate() {
[ "$INTERNAL_AUTOUPDATE" = "true" ] || return 1
}
############################################################
# Check if the install script is a post update step
#
# Returns:
# 0 if its an update, 1 otherwise
############################################################
is_postupdate() {
[ "$INTERNAL_POSTUPDATE" = "true" ] || return 1
}
############################################################
# Disable user input by redirecting /dev/null
# to the standard input of the install script.
#
# Returns:
# None
############################################################
disable_tty_input() {
exec </dev/null
}
############################################################
# Checks whether the given directory
# is a Git repository (bare included) or not.
#
# Returns:
# 1 if failed, 0 otherwise
############################################################
is_git_repo() {
git -C "$1" rev-parse >/dev/null 2>&1 || return 1
}
############################################################
# Checks whether the current working directory
# is a Git repository and sets `CWD_GIT_DIR` to the
# git directory.
#
# Returns:
# 1 if failed, 0 otherwise
############################################################
get_cwd_git_dir() {
if ! is_git_repo "$(pwd)"; then
echo "! The current directory is not a Git repository" >&2
unset CWD_GIT_DIR
return 1
else
CWD_GIT_DIR="$(cd "$(git rev-parse --git-common-dir 2>/dev/null)" && pwd)"
fi
return 0
}
############################################################
# Gets all worktrees attached to the given repository `$1`
# and sets `REPO_WORKTREES`
#
# Returns: None
############################################################
get_repo_worktrees() {
# This feature is kind of buggy in earlier version of git < 2.28.0
# it returns a git directory instead of the work tree
# We strip "/.git" from the output.
git -C "$1" worktree list --porcelain | grep "worktree" | sed "s/worktree //g" | sed "s@/\.git@@"
}
############################################################
# Prepare the target template directory variable,
# and make sure it points to a directory when set.
#
# Sets the ${TARGET_TEMPLATE_DIR} variable.
#
# Returns:
# 1 if failed, 0 otherwise
############################################################
prepare_target_template_directory() {
if [ -z "$TARGET_TEMPLATE_DIR" ]; then
# Automatically find a template directory
if ! find_git_hook_templates; then
echo "! Git hook templates directory not found" >&2
return 1
fi
else
# The user provided a template directory, check it and
# add `hooks` which is needed.
if [ ! -d "$TARGET_TEMPLATE_DIR" ]; then
echo "! Git hook templates directory does not exists" >&2
return 1
else
TARGET_TEMPLATE_DIR="$TARGET_TEMPLATE_DIR/hooks"
fi
fi
# TARGET_TEMPLATE_DIR is now `<template-dir>/hooks`
# Create the `hooks` directory if it does not yet exist:
if [ ! -d "$TARGET_TEMPLATE_DIR" ]; then
if ! mkdir -p "$TARGET_TEMPLATE_DIR" >/dev/null 2>&1; then
echo "! Could not create template folder \`$TARGET_TEMPLATE_DIR\`" >&2
return 1
fi
fi
# Up to now the directories would not have been set if
# --use-core-hookspath is used, we set it now here.
if ! is_dry_run && use_core_hookspath &&
! set_githooks_directory --core-hooks-path "$TARGET_TEMPLATE_DIR"; then
echo "! Failed to set \`core.hooksPath\` to template dir" >&2
fi
}
############################################################
# Try to find the directory where the Git
# hook templates are currently.
#
# Sets ${TARGET_TEMPLATE_DIR} if found.
#
# Returns: 0 if successful found, otherwise 1
############################################################
find_git_hook_templates() {
INSTALL_USES_CORE_HOOKS_PATH=$(git config --global githooks.useCoreHooksPath)
# 1. from environment variables
mark_directory_as_target "$GIT_TEMPLATE_DIR" "hooks" && return 0
# 2. from git config
if use_core_hookspath || [ "$INSTALL_USES_CORE_HOOKS_PATH" = "true" ]; then
mark_directory_as_target "$(git config --global core.hooksPath)" && return 0
elif ! use_core_hookspath || [ "$INSTALL_USES_CORE_HOOKS_PATH" = "false" ]; then
mark_directory_as_target "$(git config --global init.templateDir)" "hooks" && return 0
fi
# 3. from the default location
mark_directory_as_target "/usr/share/git-core/templates/hooks" && return 0
# If we have an installation, and have not found the template folder by now...
if [ -n "$INSTALL_USES_CORE_HOOKS_PATH" ]; then
echo "! Your installation is corrupt." >&2
echo " The global value \`githooks.useCoreHooksPath = $INSTALL_USES_CORE_HOOKS_PATH\`" >&2
echo " is set but the corresponding hook templates directory" >&2
echo " is not found." >&2
return 1
fi
# 4. Setup new folder if running non-interactively and no folder is found by now
if is_non_interactive; then
setup_new_templates_folder || return 1
return 0 # we are finished either way here
fi
# 5. try to search for it on disk
printf 'Could not find the Git hook template directory. '
printf 'Do you want to search for it? [y/N] '
read -r DO_SEARCH </dev/tty
if [ "${DO_SEARCH}" = "y" ] || [ "${DO_SEARCH}" = "Y" ]; then
search_for_templates_dir
if [ "$TARGET_TEMPLATE_DIR" != "" ]; then
printf 'Do you want to set this up as the Git template directory for future use? [y/N] '
read -r MARK_AS_TEMPLATES </dev/tty
if [ "$MARK_AS_TEMPLATES" = "y" ] || [ "$MARK_AS_TEMPLATES" = "Y" ]; then
TEMPLATE_DIR=$(dirname "$TARGET_TEMPLATE_DIR")
if ! is_dry_run &&
! use_core_hookspath &&
! set_githooks_directory --template-dir "$TEMPLATE_DIR"; then
echo "! Failed to set it up as Git template directory" >&2
return 1
fi
return 0
fi
return 1
fi
fi
# 6. set up as new
printf "Do you want to set up a new Git templates folder? [y/N] "
read -r SETUP_NEW_FOLDER </dev/tty
if [ "${SETUP_NEW_FOLDER}" = "y" ] || [ "${SETUP_NEW_FOLDER}" = "Y" ]; then
setup_new_templates_folder || return 1
return 0
fi
return 1
}
############################################################
# Sets the ${TARGET_TEMPLATE_DIR} variable if the
# `$1` is a writable directory.
# `$2` is a subfolder applied to the result.
# Returns: 0 if `$TARGET_TEMPLATE_DIR` is set, 1 otherwise
############################################################
mark_directory_as_target() {
TARGET="$1"
if [ -z "$TARGET" ]; then
return 1
fi
# Check if its writable
if [ -w "$TARGET" ]; then
TARGET_TEMPLATE_DIR="$TARGET"
else
# Try to see if the path is given with a tilde
TILDE_REPLACED=$(echo "$TARGET" | awk 'gsub("~", "'"$HOME"'", $0)')
if [ -n "$TILDE_REPLACED" ] && [ -w "$TILDE_REPLACED" ]; then
TARGET_TEMPLATE_DIR="$TILDE_REPLACED"
else
return 1
fi
fi
# Add the subfolder if given
if [ -n "$TARGET_TEMPLATE_DIR" ] && [ "$2" != "" ]; then
TARGET_TEMPLATE_DIR="$TARGET_TEMPLATE_DIR/$2"
fi
return 0
}
############################################################
# Search for the template directory on the file system.
#
# Sets ${TARGET_TEMPLATE_DIR} if found.
#
# Returns:
# None
############################################################
search_for_templates_dir() {
if [ -d "/usr" ]; then
echo "Searching for potential locations in /usr ..."
search_pre_commit_sample_file "/usr"