3

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!

melkoth
  • 178
  • 7

2 Answers2

5

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.

SegFault
  • 2,526
  • 4
  • 21
  • 41
2

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.

romellem
  • 5,792
  • 1
  • 32
  • 64