Skip to content

Commit c3579a8

Browse files
authored
Merge pull request #204 from sysprog21/refine-commit-hook
Refine commit hook
2 parents 105253f + 6c6aa7d commit c3579a8

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

scripts/commit-msg.hook

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,31 @@ validate_commit_message() {
251251

252252
URL_REGEX='^[[:blank:]]*(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]'
253253

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
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+
# Skip the first line (the subject) because the limit applies to the body.
259+
if [ "$i" -eq 0 ]; then
260+
continue
261+
fi
262+
263+
LINE="${COMMIT_MSG_LINES[$i]}"
264+
265+
# Skip the line if it is a comment.
266+
if [[ "$LINE" =~ ^[[:space:]]*# ]]; then
267+
continue
268+
fi
269+
270+
# Trim leading and trailing whitespace.
271+
TRIMMED_LINE="${LINE#"${LINE%%[![:space:]]*}"}"
272+
TRIMMED_LINE="${TRIMMED_LINE%"${TRIMMED_LINE##*[![:space:]]}"}"
273+
LINE_NUMBER=$((i+1))
274+
275+
if [ "${#TRIMMED_LINE}" -gt 72 ] && ! [[ "$TRIMMED_LINE" =~ $URL_REGEX ]]; then
276+
add_warning "$LINE_NUMBER" "Wrap the body at 72 characters (${#TRIMMED_LINE} chars)"
277+
fi
278+
done
267279

268280
# 7. Ensure the commit subject has more than one word.
269281
# ------------------------------------------------------------------------------
@@ -282,6 +294,12 @@ validate_commit_message() {
282294
add_warning 1 "Avoid using parentheses '()' in commit subjects"
283295
fi
284296

297+
# 7c. Alert if the commit subject starts with "Implementation"
298+
# ------------------------------------------------------------------------------
299+
if [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^(First|My|Implementation|Creation|Modification) ]]; then
300+
add_warning 1 "Commit subject should use imperative mood"
301+
fi
302+
285303
# 8. Use the body to explain what and why vs. how
286304
# ------------------------------------------------------------------------------
287305

scripts/pre-commit.hook

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ done
88
# We suppress the checkLevelNormal warning for Cppcheck versions 2.11 and above.
99
# Please refer to issues/153 for more details.
1010
CPPCHECK_suppresses="--inline-suppr harness.c \
11+
--suppress=checkersReport \
1112
--suppress=unmatchedSuppression \
1213
--suppress=normalCheckLevelMaxBranches \
1314
--suppress=missingIncludeSystem \

0 commit comments

Comments
 (0)