blob: 87fd41628f0764f1442cea0592673b85e713378f [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
Søren Gjesseffc06192022-06-03 11:11:27 +020018FMT_CMD_JDK17 = path.join('tools','google-java-format-diff.py')
19
20
Rico Wind62d03202018-11-30 13:43:49 +010021def CheckDoNotMerge(input_api, output_api):
22 for l in input_api.change.FullDescriptionText().splitlines():
23 if l.lower().startswith('do not merge'):
24 msg = 'Your cl contains: \'Do not merge\' - this will break WIP bots'
25 return [output_api.PresubmitPromptWarning(msg, [])]
26 return []
27
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020028def CheckFormatting(input_api, output_api, branch):
Rico Wind62d03202018-11-30 13:43:49 +010029 results = []
Ian Zernydaac9c52020-03-03 10:57:17 +010030 for f in input_api.AffectedFiles():
31 path = f.LocalPath()
32 if not path.endswith('.java'):
33 continue
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010034 diff = check_output(
35 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
Ian Zernydaac9c52020-03-03 10:57:17 +010036 proc = Popen(FMT_CMD, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Christoffer Quist Adamsen4411f072020-03-05 13:28:31 +010037 (stdout, stderr) = proc.communicate(input=diff)
Ian Zernydaac9c52020-03-03 10:57:17 +010038 if len(stdout) > 0:
39 results.append(output_api.PresubmitError(stdout))
40 if len(results) > 0:
41 results.append(output_api.PresubmitError(
42 """Please fix the formatting by running:
43
44 git diff -U0 $(git cl upstream) | %s -p1 -i
45
Søren Gjesse641b9ab2020-10-08 13:28:37 +020046or fix formatting, commit and upload:
47
48 git diff -U0 $(git cl upstream) | %s -p1 -i && git commit -a --amend --no-edit && git cl upload
49
Ian Zernydaac9c52020-03-03 10:57:17 +010050or bypass the checks with:
51
Søren Gjesse641b9ab2020-10-08 13:28:37 +020052 git cl upload --bypass-hooks
Søren Gjesseffc06192022-06-03 11:11:27 +020053
54If formatting fails with 'No enum constant javax.lang.model.element.Modifier.SEALED' try
55
56 git diff -U0 $(git cl upstream) | %s %s %s -p1 -i && git commit -a --amend --no-edit && git cl upload
57 """ % (
58 FMT_CMD,
59 FMT_CMD,
60 FMT_CMD_JDK17,
61 '--google-java-format-jar',
62 'third_party/google/google-java-format/1.14.0/google-java-format-1.14.0-all-deps.jar'
63)))
Ian Zernydaac9c52020-03-03 10:57:17 +010064 return results
65
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020066def CheckDeterministicDebuggingChanged(input_api, output_api, branch):
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010067 for f in input_api.AffectedFiles():
68 path = f.LocalPath()
69 if not path.endswith('InternalOptions.java'):
70 continue
Morten Krogh-Jespersen30d1f1b2020-03-26 11:39:19 +010071 diff = check_output(
72 ['git', 'diff', '--no-prefix', '-U0', branch, '--', path])
73 if 'DETERMINISTIC_DEBUGGING' in diff:
74 return [output_api.PresubmitError(diff)]
75 return []
76
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020077def CheckForAddedDisassemble(input_api, output_api):
Ian Zernydaac9c52020-03-03 10:57:17 +010078 results = []
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020079 for (file, line_nr, line) in input_api.RightHandSideLines():
Ian Zernya84bc1b2020-08-11 09:47:00 +020080 if file.LocalPath().endswith('.java') and '.disassemble()' in line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020081 results.append(
82 output_api.PresubmitError(
Ian Zernya84bc1b2020-08-11 09:47:00 +020083 'Test call to disassemble\n%s:%s %s' % (file.LocalPath(), line_nr, line)))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +020084 return results
85
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020086def CheckForCopyRight(input_api, output_api, branch):
87 results = []
88 for f in input_api.AffectedSourceFiles(None):
89 # Check if it is a new file.
90 if f.OldContents():
91 continue
92 contents = f.NewContents()
93 if (not contents) or (len(contents) == 0):
94 continue
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +020095 if not CopyRightInContents(f, contents):
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020096 results.append(
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +020097 output_api.PresubmitError('Could not find correctly formatted '
98 'copyright in file: %s' % f))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +020099 return results
100
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200101def CopyRightInContents(f, contents):
Søren Gjesse09c3dd02021-06-17 08:21:51 +0200102 expected = '//'
103 if f.LocalPath().endswith('.py') or f.LocalPath().endswith('.sh'):
104 expected = '#'
Morten Krogh-Jespersenf9977862021-09-14 12:36:28 +0200105 expected = expected + ' Copyright (c) ' + str(datetime.datetime.now().year)
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200106 for content_line in contents:
Christoffer Quist Adamsenc1e18d62020-04-15 08:45:01 +0200107 if expected in content_line:
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200108 return True
109 return False
110
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200111def CheckChange(input_api, output_api):
112 branch = (
113 check_output(['git', 'cl', 'upstream'])
114 .strip()
115 .replace('refs/heads/', ''))
116 results = []
Rico Wind62d03202018-11-30 13:43:49 +0100117 results.extend(CheckDoNotMerge(input_api, output_api))
Morten Krogh-Jespersenebc876f2020-04-01 10:58:02 +0200118 results.extend(CheckFormatting(input_api, output_api, branch))
119 results.extend(
120 CheckDeterministicDebuggingChanged(input_api, output_api, branch))
Morten Krogh-Jespersen3285e462020-04-01 13:10:46 +0200121 results.extend(CheckForAddedDisassemble(input_api, output_api))
122 results.extend(CheckForCopyRight(input_api, output_api, branch))
Rico Wind62d03202018-11-30 13:43:49 +0100123 return results
Ian Zernydaac9c52020-03-03 10:57:17 +0100124
125def CheckChangeOnCommit(input_api, output_api):
126 return CheckChange(input_api, output_api)
127
128def CheckChangeOnUpload(input_api, output_api):
129 return CheckChange(input_api, output_api)