Skip to content

Commit 35ebb65

Browse files
committed
Wrap the body at 72 characters
The change should correctly alert when a commit message line exceeds 72 characters, even if extra whitespace might otherwise hide its true length. Change-Id: Ic4cfb9c30028e20322b733da2c4d58002349c181
1 parent cdcfc66 commit 35ebb65

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

scripts/commit-msg.hook

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,21 @@ 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-
for i in "${!COMMIT_MSG_LINES[@]}"; do
255-
LINE_NUMBER=$((i+1))
256-
test "${#COMMIT_MSG_LINES[$i]}" -le 72 || [[ ${COMMIT_MSG_LINES[$i]} =~ $URL_REGEX ]]
257-
test $? -eq 0 || add_warning $LINE_NUMBER "Wrap the body at 72 characters (${#COMMIT_MSG_LINES[$i]} chars)"
258-
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
259267

260268
# 7. Use the body to explain what and why vs. how
261269
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)