-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconventional-commit
More file actions
executable file
·299 lines (243 loc) · 8.66 KB
/
conventional-commit
File metadata and controls
executable file
·299 lines (243 loc) · 8.66 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
#!/bin/sh
# =============================================================================
# BSD 2-Clause License
#
# Copyright (c) 2019, Veaceslav Medvedev
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============================================================================
FCR='\033[1;31m' # Red
FCG='\033[1;32m' # Green
FCY='\033[0;33m' # Yellow
FCS='\033[0;37m' # Light gray (silver)
NC='\033[0m'
CMD="conventional-commit"
VERSION="1.0.0"
HOME="https://github.com/slavcodev/git-hooks-scripts"
ABOUT="${FCG}Conventional commit title${NC} version ${FCY}$VERSION${NC}"
OPTION_EXCLUDE_BRANCH="excluded-branch"
OPTION_EXCLUDE_COMMIT="excluded-commit"
OPTION_PREFIX_PATTERN="prefix-pattern"
OPTION_SUFFIX_PATTERN="suffix-pattern"
OPTION_MAX_LENGTH="max-length"
OPTION_EXCLUDE_BRANCH_DEFAULT="master"
OPTION_EXCLUDE_COMMIT_DEFAULT=$(printf "Merge branch\nRevert")
OPTION_MAX_LENGTH_DEFAULT=75
HELP="$ABOUT
Git 'commit-msg' hook that looks for patterns the branch name
and uses the matches as prefix or suffix of every commit title.
This is useful if you have convention to use issue number in the branch,
and want to add it automatically to the commit title.
For example, you have branch like 'FOO-9999-add-hook', on adding the commit 'Add Hook,
the hook may add suffix 'Add Hook (FOO-9999)' or prefix '[FOO-9999] Add Hook'.
Note: you can use 'commit --no-verify' to skip 'commit-msg' hooks.
${FCY}Example of config:${NC}
${FCS}~~~
[${CMD}]
${OPTION_EXCLUDE_BRANCH} = master
${OPTION_EXCLUDE_BRANCH} = development
${OPTION_EXCLUDE_COMMIT} = Merge branch
${OPTION_EXCLUDE_COMMIT} = Revert
${OPTION_PREFIX_PATTERN} = "FOO-[0-9]+"
${OPTION_SUFFIX_PATTERN} = "FOO-[0-9]+"
${OPTION_MAX_LENGTH} = ${OPTION_MAX_LENGTH_DEFAULT}
~~~${NC}
$HOME
> Enjoy coding ❤️"
report_start() {
echo "${FCG}$@${NC}"
}
report_error() {
echo "${FCR}$@${NC}"
}
report_done() {
[[ $# > 0 ]] && echo "${FCG}✓ $@${NC}" || echo "${FCG}✓ Done${NC}"
}
report_test_success() {
echo " ✓ $@"
}
report_test_failure() {
echo " × ${FCR}$@${NC}"
}
chr() {
[ "$1" -lt 256 ] || return 1
printf "\\$(printf '%03o' "$1")"
}
ord() {
LC_CTYPE=C printf '%d\n' "'$1"
}
read_config() {
option="$1"
default="$2"
value="$(git config --get ${CMD}.${option})"
if [[ -z ${value} ]] ; then
echo ${default}
else
echo ${value}
fi
}
read_all_configs() {
option="$1"
default="$2"
value="$(git config --get-all ${CMD}.${option})"
if [[ -z "${value}" ]] ; then
echo "${default}"
else
echo "${value}"
fi
}
filter_string_by_prefixes() {
string="$1"
filters="$2"
while IFS=$(chr 10) read -ra options; do
for prefix in "${options[@]}"; do
if [[ "${string}" =~ "${prefix}" ]]; then
return
fi
done
done <<< "${filters}"
echo "${string}"
}
extract_addon_text() {
branch_name="$1"
pattern="$2"
if [[ -n "${pattern}" ]]; then
echo "${branch_name}" | grep -Eo "${pattern}"
else
echo ""
fi
}
add_prefix() {
commit_title="$1"
branch_name="$2"
pattern="$3"
prefix=$(extract_addon_text "${branch_name}" "${pattern}")
if [[ -n "${prefix}" ]]; then
prefix_exists=$(echo "${commit_title}" | grep -o "^\[${prefix}\] ")
if [[ -z "${prefix_exists}" ]]; then
commit_title="[${prefix}] ${commit_title}"
fi
fi
echo "${commit_title}"
}
add_suffix() {
commit_title="$1"
branch_name="$2"
pattern="$3"
suffix=$(extract_addon_text "${branch_name}" "${pattern}")
if [[ -n "${suffix}" ]]; then
suffix_exists=$(echo "${commit_title}" | grep -o " (${suffix})$")
if [[ -z "${suffix_exists}" ]]; then
commit_title="${commit_title} (${suffix})"
fi
fi
echo "${commit_title}"
}
main() {
report_start "Preparing conventional prefix or suffix for the commit..."
commit_file="${1}"
if [[ -f "${commit_file}" ]] ; then
is_file="Yes"
else
is_file="No"
fi
if [[ "${is_file}" == "Yes" ]] ; then
commit_title="$(head -n 1 "${commit_file}")"
else
commit_title="${commit_file}"
fi
if [[ -z "${commit_title}" ]]; then
return
fi
excluded_commits="$(read_all_configs "${OPTION_EXCLUDE_COMMIT}" "${OPTION_EXCLUDE_COMMIT_DEFAULT}")"
commit_title=$(filter_string_by_prefixes "${commit_title}" "${excluded_commits}")
if [[ -z "${commit_title}" ]]; then
return
fi
excluded_branches="$(read_all_configs "${OPTION_EXCLUDE_BRANCH}" "${OPTION_EXCLUDE_BRANCH_DEFAULT}")"
branch_name=$(filter_string_by_prefixes "$(git rev-parse --abbrev-ref HEAD)" "${excluded_branches}")
if [[ -z "${branch_name}" ]]; then
return
fi
mutated_commit_title="${commit_title}"
prefix_pattern="$(read_config ${OPTION_PREFIX_PATTERN})"
mutated_commit_title="$(add_prefix "${mutated_commit_title}" "branch""${branch_name}" "${prefix_pattern}")"
suffix_pattern="$(read_config ${OPTION_SUFFIX_PATTERN})"
mutated_commit_title="$(add_suffix "${mutated_commit_title}" "branch""${branch_name}" "${suffix_pattern}")"
max_length="$(read_config ${OPTION_MAX_LENGTH} ${OPTION_MAX_LENGTH_DEFAULT})"
if [[ ${#mutated_commit_title} -gt ${max_length} ]]; then
report_error "Too long commit message, must be maximum ${max_length} characters"
return 1
fi
if [[ "${mutated_commit_title}" != "${commit_title}" ]] ; then
if [[ "${is_file}" == "Yes" ]] ; then
sed -i -e 's/'"${commit_title}"'/'"${mutated_commit_title}"'/g' "${commit_file}"
echo "Commit message was changed to '$(head -n 1 "${commit_file}")'"
else
echo "Commit message was changed to '${mutated_commit_title}'"
fi
else
echo "No changes required"
fi
report_done
}
assert() {
rule=$1
actual=$2
expected=$3
if [[ "${actual}" == "${expected}" ]]; then
report_test_success "${rule}"
else
report_test_failure "${rule}" "(expected: ${expected}, got: ${actual})"
fi
}
test() {
report_start "Testing conventional prefix or prefix for the commit..."
assert "Allow strings started with specific prefixes" "$(filter_string_by_prefixes "foo" "$(printf "bar\nbaz")")" "foo"
assert "Skip strings started with specific prefixes" "$(filter_string_by_prefixes "foo" "$(printf "bar\nfoo")")" ""
assert "Skip pattern if not found in the branch name" "$(extract_addon_text "master" "FOO-\d+")" ""
assert "Find placeholder value in the branch name" "$(extract_addon_text "FOO-123-feature" "FOO-\d+")" "FOO-123"
assert "Find placeholder value in any part of branch name" "$(extract_addon_text "featureFOO-123" "FOO-\d+")" "FOO-123"
assert "Skip prefix rule if pattern not set in config" "$(add_prefix "Commit" "branch" "")" "Commit"
assert "Skip prefix rule if pattern not found in branch" "$(add_prefix "Commit" "branch" "FOO")" "Commit"
assert "Execute prefix rule if pattern found in branch" "$(add_prefix "Commit" "branch-FOO" "FOO")" "[FOO] Commit"
assert "Skip prefix rule if pattern already exists in commit message" "$(add_prefix "[FOO] Commit" "branch" "FOO")" "[FOO] Commit"
assert "Skip suffix rule if pattern not set in config" "$(add_suffix "Commit" "branch" "")" "Commit"
assert "Skip suffix rule if pattern not found in branch" "$(add_suffix "Commit" "branch" "FOO")" "Commit"
assert "Execute suffix rule if pattern found in branch" "$(add_suffix "Commit" "branch-FOO" "FOO")" "Commit (FOO)"
assert "Skip suffix rule if pattern already exists in commit message" "$(add_suffix "Commit (FOO)" "branch-FOO" "FOO")" "Commit (FOO)"
report_done
}
case "$1" in
-h|--help|-? )
echo "${HELP}"
;;
--test )
test "$@"
exit $?
;;
* )
main "$@"
exit $?
esac