forked from pbetkier/add-issue-id-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg
executable file
·54 lines (40 loc) · 1.74 KB
/
commit-msg
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# add-issue-id-hook version 1.1.0
#
# Created by Piotr Betkier
# https://github.com/pbetkier/add-issue-id-hook
# customize the final commit message using placeholders:
# - {issue_id} replaced with discovered issue id
# - {user_message} replaced with message provided by the user
commit_message_format = '{issue_id} {user_message}'
# you may set this to your custom JIRA project key format
# or explicitly specify a single project name, e.g. 'EXAMPLE'
project_format = '[A-Z][A-Z]+'
# if not using JIRA, set this to your ticket system's issue pattern
issue_pattern = '{}-[\d]+'.format(project_format)
import subprocess
import sys
import re
def read_current_message():
with open(sys.argv[1], 'r') as f:
return f.read()
def write_message(message):
with open(sys.argv[1], 'w') as f:
f.write(message)
def contains_message(message):
return message and not message.isspace()
def remove_editor_help_message(message):
return message[:message.find("# Please enter the commit message for your changes.")].rstrip()
def read_branch_or_exit():
try:
current_ref = subprocess.check_output('git symbolic-ref HEAD', shell=True).decode()
return current_ref[len('refs/heads/'):]
except subprocess.CalledProcessError:
print("add-issue-id-hook: Adding issue id failed. Are you in detached HEAD state?")
sys.exit()
issue_id_match = re.search(issue_pattern, read_branch_or_exit())
if issue_id_match:
found_issue_id = issue_id_match.group()
user_message = remove_editor_help_message(read_current_message())
if contains_message(user_message) and found_issue_id not in user_message:
write_message(commit_message_format.format(issue_id=found_issue_id, user_message=user_message))