Skip to content

Instantly share code, notes, and snippets.

@b00f
Forked from bean5/commit-msg
Last active June 17, 2020 07:08
Show Gist options
  • Save b00f/22c4233c106ea39f1197b3e4f7724ce9 to your computer and use it in GitHub Desktop.
Save b00f/22c4233c106ea39f1197b3e4f7724ce9 to your computer and use it in GitHub Desktop.
git commit-msg hook, Hunspell spellchecking with custom dictionary
#!/usr/bin/env ruby
# 1. Install hunspell
# $ pacman -S 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 "misspeling commit message"
# 5. Amend the last commit message
# $ git commit --amend -m "correct commit message"
class GitSpellCheckCommit
def self.locale=(locale)
@@locale = locale
end
def initialize(file)
@file = file
end
def validate!
@check = `cat #{@file} | LC_ALL=#{@@locale} hunspell -p #{$HOME}/.dicts/en.add`
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
GitSpellCheckCommit.locale = :en_US
commit = GitSpellCheckCommit.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 "---------------------------------------------------------------------"
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment