Skip to content

Instantly share code, notes, and snippets.

@goyalmohit
Last active April 14, 2019 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goyalmohit/57933a6a6f4f9d302c3d9734089c1f69 to your computer and use it in GitHub Desktop.
Save goyalmohit/57933a6a6f4f9d302c3d9734089c1f69 to your computer and use it in GitHub Desktop.
Python script for pre-commit message git hook
#!/usr/bin/env python
import sys, os, re
from subprocess import check_output
# Collect the parameters
commit_msg_filepath = sys.argv[1]
if len(sys.argv) > 2:
commit_type = sys.argv[2]
else:
commit_type = ''
if len(sys.argv) > 3:
commit_hash = sys.argv[3]
else:
commit_hash = ''
print "prepare-commit-msg: File: %s\nType: %s\nHash: %s" % (commit_msg_filepath, commit_type, commit_hash)
# Figure out which branch we're on
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
print "prepare-commit-msg: On branch '%s'" % branch
# Populate the commit message with the issue #, if there is one
if branch.startswith('issue-'):
print "prepare-commit-msg: Oh hey, it's an issue branch."
result = re.match('issue-(.*)', branch)
issue_number = result.group(1)
with open(commit_msg_filepath, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write("ISSUE-%s %s" % (issue_number, content))
# Populate the commit message with the feature #, if there is one
if branch.startswith('feature-'):
print "prepare-commit-msg: Oh hey, it's an feature branch."
result = re.match('feature-(.*)', branch)
feature_number = result.group(1)
with open(commit_msg_filepath, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write("FEATURE-%s %s" % (feature__number, content))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment