-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_commit_message
executable file
·70 lines (54 loc) · 1.82 KB
/
check_commit_message
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
68
69
70
#!/usr/bin/env python
"""Checks that a commit message conforms the the style guide."""
import sys
# Get the commit message location
commit_msg_filepath = sys.argv[1]
# Extract the message lines into a list
lines = list((line.rstrip() for line in open(commit_msg_filepath, 'r')))
# Remove all commented lines
lines = [line for line in lines if len(line) == 0 or line[0] != '#']
# Skip if this is an automated backport
if len(lines) >= 2 and 'cherry picked from commit' in lines[-2]:
sys.exit(0)
exit_code = 0
# Error: no commit message
if len(lines) == 0 or len(lines[0]) == 0:
print('Error: commit message cannot be blank.')
sys.exit(1)
# Error: invalid subject line length
if len(lines[0]) > 50:
print('Error: your subject line must be within 50 characters.')
exit_code = 1
# Error: subject line not capitalized
if lines[0][0].isalpha() and not lines[0][0].isupper():
print('Error: your subject line must be capitalized.')
exit_code = 1
# Error: subject line ends with period
if lines[0].endswith('.'):
print('Error: your subject line cannot end with a period.')
exit_code = 1
# No body paragraph
if len(lines) == 1:
sys.exit(exit_code)
# Error: subject and body not separated by newline
if len(lines[1]) != 0:
print('Error: you must separate your subject '
'and body paragraph with a newline.')
exit_code = 1
lines = lines[2:]
count = 0
for line in lines:
count += 1
# Ignore newline
if len(line) == 0:
continue
# Ignore lines containing URLs, which can get long
if 'http' in line:
continue
# Error: invalid body paragraph length
if len(line) > 72:
print('Error: line {} of your body paragraph '
'must be wrapped to 72 characters:'.format(count))
print('>> ' + line)
exit_code = 1
sys.exit(exit_code)