blob: 8fbb76e53076ce24abad9eeeef9e9ac954a67064 [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
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +02006import datetime
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +01007from subprocess import check_output, Popen, PIPE, STDOUT
Ian Zernydaac9c52020-03-03 10:57:17 +01008
9FMT_CMD = path.join(
10 'third_party',
11 'google-java-format',
12 'google-java-format-google-java-format-1.7',
13 'scripts',
14 'google-java-format-diff.py')
15
Rico Wind62d03202018-11-30 13:43:49 +010016def CheckDoNotMerge(input_api, output_api):
17 for l in input_api.change.FullDescriptionText().splitlines():
18 if l.lower().startswith('do not merge'):
19 msg = 'Your cl contains: \'Do not merge\' - this will break WIP bots'
20 return [output_api.PresubmitPromptWarning(msg, [])]
21 return []
22
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020023def CheckFormatting(input_api, output_api, branch):
Rico Wind62d03202018-11-30 13:43:49 +010024 results = []
Ian Zernydaac9c52020-03-03 10:57:17 +010025 for f in input_api.AffectedFiles():
26 path = f.LocalPath()
27 if not path.endswith('.java'):
28 continue
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010029 diff = check_output(
30 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
Ian Zernydaac9c52020-03-03 10:57:17 +010031 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010032 (stdout, stderr) = proc.communicate(input=diff)
Ian Zernydaac9c52020-03-03 10:57:17 +010033 if len(stdout) > 0:
34 results.append(output_api.PresubmitError(stdout))
35 if len(results) > 0:
36 results.append(output_api.PresubmitError(
37 """Please fix the formatting by running:
38
39 git diff -U0 $(git cl upstream) | %s -p1 -i
40
Søren Gjesse641b9ab2020-10-08 13:28:37 +020041or fix formatting, commit and upload:
42
43 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
44
Ian Zernydaac9c52020-03-03 10:57:17 +010045or bypass the checks with:
46
Søren Gjesse641b9ab2020-10-08 13:28:37 +020047 git cl upload --bypass-hooks
48 """ % (FMT_CMD, FMT_CMD)))
Ian Zernydaac9c52020-03-03 10:57:17 +010049 return results
50
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020051def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010052 for f in input_api.AffectedFiles():
53 path = f.LocalPath()
54 if not path.endswith('InternalOptions.java'):
55 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010056 diff = check_output(
57 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
58 if 'DETERMINISTIC_DEBUGGING' in diff:
59 return [output_api.PresubmitError(diff)]
60 return []
61
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020062def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +010063 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020064 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +020065 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020066 results.append(
67 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +020068 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020069 return results
70
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020071def CheckForCopyRight(input_api, output_api, branch):
72 results = []
73 for f in input_api.AffectedSourceFiles(None):
74 # Check if it is a new file.
75 if f.OldContents():
76 continue
77 contents = f.NewContents()
78 if (not contents) or (len(contents) == 0):
79 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020080 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020081 results.append(
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +020082 output_api.PresubmitError('Could not find correctly formatted '
83 'copyright in file: %s' % f))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020084 return results
85
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020086def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +020087 expected = '//'
88 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
89 expected = '#'
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +020090 expected = expected + ' Copyright (c) ' + str(datetime.datetime.now().year)
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020091 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020092 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020093 return True
94 return False
95
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020096def CheckChange(input_api, output_api):
97 branch = (
98 check_output(['git', 'cl', 'upstream'])
99 .strip()
100 .replace('refs/heads/', ''))
101 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100102 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200103 results.extend(CheckFormatting(input_api, output_api, branch))
104 results.extend(
105 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200106 results.extend(CheckForAddedDisassemble(input_api, output_api))
107 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100108 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100109
110def CheckChangeOnCommit(input_api, output_api):
111 return CheckChange(input_api, output_api)
112
113def CheckChangeOnUpload(input_api, output_api):
114 return CheckChange(input_api, output_api)