Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Hook Spellchecker Ignore Indented Lines

I am writing a git hook that spell checks my commit messages. This is what I have so far:

#!/bin/sh

ASPELL=$(which aspell)

WORDS=$($ASPELL list < "$1")

if [ -n "$WORDS" ]; then
    echo -e "Possible spelling errors found in commit message. Consider using git commit --amend to change the message.\n\tPossible mispelled words: " $WORDS
fi

I'm not sure how to tell aspell that I want to ignore lines that are indented (two or more spaces). This will avoid annoying messages about file names, comments, etc.

Thank you!

like image 514
melkoth Avatar asked Dec 07 '25 23:12

melkoth


2 Answers

Are you doing EECS 398 by any chance?

Just giving you a hint without breaking the honor code.

Put the commit message into a text file. Use a text editor to delete the indented lines from the text file. Then put the text file through aspell.

like image 154
SegFault Avatar answered Dec 09 '25 16:12

SegFault


If anyone else comes across this and isn't in whatever college class the original poster was talking about, here's how I solved the problem:

#!/bin/bash
ASPELL=$(which aspell)
if [ $? -ne 0 ]; then
    echo "Aspell not installed - unable to check spelling" >&2
    exit
else
    WORDS=$($ASPELL --mode=email --add-email-quote='#' list < "$1")
fi
if [ -n "$WORDS" ]; then
    printf "\e[1;33m  Possible spelling errors found in commit message.\n  Possible mispelled words: \n\e[0m\e[0;31m%s\n\e[0m\e[1;33m  Use git commit --amend to change the message.\e[0m\n\n" "$WORDS" >&2
fi

The key part being --mode=email --add-email-quote='#' arguments when we actually call aspell.

--mode sets the filter mode, and in our case we're setting it to email mode. Which is described as:

The email filter mode skips over quoted text. ... The option add|rem-email-quote controls the characters that are considered quote characters, the defaults are '>' and '|'.

So, if we add '#' to that list of "quote" characters, aspell will skip over it. We do that of course with the --add-email-quote option.

Note that we're also skipping over '>' and '|', per the documentation. If you don't want aspell to skip over these, use the --rem-email-quote option.

See aspell's man page here for more info.

like image 26
romellem Avatar answered Dec 09 '25 14:12

romellem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!