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