Skip to content

Commit 805e2be

Browse files
committed
Exclude repository filenames from spell check
The commit-msg hook now filters out repository filenames when checking for spelling errors. Previously, mentioning files like CONTRIBUTING.md would incorrectly flag "md" as a misspelling, creating unnecessary friction when referencing project files in commit messages. Change-Id: I62d87d9f1ac5cd3ab9c992d49a2fc8331eadd5e0
1 parent cacb312 commit 805e2be

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

scripts/commit-msg.hook

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,44 @@ validate_commit_message() {
503503

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

509-
while read -r result; do
510-
add_warning "${result#*:}" "Avoid using non-American English words: ${result%%:*}"
511-
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
512544
fi
513545
}
514546

0 commit comments

Comments
 (0)