Skip to content

Commit 2cdce75

Browse files
authored
Merge pull request #299 from sysprog21/refine-hooks
Refine hooks
2 parents fa7fad6 + 805e2be commit 2cdce75

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

scripts/commit-msg.hook

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,25 +394,24 @@ validate_commit_message() {
394394
add_warning 1 "Do not write single-word commits. Provide a descriptive subject"
395395
fi
396396

397-
# 7a. Avoid using C source filenames as the commit subject.
397+
# 7a. Avoid using C source filenames as the commit subject
398398
if [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^[_a-zA-Z0-9]+\.[ch]$ ]]; then
399399
add_warning 1 "Avoid mentioning C source filenames in the commit subject"
400400
fi
401401

402-
# 7b. Disallow parentheses in the commit subject.
402+
# 7b. Disallow parentheses in the commit subject
403403
if [[ ${COMMIT_SUBJECT_TO_PROCESS} =~ [\(\)] ]]; then
404404
add_warning 1 "Avoid using parentheses '()' in commit subjects"
405405
fi
406406

407-
# 7c. Disallow conventional commit format (e.g., "chore(scope):", "feat:", etc.)
407+
# 7c. Disallow conventional commit format
408408
# These formats waste precious characters from the 50-character limit
409409
# Check for patterns like "type:" or "type(scope):" with optional breaking change indicator
410410
if [[ ${COMMIT_SUBJECT} =~ ^[a-z]+\([^\)]+\):[[:space:]] ]] || [[ ${COMMIT_SUBJECT} =~ ^[a-z]+!?:[[:space:]] ]]; then
411411
add_warning 1 "Avoid conventional commit format (e.g., 'chore(scripts):', 'feat:', 'fix:'). Write a direct, descriptive subject"
412412
fi
413413

414-
# 7d. Alert if the commit subject starts with "Implementation"
415-
# ------------------------------------------------------------------------------
414+
# 7d. Alert if the commit subject starts with non-imperative words
416415
if [[ "${COMMIT_SUBJECT_TO_PROCESS}" =~ ^(First|My|Implementation|Implementations|Creation|Modification|Queue) ]]; then
417416
add_warning 1 "Commit subject should use imperative mood"
418417
fi
@@ -439,6 +438,14 @@ validate_commit_message() {
439438
fi
440439
fi
441440

441+
# 8a. For queue functions (q_*), require detailed explanation in body
442+
# Check if subject mentions queue functions like "Implement q_size" or "Fix q_new"
443+
if [[ "${COMMIT_SUBJECT}" =~ q_[a-zA-Z_]+ ]]; then
444+
if [ "${NON_COMMENT_COUNT}" -le 1 ]; then
445+
add_warning 1 "Queue function commits require detailed explanation in the body"
446+
fi
447+
fi
448+
442449
# 9. Do not start the subject line with whitespace
443450
# ------------------------------------------------------------------------------
444451

@@ -496,12 +503,44 @@ validate_commit_message() {
496503

497504
# Use aspell to list misspelled words according to American English, ignoring quoted text.
498505
MISSPELLED_WORDS=$(echo "$MSG_FOR_SPELLCHECK" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
499-
if [ -n "$MISSPELLED_WORDS" ]; then
500-
results=$(get_all_match_positions "$MSG_FOR_SPELLCHECK_LINE_FINDING" "$MISSPELLED_WORDS")
501506

502-
while read -r result; do
503-
add_warning "${result#*:}" "Avoid using non-American English words: ${result%%:*}"
504-
done <<< "$results"
507+
# Filter out words that are filenames in the git repository
508+
if [ -n "$MISSPELLED_WORDS" ]; then
509+
# Get comprehensive list of repository-related words to exclude
510+
# 1. Full filenames with extensions
511+
# 2. Filenames without extensions
512+
# 3. Directory names
513+
# 4. File extensions without the dot
514+
GIT_WORDS=$(
515+
{
516+
# Full filenames
517+
git ls-files 2>/dev/null | xargs -n1 basename 2>/dev/null
518+
# Filenames without extensions
519+
git ls-files 2>/dev/null | xargs -n1 basename 2>/dev/null | sed 's/\.[^.]*$//'
520+
# Directory names
521+
git ls-files 2>/dev/null | xargs -n1 dirname 2>/dev/null | tr '/' '\n' | grep -v '^\.$'
522+
# File extensions (without dot)
523+
git ls-files 2>/dev/null | grep '\.' | sed 's/.*\.//'
524+
} | sort -u
525+
)
526+
# Filter out repository filenames from misspelled words
527+
FILTERED_MISSPELLED=""
528+
while IFS= read -r word; do
529+
# Check if the word matches any filename or file component
530+
if ! echo "$GIT_WORDS" | grep -qxFi "$word"; then
531+
FILTERED_MISSPELLED="$FILTERED_MISSPELLED$word"$'\n'
532+
fi
533+
done <<< "$MISSPELLED_WORDS"
534+
535+
# Remove trailing newline
536+
FILTERED_MISSPELLED="${FILTERED_MISSPELLED%$'\n'}"
537+
if [ -n "$FILTERED_MISSPELLED" ]; then
538+
results=$(get_all_match_positions "$MSG_FOR_SPELLCHECK_LINE_FINDING" "$FILTERED_MISSPELLED")
539+
540+
while read -r result; do
541+
add_warning "${result#*:}" "Avoid using non-American English words: ${result%%:*}"
542+
done <<< "$results"
543+
fi
505544
fi
506545
}
507546

0 commit comments

Comments
 (0)