Skip to content

Instantly share code, notes, and snippets.

@Rpsl
Last active Dec 16, 2016
Embed
What would you like to do?
Check commit msg before commit
#!/usr/bin/env python
# Install:
# cp ./.git/hooks/commit-msg ./.git/hooks/commit-msg.old 2>/dev/null; curl https://gist.githubusercontent.com/Rpsl/4dabaa7120f8a5e08b918a6171aef832/raw > ./.git/hooks/commit-msg && chmod +x ./.git/hooks/commit-msg
import sys
import os
import re
from subprocess import check_output
# Collect the parameters
commit_msg_filepath = sys.argv[1]
def isEnglish(s):
try:
s.decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
with open(commit_msg_filepath, 'r') as f:
commit_msg = f.read()
if not isEnglish(commit_msg):
print "Not Latin symbols in commit message"
sys.exit(1)
try:
task = re.findall('(([A-Z]+)-([0-9]+)-)(.*)', branch)[0][0]
if not commit_msg.startswith(task):
print 'Commit message must be start from task name, like AVPM-0000-my-message'
sys.exit(1)
except IndexError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment