Skip to content

Instantly share code, notes, and snippets.

@GianlucaGuarini
Forked from remi/commit-msg
Last active March 17, 2021 23:08
Show Gist options
  • Save GianlucaGuarini/7f24402547e289bd00d8 to your computer and use it in GitHub Desktop.
Save GianlucaGuarini/7f24402547e289bd00d8 to your computer and use it in GitHub Desktop.
Commits typos checker hook
#!/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
class String
def black; "\033[30m#{self}\033[0m" end
def red; "\033[31m#{self}\033[0m" end
def green; "\033[32m#{self}\033[0m" end
def brown; "\033[33m#{self}\033[0m" end
def blue; "\033[34m#{self}\033[0m" end
def magenta; "\033[35m#{self}\033[0m" end
def cyan; "\033[36m#{self}\033[0m" end
def gray; "\033[37m#{self}\033[0m" end
def bg_black; "\033[40m#{self}\033[0m" end
def bg_red; "\033[41m#{self}\033[0m" end
def bg_green; "\033[42m#{self}\033[0m" end
def bg_brown; "\033[43m#{self}\033[0m" end
def bg_blue; "\033[44m#{self}\033[0m" end
def bg_magenta; "\033[45m#{self}\033[0m" end
def bg_cyan; "\033[46m#{self}\033[0m" end
def bg_gray; "\033[47m#{self}\033[0m" end
def bold; "\033[1m#{self}\033[22m" end
def reverse_color; "\033[7m#{self}\033[27m" end
end
GitSpellCheckeCommit.locale = :en_US
commit = GitSpellCheckeCommit.new(ARGV.first)
unless commit.valid?
puts "---------------------------------------------------------------------".red
puts "It looks like you have spell checking errors in your commit message:".red
puts commit.spelling_errors.join("\n").green
puts "---------------------------------------------------------------------".red
# exit 1 # uncomment this line if you want block this commit
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment