-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-commit-msg
executable file
·66 lines (50 loc) · 2.03 KB
/
prepare-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
55
56
57
58
59
60
61
62
63
64
65
#!python
from __future__ import print_function
from hooks_common import *
def build_commit_message(original_raw, feature_name, issue, warnings):
lines = original_raw.splitlines()
original = "\n".join([l for l in lines if not l.strip().startswith("#")])
comments = "\n".join([l for l in lines if l.strip().startswith("#")])
prefix_string = "({0}) ".format(feature_name)
issue_string = "\n{0}".format(issue)
message = ""
if feature_name != None and not original.startswith(prefix_string):
message += prefix_string
message += original + "\n"
if issue != None and not original.endswith(issue_string):
message += issue_string + "\n"
for w in warnings:
message += "# " + w.replace("\n", "\n# ") + "\n# \n"
message += comments + "\n"
return message
# Don't alter '--amend' messages, merges, cherry-picks
if len(sys.argv) > 2 and ( (sys.argv[2] == "commit")
or (sys.argv[2] == "merge")
or (sys.argv[2] == "squash") ) :
exit()
message_filename = sys.argv[1]
is_command_line_message = ((len(sys.argv) > 2) and (sys.argv[2] == "message"))
branch = get_current_branch()
if not is_excluded_branch(branch):
keys = config_keys()
warnings = []
values = {key : None for key in keys}
for key in keys:
if has_branch_config(branch, key):
values[key] = get_branch_config(branch, key)
else:
warning = no_config_warning(branch, key)
if is_command_line_message:
print(warning, file=sys.stderr)
print("\n")
else:
warnings.append(warning)
original_message = ""
with open(message_filename, "r") as f:
original_message = f.read()
message = build_commit_message( original_message,
values["commitprefix"],
values["issue"],
warnings)
with open(message_filename, "w") as f:
f.write(message)