Skip to content

Instantly share code, notes, and snippets.

@bitle
Created January 21, 2015 21:20
Show Gist options
  • Save bitle/d2ece3b3de28ba4b748e to your computer and use it in GitHub Desktop.
Save bitle/d2ece3b3de28ba4b748e to your computer and use it in GitHub Desktop.
Git message format git hook
#!/usr/bin/python
import sys, os
from subprocess import call
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
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:
for error in errors:
print('%s\n' % (error,))
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment