Rico Wind | 62d0320 | 2018-11-30 13:43:49 +0100 | [diff] [blame] | 1 | # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 2 | # for details. All rights reserved. Use of this source code is governed by a |
| 3 | # BSD-style license that can be found in the LICENSE file. |
| 4 | |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 5 | from os import path |
Christoffer Quist Adamsen | 4411f07 | 2020-03-05 13:28:31 +0100 | [diff] [blame] | 6 | from subprocess import check_output, Popen, PIPE, STDOUT |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 7 | |
| 8 | FMT_CMD = path.join( |
| 9 | 'third_party', |
| 10 | 'google-java-format', |
| 11 | 'google-java-format-google-java-format-1.7', |
| 12 | 'scripts', |
| 13 | 'google-java-format-diff.py') |
| 14 | |
Rico Wind | 62d0320 | 2018-11-30 13:43:49 +0100 | [diff] [blame] | 15 | def CheckDoNotMerge(input_api, output_api): |
| 16 | for l in input_api.change.FullDescriptionText().splitlines(): |
| 17 | if l.lower().startswith('do not merge'): |
| 18 | msg = 'Your cl contains: \'Do not merge\' - this will break WIP bots' |
| 19 | return [output_api.PresubmitPromptWarning(msg, [])] |
| 20 | return [] |
| 21 | |
Morten Krogh-Jespersen | ebc876f | 2020-04-01 10:58:02 +0200 | [diff] [blame] | 22 | def CheckFormatting(input_api, output_api, branch): |
Rico Wind | 62d0320 | 2018-11-30 13:43:49 +0100 | [diff] [blame] | 23 | results = [] |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 24 | for f in input_api.AffectedFiles(): |
| 25 | path = f.LocalPath() |
| 26 | if not path.endswith('.java'): |
| 27 | continue |
Christoffer Quist Adamsen | 4411f07 | 2020-03-05 13:28:31 +0100 | [diff] [blame] | 28 | diff = check_output( |
| 29 | ['git', 'diff', '--no-prefix', '-U0', branch, '--', path]) |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 30 | proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT) |
Christoffer Quist Adamsen | 4411f07 | 2020-03-05 13:28:31 +0100 | [diff] [blame] | 31 | (stdout, stderr) = proc.communicate(input=diff) |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 32 | if len(stdout) > 0: |
| 33 | results.append(output_api.PresubmitError(stdout)) |
| 34 | if len(results) > 0: |
| 35 | results.append(output_api.PresubmitError( |
| 36 | """Please fix the formatting by running: |
| 37 | |
| 38 | git diff -U0 $(git cl upstream) | %s -p1 -i |
| 39 | |
| 40 | or bypass the checks with: |
| 41 | |
| 42 | cl upload --bypass-hooks |
| 43 | """ % FMT_CMD)) |
| 44 | return results |
| 45 | |
Morten Krogh-Jespersen | ebc876f | 2020-04-01 10:58:02 +0200 | [diff] [blame] | 46 | def CheckDeterministicDebuggingChanged(input_api, output_api, branch): |
Morten Krogh-Jespersen | 30d1f1b | 2020-03-26 11:39:19 +0100 | [diff] [blame] | 47 | for f in input_api.AffectedFiles(): |
| 48 | path = f.LocalPath() |
| 49 | if not path.endswith('InternalOptions.java'): |
| 50 | continue |
Morten Krogh-Jespersen | 30d1f1b | 2020-03-26 11:39:19 +0100 | [diff] [blame] | 51 | diff = check_output( |
| 52 | ['git', 'diff', '--no-prefix', '-U0', branch, '--', path]) |
| 53 | if 'DETERMINISTIC_DEBUGGING' in diff: |
| 54 | return [output_api.PresubmitError(diff)] |
| 55 | return [] |
| 56 | |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 57 | def CheckForAddedDisassemble(input_api, output_api): |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 58 | results = [] |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 59 | for (file, line_nr, line) in input_api.RightHandSideLines(): |
| 60 | if 'disassemble()' in line: |
| 61 | results.append( |
| 62 | output_api.PresubmitError( |
| 63 | '%s:%s %s' % (file.LocalPath(), line_nr, line))) |
Morten Krogh-Jespersen | ebc876f | 2020-04-01 10:58:02 +0200 | [diff] [blame] | 64 | return results |
| 65 | |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 66 | def CheckForCopyRight(input_api, output_api, branch): |
| 67 | results = [] |
| 68 | for f in input_api.AffectedSourceFiles(None): |
| 69 | # Check if it is a new file. |
| 70 | if f.OldContents(): |
| 71 | continue |
| 72 | contents = f.NewContents() |
| 73 | if (not contents) or (len(contents) == 0): |
| 74 | continue |
Christoffer Quist Adamsen | c1e18d6 | 2020-04-15 08:45:01 +0200 | [diff] [blame] | 75 | if not CopyRightInContents(f, contents): |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 76 | results.append( |
| 77 | output_api.PresubmitError('Could not find Copyright in file: %s' % f)) |
| 78 | return results |
| 79 | |
Christoffer Quist Adamsen | c1e18d6 | 2020-04-15 08:45:01 +0200 | [diff] [blame] | 80 | def CopyRightInContents(f, contents): |
| 81 | expected = ('#' if f.LocalPath().endswith('.py') else '//') + ' Copyright' |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 82 | for content_line in contents: |
Christoffer Quist Adamsen | c1e18d6 | 2020-04-15 08:45:01 +0200 | [diff] [blame] | 83 | if expected in content_line: |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 84 | return True |
| 85 | return False |
| 86 | |
Morten Krogh-Jespersen | ebc876f | 2020-04-01 10:58:02 +0200 | [diff] [blame] | 87 | def CheckChange(input_api, output_api): |
| 88 | branch = ( |
| 89 | check_output(['git', 'cl', 'upstream']) |
| 90 | .strip() |
| 91 | .replace('refs/heads/', '')) |
| 92 | results = [] |
Rico Wind | 62d0320 | 2018-11-30 13:43:49 +0100 | [diff] [blame] | 93 | results.extend(CheckDoNotMerge(input_api, output_api)) |
Morten Krogh-Jespersen | ebc876f | 2020-04-01 10:58:02 +0200 | [diff] [blame] | 94 | results.extend(CheckFormatting(input_api, output_api, branch)) |
| 95 | results.extend( |
| 96 | CheckDeterministicDebuggingChanged(input_api, output_api, branch)) |
Morten Krogh-Jespersen | 3285e46 | 2020-04-01 13:10:46 +0200 | [diff] [blame] | 97 | results.extend(CheckForAddedDisassemble(input_api, output_api)) |
| 98 | results.extend(CheckForCopyRight(input_api, output_api, branch)) |
Rico Wind | 62d0320 | 2018-11-30 13:43:49 +0100 | [diff] [blame] | 99 | return results |
Ian Zerny | daac9c5 | 2020-03-03 10:57:17 +0100 | [diff] [blame] | 100 | |
| 101 | def CheckChangeOnCommit(input_api, output_api): |
| 102 | return CheckChange(input_api, output_api) |
| 103 | |
| 104 | def CheckChangeOnUpload(input_api, output_api): |
| 105 | return CheckChange(input_api, output_api) |