blob: c3902b8f3f13a5e284803178641dc23279cbda13 [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
Søren Gjesse641b9ab2020-10-08 13:28:37 +020040or fix formatting, commit and upload:
41
42 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
43
Ian Zernydaac9c52020-03-03 10:57:17 +010044or bypass the checks with:
45
Søren Gjesse641b9ab2020-10-08 13:28:37 +020046 git cl upload --bypass-hooks
47 """ % (FMT_CMD, FMT_CMD)))
Ian Zernydaac9c52020-03-03 10:57:17 +010048 return results
49
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020050def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010051 for f in input_api.AffectedFiles():
52 path = f.LocalPath()
53 if not path.endswith('InternalOptions.java'):
54 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010055 diff = check_output(
56 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
57 if 'DETERMINISTIC_DEBUGGING' in diff:
58 return [output_api.PresubmitError(diff)]
59 return []
60
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020061def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +010062 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020063 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +020064 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020065 results.append(
66 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +020067 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020068 return results
69
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020070def CheckForCopyRight(input_api, output_api, branch):
71 results = []
72 for f in input_api.AffectedSourceFiles(None):
73 # Check if it is a new file.
74 if f.OldContents():
75 continue
76 contents = f.NewContents()
77 if (not contents) or (len(contents) == 0):
78 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020079 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020080 results.append(
81 output_api.PresubmitError('Could not find Copyright in file: %s' % f))
82 return results
83
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020084def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +020085 expected = '//'
86 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
87 expected = '#'
88 expected = expected + ' Copyright'
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020089 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020090 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020091 return True
92 return False
93
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020094def CheckChange(input_api, output_api):
95 branch = (
96 check_output(['git', 'cl', 'upstream'])
97 .strip()
98 .replace('refs/heads/', ''))
99 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100100 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200101 results.extend(CheckFormatting(input_api, output_api, branch))
102 results.extend(
103 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200104 results.extend(CheckForAddedDisassemble(input_api, output_api))
105 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100106 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100107
108def CheckChangeOnCommit(input_api, output_api):
109 return CheckChange(input_api, output_api)
110
111def CheckChangeOnUpload(input_api, output_api):
112 return CheckChange(input_api, output_api)