Python script to control the git commit message as git hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() | |
print "commit-msg: On branch '%s'" % branch | |
# Check the commit message if we're on an issue branch | |
if branch.startswith('issue-'): | |
print "commit-msg: Oh hey, it's an issue branch." | |
result = re.match('issue-(.*)', branch) | |
issue_number = result.group(1) | |
required_message = "ISSUE-%s" % issue_number | |
with open(commit_msg_filepath, 'r') as f: | |
content = f.read() | |
if not content.startswith(required_message): | |
print "commit-msg: ERROR! The commit message must start with '%s'" % required_message | |
sys.exit(1) | |
# Check the commit message if we're on an feature branch | |
if branch.startswith('feature-'): | |
print "commit-msg: Oh hey, it's an feature branch." | |
result = re.match('feature-(.*)', branch) | |
feature_number = result.group(1) | |
required_message = "FEATURE-%s" % feature_number | |
with open(commit_msg_filepath, 'r') as f: | |
content = f.read() | |
if not content.startswith(required_message): | |
print "commit-msg: ERROR! The commit message must start with '%s'" % required_message | |
sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment