aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rh/hooks.py13
-rwxr-xr-xrh/hooks_unittest.py2
-rwxr-xr-xtools/google-java-format.py60
3 files changed, 52 insertions, 23 deletions
diff --git a/rh/hooks.py b/rh/hooks.py
index 7edda83..0e9ee40 100644
--- a/rh/hooks.py
+++ b/rh/hooks.py
@@ -352,15 +352,22 @@ def check_bpfmt(project, commit, _desc, diff, options=None):
return None
bpfmt = options.tool_path('bpfmt')
- cmd = [bpfmt, '-l'] + options.args((), filtered)
+ bpfmt_options = options.args((), filtered)
+ cmd = [bpfmt, '-l'] + bpfmt_options
ret = []
for d in filtered:
data = rh.git.get_file_content(commit, d.file)
result = _run(cmd, input=data)
if result.stdout:
+ fixup_cmd = [bpfmt, '-w']
+ if '-s' in bpfmt_options:
+ fixup_cmd.append('-s')
+ fixup_cmd.append(os.path.join(project.dir, d.file))
ret.append(rh.results.HookResult(
- 'bpfmt', project, commit, error=result.stdout,
- files=(d.file,)))
+ 'bpfmt', project, commit,
+ error=result.stdout,
+ files=(d.file,),
+ fixup_func=_fixup_func_caller(fixup_cmd)))
return ret
diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py
index ba5c5fa..c366e1f 100755
--- a/rh/hooks_unittest.py
+++ b/rh/hooks_unittest.py
@@ -370,6 +370,8 @@ class BuiltinHooksTests(unittest.TestCase):
ret = rh.hooks.check_bpfmt(
self.project, 'commit', 'desc', diff, options=self.options)
self.assertIsNotNone(ret)
+ for result in ret:
+ self.assertIsNotNone(result.fixup_func)
def test_checkpatch(self, mock_check, _mock_run):
"""Verify the checkpatch builtin hook."""
diff --git a/tools/google-java-format.py b/tools/google-java-format.py
index f951981..e25d781 100755
--- a/tools/google-java-format.py
+++ b/tools/google-java-format.py
@@ -18,7 +18,7 @@
import argparse
import os
import sys
-from distutils.spawn import find_executable
+from shutil import which
_path = os.path.realpath(__file__ + '/../..')
if sys.path[0] != _path:
@@ -28,8 +28,8 @@ del _path
# We have to import our local modules after the sys.path tweak. We can't use
# relative imports because this is an executable program, not a module.
# pylint: disable=wrong-import-position
-import rh.shell
-import rh.utils
+import rh.shell # pylint: disable=import-error
+import rh.utils # pylint: disable=import-error
def get_parser():
@@ -52,6 +52,8 @@ def get_parser():
parser.add_argument('files', nargs='*',
help='If specified, only consider differences in '
'these files.')
+ parser.add_argument('--verbose', action='store_true',
+ help='Explain what is being done.')
return parser
@@ -60,11 +62,7 @@ def main(argv):
parser = get_parser()
opts = parser.parse_args(argv)
- # google-java-format-diff.py looks for google-java-format in $PATH, so find
- # the parent dir up front and inject it into $PATH when launching it.
- # TODO: Pass the path in directly once this issue is resolved:
- # https://github.com/google/google-java-format/issues/108
- format_path = find_executable(opts.google_java_format)
+ format_path = which(opts.google_java_format)
if not format_path:
print(
f'Unable to find google-java-format at: {opts.google_java_format}',
@@ -72,29 +70,51 @@ def main(argv):
)
return 1
- extra_env = {
- 'PATH': os.path.dirname(format_path) + os.pathsep + os.environ['PATH'],
- }
-
# TODO: Delegate to the tool once this issue is resolved:
# https://github.com/google/google-java-format/issues/107
diff_cmd = ['git', 'diff', '--no-ext-diff', '-U0', f'{opts.commit}^!']
diff_cmd.extend(['--'] + opts.files)
diff = rh.utils.run(diff_cmd, capture_output=True).stdout
- cmd = [opts.google_java_format_diff, '-p1', '--aosp']
+ format_cmd = [
+ opts.google_java_format_diff,
+ '-p1',
+ '--aosp',
+ '-b',
+ format_path,
+ ]
if opts.fix:
- cmd.extend(['-i'])
+ format_cmd.extend(['-i'])
if not opts.sort_imports:
- cmd.extend(['--skip-sorting-imports'])
-
- stdout = rh.utils.run(cmd, input=diff, capture_output=True,
- extra_env=extra_env).stdout
- if stdout:
+ format_cmd.extend(['--skip-sorting-imports'])
+
+ format_cmd_result = rh.utils.run(
+ format_cmd, input=diff, capture_output=True)
+
+ if format_cmd_result.returncode != 0:
+ print("Failed due to non-zero exit code.")
+ if opts.verbose:
+ # print out the full command that was called, including pipes
+ print("Called:")
+ print(f" {' '.join(diff_cmd)} |")
+ print(f" {' '.join(format_cmd)}")
+ for line in format_cmd_result.stdout.splitlines():
+ print(f"[captured stdout] {line}")
+ for line in format_cmd_result.stderr.splitlines():
+ print(f"[captured stderr] {line}")
+ return format_cmd_result.returncode
+ if format_cmd_result.stdout:
print('One or more files in your commit have Java formatting errors.')
print(f'You can run: {sys.argv[0]} --fix {rh.shell.cmd_to_str(argv)}')
return 1
-
+ if format_cmd_result.stderr:
+ # We need to use stderr to catch errors in google-java-format since we
+ # cannot listen for a non-zero error code until
+ # https://github.com/google/google-java-format/pull/848 is merged.
+ print("Errors have been captured in stderr.")
+ for line in format_cmd_result.stderr.splitlines():
+ print(f"[captured stderr] {line}")
+ return 1
return 0