-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg
More file actions
executable file
·38 lines (27 loc) · 996 Bytes
/
prepare-commit-msg
File metadata and controls
executable file
·38 lines (27 loc) · 996 Bytes
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
#!/usr/bin/env python3
import re
import sys
from subprocess import check_output
commit_msg_filepath = sys.argv[1]
stdout_encoding = sys.stdout.encoding
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
branch = branch.decode(stdout_encoding)
branch = branch.strip()
# Skip special branches
if branch in ['master', 'dev', 'stage']:
sys.exit(0)
branch_name_fragments = branch.split("_")
if len(branch_name_fragments) != 3:
print('your branch name is incorrectly formatted')
sys.exit(1)
ticket_type, ticket_message, ticket_number = branch.split("_")
with open(commit_msg_filepath, 'r+') as fh:
commit_msg = fh.read()
# check that our commit message doesn't already have a prefix
if not re.match('^\[\w|#\d+', commit_msg):
fh.seek(0, 0)
fh.write("[{ticket_type}|#{ticket_number}] {commit_msg}".format(
ticket_type=ticket_type,
ticket_number=ticket_number,
commit_msg=commit_msg
))