Skip to content

Commit 779564d

Browse files
committed
Alert oversimplified commit messages for queue
For functions with names starting with "q_" (e.g., "Implement q_size" or "Finish q_new"), require that the commit message include more detail in the body. Change-Id: I7f3d6f1f13d2f56ae69e858617805baf4c25a1e8
1 parent 9013ef3 commit 779564d

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

scripts/commit-msg.hook

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,36 +249,46 @@ 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
267-
268-
# 7. Use the body to explain what and why vs. how
269-
# ------------------------------------------------------------------------------
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
270267

271-
# 8. Do no write single worded commits
268+
# 7. Do no write single worded commits
272269
# ------------------------------------------------------------------------------
273270

274271
COMMIT_SUBJECT_WORDS=(${COMMIT_SUBJECT_TO_PROCESS})
275272
test "${#COMMIT_SUBJECT_WORDS[@]}" -gt 1
276273
test $? -eq 0 || add_warning 1 "Do no write single worded commits"
277274

278-
# 8a. Do not mention C source filenames
275+
# 7a. Do not mention C source filenames
279276
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ [_a-zA-Z0-9]+\.[ch]$ ]]
280277
test $? -eq 1 || add_warning 1 "Avoid mentioning C source filenames"
281278

279+
# 8. Use the body to explain what and why vs. how
280+
# ------------------------------------------------------------------------------
281+
282+
# Count non-comment, non-blank lines excluding "Change-Id:".
283+
NON_COMMENT_COUNT=$(sed '/^[[:space:]]*#/d;/^[[:space:]]*$/d;/^[[:space:]]*Change-Id:/d' "$COMMIT_MSG_FILE" | wc -l | xargs)
284+
285+
# If the subject is oversimplified for a queue function OR queue.c is modified,
286+
# and there is only one meaningful line, issue a warning.
287+
if { [[ "$COMMIT_SUBJECT_TO_PROCESS" =~ ^(Implement|Finish)[[:space:]]+q_[[:alnum:]_]+\(?\)?$ ]] || \
288+
git diff --cached --name-only | grep -Eq '(^|/)queue\.c$'; } && [ "$NON_COMMENT_COUNT" -le 1 ]; then
289+
add_warning 1 "Commit message oversimplified. Use the commit message body to explain what and why."
290+
fi
291+
282292
# 9. Do not start the subject line with whitespace
283293
# ------------------------------------------------------------------------------
284294

0 commit comments

Comments
 (0)