Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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