-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg
More file actions
executable file
·67 lines (50 loc) · 1.9 KB
/
commit-msg
File metadata and controls
executable file
·67 lines (50 loc) · 1.9 KB
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
66
67
#!/usr/bin/env python3
# Commit message hook for Git repos.
# Andrew Benson (28-July-2021) — ported to Python
import re
import sys
GREEN = '\033[1;32m'
RED = '\033[1;31m'
RESET = '\033[0m'
def ok(msg):
print(f"{GREEN}\u2714 {RESET}{msg}")
def fail(msg):
print(f"{RED}\u2715 {RESET}{msg}")
sys.exit(1)
def main():
message_file = sys.argv[1]
with open(message_file, encoding='utf-8') as fh:
lines = fh.readlines()
first_line = lines[0] if lines else ''
# Enforce conventional commits (https://www.conventionalcommits.org)
## Header line format
header_re = re.compile(r'^([a-zA-Z0-9]+)(\([a-zA-Z0-9]+\))?(!?):\s\S')
m = header_re.match(first_line)
if m:
ok('Commit message does follow Conventional Commits format'
' (https://www.conventionalcommits.org)')
else:
fail('Commit message does not follow Conventional Commits format'
' (https://www.conventionalcommits.org)')
type_ = m.group(1)
breaking = m.group(3)
## Type is valid
valid_types = {'fix', 'feat', 'build', 'docs', 'style',
'test', 'refactor', 'perf', 'clean'}
if type_ in valid_types:
ok('Type is valid (https://www.conventionalcommits.org)')
else:
fail('Type is invalid (https://www.conventionalcommits.org)')
## BREAKING CHANGE consistency
breaking_found = any(re.match(r'^BREAKING\sCHANGE:\s', line) for line in lines)
if breaking == '!' and not breaking_found:
fail("'BREAKING CHANGE' footer missing"
' (https://www.conventionalcommits.org)')
elif breaking_found and breaking != '!':
fail("'BREAKING CHANGE' footer present but '!' missing from header"
' (https://www.conventionalcommits.org)')
else:
ok('Consistent breaking change status'
' (https://www.conventionalcommits.org)')
if __name__ == '__main__':
main()