Last active
June 17, 2020 14:40
-
-
Save przemkow/bf1127debfe285b0de4e49acd6b04823 to your computer and use it in GitHub Desktop.
git commit spellchecker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
INPUT_FILE=$1 | |
COMMIT_MESSAGE=`head -n1 $INPUT_FILE` | |
# The following is a text file that represents your custom dictionary; edit as necessary. Add words to it that you wish | |
# to ignore for the spell check. | |
dict=~/.git-spell-check-dictionary | |
if [ ! -f $dict ]; then | |
touch ~/.git-spell-check-dictionary | |
dict=~/.git-spell-check-dictionary | |
printf "%s\n" "Custom dictionary not found. Created ~/.git-spell-check-dictionary..." | |
fi | |
temp_dict=$(mktemp docs-dictionary-XXXXXX) | |
extension=pws | |
# Language of your doc. When using a non-English language, make sure you have the appropriate aspell libraries | |
# installed: "yum search aspell". For example, to spell check in Slovak, you must have the aspell-sk package installed. | |
lang=en | |
trap "cleanup" SIGINT SIGTERM | |
# Prepares the dictionary from scratch in case new words were added since last time. | |
function prepare_dictionary() { | |
aspell --lang="$lang" create master "$temp_dict" < "$dict" | |
} | |
# Removes the temporary dictionary. | |
function cleanup() { | |
/bin/rm -f "$temp_dict" | |
} | |
# Spell checks content you're about to commit. Writes out words that are misspelled or exits with 0 (i.e. continues with | |
# commit). | |
function spell_check() { | |
words=$(echo $COMMIT_MESSAGE | aspell --mode=sgml list --lang="$lang" --extra-dicts="$temp_dict") | |
if [ ! "$words" ]; then | |
printf "%s\n" "No typos found. Proceeding with commit..." | |
cleanup; exit 0 | |
fi | |
printf "%s\n" "Spell check failed on the following words:" | |
printf "%s\n" "---------------------" | |
for word in $words; do | |
printf "* %s\n" $word | |
done | |
printf "%s\n" "---------------------" | |
} | |
# Adds all, some, or none of the misspelled words to the custom dictionary. | |
function add_words_to_dict() { | |
printf "%s\n" " | |
Would you like to create a commit anyway? | |
* [y]es (create a commit) | |
* [n]o (no commit) | |
* [a]ll (create a commit, add all words to dictionary) | |
* [s]ome (create a commit, add selected words to dictionary) | |
" | |
while true; do | |
exec < /dev/tty # Simply reading user input does not work because Git hooks have stdin detached. | |
read answer | |
shopt -s nocasematch | |
case "$answer" in | |
y|yes) | |
create_commit | |
cleanup; exit 0 | |
;; | |
n|no) | |
no_commit | |
cleanup; exit 1 | |
;; | |
a|all) | |
add_all | |
cleanup; exit 0 | |
;; | |
s|some) | |
add_some | |
cleanup; exit 0 | |
;; | |
*) | |
printf "%s\n" "Incorrect answer. Try again." | |
continue | |
esac | |
shopt -u nocasematch | |
done | |
} | |
# Create a commit, do not add words to dictionary | |
function create_commit() { | |
printf "%s\n" "No words were added to your custom dictionary." | |
printf "%s\n" "Commit was created succesfully." | |
} | |
# Abbandon creating a commit | |
function no_commit() { | |
printf "%s\n" "No words were added to your custom dictionary." | |
printf "%s\n" "Please fix remaining typos, use \"git add\" to add fixed files, and commit." | |
} | |
# Adds all words to the custom dictionary and continues with the commit. | |
function add_all() { | |
for word in $words; do | |
echo $word >> "$dict" | |
done | |
} | |
# Adds some (selected by user) of the words to the dictinary and exits with 1. | |
function add_some() { | |
for word in $words; do | |
printf "%s\n" "Do you want to add the following word to your custom dictionary: $word ([y]es or [n]o)" | |
while true; do | |
exec < /dev/tty | |
read answer | |
shopt -s nocasematch | |
case "$answer" in | |
y|yes) | |
echo $word >> "$dict" | |
printf "%s\n" "\"$word\" added to your custom dictionary." | |
break ;; | |
n|no) | |
break ;; | |
*) | |
printf "%s\n" "Incorrect answer. Try again." | |
continue | |
esac | |
shopt -u nocasematch | |
done | |
done | |
} | |
prepare_dictionary | |
spell_check | |
add_words_to_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment