blob: dde268015385d45acae814c75fe9065d89875fd8 [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
Dale Curtis2975c432011-05-03 17:25:20 -07005import json
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07006import os
Ryan Cuiec4d6332011-05-02 14:15:25 -07007import re
Mandeep Singh Bainesa7ffa4b2011-05-03 11:37:02 -07008import sys
Mandeep Singh Baines116ad102011-04-27 15:16:37 -07009import subprocess
10
Ryan Cuiec4d6332011-05-02 14:15:25 -070011
12# General Helpers
13
14COMMON_INCLUDED_PATHS = [
15 # C++ and friends
16 r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$",
17 r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$",
18 # Scripts
19 r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$",
20 # No extension at all, note that ALL CAPS files are black listed in
21 # COMMON_EXCLUDED_LIST below.
22 r"(^|.*?[\\\/])[^.]+$",
23 # Other
24 r".*\.java$", r".*\.mk$", r".*\.am$",
25]
26
27COMMON_EXCLUDED_PATHS = [
Ryan Cuie37fe1a2011-05-03 19:00:10 -070028 # don't do source file checks for all third_party projects for now
29 r"/src/third_party/",
Ryan Cuiec4d6332011-05-02 14:15:25 -070030 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 + '^!']
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700140 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
Ryan Cuiec4d6332011-05-02 14:15:25 -0700141
142
143# Common Hooks
144
145def _check_no_long_lines(project, commit):
146 """Checks that there aren't any lines longer than maxlen characters in any of
147 the text files to be submitted.
148 """
149 MAX_LEN = 80
150
151 errors = []
152 files = _filter_files(_get_affected_files(commit),
153 COMMON_INCLUDED_PATHS,
154 COMMON_EXCLUDED_PATHS)
155
156 for afile in files:
157 for line_num, line in _get_file_diff(afile, commit):
158 # Allow certain lines to exceed the maxlen rule.
159 if (len(line) > MAX_LEN and
160 not 'http://' in line and
161 not 'https://' in line and
162 not line.startswith('#define') and
163 not line.startswith('#include') and
164 not line.startswith('#import') and
165 not line.startswith('#pragma') and
166 not line.startswith('#if') and
167 not line.startswith('#endif')):
168 errors.append('%s, line %s, %s chars' % (afile, line_num, len(line)))
169 if len(errors) == 5: # Just show the first 5 errors.
170 break
171
172 if errors:
173 msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN
174 _report_error(msg, errors)
175
176def _check_no_stray_whitespace(project, commit):
177 """Checks that there is no stray whitespace at source lines end."""
178 errors = []
179 files = _filter_files(_get_affected_files(commit),
180 COMMON_INCLUDED_PATHS,
181 COMMON_EXCLUDED_PATHS)
182
183 for afile in files:
184 for line_num, line in _get_file_diff(afile, commit):
185 if line.rstrip() != line:
186 errors.append('%s, line %s' % (afile, line_num))
187 if errors:
188 _report_error('Found line ending with white space in:', errors)
189
190def _check_no_tabs(project, commit):
191 """Checks there are no unexpanded tabs."""
192 TAB_OK_PATHS = [
Ryan Cuiec4d6332011-05-02 14:15:25 -0700193 r".*\.ebuild$",
194 r".*\.eclass$",
195 r".*/[M|m]akefile$"
196 ]
197
198 errors = []
199 files = _filter_files(_get_affected_files(commit),
200 COMMON_INCLUDED_PATHS,
201 COMMON_EXCLUDED_PATHS + TAB_OK_PATHS)
202
203 for afile in files:
204 for line_num, line in _get_file_diff(afile, commit):
205 if '\t' in line:
206 errors.append('%s, line %s' % (afile, line_num))
207 if errors:
208 _report_error('Found a tab character in:', errors)
209
210def _check_change_has_test_field(project, commit):
211 """Check for a non-empty 'TEST=' field in the commit message."""
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700212 TEST_RE = r'\n\s*TEST\s*=[^\n]*\S+'
Ryan Cuiec4d6332011-05-02 14:15:25 -0700213
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700214 if not re.search(TEST_RE, _get_commit_desc(commit)):
215 _report_error('Changelist description needs TEST field (after first line)')
Ryan Cuiec4d6332011-05-02 14:15:25 -0700216
217def _check_change_has_bug_field(project, commit):
218 """Check for a non-empty 'BUG=' field in the commit message."""
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700219 BUG_RE = r'\n\s*BUG\s*=[^\n]*\S+'
Ryan Cuiec4d6332011-05-02 14:15:25 -0700220
Mandeep Singh Baines96a53be2011-05-03 11:10:25 -0700221 if not re.search(BUG_RE, _get_commit_desc(commit)):
222 _report_error('Changelist description needs BUG field (after first line)')
Ryan Cuiec4d6332011-05-02 14:15:25 -0700223
Mandeep Singh Bainesa23eb5f2011-05-04 13:43:25 -0700224def _check_change_has_proper_changeid(project, commit):
225 """Verify that Change-ID is present in last paragraph of commit message."""
226 desc = _get_commit_desc(commit)
227 loc = desc.rfind('\nChange-Id:')
228 if loc == -1 or re.search('\n\s*\n\s*\S+', desc[loc:]):
229 _report_error('Change-Id must be in last paragraph of description.')
230
Ryan Cuiec4d6332011-05-02 14:15:25 -0700231def _check_license(project, commit):
232 """Verifies the license header."""
233 LICENSE_HEADER = (
234 r".*? Copyright \(c\) 20[-0-9]{2,7} The Chromium OS Authors\. All rights "
235 r"reserved\." "\n"
236 r".*? Use of this source code is governed by a BSD-style license that can "
237 "be\n"
238 r".*? found in the LICENSE file\."
239 "\n"
240 )
241
242 license_re = re.compile(LICENSE_HEADER, re.MULTILINE)
243 bad_files = []
244 files = _filter_files(_get_affected_files(commit),
245 COMMON_INCLUDED_PATHS,
246 COMMON_EXCLUDED_PATHS)
247
248 for f in files:
249 contents = open(f).read()
250 if len(contents) == 0: continue # Ignore empty files
251 if not license_re.search(contents):
252 bad_files.append(f)
253 if bad_files:
254 _report_error('License must match:\n%s\n' % license_re.pattern +
255 'Found a bad license header in these files:',
256 bad_files)
257
258
259# Project-specific hooks
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700260
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700261def _run_checkpatch(project, commit):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700262 """Runs checkpatch.pl on the given project"""
263 hooks_dir = _get_hooks_dir()
264 cmd = ['%s/checkpatch.pl' % hooks_dir, '-']
265 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Mandeep Singh Bainesb9ed1402011-04-29 15:32:06 -0700266 output = p.communicate(_get_diff(commit))[0]
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700267 if p.returncode:
Ryan Cuiec4d6332011-05-02 14:15:25 -0700268 _report_error('checkpatch.pl errors/warnings\n\n' + output)
269
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700270
Dale Curtis2975c432011-05-03 17:25:20 -0700271def _run_json_check(project, commit):
272 """Checks that all JSON files are syntactically valid."""
Dale Curtisa039cfd2011-05-04 12:01:05 -0700273 for f in _filter_files(_get_affected_files(commit), [r'.*\.json']):
Dale Curtis2975c432011-05-03 17:25:20 -0700274 try:
275 json.load(open(f))
276 except Exception, e:
277 _report_error('Invalid JSON in %s: %s' % (f, e))
278
279
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700280# Base
281
Ryan Cuie37fe1a2011-05-03 19:00:10 -0700282COMMON_HOOKS = [_check_change_has_bug_field,
283 _check_change_has_test_field,
284 _check_change_has_proper_changeid,
285 _check_no_long_lines,
Ryan Cuiec4d6332011-05-02 14:15:25 -0700286 _check_no_stray_whitespace,
287 _check_no_tabs,
Ryan Cuiec4d6332011-05-02 14:15:25 -0700288 _check_license]
289
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700290def _setup_project_hooks():
291 """Returns a dictionay of callbacks: dict[project] = [callback1, callback2]"""
292 return {
Doug Anderson830216f2011-05-02 10:08:37 -0700293 "chromiumos/third_party/kernel": [_run_checkpatch],
294 "chromiumos/third_party/kernel-next": [_run_checkpatch],
Dale Curtis2975c432011-05-03 17:25:20 -0700295 "chromeos/autotest-tools": [_run_json_check],
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700296 }
297
298def _run_project_hooks(project, hooks):
299 """For each project run its project specific hook from the hooks dictionary"""
300 cmd = ['repo', 'forall', project, '-c', 'pwd']
301 proj_dir = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
302 proj_dir = proj_dir.strip()
Ryan Cuiec4d6332011-05-02 14:15:25 -0700303 pwd = os.getcwd()
304 # hooks assume they are run from the root of the project
305 os.chdir(proj_dir)
306
307 project_specific_hooks = []
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700308 if project in hooks:
Ryan Cuiec4d6332011-05-02 14:15:25 -0700309 project_specific_hooks = hooks[project]
310
311 for commit in _get_commits():
Mandeep Singh Bainesa7ffa4b2011-05-03 11:37:02 -0700312 try:
313 for hook in COMMON_HOOKS + project_specific_hooks:
314 hook(project, commit)
315 except:
316 msg = 'ERROR: pre-upload failed: commit=%s, project=%s' % (commit[:8],
317 project)
318 print >> sys.stderr, msg
319 raise
Ryan Cuiec4d6332011-05-02 14:15:25 -0700320 os.chdir(pwd)
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700321
322# Main
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700323
Anush Elangovan63afad72011-03-23 00:41:27 -0700324def main(project_list, **kwargs):
Mandeep Singh Baines116ad102011-04-27 15:16:37 -0700325 hooks = _setup_project_hooks()
326 for project in project_list:
327 _run_project_hooks(project, hooks)
Anush Elangovan63afad72011-03-23 00:41:27 -0700328
Mandeep Singh Baines69e470e2011-04-06 10:34:52 -0700329if __name__ == '__main__':
330 main()