-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg
More file actions
55 lines (45 loc) · 1.62 KB
/
Copy pathcommit-msg
File metadata and controls
55 lines (45 loc) · 1.62 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
#!/usr/bin/env python3
import re
import sys
import string
TYPES = [
"feat", "fix", "chore", "docs", "style",
"refactor", "test", "perf", "ci", "build", "revert"
]
PATTERN = re.compile(
r'^(?P<type>[a-zA-Z]+)' # type (letters only)
r'(?:\([^)]+\))?' # optional scope
r'!?' # optional "!" after type/scope
r':\s+' # colon and spaces
r'(?P<desc>.+)$' # description
)
def check_line(first_line: str) -> bool:
match = PATTERN.match(first_line)
if not match:
return fail(first_line, "Message must match format: type(optional_scope): description")
ctype = match.group("type").lower()
desc = match.group("desc")
if ctype not in TYPES:
return fail(first_line, f"Invalid type '{ctype}'. Allowed: {', '.join(TYPES)}")
if not desc or desc[0] in string.punctuation:
return fail(first_line, "Description can not be empty or start with punctuation")
# passed all checks
return True
def fail(msg, reason):
print("------------------------------")
print("ERROR: Invalid commit message")
print(f"REASON: {reason}")
print("------------------------------")
print("Valid examples:")
print(" fix: fixed some bug")
print(" feat!: added feature with breaking changes")
print(" fix(parser): fixed problem with parser")
return False
def main():
commit_msg_file = sys.argv[1]
with open(commit_msg_file, encoding="utf-8") as f:
first_line = f.readline().strip()
ok = check_line(first_line)
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()