Merge "repohooks: pass through REPO_PROJECT" into main
diff --git a/README.md b/README.md
index 3741fd9..9b855ec 100644
--- a/README.md
+++ b/README.md
@@ -198,7 +198,9 @@
* `cpplint`: Run through the cpplint tool (for C++ code).
* `gofmt`: Run Go code through `gofmt`.
* `google_java_format`: Run Java code through
- [`google-java-format`](https://github.com/google/google-java-format)
+ [`google-java-format`](https://github.com/google/google-java-format).
+ Supports an additional option --include-dirs, which if specified will limit
+ enforcement to only files under the specified directories.
* `jsonlint`: Verify JSON code is sane.
* `ktfmt`: Run Kotlin code through `ktfmt`. Supports an additional option
--include-dirs, which if specified will limit enforcement to only files under
diff --git a/rh/hooks.py b/rh/hooks.py
index c798298..453f74e 100644
--- a/rh/hooks.py
+++ b/rh/hooks.py
@@ -395,17 +395,35 @@
def check_google_java_format(project, commit, _desc, _diff, options=None):
"""Run google-java-format on the commit."""
+ include_dir_args = [x for x in options.args()
+ if x.startswith('--include-dirs=')]
+ include_dirs = [x[len('--include-dirs='):].split(',')
+ for x in include_dir_args]
+ patterns = [fr'^{x}/.*\.java$' for dir_list in include_dirs
+ for x in dir_list]
+ if not patterns:
+ patterns = [r'\.java$']
+
+ filtered = _filter_diff(_diff, patterns)
+
+ if not filtered:
+ return None
+
+ args = [x for x in options.args() if x not in include_dir_args]
tool = get_helper_path('google-java-format.py')
google_java_format = options.tool_path('google-java-format')
google_java_format_diff = options.tool_path('google-java-format-diff')
tool_args = ['--google-java-format', google_java_format,
'--google-java-format-diff', google_java_format_diff,
- '--commit', commit] + options.args()
- cmd = [tool] + tool_args
+ '--commit', commit] + args
+ cmd = [tool] + tool_args + HookOptions.expand_vars(
+ ('${PREUPLOAD_FILES}',), filtered)
fixup_cmd = [tool, '--fix'] + tool_args
- return _check_cmd('google-java-format', project, commit, cmd,
- fixup_cmd=fixup_cmd)
+ return [rh.results.HookCommandResult('google-java-format', project, commit,
+ _run(cmd),
+ files=[x.file for x in filtered],
+ fixup_cmd=fixup_cmd)]
def check_ktfmt(project, commit, _desc, diff, options=None):
diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py
index c261967..389fe07 100755
--- a/rh/hooks_unittest.py
+++ b/rh/hooks_unittest.py
@@ -400,9 +400,26 @@
def test_google_java_format(self, mock_check, _mock_run):
"""Verify the google_java_format builtin hook."""
+ # First call should do nothing as there are no files to check.
ret = rh.hooks.check_google_java_format(
self.project, 'commit', 'desc', (), options=self.options)
- self.assertEqual(ret, mock_check.return_value)
+ self.assertIsNone(ret)
+ self.assertFalse(mock_check.called)
+ # Check that .java files are included by default.
+ diff = [rh.git.RawDiffEntry(file='foo.java'),
+ rh.git.RawDiffEntry(file='bar.kt'),
+ rh.git.RawDiffEntry(file='baz/blah.java')]
+ ret = rh.hooks.check_google_java_format(
+ self.project, 'commit', 'desc', diff, options=self.options)
+ self.assertListEqual(ret[0].files, ['foo.java', 'baz/blah.java'])
+ diff = [rh.git.RawDiffEntry(file='foo/f1.java'),
+ rh.git.RawDiffEntry(file='bar/f2.java'),
+ rh.git.RawDiffEntry(file='baz/f2.java')]
+ ret = rh.hooks.check_google_java_format(
+ self.project, 'commit', 'desc', diff,
+ options=rh.hooks.HookOptions('hook name',
+ ['--include-dirs=foo,baz'], {}))
+ self.assertListEqual(ret[0].files, ['foo/f1.java', 'baz/f2.java'])
def test_commit_msg_bug_field(self, _mock_check, _mock_run):
"""Verify the commit_msg_bug_field builtin hook."""
diff --git a/tools/google-java-format.py b/tools/google-java-format.py
index fcb5521..88ffed8 100755
--- a/tools/google-java-format.py
+++ b/tools/google-java-format.py
@@ -44,11 +44,11 @@
help='Fix any formatting errors automatically.')
parser.add_argument('--commit', type=str, default='HEAD',
help='Specify the commit to validate.')
- # While the formatter defaults to sorting imports, in the Android codebase,
- # the standard import order doesn't match the formatter's, so flip the
- # default to not sort imports, while letting callers override as desired.
+ # TODO: b/344615661 — Remove argument when all usage has been updated
parser.add_argument('--sort-imports', action='store_true',
- help='If true, imports will be sorted.')
+ help='Deprecated, do nothing')
+ parser.add_argument('--skip-sorting-imports', action='store_true',
+ help='If true, imports will not be sorted.')
parser.add_argument('files', nargs='*',
help='If specified, only consider differences in '
'these files.')
@@ -77,7 +77,7 @@
cmd = [opts.google_java_format_diff, '-p1', '--aosp', '-b', format_path]
if opts.fix:
cmd.extend(['-i'])
- if not opts.sort_imports:
+ if opts.skip_sorting_imports:
cmd.extend(['--skip-sorting-imports'])
stdout = rh.utils.run(cmd, input=diff, capture_output=True).stdout