blob: 088b42fb7cf51633e3db1c2643149e553cd45dee [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',
Christoffer Quist Adamsenbfe52fd2022-02-15 14:32:52 +010011 'google',
Ian Zernydaac9c52020-03-03 10:57:17 +010012 'google-java-format',
Christoffer Quist Adamsenbfe52fd2022-02-15 14:32:52 +010013 '1.14.0',
14 'google-java-format-1.14.0',
Ian Zernydaac9c52020-03-03 10:57:17 +010015 'scripts',
16 'google-java-format-diff.py')
17
Rico Wind62d03202018-11-30 13:43:49 +010018def CheckDoNotMerge(input_api, output_api):
19 for l in input_api.change.FullDescriptionText().splitlines():
20 if l.lower().startswith('do not merge'):
21 msg = 'Your cl contains: \'Do not merge\' - this will break WIP bots'
22 return [output_api.PresubmitPromptWarning(msg, [])]
23 return []
24
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020025def CheckFormatting(input_api, output_api, branch):
Rico Wind62d03202018-11-30 13:43:49 +010026 results = []
Ian Zernydaac9c52020-03-03 10:57:17 +010027 for f in input_api.AffectedFiles():
28 path = f.LocalPath()
29 if not path.endswith('.java'):
30 continue
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010031 diff = check_output(
32 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
Ian Zernydaac9c52020-03-03 10:57:17 +010033 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010034 (stdout, stderr) = proc.communicate(input=diff)
Ian Zernydaac9c52020-03-03 10:57:17 +010035 if len(stdout) > 0:
36 results.append(output_api.PresubmitError(stdout))
37 if len(results) > 0:
38 results.append(output_api.PresubmitError(
39 """Please fix the formatting by running:
40
41 git diff -U0 $(git cl upstream) | %s -p1 -i
42
Søren Gjesse641b9ab2020-10-08 13:28:37 +020043or fix formatting, commit and upload:
44
45 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
46
Ian Zernydaac9c52020-03-03 10:57:17 +010047or bypass the checks with:
48
Søren Gjesse641b9ab2020-10-08 13:28:37 +020049 git cl upload --bypass-hooks
50 """ % (FMT_CMD, FMT_CMD)))
Ian Zernydaac9c52020-03-03 10:57:17 +010051 return results
52
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020053def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010054 for f in input_api.AffectedFiles():
55 path = f.LocalPath()
56 if not path.endswith('InternalOptions.java'):
57 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010058 diff = check_output(
59 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
60 if 'DETERMINISTIC_DEBUGGING' in diff:
61 return [output_api.PresubmitError(diff)]
62 return []
63
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020064def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +010065 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020066 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +020067 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020068 results.append(
69 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +020070 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020071 return results
72
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020073def CheckForCopyRight(input_api, output_api, branch):
74 results = []
75 for f in input_api.AffectedSourceFiles(None):
76 # Check if it is a new file.
77 if f.OldContents():
78 continue
79 contents = f.NewContents()
80 if (not contents) or (len(contents) == 0):
81 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020082 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020083 results.append(
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +020084 output_api.PresubmitError('Could not find correctly formatted '
85 'copyright in file: %s' % f))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020086 return results
87
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020088def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +020089 expected = '//'
90 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
91 expected = '#'
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +020092 expected = expected + ' Copyright (c) ' + str(datetime.datetime.now().year)
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020093 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020094 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020095 return True
96 return False
97
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020098def CheckChange(input_api, output_api):
99 branch = (
100 check_output(['git', 'cl', 'upstream'])
101 .strip()
102 .replace('refs/heads/', ''))
103 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100104 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200105 results.extend(CheckFormatting(input_api, output_api, branch))
106 results.extend(
107 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200108 results.extend(CheckForAddedDisassemble(input_api, output_api))
109 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100110 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100111
112def CheckChangeOnCommit(input_api, output_api):
113 return CheckChange(input_api, output_api)
114
115def CheckChangeOnUpload(input_api, output_api):
116 return CheckChange(input_api, output_api)