-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathenforce-insert-issue-number.hook
35 lines (31 loc) · 1.15 KB
/
enforce-insert-issue-number.hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# Based on a hook from dzone.com
# Source: https://dzone.com/articles/an-in-depth-look-at-git-hooks
#
# Makes sure user did not delete the ISSUE-[#] string that was generated by prepare-commit-msg/insert-issue-number.sample
#
# The hook should exit with non-zero status after issuing an appropriate
# message if it stops the commit.
#
# Requirements:
# * Python 2/3
#
# To enable this hook, rename this file to "commit-msg".
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)