Skip to content

Instantly share code, notes, and snippets.

@Dih5
Created January 11, 2017 12:39
Show Gist options
  • Save Dih5/16fcb55f254cfaab3e4ffe3cefb1d28c to your computer and use it in GitHub Desktop.
Save Dih5/16fcb55f254cfaab3e4ffe3cefb1d28c to your computer and use it in GitHub Desktop.
commit-msg hook for git. Checks some format conventions.
#!/usr/bin/env python
# Put in .git/hooks/commit-msg and chmod +x it.
from __future__ import print_function
import sys
def fail():
print("Commit aborted.")
sys.exit(1)
# Get the commit message
f = open(sys.argv[1])
msg = f.read()
lines = msg.split('\n')
first_line = lines[0]
if len(first_line)>50:
print("Subject line is limited to 50 chars.")
fail()
if first_line[0].islower():
print("Subject line must be capitalized.")
fail()
if first_line[-1]=='.':
print("Subject line must not end in period.")
fail()
if len(lines)>2: # If there is a body
if lines[1]!="":
print("Subject line and body must be separated with a blank line.")
fail()
for l in lines[2:]:
if len(l)>72:
print("Body must be wrapped at 72 characters max.")
fail()
# Other convenient conventions not checked by the script:
# Use the imperative mood in the subject line
# Use the body to explain what and why vs. how
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment