Repository navigation
Expand file tree
/
Copy pathgit-config-pre-commit
More file actions
101 lines (93 loc) · 3.22 KB
/
Copy pathgit-config-pre-commit
File metadata and controls
101 lines (93 loc) · 3.22 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
#!/usr/bin/env bash
# Canonical : https://github.com/lduran2/git-unzip-all/git-config-pre-commit
# Configure the pre-commit hook to run hooks/unzip-all.
# By : Leomar Duran <https://github.com/lduran2>
# When : 2022-02-01t09:15R
# Version : 1.3.2
#
# CHANGELOG :
# v1.3.2 - 2022-02-01t09:15R
# cleaned up logs, `HOOK_TO_SKIP`
#
# v1.3.1 - 2022-02-01t09:03R
# logging for pre-commit templating
#
# v1.3.0 - 2022-02-01t08:52R
# single file for pre-commit template
# variable template
#
# v1.2.3 - 2022-02-01t07:32R
# checking for hook file before assembly
#
# v1.2.2 - 2022-02-01t06:30R
# staging pattern file
#
# v1.2.1 - 2022-02-01t06:02R
# fixed `$SCRIPT_ROOT.` -> `$SOURCE_DIR`
#
# v1.2.0 - 2022-02-01t05:11R
# use `dirname` to hangle resource locality
#
# v1.1.4 - 2022-01-25t22:42R
# add variable references in strings
#
# v1.1.3 - 2022-01-25t22:32R
# added `SCRIPT_ROOT` for files rel to script
#
# v1.1.2 - 2022-01-25t22:24R
# fixed the unformated strings for the life cycle messages
#
# v1.1.1 - 2022-01-25t22:16R
# fixed the template strings for the config filenames
#
# v1.1.0 - 2022-01-25t22:02R
# sets up the pattern file
# reconfigured for submodule
#
# v1.0.0 - 2022-01-25t19:39R
# composes the hook file for pre-commit
#
# working directories
declare -r SOURCE_DIR=$(dirname $0)
declare -r ROOT_DIR="$SOURCE_DIR/../.."
declare -r TEMPLATE_DIR="$SOURCE_DIR/templates"
# hook file to which to attach `./main`
declare -r HOOK_FILE="$ROOT_DIR/.git/hooks/pre-commit"
# contains the patterns of files to unzip (relative to root)
declare -r ROOT_TO_PATTERN_FILE=".unzip"
# the unzip pattern file resolved to this script
declare -r PATTERN_FILE="$ROOT_DIR/$ROOT_TO_PATTERN_FILE"
# line in the template to which to skip (if the hook file exists)
declare -r HOOK_TO_SKIP=2
# find this directory relative to the repo root
declare -r ROOT_TO_SOURCE=$(realpath --relative-to=$ROOT_DIR $SOURCE_DIR)
# check if the hook file exists before starting assembling
# [] returns 0 if the file exists, 1 otherwise
[ -f "$HOOK_FILE" ] ; should_create_hook=$?
# read from the template
cat "$TEMPLATE_DIR/pre-commit" |
# if the hook file does not exist
if [ $should_create_hook -ne 0 ] ; then
>&2 echo 'Creating the pre-commit hook file . . .'
# just forward the file as is
cat
else # to [ $should_create_hook -ne 0 ]
>&2 echo 'Appending `unzip-all` to the pre-commit hook file . . .'
# otherwise, newline and skip the script shebang
echo ''
tail "-n+$HOOK_TO_SKIP"
fi | # [ $should_create_hook -ne 0 ]
# replace the template source path
sed -e "s:\\\$ROOT_TO_SOURCE:$ROOT_TO_SOURCE:g" >> "$HOOK_FILE"
# end cat "$TEMPLATE_DIR/pre-commit"
# if the pattern file does not exist
if [[ ! -f "$PATTERN_FILE" ]] ; then
>&2 echo "Creating the pattern file \`/$ROOT_TO_PATTERN_FILE\` . . ."
# copy it from the template
cat "$TEMPLATE_DIR/$ROOT_TO_PATTERN_FILE" > "$PATTERN_FILE"
fi # [[ ! -f "$PATTERN_FILE" ]]
>&2 echo "Staging the pattern file \`/$ROOT_TO_PATTERN_FILE\` . . ."
# either way, stage the pattern file
git add "$PATTERN_FILE"
# confirm finished
>&2 echo 'Done.'