-
Notifications
You must be signed in to change notification settings - Fork 17
/
hook_lib.py
83 lines (64 loc) · 3.02 KB
/
hook_lib.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Library for code shared by hghook.py and githook.py.
This library does the actual linting, after the VCS-specific commands
to get the data that needs linting.
"""
import re
import subprocess
import sys
def lint_files(files_to_lint):
"""Given a list of filenames in the commit, lint them all.
We use `ka_lint` to lint them all, which in most repos will call
runlint.py (in this directory) but in other repos -- notably webapp
-- will do its own thing. In all cases, ka_lint will emit lint
errors to stdout, lint-framework errors to stderr, and have an rc of
0 if linting was successful or 1 if there were lint or framework
errors.
This helper also returns 0 if linting was successful or 1 otherwise.
"""
# We could pass in the files-to-lint on the commandline, but using
# --stdin means we don't need to worry about how many files there are.
p = subprocess.Popen(['ka-lint', '--stdin', '--blacklist=yes'],
stdin=subprocess.PIPE)
p.communicate(input='\n'.join(files_to_lint).encode('utf-8'))
return p.wait()
def lint_commit_message(commit_message):
"""Given the text of a commit message, lint it for correctness.
Every non-merge commit must list either a test plan or a review
that it's part of (the first commit in a review must have a test
plan, but subsequent ones don't need to restate it).
TODO(csilvers): should we do anything special with substate-update
commits?
Emits errors it sees to stderr.
Returns the number of lint errors seen in the commit message.
"""
num_errors = 0
if not re.search('^(test plan|review):', commit_message, re.I | re.M):
print('Missing "Test plan:" or "Review:" section '
'in the commit message.', file=sys.stderr)
num_errors += 1
elif re.search('^ <see below>$', commit_message, re.M):
print('Must enter a "Test plan:" (or "Review:") '
'in the commit message.', file=sys.stderr)
num_errors += 1
if re.search('^<one-line summary, followed by ', commit_message, re.M):
print('Must enter a summary in the commit message.', file=sys.stderr)
num_errors += 1
# TODO(csilvers): verify the first-line summary is actually 1 line long?
return num_errors
def report_errors_and_exit(num_errors, commit_message, save_filename):
"""If num_errors > 0, print a summary message and exit 1.
In that case, we save the commit message to save_filename.
"""
if num_errors:
# save the commit message so we don't need to retype it
with open(save_filename, 'w') as f:
f.write(commit_message)
print('\n--- %s commit message errors ---\n'
'Commit message saved to %s'
% (num_errors, save_filename),
file=sys.stderr)
print('Use "git commit -a --template .git/commit.save" to commit'
' with a fixed message.', file=sys.stderr)
sys.exit(1)
print('khan-linter: commit message passed', file=sys.stderr)
sys.exit(0)