Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 6 | """Presubmit checks to run when doing `repo upload`. |
| 7 | |
| 8 | You can add new checks by adding a functions to the HOOKS constants. |
| 9 | """ |
| 10 | |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 11 | from __future__ import print_function |
| 12 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 13 | import argparse |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 14 | import collections |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 15 | import ConfigParser |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 16 | import fnmatch |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 17 | import functools |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 18 | import json |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 19 | import os |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 20 | import re |
Mandeep Singh Baines | a7ffa4b | 2011-05-03 11:37:02 -0700 | [diff] [blame] | 21 | import sys |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 22 | import stat |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 23 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 24 | from errors import (VerifyException, HookFailure, PrintErrorForProject, |
| 25 | PrintErrorsForCommit) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 26 | |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 27 | # If repo imports us, the __name__ will be __builtin__, and the wrapper will |
| 28 | # be in $CHROMEOS_CHECKOUT/.repo/repo/main.py, so we need to go two directories |
| 29 | # up. The same logic also happens to work if we're executed directly. |
| 30 | if __name__ in ('__builtin__', '__main__'): |
| 31 | sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '..', '..')) |
| 32 | |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 33 | from chromite.lib import commandline |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 34 | from chromite.lib import cros_build_lib |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 35 | from chromite.lib import git |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 36 | from chromite.lib import osutils |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 37 | from chromite.lib import patch |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 38 | from chromite.licensing import licenses_lib |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 39 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 40 | PRE_SUBMIT = 'pre-submit' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 41 | |
| 42 | COMMON_INCLUDED_PATHS = [ |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 43 | # C++ and friends |
| 44 | r".*\.c$", r".*\.cc$", r".*\.cpp$", r".*\.h$", r".*\.m$", r".*\.mm$", |
| 45 | r".*\.inl$", r".*\.asm$", r".*\.hxx$", r".*\.hpp$", r".*\.s$", r".*\.S$", |
| 46 | # Scripts |
| 47 | r".*\.js$", r".*\.py$", r".*\.sh$", r".*\.rb$", r".*\.pl$", r".*\.pm$", |
| 48 | # No extension at all, note that ALL CAPS files are black listed in |
| 49 | # COMMON_EXCLUDED_LIST below. |
| 50 | r"(^|.*[\\\/])[^.]+$", |
| 51 | # Other |
| 52 | r".*\.java$", r".*\.mk$", r".*\.am$", |
Brian Norris | fdbac8e | 2016-06-16 11:09:05 -0700 | [diff] [blame^] | 53 | r".*\.policy$", r".*\.conf$", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 54 | ] |
| 55 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 56 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 57 | COMMON_EXCLUDED_PATHS = [ |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 58 | # Avoid doing source file checks for kernel. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 59 | r"/src/third_party/kernel/", |
| 60 | r"/src/third_party/kernel-next/", |
| 61 | r"/src/third_party/ktop/", |
| 62 | r"/src/third_party/punybench/", |
| 63 | r".*\bexperimental[\\\/].*", |
| 64 | r".*\b[A-Z0-9_]{2,}$", |
| 65 | r".*[\\\/]debian[\\\/]rules$", |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 66 | |
| 67 | # For ebuild trees, ignore any caches and manifest data. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 68 | r".*/Manifest$", |
| 69 | r".*/metadata/[^/]*cache[^/]*/[^/]+/[^/]+$", |
Doug Anderson | 5bfb679 | 2011-10-25 16:45:41 -0700 | [diff] [blame] | 70 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 71 | # Ignore profiles data (like overlay-tegra2/profiles). |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 72 | r"(^|.*/)overlay-.*/profiles/.*", |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 73 | r"^profiles/.*$", |
| 74 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 75 | # Ignore minified js and jquery. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 76 | r".*\.min\.js", |
| 77 | r".*jquery.*\.js", |
Mike Frysinger | 33a458d | 2014-03-03 17:00:51 -0500 | [diff] [blame] | 78 | |
| 79 | # Ignore license files as the content is often taken verbatim. |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 80 | r".*/licenses/.*", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 81 | ] |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 82 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 83 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 84 | _CONFIG_FILE = 'PRESUBMIT.cfg' |
| 85 | |
| 86 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 87 | # File containing wildcards, one per line, matching files that should be |
| 88 | # excluded from presubmit checks. Lines beginning with '#' are ignored. |
| 89 | _IGNORE_FILE = '.presubmitignore' |
| 90 | |
| 91 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 92 | # Exceptions |
| 93 | |
| 94 | |
| 95 | class BadInvocation(Exception): |
| 96 | """An Exception indicating a bad invocation of the program.""" |
| 97 | pass |
| 98 | |
| 99 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 100 | # General Helpers |
| 101 | |
Sean Paul | ba01d40 | 2011-05-05 11:36:23 -0400 | [diff] [blame] | 102 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 103 | Project = collections.namedtuple('Project', ['name', 'dir', 'remote']) |
| 104 | |
| 105 | |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 106 | # pylint: disable=redefined-builtin |
| 107 | def _run_command(cmd, cwd=None, input=None, |
| 108 | redirect_stderr=False, combine_stdout_stderr=False): |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 109 | """Executes the passed in command and returns raw stdout output. |
| 110 | |
| 111 | Args: |
| 112 | cmd: The command to run; should be a list of strings. |
| 113 | cwd: The directory to switch to for running the command. |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 114 | input: The data to pipe into this command through stdin. If a file object |
| 115 | or file descriptor, stdin will be connected directly to that. |
| 116 | redirect_stderr: Redirect stderr away from console. |
| 117 | combine_stdout_stderr: Combines stdout and stderr streams into stdout. |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 118 | |
| 119 | Returns: |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 120 | The stdout from the process (discards stderr and returncode). |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 121 | """ |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 122 | return cros_build_lib.RunCommand(cmd=cmd, |
| 123 | cwd=cwd, |
| 124 | print_cmd=False, |
| 125 | input=input, |
| 126 | stdout_to_pipe=True, |
| 127 | redirect_stderr=redirect_stderr, |
| 128 | combine_stdout_stderr=combine_stdout_stderr, |
| 129 | error_code_ok=True).output |
| 130 | # pylint: enable=redefined-builtin |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 131 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 132 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 133 | def _get_hooks_dir(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 134 | """Returns the absolute path to the repohooks directory.""" |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 135 | if __name__ == '__main__': |
| 136 | # Works when file is run on its own (__file__ is defined)... |
| 137 | return os.path.abspath(os.path.dirname(__file__)) |
| 138 | else: |
| 139 | # We need to do this when we're run through repo. Since repo executes |
| 140 | # us with execfile(), we don't get __file__ defined. |
| 141 | cmd = ['repo', 'forall', 'chromiumos/repohooks', '-c', 'pwd'] |
| 142 | return _run_command(cmd).strip() |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 143 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 144 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 145 | def _match_regex_list(subject, expressions): |
| 146 | """Try to match a list of regular expressions to a string. |
| 147 | |
| 148 | Args: |
| 149 | subject: The string to match regexes on |
| 150 | expressions: A list of regular expressions to check for matches with. |
| 151 | |
| 152 | Returns: |
| 153 | Whether the passed in subject matches any of the passed in regexes. |
| 154 | """ |
| 155 | for expr in expressions: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 156 | if re.search(expr, subject): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 157 | return True |
| 158 | return False |
| 159 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 160 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 161 | def _filter_files(files, include_list, exclude_list=()): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 162 | """Filter out files based on the conditions passed in. |
| 163 | |
| 164 | Args: |
| 165 | files: list of filepaths to filter |
| 166 | include_list: list of regex that when matched with a file path will cause it |
| 167 | to be added to the output list unless the file is also matched with a |
| 168 | regex in the exclude_list. |
| 169 | exclude_list: list of regex that when matched with a file will prevent it |
| 170 | from being added to the output list, even if it is also matched with a |
| 171 | regex in the include_list. |
| 172 | |
| 173 | Returns: |
| 174 | A list of filepaths that contain files matched in the include_list and not |
| 175 | in the exclude_list. |
| 176 | """ |
| 177 | filtered = [] |
| 178 | for f in files: |
| 179 | if (_match_regex_list(f, include_list) and |
| 180 | not _match_regex_list(f, exclude_list)): |
| 181 | filtered.append(f) |
| 182 | return filtered |
| 183 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 184 | |
| 185 | # Git Helpers |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 186 | |
| 187 | |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 188 | def _get_upstream_branch(): |
| 189 | """Returns the upstream tracking branch of the current branch. |
| 190 | |
| 191 | Raises: |
| 192 | Error if there is no tracking branch |
| 193 | """ |
| 194 | current_branch = _run_command(['git', 'symbolic-ref', 'HEAD']).strip() |
| 195 | current_branch = current_branch.replace('refs/heads/', '') |
| 196 | if not current_branch: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 197 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 198 | |
| 199 | cfg_option = 'branch.' + current_branch + '.%s' |
| 200 | full_upstream = _run_command(['git', 'config', cfg_option % 'merge']).strip() |
| 201 | remote = _run_command(['git', 'config', cfg_option % 'remote']).strip() |
| 202 | if not remote or not full_upstream: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 203 | raise VerifyException('Need to be on a tracking branch') |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 204 | |
| 205 | return full_upstream.replace('heads', 'remotes/' + remote) |
| 206 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 207 | |
Che-Liang Chiou | 5ce2d7b | 2013-03-22 18:47:55 -0700 | [diff] [blame] | 208 | def _get_patch(commit): |
| 209 | """Returns the patch for this commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 210 | if commit == PRE_SUBMIT: |
| 211 | return _run_command(['git', 'diff', '--cached', 'HEAD']) |
| 212 | else: |
| 213 | return _run_command(['git', 'format-patch', '--stdout', '-1', commit]) |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 214 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 215 | |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 216 | def _try_utf8_decode(data): |
| 217 | """Attempts to decode a string as UTF-8. |
| 218 | |
| 219 | Returns: |
| 220 | The decoded Unicode object, or the original string if parsing fails. |
| 221 | """ |
| 222 | try: |
| 223 | return unicode(data, 'utf-8', 'strict') |
| 224 | except UnicodeDecodeError: |
| 225 | return data |
| 226 | |
| 227 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 228 | def _get_file_content(path, commit): |
| 229 | """Returns the content of a file at a specific commit. |
| 230 | |
| 231 | We can't rely on the file as it exists in the filesystem as people might be |
| 232 | uploading a series of changes which modifies the file multiple times. |
| 233 | |
| 234 | Note: The "content" of a symlink is just the target. So if you're expecting |
| 235 | a full file, you should check that first. One way to detect is that the |
| 236 | content will not have any newlines. |
| 237 | """ |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 238 | if commit == PRE_SUBMIT: |
| 239 | return _run_command(['git', 'diff', 'HEAD', path]) |
| 240 | else: |
| 241 | return _run_command(['git', 'show', '%s:%s' % (commit, path)]) |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 242 | |
| 243 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 244 | def _get_file_diff(path, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 245 | """Returns a list of (linenum, lines) tuples that the commit touched.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 246 | command = ['git', 'diff', '-p', '--pretty=format:', '--no-ext-diff'] |
| 247 | if commit == PRE_SUBMIT: |
| 248 | command += ['HEAD', path] |
| 249 | else: |
| 250 | command += [commit, path] |
| 251 | output = _run_command(command) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 252 | |
| 253 | new_lines = [] |
| 254 | line_num = 0 |
| 255 | for line in output.splitlines(): |
| 256 | m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
| 257 | if m: |
| 258 | line_num = int(m.groups(1)[0]) |
| 259 | continue |
| 260 | if line.startswith('+') and not line.startswith('++'): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 261 | new_lines.append((line_num, _try_utf8_decode(line[1:]))) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 262 | if not line.startswith('-'): |
| 263 | line_num += 1 |
| 264 | return new_lines |
| 265 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 266 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 267 | def _get_ignore_wildcards(directory, cache): |
| 268 | """Get wildcards listed in a directory's _IGNORE_FILE. |
| 269 | |
| 270 | Args: |
| 271 | directory: A string containing a directory path. |
| 272 | cache: A dictionary (opaque to caller) caching previously-read wildcards. |
| 273 | |
| 274 | Returns: |
| 275 | A list of wildcards from _IGNORE_FILE or an empty list if _IGNORE_FILE |
| 276 | wasn't present. |
| 277 | """ |
| 278 | # In the cache, keys are directories and values are lists of wildcards from |
| 279 | # _IGNORE_FILE within those directories (and empty if no file was present). |
| 280 | if directory not in cache: |
| 281 | wildcards = [] |
| 282 | dotfile_path = os.path.join(directory, _IGNORE_FILE) |
| 283 | if os.path.exists(dotfile_path): |
| 284 | # TODO(derat): Consider using _get_file_content() to get the file as of |
| 285 | # this commit instead of the on-disk version. This may have a noticeable |
| 286 | # performance impact, as each call to _get_file_content() runs git. |
| 287 | with open(dotfile_path, 'r') as dotfile: |
| 288 | for line in dotfile.readlines(): |
| 289 | line = line.strip() |
| 290 | if line.startswith('#'): |
| 291 | continue |
| 292 | if line.endswith('/'): |
| 293 | line += '*' |
| 294 | wildcards.append(line) |
| 295 | cache[directory] = wildcards |
| 296 | |
| 297 | return cache[directory] |
| 298 | |
| 299 | |
| 300 | def _path_is_ignored(path, cache): |
| 301 | """Check whether a path is ignored by _IGNORE_FILE. |
| 302 | |
| 303 | Args: |
| 304 | path: A string containing a path. |
| 305 | cache: A dictionary (opaque to caller) caching previously-read wildcards. |
| 306 | |
| 307 | Returns: |
| 308 | True if a file named _IGNORE_FILE in one of the passed-in path's parent |
| 309 | directories contains a wildcard matching the path. |
| 310 | """ |
| 311 | # Skip ignore files. |
| 312 | if os.path.basename(path) == _IGNORE_FILE: |
| 313 | return True |
| 314 | |
| 315 | path = os.path.abspath(path) |
| 316 | base = os.getcwd() |
| 317 | |
| 318 | prefix = os.path.dirname(path) |
| 319 | while prefix.startswith(base): |
| 320 | rel_path = path[len(prefix) + 1:] |
| 321 | for wildcard in _get_ignore_wildcards(prefix, cache): |
| 322 | if fnmatch.fnmatch(rel_path, wildcard): |
| 323 | return True |
| 324 | prefix = os.path.dirname(prefix) |
| 325 | |
| 326 | return False |
| 327 | |
| 328 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 329 | def _get_affected_files(commit, include_deletes=False, relative=False, |
| 330 | include_symlinks=False, include_adds=True, |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 331 | full_details=False, use_ignore_files=True): |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 332 | """Returns list of file paths that were modified/added, excluding symlinks. |
| 333 | |
| 334 | Args: |
| 335 | commit: The commit |
| 336 | include_deletes: If true, we'll include deleted files in the result |
| 337 | relative: Whether to return relative or full paths to files |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 338 | include_symlinks: If true, we'll include symlinks in the result |
| 339 | include_adds: If true, we'll include new files in the result |
| 340 | full_details: If False, return filenames, else return structured results. |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 341 | use_ignore_files: Whether we ignore files matched by _IGNORE_FILE files. |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 342 | |
| 343 | Returns: |
| 344 | A list of modified/added (and perhaps deleted) files |
| 345 | """ |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 346 | if not relative and full_details: |
| 347 | raise ValueError('full_details only supports relative paths currently') |
| 348 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 349 | if commit == PRE_SUBMIT: |
| 350 | return _run_command(['git', 'diff-index', '--cached', |
| 351 | '--name-only', 'HEAD']).split() |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 352 | |
| 353 | path = os.getcwd() |
| 354 | files = git.RawDiff(path, '%s^!' % commit) |
| 355 | |
| 356 | # Filter out symlinks. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 357 | if not include_symlinks: |
| 358 | files = [x for x in files if not stat.S_ISLNK(int(x.dst_mode, 8))] |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 359 | |
| 360 | if not include_deletes: |
| 361 | files = [x for x in files if x.status != 'D'] |
| 362 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 363 | if not include_adds: |
| 364 | files = [x for x in files if x.status != 'A'] |
| 365 | |
Daniel Erat | e3ea3fc | 2015-02-13 15:27:52 -0700 | [diff] [blame] | 366 | if use_ignore_files: |
| 367 | cache = {} |
| 368 | is_ignored = lambda x: _path_is_ignored(x.dst_file or x.src_file, cache) |
| 369 | files = [x for x in files if not is_ignored(x)] |
| 370 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 371 | if full_details: |
| 372 | # Caller wants the raw objects to parse status/etc... themselves. |
Mike Frysinger | d3bd32c | 2014-11-24 23:34:29 -0500 | [diff] [blame] | 373 | return files |
| 374 | else: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 375 | # Caller only cares about filenames. |
| 376 | files = [x.dst_file if x.dst_file else x.src_file for x in files] |
| 377 | if relative: |
| 378 | return files |
| 379 | else: |
| 380 | return [os.path.join(path, x) for x in files] |
Peter Ammon | 811f670 | 2014-06-12 15:45:38 -0700 | [diff] [blame] | 381 | |
| 382 | |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 383 | def _get_commits(): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 384 | """Returns a list of commits for this review.""" |
Ryan Cui | 4725d95 | 2011-05-05 15:41:19 -0700 | [diff] [blame] | 385 | cmd = ['git', 'log', '%s..' % _get_upstream_branch(), '--format=%H'] |
Ryan Cui | 72834d1 | 2011-05-05 14:51:33 -0700 | [diff] [blame] | 386 | return _run_command(cmd).split() |
Mandeep Singh Baines | b9ed140 | 2011-04-29 15:32:06 -0700 | [diff] [blame] | 387 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 388 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 389 | def _get_commit_desc(commit): |
| 390 | """Returns the full commit message of a commit.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 391 | if commit == PRE_SUBMIT: |
| 392 | return '' |
Sean Paul | 23a2c58 | 2011-05-06 13:10:44 -0400 | [diff] [blame] | 393 | return _run_command(['git', 'log', '--format=%s%n%n%b', commit + '^!']) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 394 | |
| 395 | |
| 396 | # Common Hooks |
| 397 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 398 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 399 | def _check_no_long_lines(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 400 | """Checks there are no lines longer than MAX_LEN in any of the text files.""" |
| 401 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 402 | MAX_LEN = 80 |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 403 | SKIP_REGEXP = re.compile('|'.join([ |
| 404 | r'https?://', |
| 405 | r'^#\s*(define|include|import|pragma|if|endif)\b'])) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 406 | |
| 407 | errors = [] |
| 408 | files = _filter_files(_get_affected_files(commit), |
| 409 | COMMON_INCLUDED_PATHS, |
| 410 | COMMON_EXCLUDED_PATHS) |
| 411 | |
| 412 | for afile in files: |
| 413 | for line_num, line in _get_file_diff(afile, commit): |
| 414 | # Allow certain lines to exceed the maxlen rule. |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 415 | if len(line) <= MAX_LEN or SKIP_REGEXP.search(line): |
Jon Salz | 9825593 | 2012-08-18 14:48:02 +0800 | [diff] [blame] | 416 | continue |
| 417 | |
| 418 | errors.append('%s, line %s, %s chars' % (afile, line_num, len(line))) |
| 419 | if len(errors) == 5: # Just show the first 5 errors. |
| 420 | break |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 421 | |
| 422 | if errors: |
| 423 | msg = 'Found lines longer than %s characters (first 5 shown):' % MAX_LEN |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 424 | return HookFailure(msg, errors) |
| 425 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 426 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 427 | def _check_no_stray_whitespace(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 428 | """Checks that there is no stray whitespace at source lines end.""" |
| 429 | errors = [] |
| 430 | files = _filter_files(_get_affected_files(commit), |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 431 | COMMON_INCLUDED_PATHS, |
| 432 | COMMON_EXCLUDED_PATHS) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 433 | for afile in files: |
| 434 | for line_num, line in _get_file_diff(afile, commit): |
| 435 | if line.rstrip() != line: |
| 436 | errors.append('%s, line %s' % (afile, line_num)) |
| 437 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 438 | return HookFailure('Found line ending with white space in:', errors) |
| 439 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 440 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 441 | def _check_no_tabs(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 442 | """Checks there are no unexpanded tabs.""" |
| 443 | TAB_OK_PATHS = [ |
Ryan Cui | 31e0c17 | 2011-05-04 21:00:45 -0700 | [diff] [blame] | 444 | r"/src/third_party/u-boot/", |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 445 | r".*\.ebuild$", |
| 446 | r".*\.eclass$", |
Elly Jones | 5ab3419 | 2011-11-15 14:57:06 -0500 | [diff] [blame] | 447 | r".*/[M|m]akefile$", |
| 448 | r".*\.mk$" |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 449 | ] |
| 450 | |
| 451 | errors = [] |
| 452 | files = _filter_files(_get_affected_files(commit), |
| 453 | COMMON_INCLUDED_PATHS, |
| 454 | COMMON_EXCLUDED_PATHS + TAB_OK_PATHS) |
| 455 | |
| 456 | for afile in files: |
| 457 | for line_num, line in _get_file_diff(afile, commit): |
| 458 | if '\t' in line: |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 459 | errors.append('%s, line %s' % (afile, line_num)) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 460 | if errors: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 461 | return HookFailure('Found a tab character in:', errors) |
| 462 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 463 | |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 464 | def _check_gofmt(_project, commit): |
| 465 | """Checks that Go files are formatted with gofmt.""" |
| 466 | errors = [] |
| 467 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 468 | [r'\.go$']) |
| 469 | |
| 470 | for gofile in files: |
| 471 | contents = _get_file_content(gofile, commit) |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 472 | output = _run_command(cmd=['gofmt', '-l'], input=contents, |
| 473 | combine_stdout_stderr=True) |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 474 | if output: |
| 475 | errors.append(gofile) |
| 476 | if errors: |
| 477 | return HookFailure('Files not formatted with gofmt:', errors) |
| 478 | |
| 479 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 480 | def _check_change_has_test_field(_project, commit): |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 481 | """Check for a non-empty 'TEST=' field in the commit message.""" |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 482 | TEST_RE = r'\nTEST=\S+' |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 483 | |
Mandeep Singh Baines | 96a53be | 2011-05-03 11:10:25 -0700 | [diff] [blame] | 484 | if not re.search(TEST_RE, _get_commit_desc(commit)): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 485 | msg = 'Changelist description needs TEST field (after first line)' |
| 486 | return HookFailure(msg) |
| 487 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 488 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 489 | def _check_change_has_valid_cq_depend(_project, commit): |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 490 | """Check for a correctly formatted CQ-DEPEND field in the commit message.""" |
| 491 | msg = 'Changelist has invalid CQ-DEPEND target.' |
| 492 | example = 'Example: CQ-DEPEND=CL:1234, CL:2345' |
| 493 | try: |
| 494 | patch.GetPaladinDeps(_get_commit_desc(commit)) |
| 495 | except ValueError as ex: |
| 496 | return HookFailure(msg, [example, str(ex)]) |
| 497 | |
| 498 | |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 499 | def _check_change_is_contribution(_project, commit): |
| 500 | """Check that the change is a contribution.""" |
| 501 | NO_CONTRIB = 'not a contribution' |
| 502 | if NO_CONTRIB in _get_commit_desc(commit).lower(): |
| 503 | msg = ('Changelist is not a contribution, this cannot be accepted.\n' |
| 504 | 'Please remove the "%s" text from the commit message.') % NO_CONTRIB |
| 505 | return HookFailure(msg) |
| 506 | |
| 507 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 508 | def _check_change_has_bug_field(project, commit): |
David McMahon | 8f6553e | 2011-06-10 15:46:36 -0700 | [diff] [blame] | 509 | """Check for a correctly formatted 'BUG=' field in the commit message.""" |
David James | 5c0073d | 2013-04-03 08:48:52 -0700 | [diff] [blame] | 510 | OLD_BUG_RE = r'\nBUG=.*chromium-os' |
| 511 | if re.search(OLD_BUG_RE, _get_commit_desc(commit)): |
| 512 | msg = ('The chromium-os bug tracker is now deprecated. Please use\n' |
| 513 | 'the chromium tracker in your BUG= line now.') |
| 514 | return HookFailure(msg) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 515 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 516 | # Android internal and external projects use "Bug: " to track bugs in |
| 517 | # buganizer. |
| 518 | BUG_COLON_REMOTES = ( |
| 519 | 'aosp', |
| 520 | 'goog', |
| 521 | ) |
| 522 | if project.remote in BUG_COLON_REMOTES: |
| 523 | BUG_RE = r'\nBug: ?([Nn]one|\d+)' |
| 524 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
| 525 | msg = ('Changelist description needs BUG field (after first line):\n' |
| 526 | 'Bug: 9999 (for buganizer)\n' |
| 527 | 'BUG=None') |
| 528 | return HookFailure(msg) |
| 529 | else: |
| 530 | BUG_RE = r'\nBUG=([Nn]one|(chrome-os-partner|chromium|brillo|b):\d+)' |
| 531 | if not re.search(BUG_RE, _get_commit_desc(commit)): |
| 532 | msg = ('Changelist description needs BUG field (after first line):\n' |
| 533 | 'BUG=brillo:9999 (for Brillo tracker)\n' |
| 534 | 'BUG=chromium:9999 (for public tracker)\n' |
| 535 | 'BUG=chrome-os-partner:9999 (for partner tracker)\n' |
| 536 | 'BUG=b:9999 (for buganizer)\n' |
| 537 | 'BUG=None') |
| 538 | return HookFailure(msg) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 539 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 540 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 541 | def _check_for_uprev(project, commit, project_top=None): |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 542 | """Check that we're not missing a revbump of an ebuild in the given commit. |
| 543 | |
| 544 | If the given commit touches files in a directory that has ebuilds somewhere |
| 545 | up the directory hierarchy, it's very likely that we need an ebuild revbump |
| 546 | in order for those changes to take effect. |
| 547 | |
| 548 | It's not totally trivial to detect a revbump, so at least detect that an |
| 549 | ebuild with a revision number in it was touched. This should handle the |
| 550 | common case where we use a symlink to do the revbump. |
| 551 | |
| 552 | TODO: it would be nice to enhance this hook to: |
| 553 | * Handle cases where people revbump with a slightly different syntax. I see |
| 554 | one ebuild (puppy) that revbumps with _pN. This is a false positive. |
| 555 | * Catches cases where people aren't using symlinks for revbumps. If they |
| 556 | edit a revisioned file directly (and are expected to rename it for revbump) |
| 557 | we'll miss that. Perhaps we could detect that the file touched is a |
| 558 | symlink? |
| 559 | |
| 560 | If a project doesn't use symlinks we'll potentially miss a revbump, but we're |
| 561 | still better off than without this check. |
| 562 | |
| 563 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 564 | project: The Project to look at |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 565 | commit: The commit to look at |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 566 | project_top: Top dir to process commits in |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 567 | |
| 568 | Returns: |
| 569 | A HookFailure or None. |
| 570 | """ |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 571 | # If this is the portage-stable overlay, then ignore the check. It's rare |
| 572 | # that we're doing anything other than importing files from upstream, so |
| 573 | # forcing a rev bump makes no sense. |
| 574 | whitelist = ( |
| 575 | 'chromiumos/overlays/portage-stable', |
| 576 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 577 | if project.name in whitelist: |
Mike Frysinger | 011af94 | 2014-01-17 16:12:22 -0500 | [diff] [blame] | 578 | return None |
| 579 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 580 | def FinalName(obj): |
| 581 | # If the file is being deleted, then the dst_file is not set. |
| 582 | if obj.dst_file is None: |
| 583 | return obj.src_file |
| 584 | else: |
| 585 | return obj.dst_file |
| 586 | |
| 587 | affected_path_objs = _get_affected_files( |
| 588 | commit, include_deletes=True, include_symlinks=True, relative=True, |
| 589 | full_details=True) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 590 | |
| 591 | # Don't yell about changes to whitelisted files... |
Aviv Keshet | 272f2e5 | 2016-04-25 14:49:44 -0700 | [diff] [blame] | 592 | whitelist = ('ChangeLog', 'Manifest', 'metadata.xml', 'COMMIT-QUEUE.ini') |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 593 | affected_path_objs = [x for x in affected_path_objs |
| 594 | if os.path.basename(FinalName(x)) not in whitelist] |
| 595 | if not affected_path_objs: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 596 | return None |
| 597 | |
| 598 | # If we've touched any file named with a -rN.ebuild then we'll say we're |
| 599 | # OK right away. See TODO above about enhancing this. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 600 | touched_revved_ebuild = any(re.search(r'-r\d*\.ebuild$', FinalName(x)) |
| 601 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 602 | if touched_revved_ebuild: |
| 603 | return None |
| 604 | |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 605 | # If we're creating new ebuilds from scratch, then we don't need an uprev. |
| 606 | # Find all the dirs that new ebuilds and ignore their files/. |
| 607 | ebuild_dirs = [os.path.dirname(FinalName(x)) + '/' for x in affected_path_objs |
| 608 | if FinalName(x).endswith('.ebuild') and x.status == 'A'] |
| 609 | affected_path_objs = [obj for obj in affected_path_objs |
| 610 | if not any(FinalName(obj).startswith(x) |
| 611 | for x in ebuild_dirs)] |
| 612 | if not affected_path_objs: |
| 613 | return |
| 614 | |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 615 | # We want to examine the current contents of all directories that are parents |
| 616 | # of files that were touched (up to the top of the project). |
| 617 | # |
| 618 | # ...note: we use the current directory contents even though it may have |
| 619 | # changed since the commit we're looking at. This is just a heuristic after |
| 620 | # all. Worst case we don't flag a missing revbump. |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 621 | if project_top is None: |
| 622 | project_top = os.getcwd() |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 623 | dirs_to_check = set([project_top]) |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 624 | for obj in affected_path_objs: |
| 625 | path = os.path.join(project_top, os.path.dirname(FinalName(obj))) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 626 | while os.path.exists(path) and not os.path.samefile(path, project_top): |
| 627 | dirs_to_check.add(path) |
| 628 | path = os.path.dirname(path) |
| 629 | |
| 630 | # Look through each directory. If it's got an ebuild in it then we'll |
| 631 | # consider this as a case when we need a revbump. |
Gwendal Grignou | a3086c3 | 2014-12-09 11:17:22 -0800 | [diff] [blame] | 632 | affected_paths = set(os.path.join(project_top, FinalName(x)) |
| 633 | for x in affected_path_objs) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 634 | for dir_path in dirs_to_check: |
| 635 | contents = os.listdir(dir_path) |
| 636 | ebuilds = [os.path.join(dir_path, path) |
| 637 | for path in contents if path.endswith('.ebuild')] |
| 638 | ebuilds_9999 = [path for path in ebuilds if path.endswith('-9999.ebuild')] |
| 639 | |
| 640 | # If the -9999.ebuild file was touched the bot will uprev for us. |
| 641 | # ...we'll use a simple intersection here as a heuristic... |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 642 | if set(ebuilds_9999) & affected_paths: |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 643 | continue |
| 644 | |
| 645 | if ebuilds: |
Mike Frysinger | 292b45d | 2014-11-25 01:17:10 -0500 | [diff] [blame] | 646 | return HookFailure('Changelist probably needs a revbump of an ebuild, ' |
| 647 | 'or a -r1.ebuild symlink if this is a new ebuild:\n' |
| 648 | '%s' % dir_path) |
Doug Anderson | 42b8a05 | 2013-06-26 10:45:36 -0700 | [diff] [blame] | 649 | |
| 650 | return None |
| 651 | |
| 652 | |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 653 | def _check_ebuild_eapi(project, commit): |
| 654 | """Make sure we have people use EAPI=4 or newer with custom ebuilds. |
| 655 | |
| 656 | We want to get away from older EAPI's as it makes life confusing and they |
| 657 | have less builtin error checking. |
| 658 | |
| 659 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 660 | project: The Project to look at |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 661 | commit: The commit to look at |
| 662 | |
| 663 | Returns: |
| 664 | A HookFailure or None. |
| 665 | """ |
| 666 | # If this is the portage-stable overlay, then ignore the check. It's rare |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 667 | # that we're doing anything other than importing files from upstream, and |
| 668 | # we shouldn't be rewriting things fundamentally anyways. |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 669 | whitelist = ( |
| 670 | 'chromiumos/overlays/portage-stable', |
| 671 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 672 | if project.name in whitelist: |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 673 | return None |
| 674 | |
| 675 | BAD_EAPIS = ('0', '1', '2', '3') |
| 676 | |
| 677 | get_eapi = re.compile(r'^\s*EAPI=[\'"]?([^\'"]+)') |
| 678 | |
| 679 | ebuilds_re = [r'\.ebuild$'] |
| 680 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 681 | ebuilds_re) |
| 682 | bad_ebuilds = [] |
| 683 | |
| 684 | for ebuild in ebuilds: |
| 685 | # If the ebuild does not specify an EAPI, it defaults to 0. |
| 686 | eapi = '0' |
| 687 | |
| 688 | lines = _get_file_content(ebuild, commit).splitlines() |
| 689 | if len(lines) == 1: |
| 690 | # This is most likely a symlink, so skip it entirely. |
| 691 | continue |
| 692 | |
| 693 | for line in lines: |
| 694 | m = get_eapi.match(line) |
| 695 | if m: |
| 696 | # Once we hit the first EAPI line in this ebuild, stop processing. |
| 697 | # The spec requires that there only be one and it be first, so |
| 698 | # checking all possible values is pointless. We also assume that |
| 699 | # it's "the" EAPI line and not something in the middle of a heredoc. |
| 700 | eapi = m.group(1) |
| 701 | break |
| 702 | |
| 703 | if eapi in BAD_EAPIS: |
| 704 | bad_ebuilds.append((ebuild, eapi)) |
| 705 | |
| 706 | if bad_ebuilds: |
| 707 | # pylint: disable=C0301 |
| 708 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/upgrade-ebuild-eapis' |
| 709 | # pylint: enable=C0301 |
| 710 | return HookFailure( |
Mike Frysinger | cd6adfc | 2014-02-06 01:03:56 -0500 | [diff] [blame] | 711 | 'These ebuilds are using old EAPIs. If these are imported from\n' |
| 712 | 'Gentoo, then you may ignore and upload once with the --no-verify\n' |
| 713 | 'flag. Otherwise, please update to 4 or newer.\n' |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 714 | '\t%s\n' |
| 715 | 'See this guide for more details:\n%s\n' % |
| 716 | ('\n\t'.join(['%s: EAPI=%s' % x for x in bad_ebuilds]), url)) |
| 717 | |
| 718 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 719 | def _check_ebuild_keywords(_project, commit): |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 720 | """Make sure we use the new style KEYWORDS when possible in ebuilds. |
| 721 | |
| 722 | If an ebuild generally does not care about the arch it is running on, then |
| 723 | ebuilds should flag it with one of: |
| 724 | KEYWORDS="*" # A stable ebuild. |
| 725 | KEYWORDS="~*" # An unstable ebuild. |
| 726 | KEYWORDS="-* ..." # Is known to only work on specific arches. |
| 727 | |
| 728 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 729 | project: The Project to look at |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 730 | commit: The commit to look at |
| 731 | |
| 732 | Returns: |
| 733 | A HookFailure or None. |
| 734 | """ |
| 735 | WHITELIST = set(('*', '-*', '~*')) |
| 736 | |
| 737 | get_keywords = re.compile(r'^\s*KEYWORDS="(.*)"') |
| 738 | |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 739 | ebuilds_re = [r'\.ebuild$'] |
| 740 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 741 | ebuilds_re) |
| 742 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 743 | bad_ebuilds = [] |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 744 | for ebuild in ebuilds: |
Mike Frysinger | 5c9e58d | 2014-09-09 03:32:50 -0400 | [diff] [blame] | 745 | # We get the full content rather than a diff as the latter does not work |
| 746 | # on new files (like when adding new ebuilds). |
| 747 | lines = _get_file_content(ebuild, commit).splitlines() |
| 748 | for line in lines: |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 749 | m = get_keywords.match(line) |
| 750 | if m: |
| 751 | keywords = set(m.group(1).split()) |
| 752 | if not keywords or WHITELIST - keywords != WHITELIST: |
| 753 | continue |
| 754 | |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 755 | bad_ebuilds.append(ebuild) |
| 756 | |
| 757 | if bad_ebuilds: |
| 758 | return HookFailure( |
| 759 | '%s\n' |
| 760 | 'Please update KEYWORDS to use a glob:\n' |
| 761 | 'If the ebuild should be marked stable (normal for non-9999 ebuilds):\n' |
| 762 | ' KEYWORDS="*"\n' |
| 763 | 'If the ebuild should be marked unstable (normal for ' |
| 764 | 'cros-workon / 9999 ebuilds):\n' |
| 765 | ' KEYWORDS="~*"\n' |
Mike Frysinger | de8efea | 2015-05-17 03:42:26 -0400 | [diff] [blame] | 766 | 'If the ebuild needs to be marked for only specific arches, ' |
Mike Frysinger | 8d42d74 | 2014-09-22 15:50:21 -0400 | [diff] [blame] | 767 | 'then use -* like so:\n' |
| 768 | ' KEYWORDS="-* arm ..."\n' % '\n* '.join(bad_ebuilds)) |
Mike Frysinger | c51ece7 | 2014-01-17 16:23:40 -0500 | [diff] [blame] | 769 | |
| 770 | |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 771 | def _check_ebuild_licenses(_project, commit): |
| 772 | """Check if the LICENSE field in the ebuild is correct.""" |
Brian Norris | 7a610e8 | 2016-02-17 12:24:54 -0800 | [diff] [blame] | 773 | affected_paths = _get_affected_files(commit, relative=True) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 774 | touched_ebuilds = [x for x in affected_paths if x.endswith('.ebuild')] |
| 775 | |
| 776 | # A list of licenses to ignore for now. |
Yu-Ju Hong | c0963fa | 2014-03-03 12:36:52 -0800 | [diff] [blame] | 777 | LICENSES_IGNORE = ['||', '(', ')'] |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 778 | |
| 779 | for ebuild in touched_ebuilds: |
| 780 | # Skip virutal packages. |
| 781 | if ebuild.split('/')[-3] == 'virtual': |
| 782 | continue |
| 783 | |
| 784 | try: |
Brian Norris | 7a610e8 | 2016-02-17 12:24:54 -0800 | [diff] [blame] | 785 | ebuild_content = _get_file_content(ebuild, commit) |
| 786 | license_types = licenses_lib.GetLicenseTypesFromEbuild(ebuild_content) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 787 | except ValueError as e: |
| 788 | return HookFailure(e.message, [ebuild]) |
| 789 | |
| 790 | # Also ignore licenses ending with '?' |
| 791 | for license_type in [x for x in license_types |
| 792 | if x not in LICENSES_IGNORE and not x.endswith('?')]: |
| 793 | try: |
Mike Frysinger | 2ec70ed | 2014-08-17 19:28:34 -0400 | [diff] [blame] | 794 | licenses_lib.Licensing.FindLicenseType(license_type) |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 795 | except AssertionError as e: |
| 796 | return HookFailure(e.message, [ebuild]) |
| 797 | |
| 798 | |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 799 | def _check_ebuild_virtual_pv(project, commit): |
| 800 | """Enforce the virtual PV policies.""" |
| 801 | # If this is the portage-stable overlay, then ignore the check. |
| 802 | # We want to import virtuals as-is from upstream Gentoo. |
| 803 | whitelist = ( |
| 804 | 'chromiumos/overlays/portage-stable', |
| 805 | ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 806 | if project.name in whitelist: |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 807 | return None |
| 808 | |
| 809 | # We assume the repo name is the same as the dir name on disk. |
| 810 | # It would be dumb to not have them match though. |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 811 | project_base = os.path.basename(project.name) |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 812 | |
| 813 | is_variant = lambda x: x.startswith('overlay-variant-') |
| 814 | is_board = lambda x: x.startswith('overlay-') |
| 815 | is_private = lambda x: x.endswith('-private') |
| 816 | |
| 817 | get_pv = re.compile(r'(.*?)virtual/([^/]+)/\2-([^/]*)\.ebuild$') |
| 818 | |
| 819 | ebuilds_re = [r'\.ebuild$'] |
| 820 | ebuilds = _filter_files(_get_affected_files(commit, relative=True), |
| 821 | ebuilds_re) |
| 822 | bad_ebuilds = [] |
| 823 | |
| 824 | for ebuild in ebuilds: |
| 825 | m = get_pv.match(ebuild) |
| 826 | if m: |
| 827 | overlay = m.group(1) |
| 828 | if not overlay or not is_board(overlay): |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 829 | overlay = project_base |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 830 | |
| 831 | pv = m.group(3).split('-', 1)[0] |
| 832 | |
Bernie Thompson | e5ee182 | 2016-01-12 14:22:23 -0800 | [diff] [blame] | 833 | # Virtual versions >= 4 are special cases used above the standard |
| 834 | # versioning structure, e.g. if one has a board inheriting a board. |
| 835 | if float(pv) >= 4: |
| 836 | want_pv = pv |
| 837 | elif is_private(overlay): |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 838 | want_pv = '3.5' if is_variant(overlay) else '3' |
| 839 | elif is_board(overlay): |
| 840 | want_pv = '2.5' if is_variant(overlay) else '2' |
| 841 | else: |
| 842 | want_pv = '1' |
| 843 | |
| 844 | if pv != want_pv: |
| 845 | bad_ebuilds.append((ebuild, pv, want_pv)) |
| 846 | |
| 847 | if bad_ebuilds: |
| 848 | # pylint: disable=C0301 |
| 849 | url = 'http://dev.chromium.org/chromium-os/how-tos-and-troubleshooting/portage-build-faq#TOC-Virtuals-and-central-management' |
| 850 | # pylint: enable=C0301 |
| 851 | return HookFailure( |
| 852 | 'These virtuals have incorrect package versions (PVs). Please adjust:\n' |
| 853 | '\t%s\n' |
| 854 | 'If this is an upstream Gentoo virtual, then you may ignore this\n' |
| 855 | 'check (and re-run w/--no-verify). Otherwise, please see this\n' |
| 856 | 'page for more details:\n%s\n' % |
| 857 | ('\n\t'.join(['%s:\n\t\tPV is %s but should be %s' % x |
| 858 | for x in bad_ebuilds]), url)) |
| 859 | |
| 860 | |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 861 | def _check_portage_make_use_var(_project, commit): |
| 862 | """Verify that $USE is set correctly in make.conf and make.defaults.""" |
| 863 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 864 | [r'(^|/)make.(conf|defaults)$']) |
| 865 | |
| 866 | errors = [] |
| 867 | for path in files: |
| 868 | basename = os.path.basename(path) |
| 869 | |
| 870 | # Has a USE= line already been encountered in this file? |
| 871 | saw_use = False |
| 872 | |
| 873 | for i, line in enumerate(_get_file_content(path, commit).splitlines(), 1): |
| 874 | if not line.startswith('USE='): |
| 875 | continue |
| 876 | |
| 877 | preserves_use = '${USE}' in line or '$USE' in line |
| 878 | |
| 879 | if (basename == 'make.conf' or |
| 880 | (basename == 'make.defaults' and saw_use)) and not preserves_use: |
| 881 | errors.append('%s:%d: missing ${USE}' % (path, i)) |
| 882 | elif basename == 'make.defaults' and not saw_use and preserves_use: |
| 883 | errors.append('%s:%d: ${USE} referenced in initial declaration' % |
| 884 | (path, i)) |
| 885 | |
| 886 | saw_use = True |
| 887 | |
| 888 | if errors: |
| 889 | return HookFailure( |
| 890 | 'One or more Portage make files appear to set USE incorrectly.\n' |
| 891 | '\n' |
| 892 | 'All USE assignments in make.conf and all assignments after the\n' |
| 893 | 'initial declaration in make.defaults should contain "${USE}" to\n' |
| 894 | 'preserve previously-set flags.\n' |
| 895 | '\n' |
| 896 | 'The initial USE declaration in make.defaults should not contain\n' |
| 897 | '"${USE}".\n', |
| 898 | errors) |
| 899 | |
| 900 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 901 | def _check_change_has_proper_changeid(_project, commit): |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 902 | """Verify that Change-ID is present in last paragraph of commit message.""" |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 903 | CHANGE_ID_RE = r'\nChange-Id: I[a-f0-9]+\n' |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 904 | desc = _get_commit_desc(commit) |
Mike Frysinger | 4a22bf0 | 2014-10-31 13:53:35 -0400 | [diff] [blame] | 905 | m = re.search(CHANGE_ID_RE, desc) |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 906 | if not m: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 907 | return HookFailure('Change-Id must be in last paragraph of description.') |
| 908 | |
Mike Frysinger | 02b88bd | 2014-11-21 00:29:38 -0500 | [diff] [blame] | 909 | # Allow s-o-b tags to follow it, but only those. |
| 910 | end = desc[m.end():].strip().splitlines() |
| 911 | if [x for x in end if not x.startswith('Signed-off-by: ')]: |
| 912 | return HookFailure('Only "Signed-off-by:" tags may follow the Change-Id.') |
| 913 | |
Mandeep Singh Baines | a23eb5f | 2011-05-04 13:43:25 -0700 | [diff] [blame] | 914 | |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 915 | def _check_commit_message_style(_project, commit): |
| 916 | """Verify that the commit message matches our style. |
| 917 | |
| 918 | We do not check for BUG=/TEST=/etc... lines here as that is handled by other |
| 919 | commit hooks. |
| 920 | """ |
| 921 | desc = _get_commit_desc(commit) |
| 922 | |
| 923 | # The first line should be by itself. |
| 924 | lines = desc.splitlines() |
| 925 | if len(lines) > 1 and lines[1]: |
| 926 | return HookFailure('The second line of the commit message must be blank.') |
| 927 | |
| 928 | # The first line should be one sentence. |
| 929 | if '. ' in lines[0]: |
| 930 | return HookFailure('The first line cannot be more than one sentence.') |
| 931 | |
| 932 | # The first line cannot be too long. |
| 933 | MAX_FIRST_LINE_LEN = 100 |
| 934 | if len(lines[0]) > MAX_FIRST_LINE_LEN: |
| 935 | return HookFailure('The first line must be less than %i chars.' % |
| 936 | MAX_FIRST_LINE_LEN) |
| 937 | |
| 938 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 939 | def _check_cros_license(_project, commit, options=()): |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 940 | """Verifies the Chromium OS license/copyright header. |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 941 | |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 942 | Should be following the spec: |
| 943 | http://dev.chromium.org/developers/coding-style#TOC-File-headers |
| 944 | """ |
| 945 | # For older years, be a bit more flexible as our policy says leave them be. |
| 946 | LICENSE_HEADER = ( |
| 947 | r'.* Copyright( \(c\))? 20[-0-9]{2,7} The Chromium OS Authors\. ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 948 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 949 | r'.* Use of this source code is governed by a BSD-style license that can ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 950 | r'be\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 951 | r'.* found in the LICENSE file\.' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 952 | r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 953 | ) |
| 954 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 955 | |
| 956 | # For newer years, be stricter. |
| 957 | COPYRIGHT_LINE = ( |
| 958 | r'.* Copyright \(c\) 20(1[5-9]|[2-9][0-9]) The Chromium OS Authors\. ' |
Mike Frysinger | b81102f | 2014-11-21 00:33:35 -0500 | [diff] [blame] | 959 | r'All rights reserved\.' r'\n' |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 960 | ) |
| 961 | copyright_re = re.compile(COPYRIGHT_LINE) |
| 962 | |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 963 | parser = argparse.ArgumentParser() |
| 964 | parser.add_argument('--exclude_regex', action='append') |
| 965 | parser.add_argument('--include_regex', action='append') |
| 966 | opts = parser.parse_args(options) |
| 967 | included = opts.include_regex or [] |
| 968 | excluded = opts.exclude_regex or [] |
| 969 | |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 970 | bad_files = [] |
| 971 | bad_copyright_files = [] |
| 972 | files = _filter_files(_get_affected_files(commit, relative=True), |
Filipe Brandenburger | 4b542b1 | 2015-10-09 12:46:31 -0700 | [diff] [blame] | 973 | included + COMMON_INCLUDED_PATHS, |
| 974 | excluded + COMMON_EXCLUDED_PATHS) |
Mike Frysinger | 9863810 | 2014-08-28 00:15:08 -0400 | [diff] [blame] | 975 | |
| 976 | for f in files: |
| 977 | contents = _get_file_content(f, commit) |
| 978 | if not contents: |
| 979 | # Ignore empty files. |
| 980 | continue |
| 981 | |
| 982 | if not license_re.search(contents): |
| 983 | bad_files.append(f) |
| 984 | elif copyright_re.search(contents): |
| 985 | bad_copyright_files.append(f) |
| 986 | |
| 987 | if bad_files: |
| 988 | msg = '%s:\n%s\n%s' % ( |
| 989 | 'License must match', license_re.pattern, |
| 990 | 'Found a bad header in these files:') |
| 991 | return HookFailure(msg, bad_files) |
| 992 | |
| 993 | if bad_copyright_files: |
| 994 | msg = 'Do not use (c) in copyright headers in new files:' |
| 995 | return HookFailure(msg, bad_copyright_files) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 996 | |
| 997 | |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 998 | def _check_aosp_license(_project, commit): |
| 999 | """Verifies the AOSP license/copyright header. |
| 1000 | |
| 1001 | AOSP uses the Apache2 License: |
| 1002 | https://source.android.com/source/licenses.html |
| 1003 | """ |
| 1004 | LICENSE_HEADER = ( |
| 1005 | r"""^[#/\*]* |
| 1006 | [#/\*]* ?Copyright( \([cC]\))? 20[-0-9]{2,7} The Android Open Source Project |
| 1007 | [#/\*]* ? |
| 1008 | [#/\*]* ?Licensed under the Apache License, Version 2.0 \(the "License"\); |
| 1009 | [#/\*]* ?you may not use this file except in compliance with the License\. |
| 1010 | [#/\*]* ?You may obtain a copy of the License at |
| 1011 | [#/\*]* ? |
| 1012 | [#/\*]* ? http://www\.apache\.org/licenses/LICENSE-2\.0 |
| 1013 | [#/\*]* ? |
| 1014 | [#/\*]* ?Unless required by applicable law or agreed to in writing, software |
| 1015 | [#/\*]* ?distributed under the License is distributed on an "AS IS" BASIS, |
| 1016 | [#/\*]* ?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or """ |
| 1017 | r"""implied\. |
| 1018 | [#/\*]* ?See the License for the specific language governing permissions and |
| 1019 | [#/\*]* ?limitations under the License\. |
| 1020 | [#/\*]*$ |
| 1021 | """ |
| 1022 | ) |
| 1023 | license_re = re.compile(LICENSE_HEADER, re.MULTILINE) |
| 1024 | |
| 1025 | files = _filter_files(_get_affected_files(commit, relative=True), |
| 1026 | COMMON_INCLUDED_PATHS, |
| 1027 | COMMON_EXCLUDED_PATHS) |
| 1028 | |
| 1029 | bad_files = [] |
| 1030 | for f in files: |
| 1031 | contents = _get_file_content(f, commit) |
| 1032 | if not contents: |
| 1033 | # Ignore empty files. |
| 1034 | continue |
| 1035 | |
| 1036 | if not license_re.search(contents): |
| 1037 | bad_files.append(f) |
| 1038 | |
| 1039 | if bad_files: |
| 1040 | msg = ('License must match:\n%s\nFound a bad header in these files:' % |
| 1041 | license_re.pattern) |
| 1042 | return HookFailure(msg, bad_files) |
| 1043 | |
| 1044 | |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1045 | def _check_layout_conf(_project, commit): |
| 1046 | """Verifies the metadata/layout.conf file.""" |
| 1047 | repo_name = 'profiles/repo_name' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1048 | repo_names = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1049 | layout_path = 'metadata/layout.conf' |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1050 | layout_paths = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1051 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1052 | # Handle multiple overlays in a single commit (like the public tree). |
| 1053 | for f in _get_affected_files(commit, relative=True): |
| 1054 | if f.endswith(repo_name): |
| 1055 | repo_names.append(f) |
| 1056 | elif f.endswith(layout_path): |
| 1057 | layout_paths.append(f) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1058 | |
| 1059 | # Disallow new repos with the repo_name file. |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1060 | if repo_names: |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1061 | return HookFailure('%s: use "repo-name" in %s instead' % |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1062 | (repo_names, layout_path)) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1063 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1064 | # Gather all the errors in one pass so we show one full message. |
| 1065 | all_errors = {} |
| 1066 | for layout_path in layout_paths: |
| 1067 | all_errors[layout_path] = errors = [] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1068 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1069 | # Make sure the config file is sorted. |
| 1070 | data = [x for x in _get_file_content(layout_path, commit).splitlines() |
| 1071 | if x and x[0] != '#'] |
| 1072 | if sorted(data) != data: |
| 1073 | errors += ['keep lines sorted'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1074 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1075 | # Require people to set specific values all the time. |
| 1076 | settings = ( |
| 1077 | # TODO: Enable this for everyone. http://crbug.com/408038 |
| 1078 | #('fast caching', 'cache-format = md5-dict'), |
| 1079 | ('fast manifests', 'thin-manifests = true'), |
Mike Frysinger | d773452 | 2015-02-26 16:12:43 -0500 | [diff] [blame] | 1080 | ('extra features', 'profile-formats = portage-2 profile-default-eapi'), |
| 1081 | ('newer eapi', 'profile_eapi_when_unspecified = 5-progress'), |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1082 | ) |
| 1083 | for reason, line in settings: |
| 1084 | if line not in data: |
| 1085 | errors += ['enable %s with: %s' % (reason, line)] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1086 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1087 | # Require one of these settings. |
Mike Frysinger | 5fae62d | 2015-11-11 20:12:15 -0500 | [diff] [blame] | 1088 | if 'use-manifests = strict' not in data: |
| 1089 | errors += ['enable file checking with: use-manifests = strict'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1090 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1091 | # Require repo-name to be set. |
Mike Frysinger | 324cf68 | 2014-09-22 15:52:50 -0400 | [diff] [blame] | 1092 | for line in data: |
| 1093 | if line.startswith('repo-name = '): |
| 1094 | break |
| 1095 | else: |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1096 | errors += ['set the board name with: repo-name = $BOARD'] |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1097 | |
Mike Frysinger | 94a670c | 2014-09-19 12:46:26 -0400 | [diff] [blame] | 1098 | # Summarize all the errors we saw (if any). |
| 1099 | lines = '' |
| 1100 | for layout_path, errors in all_errors.items(): |
| 1101 | if errors: |
| 1102 | lines += '\n\t- '.join(['\n* %s:' % layout_path] + errors) |
| 1103 | if lines: |
| 1104 | lines = 'See the portage(5) man page for layout.conf details' + lines + '\n' |
| 1105 | return HookFailure(lines) |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1106 | |
| 1107 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1108 | # Project-specific hooks |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1109 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1110 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1111 | def _run_checkpatch(_project, commit, options=()): |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1112 | """Runs checkpatch.pl on the given project""" |
| 1113 | hooks_dir = _get_hooks_dir() |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1114 | options = list(options) |
| 1115 | if commit == PRE_SUBMIT: |
| 1116 | # The --ignore option must be present and include 'MISSING_SIGN_OFF' in |
| 1117 | # this case. |
| 1118 | options.append('--ignore=MISSING_SIGN_OFF') |
Filipe Brandenburger | 28d48d6 | 2015-10-07 09:48:54 -0700 | [diff] [blame] | 1119 | # Always ignore the check for the MAINTAINERS file. We do not track that |
| 1120 | # information on that file in our source trees, so let's suppress the |
| 1121 | # warning. |
| 1122 | options.append('--ignore=FILE_PATH_CHANGES') |
Filipe Brandenburger | ce97832 | 2015-10-09 10:04:15 -0700 | [diff] [blame] | 1123 | # Do not complain about the Change-Id: fields, since we use Gerrit. |
| 1124 | # Upstream does not want those lines (since they do not use Gerrit), but |
| 1125 | # we always do, so disable the check globally. |
Daisuke Nojiri | 4844f89 | 2015-10-08 09:54:33 -0700 | [diff] [blame] | 1126 | options.append('--ignore=GERRIT_CHANGE_ID') |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1127 | cmd = ['%s/checkpatch.pl' % hooks_dir] + options + ['-'] |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1128 | cmd_result = cros_build_lib.RunCommand(cmd=cmd, |
| 1129 | print_cmd=False, |
| 1130 | input=_get_patch(commit), |
| 1131 | stdout_to_pipe=True, |
| 1132 | combine_stdout_stderr=True, |
| 1133 | error_code_ok=True) |
| 1134 | if cmd_result.returncode: |
| 1135 | return HookFailure('checkpatch.pl errors/warnings\n\n' + cmd_result.output) |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1136 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1137 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1138 | def _kernel_configcheck(_project, commit): |
Olof Johansson | a96810f | 2012-09-04 16:20:03 -0700 | [diff] [blame] | 1139 | """Makes sure kernel config changes are not mixed with code changes""" |
| 1140 | files = _get_affected_files(commit) |
| 1141 | if not len(_filter_files(files, [r'chromeos/config'])) in [0, len(files)]: |
| 1142 | return HookFailure('Changes to chromeos/config/ and regular files must ' |
| 1143 | 'be in separate commits:\n%s' % '\n'.join(files)) |
Anton Staaf | 815d685 | 2011-08-22 10:08:45 -0700 | [diff] [blame] | 1144 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1145 | |
| 1146 | def _run_json_check(_project, commit): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1147 | """Checks that all JSON files are syntactically valid.""" |
Dale Curtis | a039cfd | 2011-05-04 12:01:05 -0700 | [diff] [blame] | 1148 | for f in _filter_files(_get_affected_files(commit), [r'.*\.json']): |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1149 | try: |
| 1150 | json.load(open(f)) |
| 1151 | except Exception, e: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1152 | return HookFailure('Invalid JSON in %s: %s' % (f, e)) |
Dale Curtis | 2975c43 | 2011-05-03 17:25:20 -0700 | [diff] [blame] | 1153 | |
| 1154 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1155 | def _check_manifests(_project, commit): |
Mike Frysinger | 52b537e | 2013-08-22 22:59:53 -0400 | [diff] [blame] | 1156 | """Make sure Manifest files only have DIST lines""" |
| 1157 | paths = [] |
| 1158 | |
| 1159 | for path in _get_affected_files(commit): |
| 1160 | if os.path.basename(path) != 'Manifest': |
| 1161 | continue |
| 1162 | if not os.path.exists(path): |
| 1163 | continue |
| 1164 | |
| 1165 | with open(path, 'r') as f: |
| 1166 | for line in f.readlines(): |
| 1167 | if not line.startswith('DIST '): |
| 1168 | paths.append(path) |
| 1169 | break |
| 1170 | |
| 1171 | if paths: |
| 1172 | return HookFailure('Please remove lines that do not start with DIST:\n%s' % |
| 1173 | ('\n'.join(paths),)) |
| 1174 | |
| 1175 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1176 | def _check_change_has_branch_field(_project, commit): |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1177 | """Check for a non-empty 'BRANCH=' field in the commit message.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1178 | if commit == PRE_SUBMIT: |
| 1179 | return |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1180 | BRANCH_RE = r'\nBRANCH=\S+' |
| 1181 | |
| 1182 | if not re.search(BRANCH_RE, _get_commit_desc(commit)): |
| 1183 | msg = ('Changelist description needs BRANCH field (after first line)\n' |
| 1184 | 'E.g. BRANCH=none or BRANCH=link,snow') |
| 1185 | return HookFailure(msg) |
| 1186 | |
| 1187 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1188 | def _check_change_has_signoff_field(_project, commit): |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1189 | """Check for a non-empty 'Signed-off-by:' field in the commit message.""" |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1190 | if commit == PRE_SUBMIT: |
| 1191 | return |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1192 | SIGNOFF_RE = r'\nSigned-off-by: \S+' |
| 1193 | |
| 1194 | if not re.search(SIGNOFF_RE, _get_commit_desc(commit)): |
| 1195 | msg = ('Changelist description needs Signed-off-by: field\n' |
| 1196 | 'E.g. Signed-off-by: My Name <me@chromium.org>') |
| 1197 | return HookFailure(msg) |
| 1198 | |
| 1199 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1200 | def _run_project_hook_script(script, project, commit): |
| 1201 | """Runs a project hook script. |
| 1202 | |
| 1203 | The script is run with the following environment variables set: |
| 1204 | PRESUBMIT_PROJECT: The affected project |
| 1205 | PRESUBMIT_COMMIT: The affected commit |
| 1206 | PRESUBMIT_FILES: A newline-separated list of affected files |
| 1207 | |
| 1208 | The script is considered to fail if the exit code is non-zero. It should |
| 1209 | write an error message to stdout. |
| 1210 | """ |
| 1211 | env = dict(os.environ) |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1212 | env['PRESUBMIT_PROJECT'] = project.name |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1213 | env['PRESUBMIT_COMMIT'] = commit |
| 1214 | |
| 1215 | # Put affected files in an environment variable |
| 1216 | files = _get_affected_files(commit) |
| 1217 | env['PRESUBMIT_FILES'] = '\n'.join(files) |
| 1218 | |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1219 | cmd_result = cros_build_lib.RunCommand(cmd=script, |
| 1220 | env=env, |
| 1221 | shell=True, |
| 1222 | print_cmd=False, |
| 1223 | input=os.devnull, |
| 1224 | stdout_to_pipe=True, |
| 1225 | combine_stdout_stderr=True, |
| 1226 | error_code_ok=True) |
| 1227 | if cmd_result.returncode: |
| 1228 | stdout = cmd_result.output |
Jon Salz | 7b618af | 2012-08-31 06:03:16 +0800 | [diff] [blame] | 1229 | if stdout: |
| 1230 | stdout = re.sub('(?m)^', ' ', stdout) |
| 1231 | return HookFailure('Hook script "%s" failed with code %d%s' % |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1232 | (script, cmd_result.returncode, |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1233 | ':\n' + stdout if stdout else '')) |
| 1234 | |
| 1235 | |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1236 | def _check_project_prefix(_project, commit): |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1237 | """Require the commit message have a project specific prefix as needed.""" |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1238 | |
| 1239 | files = _get_affected_files(commit, relative=True) |
| 1240 | prefix = os.path.commonprefix(files) |
| 1241 | prefix = os.path.dirname(prefix) |
| 1242 | |
| 1243 | # If there is no common prefix, the CL span multiple projects. |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1244 | if not prefix: |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1245 | return |
| 1246 | |
| 1247 | project_name = prefix.split('/')[0] |
Daniel Erat | a350fd3 | 2014-09-29 14:02:34 -0700 | [diff] [blame] | 1248 | |
| 1249 | # The common files may all be within a subdirectory of the main project |
| 1250 | # directory, so walk up the tree until we find an alias file. |
| 1251 | # _get_affected_files() should return relative paths, but check against '/' to |
| 1252 | # ensure that this loop terminates even if it receives an absolute path. |
| 1253 | while prefix and prefix != '/': |
| 1254 | alias_file = os.path.join(prefix, '.project_alias') |
| 1255 | |
| 1256 | # If an alias exists, use it. |
| 1257 | if os.path.isfile(alias_file): |
| 1258 | project_name = osutils.ReadFile(alias_file).strip() |
| 1259 | |
| 1260 | prefix = os.path.dirname(prefix) |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1261 | |
| 1262 | if not _get_commit_desc(commit).startswith(project_name + ': '): |
| 1263 | return HookFailure('The commit title for changes affecting only %s' |
| 1264 | ' should start with \"%s: \"' |
| 1265 | % (project_name, project_name)) |
| 1266 | |
| 1267 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1268 | # Base |
| 1269 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1270 | # A list of hooks which are not project specific and check patch description |
| 1271 | # (as opposed to patch body). |
| 1272 | _PATCH_DESCRIPTION_HOOKS = [ |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1273 | _check_change_has_bug_field, |
David James | c3b68b3 | 2013-04-03 09:17:03 -0700 | [diff] [blame] | 1274 | _check_change_has_valid_cq_depend, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1275 | _check_change_has_test_field, |
| 1276 | _check_change_has_proper_changeid, |
Mike Frysinger | 36b2ebc | 2014-10-31 14:02:03 -0400 | [diff] [blame] | 1277 | _check_commit_message_style, |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 1278 | _check_change_is_contribution, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1279 | ] |
| 1280 | |
| 1281 | |
| 1282 | # A list of hooks that are not project-specific |
| 1283 | _COMMON_HOOKS = [ |
Mike Frysinger | bf8b91c | 2014-02-01 02:50:27 -0500 | [diff] [blame] | 1284 | _check_ebuild_eapi, |
Mike Frysinger | 89bdb85 | 2014-02-01 05:26:26 -0500 | [diff] [blame] | 1285 | _check_ebuild_keywords, |
Yu-Ju Hong | 5e0efa7 | 2013-11-19 16:28:10 -0800 | [diff] [blame] | 1286 | _check_ebuild_licenses, |
Mike Frysinger | cd363c8 | 2014-02-01 05:20:18 -0500 | [diff] [blame] | 1287 | _check_ebuild_virtual_pv, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1288 | _check_for_uprev, |
Rahul Chaudhry | 09f6137 | 2015-07-31 17:14:26 -0700 | [diff] [blame] | 1289 | _check_gofmt, |
Mike Frysinger | 998c2cc | 2014-08-27 05:20:23 -0400 | [diff] [blame] | 1290 | _check_layout_conf, |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 1291 | _check_cros_license, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1292 | _check_no_long_lines, |
| 1293 | _check_no_stray_whitespace, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1294 | _check_no_tabs, |
Daniel Erat | 9d203ff | 2015-02-17 10:12:21 -0700 | [diff] [blame] | 1295 | _check_portage_make_use_var, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1296 | ] |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1297 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1298 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1299 | # A dictionary of project-specific hooks(callbacks), indexed by project name. |
| 1300 | # dict[project] = [callback1, callback2] |
| 1301 | _PROJECT_SPECIFIC_HOOKS = { |
Bertrand SIMONNET | 0022fff | 2014-07-07 09:52:15 -0700 | [diff] [blame] | 1302 | "chromiumos/platform2": [_check_project_prefix], |
Mike Frysinger | af89129 | 2015-03-25 19:46:53 -0400 | [diff] [blame] | 1303 | "chromiumos/third_party/kernel": [_kernel_configcheck], |
| 1304 | "chromiumos/third_party/kernel-next": [_kernel_configcheck], |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1305 | } |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1306 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1307 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1308 | # A dictionary of flags (keys) that can appear in the config file, and the hook |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1309 | # that the flag controls (value). |
| 1310 | _HOOK_FLAGS = { |
Mike Frysinger | a7642f5 | 2015-03-25 18:31:42 -0400 | [diff] [blame] | 1311 | 'checkpatch_check': _run_checkpatch, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1312 | 'stray_whitespace_check': _check_no_stray_whitespace, |
Mike Frysinger | ff6c7d6 | 2015-03-24 13:49:46 -0400 | [diff] [blame] | 1313 | 'json_check': _run_json_check, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1314 | 'long_line_check': _check_no_long_lines, |
Alex Deymo | f5792ce | 2015-08-24 22:50:08 -0700 | [diff] [blame] | 1315 | 'cros_license_check': _check_cros_license, |
| 1316 | 'aosp_license_check': _check_aosp_license, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1317 | 'tab_check': _check_no_tabs, |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1318 | 'branch_check': _check_change_has_branch_field, |
Shawn Nematbakhsh | 51e16ac | 2014-01-28 15:31:07 -0800 | [diff] [blame] | 1319 | 'signoff_check': _check_change_has_signoff_field, |
Josh Triplett | 0e8fc7f | 2014-04-23 16:00:00 -0700 | [diff] [blame] | 1320 | 'bug_field_check': _check_change_has_bug_field, |
| 1321 | 'test_field_check': _check_change_has_test_field, |
Steve Fung | 49ec7e9 | 2015-03-23 16:07:12 -0700 | [diff] [blame] | 1322 | 'manifest_check': _check_manifests, |
Bernie Thompson | f8fea99 | 2016-01-14 10:27:18 -0800 | [diff] [blame] | 1323 | 'contribution_check': _check_change_is_contribution, |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1327 | def _get_override_hooks(config): |
| 1328 | """Returns a set of hooks controlled by the current project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1329 | |
| 1330 | Expects to be called within the project root. |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1331 | |
| 1332 | Args: |
| 1333 | config: A ConfigParser for the project's config file. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1334 | """ |
| 1335 | SECTION = 'Hook Overrides' |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1336 | SECTION_OPTIONS = 'Hook Overrides Options' |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1337 | if not config.has_section(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1338 | return set(), set() |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1339 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1340 | valid_keys = set(_HOOK_FLAGS.iterkeys()) |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1341 | hooks = _HOOK_FLAGS.copy() |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1342 | |
| 1343 | enable_flags = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1344 | disable_flags = [] |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1345 | for flag in config.options(SECTION): |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1346 | if flag not in valid_keys: |
| 1347 | raise ValueError('Error: unknown key "%s" in hook section of "%s"' % |
| 1348 | (flag, _CONFIG_FILE)) |
| 1349 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1350 | try: |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1351 | enabled = config.getboolean(SECTION, flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1352 | except ValueError as e: |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1353 | raise ValueError('Error: parsing flag "%s" in "%s" failed: %s' % |
| 1354 | (flag, _CONFIG_FILE, e)) |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1355 | if enabled: |
| 1356 | enable_flags.append(flag) |
| 1357 | else: |
| 1358 | disable_flags.append(flag) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1359 | |
Mike Frysinger | f8ce171 | 2015-03-25 18:32:33 -0400 | [diff] [blame] | 1360 | # See if this hook has custom options. |
| 1361 | if enabled: |
| 1362 | try: |
| 1363 | options = config.get(SECTION_OPTIONS, flag) |
| 1364 | hooks[flag] = functools.partial(hooks[flag], options=options.split()) |
| 1365 | except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): |
| 1366 | pass |
| 1367 | |
| 1368 | enabled_hooks = set(hooks[x] for x in enable_flags) |
| 1369 | disabled_hooks = set(hooks[x] for x in disable_flags) |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1370 | return enabled_hooks, disabled_hooks |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1371 | |
| 1372 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1373 | def _get_project_hook_scripts(config): |
| 1374 | """Returns a list of project-specific hook scripts. |
| 1375 | |
| 1376 | Args: |
| 1377 | config: A ConfigParser for the project's config file. |
| 1378 | """ |
| 1379 | SECTION = 'Hook Scripts' |
| 1380 | if not config.has_section(SECTION): |
| 1381 | return [] |
| 1382 | |
| 1383 | hook_names_values = config.items(SECTION) |
| 1384 | hook_names_values.sort(key=lambda x: x[0]) |
| 1385 | return [x[1] for x in hook_names_values] |
| 1386 | |
| 1387 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1388 | def _get_project_hooks(project, presubmit): |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1389 | """Returns a list of hooks that need to be run for a project. |
| 1390 | |
| 1391 | Expects to be called from within the project root. |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1392 | |
| 1393 | Args: |
| 1394 | project: A string, name of the project. |
| 1395 | presubmit: A Boolean, True if the check is run as a git pre-submit script. |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1396 | """ |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1397 | config = ConfigParser.RawConfigParser() |
| 1398 | try: |
| 1399 | config.read(_CONFIG_FILE) |
| 1400 | except ConfigParser.Error: |
| 1401 | # Just use an empty config file |
| 1402 | config = ConfigParser.RawConfigParser() |
| 1403 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1404 | if presubmit: |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1405 | hooks = _COMMON_HOOKS |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1406 | else: |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1407 | hooks = _PATCH_DESCRIPTION_HOOKS + _COMMON_HOOKS |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1408 | |
Mike Frysinger | 3554bc9 | 2015-03-11 04:59:21 -0400 | [diff] [blame] | 1409 | enabled_hooks, disabled_hooks = _get_override_hooks(config) |
Filipe Brandenburger | f70d32c | 2015-10-09 13:35:45 -0700 | [diff] [blame] | 1410 | hooks = [hook for hook in hooks if hook not in disabled_hooks] |
| 1411 | |
| 1412 | # If a list is both in _COMMON_HOOKS and also enabled explicitly through an |
| 1413 | # override, keep the override only. Note that the override may end up being |
| 1414 | # a functools.partial, in which case we need to extract the .func to compare |
| 1415 | # it to the common hooks. |
| 1416 | unwrapped_hooks = [getattr(hook, 'func', hook) for hook in enabled_hooks] |
| 1417 | hooks = [hook for hook in hooks if hook not in unwrapped_hooks] |
| 1418 | |
| 1419 | hooks = list(enabled_hooks) + hooks |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1420 | |
| 1421 | if project in _PROJECT_SPECIFIC_HOOKS: |
Puneet Kumar | c80e3f6 | 2012-08-13 19:01:18 -0700 | [diff] [blame] | 1422 | hooks.extend(hook for hook in _PROJECT_SPECIFIC_HOOKS[project] |
| 1423 | if hook not in disabled_hooks) |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1424 | |
Jon Salz | 3ee59de | 2012-08-18 13:54:22 +0800 | [diff] [blame] | 1425 | for script in _get_project_hook_scripts(config): |
| 1426 | hooks.append(functools.partial(_run_project_hook_script, script)) |
| 1427 | |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1428 | return hooks |
| 1429 | |
| 1430 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1431 | def _run_project_hooks(project_name, proj_dir=None, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1432 | commit_list=None, presubmit=False): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1433 | """For each project run its project specific hook from the hooks dictionary. |
| 1434 | |
| 1435 | Args: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1436 | project_name: The name of project to run hooks for. |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1437 | proj_dir: If non-None, this is the directory the project is in. If None, |
| 1438 | we'll ask repo. |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1439 | commit_list: A list of commits to run hooks against. If None or empty list |
| 1440 | then we'll automatically get the list of commits that would be uploaded. |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1441 | presubmit: A Boolean, True if the check is run as a git pre-submit script. |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1442 | |
| 1443 | Returns: |
| 1444 | Boolean value of whether any errors were ecountered while running the hooks. |
| 1445 | """ |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1446 | if proj_dir is None: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1447 | proj_dirs = _run_command( |
| 1448 | ['repo', 'forall', project_name, '-c', 'pwd']).split() |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1449 | if len(proj_dirs) == 0: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1450 | print('%s cannot be found.' % project_name, file=sys.stderr) |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1451 | print('Please specify a valid project.', file=sys.stderr) |
| 1452 | return True |
| 1453 | if len(proj_dirs) > 1: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1454 | print('%s is associated with multiple directories.' % project_name, |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1455 | file=sys.stderr) |
| 1456 | print('Please specify a directory to help disambiguate.', file=sys.stderr) |
| 1457 | return True |
| 1458 | proj_dir = proj_dirs[0] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1459 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1460 | pwd = os.getcwd() |
| 1461 | # hooks assume they are run from the root of the project |
| 1462 | os.chdir(proj_dir) |
| 1463 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1464 | remote_branch = _run_command(['git', 'rev-parse', '--abbrev-ref', |
| 1465 | '--symbolic-full-name', '@{u}']).strip() |
| 1466 | if not remote_branch: |
| 1467 | print('Your project %s doesn\'t track any remote repo.' % project_name, |
| 1468 | file=sys.stderr) |
| 1469 | remote = None |
| 1470 | else: |
| 1471 | remote, _branch = remote_branch.split('/', 1) |
| 1472 | |
| 1473 | project = Project(name=project_name, dir=proj_dir, remote=remote) |
| 1474 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1475 | if not commit_list: |
| 1476 | try: |
| 1477 | commit_list = _get_commits() |
| 1478 | except VerifyException as e: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1479 | PrintErrorForProject(project.name, HookFailure(str(e))) |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1480 | os.chdir(pwd) |
| 1481 | return True |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1482 | |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1483 | hooks = _get_project_hooks(project.name, presubmit) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1484 | error_found = False |
Ryan Cui | fa55df5 | 2011-05-06 11:16:55 -0700 | [diff] [blame] | 1485 | for commit in commit_list: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1486 | error_list = [] |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1487 | for hook in hooks: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1488 | hook_error = hook(project, commit) |
| 1489 | if hook_error: |
| 1490 | error_list.append(hook_error) |
| 1491 | error_found = True |
| 1492 | if error_list: |
Alex Deymo | 643ac4c | 2015-09-03 10:40:50 -0700 | [diff] [blame] | 1493 | PrintErrorsForCommit(project.name, commit, _get_commit_desc(commit), |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1494 | error_list) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1495 | |
Ryan Cui | ec4d633 | 2011-05-02 14:15:25 -0700 | [diff] [blame] | 1496 | os.chdir(pwd) |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1497 | return error_found |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1498 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1499 | |
Mandeep Singh Baines | 116ad10 | 2011-04-27 15:16:37 -0700 | [diff] [blame] | 1500 | # Main |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1501 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1502 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1503 | def main(project_list, worktree_list=None, **_kwargs): |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1504 | """Main function invoked directly by repo. |
| 1505 | |
| 1506 | This function will exit directly upon error so that repo doesn't print some |
| 1507 | obscure error message. |
| 1508 | |
| 1509 | Args: |
| 1510 | project_list: List of projects to run on. |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1511 | worktree_list: A list of directories. It should be the same length as |
| 1512 | project_list, so that each entry in project_list matches with a directory |
| 1513 | in worktree_list. If None, we will attempt to calculate the directories |
| 1514 | automatically. |
Doug Anderson | 0645663 | 2012-01-05 11:02:14 -0800 | [diff] [blame] | 1515 | kwargs: Leave this here for forward-compatibility. |
| 1516 | """ |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1517 | found_error = False |
David James | 2edd900 | 2013-10-11 14:09:19 -0700 | [diff] [blame] | 1518 | if not worktree_list: |
| 1519 | worktree_list = [None] * len(project_list) |
| 1520 | for project, worktree in zip(project_list, worktree_list): |
| 1521 | if _run_project_hooks(project, proj_dir=worktree): |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1522 | found_error = True |
| 1523 | |
Mike Frysinger | ae40952 | 2014-02-01 03:16:11 -0500 | [diff] [blame] | 1524 | if found_error: |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1525 | msg = ('Preupload failed due to errors in project(s). HINTS:\n' |
Ryan Cui | 9b65163 | 2011-05-11 11:38:58 -0700 | [diff] [blame] | 1526 | '- To disable some source style checks, and for other hints, see ' |
| 1527 | '<checkout_dir>/src/repohooks/README\n' |
| 1528 | '- To upload only current project, run \'repo upload .\'') |
Mike Frysinger | 09d6a3d | 2013-10-08 22:21:03 -0400 | [diff] [blame] | 1529 | print(msg, file=sys.stderr) |
Don Garrett | dba548a | 2011-05-05 15:17:14 -0700 | [diff] [blame] | 1530 | sys.exit(1) |
Anush Elangovan | 63afad7 | 2011-03-23 00:41:27 -0700 | [diff] [blame] | 1531 | |
Ryan Cui | 1562fb8 | 2011-05-09 11:01:31 -0700 | [diff] [blame] | 1532 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1533 | def _identify_project(path): |
| 1534 | """Identify the repo project associated with the given path. |
| 1535 | |
| 1536 | Returns: |
| 1537 | A string indicating what project is associated with the path passed in or |
| 1538 | a blank string upon failure. |
| 1539 | """ |
| 1540 | return _run_command(['repo', 'forall', '.', '-c', 'echo ${REPO_PROJECT}'], |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1541 | redirect_stderr=True, cwd=path).strip() |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1542 | |
| 1543 | |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1544 | def direct_main(argv): |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1545 | """Run hooks directly (outside of the context of repo). |
| 1546 | |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1547 | Args: |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1548 | argv: The command line args to process |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1549 | |
| 1550 | Returns: |
| 1551 | 0 if no pre-upload failures, 1 if failures. |
| 1552 | |
| 1553 | Raises: |
| 1554 | BadInvocation: On some types of invocation errors. |
| 1555 | """ |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1556 | parser = commandline.ArgumentParser(description=__doc__) |
| 1557 | parser.add_argument('--dir', default=None, |
| 1558 | help='The directory that the project lives in. If not ' |
| 1559 | 'specified, use the git project root based on the cwd.') |
| 1560 | parser.add_argument('--project', default=None, |
| 1561 | help='The project repo path; this can affect how the ' |
| 1562 | 'hooks get run, since some hooks are project-specific. ' |
| 1563 | 'For chromite this is chromiumos/chromite. If not ' |
| 1564 | 'specified, the repo tool will be used to figure this ' |
| 1565 | 'out based on the dir.') |
| 1566 | parser.add_argument('--rerun-since', default=None, |
| 1567 | help='Rerun hooks on old commits since the given date. ' |
| 1568 | 'The date should match git log\'s concept of a date. ' |
| 1569 | 'e.g. 2012-06-20. This option is mutually exclusive ' |
| 1570 | 'with --pre-submit.') |
| 1571 | parser.add_argument('--pre-submit', action="store_true", |
| 1572 | help='Run the check against the pending commit. ' |
| 1573 | 'This option should be used at the \'git commit\' ' |
| 1574 | 'phase as opposed to \'repo upload\'. This option ' |
| 1575 | 'is mutually exclusive with --rerun-since.') |
| 1576 | parser.add_argument('commits', nargs='*', |
| 1577 | help='Check specific commits') |
| 1578 | opts = parser.parse_args(argv) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1579 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1580 | if opts.rerun_since: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1581 | if opts.commits: |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1582 | raise BadInvocation('Can\'t pass commits and use rerun-since: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1583 | ' '.join(opts.commits)) |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1584 | |
| 1585 | cmd = ['git', 'log', '--since="%s"' % opts.rerun_since, '--pretty=%H'] |
| 1586 | all_commits = _run_command(cmd).splitlines() |
| 1587 | bot_commits = _run_command(cmd + ['--author=chrome-bot']).splitlines() |
| 1588 | |
| 1589 | # Eliminate chrome-bot commits but keep ordering the same... |
| 1590 | bot_commits = set(bot_commits) |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1591 | opts.commits = [c for c in all_commits if c not in bot_commits] |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1592 | |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1593 | if opts.pre_submit: |
| 1594 | raise BadInvocation('rerun-since and pre-submit can not be ' |
| 1595 | 'used together') |
| 1596 | if opts.pre_submit: |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1597 | if opts.commits: |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1598 | raise BadInvocation('Can\'t pass commits and use pre-submit: %s' % |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1599 | ' '.join(opts.commits)) |
| 1600 | opts.commits = [PRE_SUBMIT,] |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1601 | |
| 1602 | # Check/normlaize git dir; if unspecified, we'll use the root of the git |
| 1603 | # project from CWD |
| 1604 | if opts.dir is None: |
| 1605 | git_dir = _run_command(['git', 'rev-parse', '--git-dir'], |
Rahul Chaudhry | 0e51534 | 2015-08-07 12:00:43 -0700 | [diff] [blame] | 1606 | redirect_stderr=True).strip() |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1607 | if not git_dir: |
| 1608 | raise BadInvocation('The current directory is not part of a git project.') |
| 1609 | opts.dir = os.path.dirname(os.path.abspath(git_dir)) |
| 1610 | elif not os.path.isdir(opts.dir): |
| 1611 | raise BadInvocation('Invalid dir: %s' % opts.dir) |
| 1612 | elif not os.path.isdir(os.path.join(opts.dir, '.git')): |
| 1613 | raise BadInvocation('Not a git directory: %s' % opts.dir) |
| 1614 | |
| 1615 | # Identify the project if it wasn't specified; this _requires_ the repo |
| 1616 | # tool to be installed and for the project to be part of a repo checkout. |
| 1617 | if not opts.project: |
| 1618 | opts.project = _identify_project(opts.dir) |
| 1619 | if not opts.project: |
| 1620 | raise BadInvocation("Repo couldn't identify the project of %s" % opts.dir) |
| 1621 | |
Doug Anderson | 1474956 | 2013-06-26 13:38:29 -0700 | [diff] [blame] | 1622 | found_error = _run_project_hooks(opts.project, proj_dir=opts.dir, |
Mike Frysinger | 6614293 | 2014-12-18 14:55:57 -0500 | [diff] [blame] | 1623 | commit_list=opts.commits, |
Vadim Bendebury | 2b62d74 | 2014-06-22 13:14:51 -0700 | [diff] [blame] | 1624 | presubmit=opts.pre_submit) |
Doug Anderson | 44a644f | 2011-11-02 10:37:37 -0700 | [diff] [blame] | 1625 | if found_error: |
| 1626 | return 1 |
| 1627 | return 0 |
| 1628 | |
| 1629 | |
Mandeep Singh Baines | 69e470e | 2011-04-06 10:34:52 -0700 | [diff] [blame] | 1630 | if __name__ == '__main__': |
Mike Frysinger | 55f85b5 | 2014-12-18 14:45:21 -0500 | [diff] [blame] | 1631 | sys.exit(direct_main(sys.argv[1:])) |