Skip to content

Instantly share code, notes, and snippets.

@remi
Created August 30, 2011 20:56
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save remi/1181996 to your computer and use it in GitHub Desktop.
Save remi/1181996 to your computer and use it in GitHub Desktop.
Git commit message spell check hook (kind of a proof of concept, but still usable)
#!/usr/bin/env ruby
# 1. Install hunspell
# $ brew install hunspell
# 2. Download a dictionary and install it in a path listed by `hunspell -D`
# $ open http://wiki.services.openoffice.org/wiki/Dictionaries
# 3. Move this file into your repository
# $ mv commit-msg /path/to/repo/.git/hooks
# $ chmod +x /path/to/repo/.git/hooks/commit-msg
# 4. Get mad each time you try to commit
# $ git commit -m "Fix some spelling erorrs in the layout"
class GitSpellCheckeCommit
def self.locale=(locale)
@@locale = locale
end
def initialize(file)
@file = file
end
def validate!
@check = `cat #{@file} | LC_ALL=#{@@locale} hunspell`
end
def valid?
validate! =~ /&/ ? false : true
end
def spelling_errors
@check.split("\n").select{ |line| line =~ /^&/ }.map do |line|
matches = line.match(/^&\s([^\s]+)\s\d+\s\d+:\s(.+)$/)
"- You used “#{matches[1]}” and hunspell suggested instead “#{matches[2]}”"
end
end
end
GitSpellCheckeCommit.locale = :en_US
commit = GitSpellCheckeCommit.new(ARGV.first)
unless commit.valid?
puts "---------------------------------------------------------------------"
puts "It looks like you have spell checking errors in your commit message:"
puts commit.spelling_errors.join("\n")
puts "---------------------------------------------------------------------"
exit 1 # comment this line if you only want the hook to list errors and keep on with the commit
end
exit 0
@bean5
Copy link

bean5 commented Apr 11, 2015

Do you have a typo? In US English, it is "check" instead of "checke". I changed it on my branch at https://gist.github.com/bean5/5c99498423b506afad5c. Feel free to merge that in.

@majamusan
Copy link

haha, the spell checking starts already!

@vanenshi
Copy link

vanenshi commented Aug 23, 2021

why is no one creating a cli for this? so we can use it with husky??
[create a new repository]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment