Skip to content

Commit 6f5a552

Browse files
authored
Merge pull request #194 from sysprog21/refine-git-hook
Refine git hook
2 parents 9013ef3 + 00e6069 commit 6f5a552

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

scripts/commit-msg.hook

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -249,53 +249,69 @@ validate_commit_message() {
249249
# 6. Wrap the body at 72 characters
250250
# ------------------------------------------------------------------------------
251251

252-
URL_REGEX='^[[:blank:]]*(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]'
253-
254-
# Ensure the commit message lines are loaded into an array.
255-
readarray -t COMMIT_MSG_LINES < "$COMMIT_MSG_FILE"
256-
257-
for i in "${!COMMIT_MSG_LINES[@]}"; do
258-
LINE="${COMMIT_MSG_LINES[$i]}"
259-
# Trim leading and trailing whitespace.
260-
TRIMMED_LINE="${LINE#"${LINE%%[![:space:]]*}"}"
261-
TRIMMED_LINE="${TRIMMED_LINE%"${TRIMMED_LINE##*[![:space:]]}"}"
262-
LINE_NUMBER=$((i+1))
263-
if [ "${#TRIMMED_LINE}" -gt 72 ] && ! [[ "$TRIMMED_LINE" =~ $URL_REGEX ]]; then
264-
add_warning "$LINE_NUMBER" "Wrap the body at 72 characters (${#TRIMMED_LINE} chars)"
265-
fi
266-
done
252+
URL_REGEX='^[[:blank:]]*(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]'
253+
254+
# Ensure the commit message lines are loaded into an array.
255+
readarray -t COMMIT_MSG_LINES < "$COMMIT_MSG_FILE"
256+
257+
for i in "${!COMMIT_MSG_LINES[@]}"; do
258+
LINE="${COMMIT_MSG_LINES[$i]}"
259+
# Trim leading and trailing whitespace.
260+
TRIMMED_LINE="${LINE#"${LINE%%[![:space:]]*}"}"
261+
TRIMMED_LINE="${TRIMMED_LINE%"${TRIMMED_LINE##*[![:space:]]}"}"
262+
LINE_NUMBER=$((i+1))
263+
if [ "${#TRIMMED_LINE}" -gt 72 ] && ! [[ "$TRIMMED_LINE" =~ $URL_REGEX ]]; then
264+
add_warning "$LINE_NUMBER" "Wrap the body at 72 characters (${#TRIMMED_LINE} chars)"
265+
fi
266+
done
267267

268-
# 7. Use the body to explain what and why vs. how
268+
# 7. Ensure the commit subject has more than one word.
269269
# ------------------------------------------------------------------------------
270270

271-
# 8. Do no write single worded commits
271+
if [ "$(echo "${COMMIT_SUBJECT_TO_PROCESS}" | wc -w)" -le 1 ]; then
272+
add_warning 1 "Do not write single-word commits. Provide a descriptive subject"
273+
fi
274+
275+
# 7a. Avoid using C source filenames as the commit subject.
276+
if [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^[_a-zA-Z0-9]+\.[ch]$ ]]; then
277+
add_warning 1 "Avoid mentioning C source filenames in the commit subject"
278+
fi
279+
280+
# 11a. Disallow parentheses in the commit subject.
281+
if [[ ${COMMIT_SUBJECT_TO_PROCESS} =~ [\(\)] ]]; then
282+
add_warning 1 "Avoid using parentheses '()' in commit subjects"
283+
fi
284+
285+
# 8. Use the body to explain what and why vs. how
272286
# ------------------------------------------------------------------------------
273287

274-
COMMIT_SUBJECT_WORDS=(${COMMIT_SUBJECT_TO_PROCESS})
275-
test "${#COMMIT_SUBJECT_WORDS[@]}" -gt 1
276-
test $? -eq 0 || add_warning 1 "Do no write single worded commits"
288+
# Count non-comment, non-blank lines excluding "Change-Id:".
289+
NON_COMMENT_COUNT=$(sed '/^[[:space:]]*#/d;/^[[:space:]]*$/d;/^[[:space:]]*Change-Id:/d' "${COMMIT_MSG_FILE}" | wc -l | xargs)
277290

278-
# 8a. Do not mention C source filenames
279-
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ [_a-zA-Z0-9]+\.[ch]$ ]]
280-
test $? -eq 1 || add_warning 1 "Avoid mentioning C source filenames"
291+
# If the subject is oversimplified for a queue function OR queue.c is modified,
292+
# and there is only one meaningful line, issue a warning.
293+
if { [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^(Implement|Finish|Complete)[[:space:]]+q_[[:alnum:]_]+\(?\)?$ ]] || \
294+
git diff --cached --name-only | grep -Eq '(^|/)queue\.c$'; } && [ "${NON_COMMENT_COUNT}" -le 1 ]; then
295+
add_warning 1 "Commit message oversimplified. Use the commit message body to explain what and why."
296+
fi
281297

282298
# 9. Do not start the subject line with whitespace
283299
# ------------------------------------------------------------------------------
284300

285301
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ ^[[:blank:]]+ ]]
286302
test $? -eq 1 || add_warning 1 "Do not start the subject line with whitespace"
287303

288-
# 10. Avoid single word commit messages in subject
304+
# 10. Disallow backticks anywhere in the commit message.
289305
# ------------------------------------------------------------------------------
290306

291-
word_count=$(echo "$COMMIT_SUBJECT_TO_PROCESS" | wc -w)
292-
test "$word_count" -gt 1
293-
test $? -eq 0 || add_warning 1 "Commit subject should contain more than one word. Summarize your changes"
307+
if sed '/^[[:space:]]*#/d;/^[[:space:]]*$/d' "${COMMIT_MSG_FILE}" | grep -q "\`"; then
308+
add_warning 1 "Avoid using backticks in commit messages"
309+
fi
294310

295311
# 11. Avoid commit subject that simply states a file update (e.g. "Update console.c")
296312
# ------------------------------------------------------------------------------
297313

298-
if [[ $COMMIT_SUBJECT_TO_PROCESS =~ ^Update[[:space:]]+([^[:space:]]+)$ ]]; then
314+
if [[ ${COMMIT_SUBJECT_TO_PROCESS} =~ ^Update[[:space:]]+([^[:space:]]+)$ ]]; then
299315
candidate="${BASH_REMATCH[1]}"
300316
# Only warn if the candidate filename ends with .c or .h
301317
if [[ $candidate =~ \.(c|h)$ ]]; then

0 commit comments

Comments
 (0)