-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg
executable file
·375 lines (316 loc) · 10.1 KB
/
commit-msg
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
#!/bin/bash
# This script is used by the commit-msg hook to check the commit message
# and to update the commit message if necessary with the correct format.
# There is based to https://www.conventionalcommits.org/en/v1.0.0/
#
# Format:
# <type>[optional scope]: <subject>
# (max 80 chars, no upper case, no final dot)
#
# [optional description]
#
# [footer(s)]
# (* && Signed-off-by: <name> <email>)
if [[ ${BASH_SOURCE[0]} -ef "$0" ]]; then
# source config and common file
source "${0%/*}/components/config"
source "${0%/*}/components/common"
else
# source from unit test
source "./components/config"
source "./components/common"
fi
# shellcheck disable=SC2206
## CM_TYPE is an array of commit message type,
## TYPE_EXTRA can be used to add extra commit message type
CM_TYPE=(
"build"
"chore"
"ci"
"docs"
"feat"
"fix"
"perf"
"refactor"
"style"
"test"
"merge"
"wip"
${TYPE_EXTRA[*]}
)
# shellcheck disable=SC2206
## CM_EMOJI is an array of emoji type, there is mapped to the commit type
## TYPE_EXTRA_EMOJI can be used to add extra emoji type
CM_EMOJI=(
"🏗️"
"🙈"
"🚀"
"📝"
"✨"
"🐛"
"⚡️"
"♻️"
"💄"
"🧪"
"🔀"
"🚧"
${TYPE_EXTRA_EMOJI[*]}
)
## RGX_MESSAGE and RGX_MESSAGE_EMOJI are the regex for commit message header
BISF="$IFS"
IFS="|"
RGX_EMOJI="(?<emoji>${CM_EMOJI[*]})"
RGX_HEADER="^(?:${RGX_EMOJI} )?(?<type>${CM_TYPE[*]})(?:\((?<scope>[^)A-Z]+)\))?(?<breaking>!)?:"
RGX_MESSAGE="^${RGX_HEADER}\s(?<subject>[^A-Z].+[^.])$"
RGX_MESSAGE_EMOJI="^${RGX_EMOJI}(?:\((?<scope>[^)A-Z]+)\))?(?<breaking>!)?:?\s(?<subject>[^A-Z].+[^.])$"
IFS="$BISF"
unset BISF
## CM_FOOTER is the regex for commit message footer
CM_FOOTER="(?:[^:]+:\s.+\n)*(?:Signed-off-by:\s(?<name>.+)\s<(?<email>[^@]+@.+)>)"
## Format description
HEADER_RULE="Commit message format: <type>(optional scope): <subject> (max $HEADER_MAX_LENGTH chars, no upper case, no final dot, with '!' before the colon for breaking change)"
FOOTER_RULE="Commit footer format: Signed-off-by: <name> <email>"
## print_available_type is a function to list all available commit type
print_available_type() {
local _types=()
for i in "${!CM_TYPE[@]}"; do
if [[ ${CM_EMOJI[$i]} == "" ]]; then
_types+=("${CM_TYPE[$i]}")
else
_types+=("${CM_TYPE[$i]}: ${CM_EMOJI[$i]}")
fi
done
info "Available commit type :"
info "$(printf "%s, " "${_types[@]}")"
}
## format_checker is the function to check and fix the commit message format
format_checker() {
local _section=1
local _empty_line=$FALSE
local _content=()
while read -r line; do
# skip comment line
if [[ $line == "#"* ]]; then
continue
fi
if [[ $line == "" ]]; then
# two empty lines are not allowed we can safely ignore it
if [[ $_empty_line -eq $TRUE ]]; then
continue
fi
# first line can't be empty
if [[ ${_content[$_section]} == "" ]]; then
error "A section can't be empty, probably a missing header"
return $FAILURE
fi
# an empty line is expect after a section
_section=$((_section + 1))
_empty_line=$TRUE
continue
fi
# reset empty line flag
_empty_line=$FALSE
# save section content
if [[ ${_content[$_section]} == "" ]]; then
_content[$_section]="$line"
else
_content[$_section]+=$'\n'"$line"
fi
continue
done <<<"$1"
if [[ $_section -gt 1 ]]; then
# if current section is not empty and not contain signedoff add a section,
# but if is empty and the previous one contains signedoff delete the last one
if [[ -n ${_content[$_section]} ]] && ! grep -sPq "Signed-off-by" <<<"${_content[$_section]}"; then
_section=$((_section + 1))
elif [[ -z ${_content[$_section]} ]] && grep -sPq "Signed-off-by" <<<"${_content[$((_section - 1))]}"; then
_section=$((_section - 1))
fi
else
# in case of only one section are found
_section=$((_section + 1))
fi
# check and fix the header format
if ! _content[1]=$(format_header "${_content[1]}"); then
return 1
fi
# check the footer format and required signature
if ! _content[$_section]=$(format_footer "${_content[$_section]}"); then
return 1
fi
# return the result with an empty line between each sections
for id in "${!_content[@]}"; do
if [[ $id -ne 1 ]]; then
echo ""
fi
echo "${_content[$id]}"
done
}
## format_header is the function to check and fix the commit message header format
format_header() {
local _error=$FALSE
# check if required arguments are provided
if [[ -z $1 ]]; then
return $FAILURE
fi
local _line="$1"
# check if there is multiple line
if [[ $_line == *$'\n'* ]]; then
error "Multiple line are not allowed in the header"
return $FAILURE
fi
# check the header length
if [[ ${#_line} -gt $HEADER_MAX_LENGTH ]]; then
warning "Current header length is ${#_line} '${_line}'"
error "The header is too long, max length is $HEADER_MAX_LENGTH"
return $FAILURE
fi
# Check the commit header
if ! (grep -sPq "${RGX_HEADER}.*" <<<"$_line"); then
print_available_type
error "Invalid header format in commit message"
error "$_line"
error "$HEADER_RULE"
return $FAILURE
fi
# check the header format RGX_MESSAGE or RGX_MESSAGE_EMOJI
(
grep -sPq "${RGX_MESSAGE}" <<<"$_line" ||
grep -sPq "${RGX_MESSAGE_EMOJI}" <<<"$_line"
) || _error=$TRUE
# try to auto fix the header if needed
if [[ $_error -eq $TRUE ]] && [[ $HEADER_AUTOFIX -eq $TRUE ]]; then
# reset error flag
_error=$FALSE
# Convert the header to lowercase
_line=$(tr '[:upper:]' '[:lower:]' <<<"$_line") || {
warning "Line: '$_line'"
error "Failed to convert the header to lowercase"
return $FAILURE
}
# Remove dot at the end of the header if any
if [[ $_line == *"." ]]; then
_line=${_line::-1}
fi
# try to fix bad colon
_line=$(sed -E 's/^([^: ]+)\s*:\s*/\1: /' <<<"$_line") || {
warning "Line: '$_line'"
error "Failed to remove bad semicolon"
return $FAILURE
}
# check the format again
(grep -sPq "${RGX_MESSAGE}" <<<"$_line" || grep -sPq "${RGX_MESSAGE_EMOJI}" <<<"$_line") || {
error "Invalid header format can't be fixed"
error "$_line"
error "$HEADER_RULE"
return $FAILURE
}
fi
if [[ $_error -eq $TRUE ]]; then
error "Invalid header format"
error "$_line"
error "$HEADER_RULE"
return $FAILURE
fi
# emoji
_line="$(emoji_me "$_line")" || {
error "Failed to apply emoji"
return $FAILURE
}
# return the result
echo "$_line"
return $SUCCESS
}
## format_footer is the function to check and fix the commit message footer
format_footer() {
local _line="$1"
# if signoff is required
if [[ $SIGNOFF_REQUIRED -eq $TRUE ]]; then
local _error=$FALSE
# check the footer format
if ! (grep -sPq "${CM_FOOTER}" <<<"$_line"); then
_error=$TRUE
fi
# autofix if needed
if [[ $_error -eq $TRUE ]] && [[ $SIGNOFF_AUTOFIX -eq $TRUE ]]; then
_line=$(sed -E 's/\n?Signed-off-by:.*//' <<<"$_line")
if [[ -z $GIT_AUTHOR_EMAIL ]] || [[ -z $GIT_AUTHOR_NAME ]]; then
error "Can't autofix the footer, missing author information"
return $FAILURE
fi
if [[ $_line != *"Signed-off-by"* ]]; then
_error=$FALSE
if [[ -n $_line ]]; then
_line+=$'\n'
fi
_line+="Signed-off-by: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
fi
fi
if [[ $_error -eq $TRUE ]]; then
error "Invalid footer format Signed-off-by is missing or badly formatted"
return $FAILURE
fi
fi
# return the result
echo "$_line"
return $SUCCESS
}
## emoji_me is a function to apply emoji to a commit message
emoji_me() {
# check if required arguments are provided
if [[ -z $1 ]]; then
return $FAILURE
fi
local _line="$1"
# check if emoji_me is disable or if line already start with an emoji
if [[ $TYPE_TO_EMOJI -eq $FALSE ]] ||
(grep -sPq "^${RGX_EMOJI}" <<<"$_line"); then
echo "$_line"
return $SUCCESS
fi
# find the commit type in CM_TYPE array
local _type_id=-1
for i in "${!CM_TYPE[@]}"; do
if (grep -sPq "^${CM_TYPE[$i]}" <<<"$_line"); then
_type_id="$i"
break
fi
done
if [[ _type_id -eq -1 ]]; then
error "No allowed commit type found in the commit message: $_line"
return $FAILURE
fi
# check if an emoji is available at this id
if [[ ${CM_EMOJI[$_type_id]} == "" ]]; then
error "No emoji found for this commit type: ${CM_TYPE[$_type_id]}"
return $FAILURE
fi
# apply the emoji
case $TYPE_TO_EMOJI in
"$TRUE")
echo "$_line" | sed -E "s/${CM_TYPE[$_type_id]}:? ?/${CM_EMOJI[$_type_id]} /"
;;
"$SUPERPOSITION")
echo "${CM_EMOJI[$_type_id]} ${_line}"
;;
*)
return $FAILURE
;;
esac
}
## MAIN
# do not execute if this script is sourced required for unit testing
if [[ ${BASH_SOURCE[0]} -ef "$0" ]]; then
# check if arg is a regular file
if [[ ! -f $1 ]]; then
warning "Argument: $1"
error "Gived argument is not a regular file"
exit $FAILURE
fi
# process the file
_result="$(format_checker "$(<"$1")")" && {
echo "$_result" >"$1"
exit $SUCCESS
}
exit $FAILURE
fi