Skip to content

Instantly share code, notes, and snippets.

@samdoran
Last active February 4, 2019 15:16
Show Gist options
  • Select an option

  • Save samdoran/864496d4bdd4fcf3ceba0a25b8214b53 to your computer and use it in GitHub Desktop.

Select an option

Save samdoran/864496d4bdd4fcf3ceba0a25b8214b53 to your computer and use it in GitHub Desktop.
Git commit message spell checker
#!/usr/bin/env python
import os
import shutil
import subprocess
import sys
try:
aspell_path = shutil.which('aspell')
except AttributeError:
path = os.environ['PATH']
paths = path.split(os.pathsep)
aspell_path = None
for p in paths:
test_path = '{0}/{1}'.format(p, 'aspell')
if os.path.isfile(test_path):
aspell_path = test_path
break
ok_words_path = '/Users/sdoran/.git/extra-words.txt'
if not aspell_path:
print("aspell not found")
sys.exit(1)
# Read in list of non-dictionary words that are ok
command = [aspell_path, 'list']
if os.path.isfile(ok_words_path):
with open(ok_words_path, 'rb') as f:
ok_words = f.readlines()
ok_words = [word.rstrip(b'\n') for word in ok_words]
# Spell check the commit message
with open(sys.argv[1], 'rb') as f:
p = subprocess.Popen(command, stdin=f, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = p.communicate()[0]
# Filter out extra words from the list of spelling mistakes
mispelled_word_set = {w.decode('utf-8') for w in stdout.splitlines()}
mistakes = mispelled_word_set.difference(ok_words)
# If anything is left, we have mistakes
if len(mistakes) > 0:
print("Found the following spelling errors:\n")
for word in mistakes:
print('{0}'.format(word))
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment