Skip to content

Instantly share code, notes, and snippets.

@danielsreichenbach
Created March 7, 2014 14:21
Show Gist options
  • Save danielsreichenbach/9412329 to your computer and use it in GitHub Desktop.
Save danielsreichenbach/9412329 to your computer and use it in GitHub Desktop.
Git commit message validation. See http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html for details.
#!/bin/sh
#
# This is .git/hooks/commit-msg.sh. Do not forget to chmod +x
#
# A commit message validator, following the rules as described in
# http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
exec < /dev/tty
.git/hooks/validate-commit.py $1
#!/usr/bin/env python2
#
# This is .git/hooks/validate-commit.py
import sys, os
from subprocess import call
print os.environ.get('EDITOR')
if os.environ.get('EDITOR') != 'none':
editor = os.environ['EDITOR']
else:
editor = "vim"
message_file = sys.argv[1]
def check_format_rules(lineno, line):
real_lineno = lineno + 1
if lineno == 0:
if len(line) > 50:
return "Error %d: First line should be less than 50 characters " \
"in length." % (real_lineno,)
if lineno == 1:
if line:
return "Error %d: Second line should be empty." % (real_lineno,)
if not line.startswith('#'):
if len(line) > 72:
return "Error %d: No line should be over 72 characters long." % (
real_lineno,)
return False
while True:
commit_msg = list()
errors = list()
with open(message_file) as commit_fd:
for lineno, line in enumerate(commit_fd):
stripped_line = line.strip()
commit_msg.append(line)
e = check_format_rules(lineno, stripped_line)
if e:
errors.append(e)
if errors:
with open(message_file, 'w') as commit_fd:
commit_fd.write('%s\n' % '# GIT COMMIT MESSAGE FORMAT ERRORS:')
for error in errors:
commit_fd.write('# %s\n' % (error,))
for line in commit_msg:
commit_fd.write(line)
re_edit = raw_input('Invalid git commit message format. Press y to edit and n to cancel the commit. [y/n]')
if re_edit.lower() in ('n','no'):
sys.exit(1)
call('%s %s' % (editor, message_file), shell=True)
continue
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment