Skip to content

Commit 1302947

Browse files
authored
Merge pull request #190 from sysprog21/refine-commit-hook
Refine commit hook
2 parents 490b66e + 35ebb65 commit 1302947

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

scripts/commit-msg.hook

Lines changed: 29 additions & 8 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
# ------------------------------------------------------------------------------
@@ -282,16 +290,29 @@ validate_commit_message() {
282290

283291
word_count=$(echo "$COMMIT_SUBJECT_TO_PROCESS" | wc -w)
284292
test "$word_count" -gt 1
285-
test $? -eq 0 || add_warning 1 "Commit subject should contain more than one word (e.g. 'Update dependencies' instead of 'Update')"
293+
test $? -eq 0 || add_warning 1 "Commit subject should contain more than one word. Summarize your changes"
286294

287295
# 11. Avoid commit subject that simply states a file update (e.g. "Update console.c")
296+
# ------------------------------------------------------------------------------
297+
288298
if [[ $COMMIT_SUBJECT_TO_PROCESS =~ ^Update[[:space:]]+([^[:space:]]+)$ ]]; then
289299
candidate="${BASH_REMATCH[1]}"
290300
# Only warn if the candidate filename ends with .c or .h
291301
if [[ $candidate =~ \.(c|h)$ ]]; then
292302
add_warning 1 "Avoid using just a filename like '$candidate'. Provide a functional, meaningful description"
293303
fi
294304
fi
305+
306+
# 12. Avoid abusive language in commit message content
307+
# ------------------------------------------------------------------------------
308+
309+
FULL_COMMIT_MSG=$(sed '/^#/d;/^[[:space:]]*$/d' "$COMMIT_MSG_FILE")
310+
# Extended list of abusive words (case-insensitive).
311+
# Adjust the list as needed.
312+
ABUSIVE_WORDS_REGEX='\b(fuck|fucking|dick|shit|bitch|asshole|cunt|motherfucker|damn|crap|dumbass|piss)\b'
313+
if echo "$FULL_COMMIT_MSG" | grep -Eiq "$ABUSIVE_WORDS_REGEX"; then
314+
add_warning 1 "Commit message contains inappropriate language. Avoid using abusive words"
315+
fi
295316
}
296317

297318
unset GREP_OPTIONS

scripts/install-git-hooks

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ chmod +x .git/hooks/commit-msg
6868
ln -sf ../../scripts/pre-push.hook .git/hooks/pre-push || exit 1
6969
chmod +x .git/hooks/pre-push
7070

71+
ln -sf ../../scripts/prepare-commit-msg.hook .git/hooks/prepare-commit-msg || exit 1
72+
chmod +x .git/hooks/prepare-commit-msg
73+
7174
touch .git/hooks/applied || exit 1
7275

7376
echo

scripts/prepare-commit-msg.hook

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
COMMIT_MSG_FILE="$1"
4+
5+
# Only proceed if the commit message file is empty (ignoring comment or blank lines).
6+
if grep -qE '^[^[:space:]#]' "$COMMIT_MSG_FILE"; then
7+
exit 0
8+
fi
9+
10+
# Define the inline message with commit guidelines.
11+
INLINE_MSG=$(cat <<'EOF'
12+
# 🎉Check the rules before writing commit messages.
13+
# https://cbea.ms/git-commit/
14+
#
15+
# Seven Rules for a Great Git Commit Message:
16+
# 1. Separate subject from body with a blank line
17+
# 2. Limit the subject line to 50 characters
18+
# 3. Capitalize the subject line
19+
# 4. Do not end the subject line with a period
20+
# 5. Use the imperative mood in the subject line
21+
# 6. Wrap the body at 72 characters
22+
# 7. Use the body to explain what and why vs. how
23+
#
24+
# You may modify this commit message.
25+
# To abort this commit, exit the editor without saving.
26+
EOF
27+
)
28+
29+
# Write the inline guidelines into the commit message file.
30+
echo > "$COMMIT_MSG_FILE"
31+
echo -e "$INLINE_MSG" >> "$COMMIT_MSG_FILE"
32+
33+
# Prompt the user to optionally abort the commit.
34+
read -rp "Do you want to abort this commit? (y/N): " answer
35+
if [[ "$answer" =~ ^[Yy]$ ]]; then
36+
echo "Commit aborted by user." >&2
37+
exit 1
38+
fi
39+
40+
exit 0

0 commit comments

Comments
 (0)