Skip to content

Commit

Permalink
Merge pull request #204 from sysprog21/refine-commit-hook
Browse files Browse the repository at this point in the history
Refine commit hook
  • Loading branch information
jserv authored Feb 23, 2025
2 parents 105253f + 6c6aa7d commit c3579a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
44 changes: 31 additions & 13 deletions scripts/commit-msg.hook
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,31 @@ validate_commit_message() {

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

# Ensure the commit message lines are loaded into an array.
readarray -t COMMIT_MSG_LINES < "$COMMIT_MSG_FILE"

for i in "${!COMMIT_MSG_LINES[@]}"; do
LINE="${COMMIT_MSG_LINES[$i]}"
# Trim leading and trailing whitespace.
TRIMMED_LINE="${LINE#"${LINE%%[![:space:]]*}"}"
TRIMMED_LINE="${TRIMMED_LINE%"${TRIMMED_LINE##*[![:space:]]}"}"
LINE_NUMBER=$((i+1))
if [ "${#TRIMMED_LINE}" -gt 72 ] && ! [[ "$TRIMMED_LINE" =~ $URL_REGEX ]]; then
add_warning "$LINE_NUMBER" "Wrap the body at 72 characters (${#TRIMMED_LINE} chars)"
fi
done
# Ensure the commit message lines are loaded into an array.
readarray -t COMMIT_MSG_LINES < "$COMMIT_MSG_FILE"

for i in "${!COMMIT_MSG_LINES[@]}"; do
# Skip the first line (the subject) because the limit applies to the body.
if [ "$i" -eq 0 ]; then
continue
fi

LINE="${COMMIT_MSG_LINES[$i]}"

# Skip the line if it is a comment.
if [[ "$LINE" =~ ^[[:space:]]*# ]]; then
continue
fi

# Trim leading and trailing whitespace.
TRIMMED_LINE="${LINE#"${LINE%%[![:space:]]*}"}"
TRIMMED_LINE="${TRIMMED_LINE%"${TRIMMED_LINE##*[![:space:]]}"}"
LINE_NUMBER=$((i+1))

if [ "${#TRIMMED_LINE}" -gt 72 ] && ! [[ "$TRIMMED_LINE" =~ $URL_REGEX ]]; then
add_warning "$LINE_NUMBER" "Wrap the body at 72 characters (${#TRIMMED_LINE} chars)"
fi
done

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

# 7c. Alert if the commit subject starts with "Implementation"
# ------------------------------------------------------------------------------
if [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^(First|My|Implementation|Creation|Modification) ]]; then
add_warning 1 "Commit subject should use imperative mood"
fi

# 8. Use the body to explain what and why vs. how
# ------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions scripts/pre-commit.hook
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ done
# We suppress the checkLevelNormal warning for Cppcheck versions 2.11 and above.
# Please refer to issues/153 for more details.
CPPCHECK_suppresses="--inline-suppr harness.c \
--suppress=checkersReport \
--suppress=unmatchedSuppression \
--suppress=normalCheckLevelMaxBranches \
--suppress=missingIncludeSystem \
Expand Down

0 comments on commit c3579a8

Please sign in to comment.