Skip to content

Instantly share code, notes, and snippets.

@kimniche
Last active December 12, 2016 13:45
Show Gist options
  • Save kimniche/63101a08e262a10669ec57ca1351c475 to your computer and use it in GitHub Desktop.
Save kimniche/63101a08e262a10669ec57ca1351c475 to your computer and use it in GitHub Desktop.
Git commit message hook that checks for issue number within your commit message if you're on a bug branch. To enable: add this file into your repo's `.git/hooks` directory and make it executable (`chmod +x commit-msg`)
#!/usr/bin/env python
import sys, os, re
from subprocess import check_output
# Collect the parameters
commit_msg_filepath = sys.argv[1]
# Figure out which branch we're on
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
# Check the commit message if we're on an issue branch
if branch.startswith('bug/i'):
print "commit-msg: Bug branch detected (%s)." % branch
result = re.match('bug/i(.*)', branch)
issue_number = result.group(1)
required_message = "#%s" % issue_number
with open(commit_msg_filepath, 'r') as f:
content = f.read()
if required_message not in content:
print "commit-msg: WARNING: Commit message omits issue number (%s)" % required_message
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment