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