Last active
August 29, 2015 14:10
-
-
Save teriiehina/ca4226a414746e5ff527 to your computer and use it in GitHub Desktop.
a hook launched just before git creates the commit (aka .git/hooks/commit-msg)
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'raspell' | |
chars_count_min = 10 | |
chars_count_max = 80 | |
message_file = ARGV[0] | |
first_line = File.open(message_file, &:gets) | |
type_regex = /^\[(.*)\] (.*)/ | |
commit_types = ["Dev" , "Pod" , "Mantis" , "Config"] | |
# Format attendu: "[Type de commit] Message simple et concis" | |
if !(first_line =~ type_regex) | |
puts "Commit message should begin with [Type]" | |
exit 1 | |
end | |
# le type de commit doit être dans une liste prédéfinie | |
# sauf si c'est une mantis avec le numéro de la mantis | |
# TODO: verifier que pour "[Mantis ...]" le formatage est correct. | |
type = type_regex.match(first_line)[1] | |
if !(commit_types.include? type) && !(type =~ /Mantis/) | |
puts "The type of this commit is unknown." | |
puts "#{type} does not belong to #{commit_types}" | |
exit 1 | |
end | |
# la longueur de la première ligne doit inférieure à 80 caractères | |
if first_line.length > chars_count_max | |
puts "Aborting commit due to commit message too long." | |
puts "(must be less than #{chars_count_max} characters)" | |
exit 1 | |
end | |
# la longueur de la première ligne doit être supérieure à 10 caractères | |
if first_line.length < chars_count_min | |
puts "Aborting commit due to commit message too short." | |
puts "(must be more than #{chars_count_max} caracters)" | |
exit 1 | |
end | |
# pas de faute d'orthographe svp | |
to_check = first_line | |
to_check = to_check.gsub(/\[(.*)\]/, '') | |
to_check = to_check.gsub(/`(.*)`/, '') | |
to_check = to_check.gsub(/'(.*)'/, '') | |
speller = Aspell.new("fr_FR") | |
wrongs = Array.new | |
speller.suggestion_mode = Aspell::NORMAL | |
to_check.gsub(/[\w\']+/) do |word| | |
if !speller.check(word) | |
wrongs.push word | |
end | |
end | |
if wrongs.length > 0 | |
wrongs.each do |word| | |
puts "We don't know the word #{word}." | |
puts "Maybe you meant #{speller.suggest(word).first}" | |
end | |
exit 1 | |
end | |
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
brew install aspell --with-lang-fr | |
sudo gem install raspell -- --with-opt-dir=/usr/local |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment