Skip to content

Instantly share code, notes, and snippets.

@jasonrobertfox
Last active August 15, 2021 10:05
Show Gist options
  • Save jasonrobertfox/8057124 to your computer and use it in GitHub Desktop.
Save jasonrobertfox/8057124 to your computer and use it in GitHub Desktop.
This is a commit-msg hook for git that will validate your commit message to conform to the rules outlined in http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html It's a ruby adaptation of the hook outlined in http://addamhardy.com/blog/2013/06/05/good-commit-messages-and-enforcing-them-with-git-hooks/ (Thanks Addam Hardy!)
#!/bin/sh
exec < /dev/tty
./.git/hooks/validate_commit.rb $1
#!/usr/bin/ruby
# Encoding: utf-8
editor = ENV['EDITOR'] != 'none' ? ENV['EDITOR'] : 'vim'
message_file = ARGV[0]
def check_format_rules(line_number, line)
real_line_number = line_number + 1
return "Error #{real_line_number}: First line should be less than 50 characters in length." if line_number == 0 && line.length > 50
return "Error #{real_line_number}: Second line should be empty." if line_number == 1 && line.length > 0
(return "Error #{real_line_number}: No line should be over 72 characters long." if line.length > 72) unless line[0,1] == '#'
false
end
while true
commit_msg = []
errors = []
File.open(message_file, 'r').each_with_index do |line, line_number|
commit_msg.push line
e = check_format_rules line_number, line.strip
errors.push e if e
end
unless errors.empty?
File.open(message_file, 'w') do |file|
file.puts "\n# GIT COMMIT MESSAGE FORMAT ERRORS:"
errors.each { |error| file.puts "# #{error}" }
file.puts "\n"
commit_msg.each { |line| file.puts line }
end
puts 'Invalid git commit message format. Press y to edit and n to cancel the commit. [y/n]'
choice = $stdin.gets.chomp
exit 1 if %w(no n).include?(choice.downcase)
next if `#{editor} #{message_file}`
end
break
end
@jasonrobertfox
Copy link
Author

You can make this global by configuring your template directory:

mkdir ~/.git_template

git config --global init.templatedir '~/.git_template'

And copying these two files into that directory.

Running git init on existing repositories will add these in.

Answer originally found here.

@ksob
Copy link

ksob commented Feb 17, 2014

I can add that these two files need to be placed inside '~/.git_template/hooks' with 'chmod a+x' called on them.

@m1foley
Copy link

m1foley commented Aug 26, 2015

Thanks for this script. After using it for a while I was inspired to write Fit Commit, a more polished tool that does essentially the same thing.

@mech
Copy link

mech commented Mar 9, 2016

If anyone getting the VIM: Warning: Output is not a terminal, you can change the last line to system(editor, message_file)

@clns
Copy link

clns commented Mar 22, 2016

Thanks. I've also used it for a while and then wrote node-commit-msg, a validator that obeys rules from different sources and that uses the Stanford Parser with a custom trained model to detect non present tense verbs.

@tommarshall
Copy link

Thanks for this. Used this for over a year. I recently wrote git-good-commit, a similar hook written in pure bash to avoid the ruby dependency. Hopefully that's useful to someone else as well.

@xelnod
Copy link

xelnod commented Dec 14, 2016

Is there a way to hook the messages passed from -m commit argument too?

@pgreene
Copy link

pgreene commented Apr 28, 2017

I have message validation working locally via commit-msg. Thank you btw for demonstrating via example. However adding the script(s) to .git_template with the proper executable permissions to make it 'global' doesn't seem to work for me. What I really want is to turn this into a server side hook. I've read different documentation and examples of doing this with no success on my end so far. Any working use cases or an example of how to turn this into a server side hook, would help. :)

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