blob: 7c9cae3ae65d8b492da3af920a62e65dc0c1d5a3 [file] [log] [blame]
Rico Wind62d03202018-11-30 13:43:49 +01001# 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 Zernydaac9c52020-03-03 10:57:17 +01005from os import path
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +01006from subprocess import check_output, Popen, PIPE, STDOUT
Ian Zernydaac9c52020-03-03 10:57:17 +01007
8FMT_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 Wind62d03202018-11-30 13:43:49 +010015def 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-Jespersenebc876f2020-04-01 10:58:02 +020022def CheckFormatting(input_api, output_api, branch):
Rico Wind62d03202018-11-30 13:43:49 +010023 results = []
Ian Zernydaac9c52020-03-03 10:57:17 +010024 for f in input_api.AffectedFiles():
25 path = f.LocalPath()
26 if not path.endswith('.java'):
27 continue
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010028 diff = check_output(
29 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
Ian Zernydaac9c52020-03-03 10:57:17 +010030 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010031 (stdout, stderr) = proc.communicate(input=diff)
Ian Zernydaac9c52020-03-03 10:57:17 +010032 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
40or bypass the checks with:
41
42 cl upload --bypass-hooks
43 """ % FMT_CMD))
44 return results
45
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020046def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010047 for f in input_api.AffectedFiles():
48 path = f.LocalPath()
49 if not path.endswith('InternalOptions.java'):
50 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010051 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-Jespersen3285e462020-04-01 13:10:46 +020057def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +010058 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020059 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-Jespersenebc876f2020-04-01 10:58:02 +020064 return results
65
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020066def 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 Adamsenc1e18d62020-04-15 08:45:01 +020075 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020076 results.append(
77 output_api.PresubmitError('Could not find Copyright in file: %s' % f))
78 return results
79
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020080def CopyRightInContents(f, contents):
81 expected = ('#' if f.LocalPath().endswith('.py') else '//') + ' Copyright'
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020082 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020083 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020084 return True
85 return False
86
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020087def CheckChange(input_api, output_api):
88 branch = (
89 check_output(['git', 'cl', 'upstream'])
90 .strip()
91 .replace('refs/heads/', ''))
92 results = []
Rico Wind62d03202018-11-30 13:43:49 +010093 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020094 results.extend(CheckFormatting(input_api, output_api, branch))
95 results.extend(
96 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020097 results.extend(CheckForAddedDisassemble(input_api, output_api))
98 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +010099 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100100
101def CheckChangeOnCommit(input_api, output_api):
102 return CheckChange(input_api, output_api)
103
104def CheckChangeOnUpload(input_api, output_api):
105 return CheckChange(input_api, output_api)