Skip to content

Instantly share code, notes, and snippets.

@Rpsl
Last active December 16, 2016 16:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rpsl/4dabaa7120f8a5e08b918a6171aef832 to your computer and use it in GitHub Desktop.
Save Rpsl/4dabaa7120f8a5e08b918a6171aef832 to your computer and use it in GitHub Desktop.
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