blob: 492279ba0d4700c1b732ccac4bca876c0bd34823 [file] [log] [blame]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
Ryan Cuiec4d6332011-05-02 14:15:25 -07006import re
Mandeep Singh Bainesa7ffa4b2011-05-03 11:37:02 -07007import sys
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07008import subprocess
9
Ryan Cuiec4d6332011-05-02 14:15:25 -070010
11# General Helpers
12
13COMMON_INCLUDED_PATHS = [
14 # C++ and friends
15 r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$",
16 r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$",
17 # Scripts
18 r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$",
19 # No extension at all, note that ALL CAPS files are black listed in
20 # COMMON_EXCLUDED_LIST below.
21 r"(^|.*?[\\\/])[^.]+$",
22 # Other
23 r".*\.java$", r".*\.mk$", r".*\.am$",
24]
25
26COMMON_EXCLUDED_PATHS = [
27 # avoid doing source file checks for kernel
28 r"/src/third_party/kernel/",
29 r"/src/third_party/kernel-next/",
30 r".*\bexperimental[\\\/].*",
31 r".*\b[A-Z0-9_]{2,}$",
32 r".*[\\\/]debian[\\\/]rules$",
33]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070034
35def _get_hooks_dir():
Ryan Cuiec4d6332011-05-02 14:15:25 -070036 """Returns the absolute path to the repohooks directory."""
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070037 cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd']
38 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()
39
Ryan Cuiec4d6332011-05-02 14:15:25 -070040def _match_regex_list(subject, expressions):
41 """Try to match a list of regular expressions to a string.
42
43 Args:
44 subject: The string to match regexes on
45 expressions: A list of regular expressions to check for matches with.
46
47 Returns:
48 Whether the passed in subject matches any of the passed in regexes.
49 """
50 for expr in expressions:
51 if (re.search(expr, subject)):
52 return True
53 return False
54
55def _filter_files(files, include_list, exclude_list=[]):
56 """Filter out files based on the conditions passed in.
57
58 Args:
59 files: list of filepaths to filter
60 include_list: list of regex that when matched with a file path will cause it
61 to be added to the output list unless the file is also matched with a
62 regex in the exclude_list.
63 exclude_list: list of regex that when matched with a file will prevent it
64 from being added to the output list, even if it is also matched with a
65 regex in the include_list.
66
67 Returns:
68 A list of filepaths that contain files matched in the include_list and not
69 in the exclude_list.
70 """
71 filtered = []
72 for f in files:
73 if (_match_regex_list(f, include_list) and
74 not _match_regex_list(f, exclude_list)):
75 filtered.append(f)
76 return filtered
77
78def _report_error(msg, items=None):
79 """Raises an exception with the passed in error message.
80
81 If extra error detail is passed in, it will be appended to the error message.
82
83 Args:
84 msg: Error message header.
85 items: A list of lines that follow the header that give extra error
86 information.
87 """
88 if items:
89 msg += '\n' + '\n'.join(items)
90 raise Exception(msg)
91
92
93# Git Helpers
94
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070095def _get_diff(commit):
Ryan Cuiec4d6332011-05-02 14:15:25 -070096 """Returns the diff for this commit."""
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -070097 cmd = ['git', 'show', commit]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -070098 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
99
Ryan Cuiec4d6332011-05-02 14:15:25 -0700100def _get_file_diff(file, commit):
101 """Returns a list of (linenum, lines) tuples that the commit touched."""
102 cmd = ['git', 'show', '-p', '--no-ext-diff', commit, file]
103 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
104
105 new_lines = []
106 line_num = 0
107 for line in output.splitlines():
108 m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line)
109 if m:
110 line_num = int(m.groups(1)[0])
111 continue
112 if line.startswith('+') and not line.startswith('++'):
113 new_lines.append((line_num, line[1:]))
114 if not line.startswith('-'):
115 line_num += 1
116 return new_lines
117
118def _get_affected_files(commit):
119 """Returns list of absolute filepaths that were modified/added."""
120 cmd = ['git', 'diff', '--name-status', commit + '^!']
121 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
122 files = []
123 for statusline in output.splitlines():
124 m = re.match('^(\w)+\t(.+)$', statusline.rstrip())
125 # Ignore deleted files, and return absolute paths of files
126 if (m.group(1)[0] != 'D'):
127 pwd = os.getcwd()
128 files.append(os.path.join(pwd, m.group(2)))
129 return files
130
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700131def _get_commits():
Ryan Cuiec4d6332011-05-02 14:15:25 -0700132 """Returns a list of commits for this review."""
133 cmd = ['git', 'log', 'm/master..', '--format=%H']
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700134 commits = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
135 return commits.split()
136
Ryan Cuiec4d6332011-05-02 14:15:25 -0700137def _get_commit_desc(commit):
138 """Returns the full commit message of a commit."""
139 cmd = ['git', 'log', '--format=%B', commit + '^!']
140 description = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
141 return description.splitlines()
142
143
144# Common Hooks
145
146def _check_no_long_lines(project, commit):
147 """Checks that there aren't any lines longer than maxlen characters in any of
148 the text files to be submitted.
149 """
150 MAX_LEN = 80
151
152 errors = []
153 files = _filter_files(_get_affected_files(commit),
154 COMMON_INCLUDED_PATHS,
155 COMMON_EXCLUDED_PATHS)
156
157 for afile in files:
158 for line_num, line in _get_file_diff(afile, commit):
159 # Allow certain lines to exceed the maxlen rule.
160 if (len(line) > MAX_LEN and
161 not 'http://' in line and
162 not 'https://' in line and
163 not line.startswith('#define') and
164 not line.startswith('#include') and
165 not line.startswith('#import') and
166 not line.startswith('#pragma') and
167 not line.startswith('#if') and
168 not line.startswith('#endif')):
169 errors.append('%s, line %s, %s chars' % (afile, line_num, len(line)))
170 if len(errors) == 5: # Just show the first 5 errors.
171 break
172
173 if errors:
174 msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN
175 _report_error(msg, errors)
176
177def _check_no_stray_whitespace(project, commit):
178 """Checks that there is no stray whitespace at source lines end."""
179 errors = []
180 files = _filter_files(_get_affected_files(commit),
181 COMMON_INCLUDED_PATHS,
182 COMMON_EXCLUDED_PATHS)
183
184 for afile in files:
185 for line_num, line in _get_file_diff(afile, commit):
186 if line.rstrip() != line:
187 errors.append('%s, line %s' % (afile, line_num))
188 if errors:
189 _report_error('Found line ending with white space in:', errors)
190
191def _check_no_tabs(project, commit):
192 """Checks there are no unexpanded tabs."""
193 TAB_OK_PATHS = [
194 r"/src/third_party/u-boot/",
195 r"/src/third_party/u-boot-next/",
196 r".*\.ebuild$",
197 r".*\.eclass$",
198 r".*/[M|m]akefile$"
199 ]
200
201 errors = []
202 files = _filter_files(_get_affected_files(commit),
203 COMMON_INCLUDED_PATHS,
204 COMMON_EXCLUDED_PATHS + TAB_OK_PATHS)
205
206 for afile in files:
207 for line_num, line in _get_file_diff(afile, commit):
208 if '\t' in line:
209 errors.append('%s, line %s' % (afile, line_num))
210 if errors:
211 _report_error('Found a tab character in:', errors)
212
213def _check_change_has_test_field(project, commit):
214 """Check for a non-empty 'TEST=' field in the commit message."""
215 TEST_RE = r'^\s*TEST\s*=\s*\S+.*$'
216
217 found_field = False
218 for line in _get_commit_desc(commit):
219
220 if re.match(TEST_RE, line):
221 found_field = True
222 break
223
224 if not found_field:
225 _report_error('Changelist description needs TEST field')
226
227def _check_change_has_bug_field(project, commit):
228 """Check for a non-empty 'BUG=' field in the commit message."""
229 BUG_RE = r'^\s*BUG\s*=\s*\S+.*$'
230
231 found_field = False
232 for line in _get_commit_desc(commit):
233 if re.match(BUG_RE, line):
234 found_field = True
235 break
236
237 if not found_field:
238 _report_error('Changelist description needs BUG field')
239
240def _check_license(project, commit):
241 """Verifies the license header."""
242 LICENSE_HEADER = (
243 r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights "
244 r"reserved\." "\n"
245 r".*? Use of this source code is governed by a BSD-style license that can "
246 "be\n"
247 r".*? found in the LICENSE file\."
248 "\n"
249 )
250
251 license_re = re.compile(LICENSE_HEADER, re.MULTILINE)
252 bad_files = []
253 files = _filter_files(_get_affected_files(commit),
254 COMMON_INCLUDED_PATHS,
255 COMMON_EXCLUDED_PATHS)
256
257 for f in files:
258 contents = open(f).read()
259 if len(contents) == 0: continue # Ignore empty files
260 if not license_re.search(contents):
261 bad_files.append(f)
262 if bad_files:
263 _report_error('License must match:\n%s\n' % license_re.pattern +
264 'Found a bad license header in these files:',
265 bad_files)
266
267
268# Project-specific hooks
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700269
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700270def _run_checkpatch(project, commit):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700271 """Runs checkpatch.pl on the given project"""
272 hooks_dir = _get_hooks_dir()
273 cmd = ['%s/checkpatch.pl' % hooks_dir, '-']
274 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700275 output = p.communicate(_get_diff(commit))[0]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700276 if p.returncode:
Ryan Cuiec4d6332011-05-02 14:15:25 -0700277 _report_error('checkpatch.pl errors/warnings\n\n' + output)
278
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700279
280# Base
281
Ryan Cuiec4d6332011-05-02 14:15:25 -0700282COMMON_HOOKS = [_check_no_long_lines,
283 _check_no_stray_whitespace,
284 _check_no_tabs,
285 _check_change_has_test_field,
286 _check_change_has_bug_field,
287 _check_license]
288
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700289def _setup_project_hooks():
290 """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]"""
291 return {
Doug Anderson830216f2011-05-02 10:08:37 -0700292 "chromiumos/third_party/kernel": [_run_checkpatch],
293 "chromiumos/third_party/kernel-next": [_run_checkpatch],
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700294 }
295
296def _run_project_hooks(project, hooks):
297 """For each project run its project specific hook from the hooks dictionary"""
298 cmd = ['repo', 'forall', project, '-c', 'pwd']
299 proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
300 proj_dir = proj_dir.strip()
Ryan Cuiec4d6332011-05-02 14:15:25 -0700301 pwd = os.getcwd()
302 # hooks assume they are run from the root of the project
303 os.chdir(proj_dir)
304
305 project_specific_hooks = []
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700306 if project in hooks:
Ryan Cuiec4d6332011-05-02 14:15:25 -0700307 project_specific_hooks = hooks[project]
308
309 for commit in _get_commits():
Mandeep Singh Bainesa7ffa4b2011-05-03 11:37:02 -0700310 try:
311 for hook in COMMON_HOOKS + project_specific_hooks:
312 hook(project, commit)
313 except:
314 msg = 'ERROR: pre-upload failed: commit=%s, project=%s' % (commit[:8],
315 project)
316 print >> sys.stderr, msg
317 raise
Ryan Cuiec4d6332011-05-02 14:15:25 -0700318 os.chdir(pwd)
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700319
320# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700321
Anush Elangovan63afad72011-03-23 00:41:27 -0700322def main(project_list, **kwargs):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700323 hooks = _setup_project_hooks()
324 for project in project_list:
325 _run_project_hooks(project, hooks)
Anush Elangovan63afad72011-03-23 00:41:27 -0700326
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700327if __name__ == '__main__':
328 main()