diff options
author | Joe Baker-Malone <jbakermalone@google.com> | 2022-05-03 05:21:12 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-05-03 05:21:12 +0000 |
commit | fa88a3ee21f3e8c5f3a9628220e911e69449f354 (patch) | |
tree | 1bd3c9393ce672354bab0f1af848012b8b1ce379 | |
parent | f5171d0395ca78e89db6479057a4262d87e5abb1 (diff) | |
parent | df057d3b9aaf80c938d07849b0dd11b918844c8a (diff) | |
download | repohooks-fa88a3ee21f3e8c5f3a9628220e911e69449f354.tar.gz |
Add ktfmt hook. am: df057d3b9a
Original change: https://android-review.googlesource.com/c/platform/tools/repohooks/+/2075314
Change-Id: I85107d487da5e67d3597579d5f06aabc098fab3b
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | rh/hooks.py | 38 | ||||
-rwxr-xr-x | rh/hooks_unittest.py | 24 |
3 files changed, 66 insertions, 0 deletions
@@ -193,6 +193,9 @@ canned hooks already included geared towards AOSP style guidelines. * `google_java_format`: Run Java code through [`google-java-format`](https://github.com/google/google-java-format) * `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 + the specified directories. * `pylint`: Alias of `pylint2`. Will change to `pylint3` by end of 2019. * `pylint2`: Run Python code through `pylint` using Python 2. * `pylint3`: Run Python code through `pylint` using Python 3. @@ -274,6 +277,7 @@ distros/versions. The following tools are recognized: * `gofmt`: used for the `gofmt` builtin hook. * `google-java-format`: used for the `google_java_format` builtin hook. * `google-java-format-diff`: used for the `google_java_format` builtin hook. +* `ktfmt`: used for the `ktfmt` builtin hook. * `pylint`: used for the `pylint` builtin hook. * `rustfmt`: used for the `rustfmt` builtin hook. diff --git a/rh/hooks.py b/rh/hooks.py index d473cb9..643001c 100644 --- a/rh/hooks.py +++ b/rh/hooks.py @@ -397,6 +397,42 @@ def check_google_java_format(project, commit, _desc, _diff, options=None): fixup_func=fixup_func) +def check_ktfmt(project, commit, _desc, diff, options=None): + """Checks that kotlin files are formatted with ktfmt.""" + + include_dir_args = [x for x in options.args() + if x.startswith('--include-dirs=')] + include_dirs = [x.removeprefix('--include-dirs=').split(',') + for x in include_dir_args] + patterns = [fr'^{x}/.*\.kt$' for dir_list in include_dirs + for x in dir_list] + if not patterns: + patterns = [r'\.kt$'] + + filtered = _filter_diff(diff, patterns) + + if not filtered: + return None + + args = [x for x in options.args() if x not in include_dir_args] + + ktfmt = options.tool_path('ktfmt') + cmd = [ktfmt, '--dry-run'] + args + HookOptions.expand_vars( + ('${PREUPLOAD_FILES}',), filtered) + result = _run(cmd) + if result.stdout: + paths = [os.path.join(project.dir, x.file) for x in filtered] + error = ( + f'\nKotlin files need formatting.\n' + 'To reformat the kotlin files in this commit:\n' + f'{ktfmt} {" ".join(paths)}' + ) + fixup_func = _fixup_func_caller([ktfmt] + paths) + return [rh.results.HookResult('ktfmt', project, commit, error=error, + files=paths, fixup_func=fixup_func)] + return None + + def check_commit_msg_bug_field(project, commit, desc, _diff, options=None): """Check the commit message for a 'Bug:' line.""" field = 'Bug' @@ -1014,6 +1050,7 @@ BUILTIN_HOOKS = { 'gofmt': check_gofmt, 'google_java_format': check_google_java_format, 'jsonlint': check_json, + 'ktfmt': check_ktfmt, 'pylint': check_pylint2, 'pylint2': check_pylint2, 'pylint3': check_pylint3, @@ -1034,6 +1071,7 @@ TOOL_PATHS = { 'gofmt': 'gofmt', 'google-java-format': 'google-java-format', 'google-java-format-diff': 'google-java-format-diff.py', + 'ktfmt': 'ktfmt', 'pylint': 'pylint', 'rustfmt': 'rustfmt', } diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py index 03f45ca..389aad8 100755 --- a/rh/hooks_unittest.py +++ b/rh/hooks_unittest.py @@ -776,6 +776,30 @@ class BuiltinHooksTests(unittest.TestCase): # TODO: Actually pass some valid/invalid json data down. + def test_ktfmt(self, mock_check, _mock_run): + """Verify the ktfmt builtin hook.""" + # First call should do nothing as there are no files to check. + ret = rh.hooks.check_ktfmt( + self.project, 'commit', 'desc', (), options=self.options) + self.assertIsNone(ret) + self.assertFalse(mock_check.called) + # Check that .kt files are included by default. + diff = [rh.git.RawDiffEntry(file='foo.kt'), + rh.git.RawDiffEntry(file='bar.java'), + rh.git.RawDiffEntry(file='baz/blah.kt')] + ret = rh.hooks.check_ktfmt( + self.project, 'commit', 'desc', diff, options=self.options) + self.assertListEqual(ret[0].files, ['/.../repo/dir/foo.kt', + '/.../repo/dir/baz/blah.kt']) + diff = [rh.git.RawDiffEntry(file='foo/f1.kt'), + rh.git.RawDiffEntry(file='bar/f2.kt'), + rh.git.RawDiffEntry(file='baz/f2.kt')] + ret = rh.hooks.check_ktfmt(self.project, 'commit', 'desc', diff, + options=rh.hooks.HookOptions('hook name', [ + '--include-dirs=foo,baz'], {})) + self.assertListEqual(ret[0].files, ['/.../repo/dir/foo/f1.kt', + '/.../repo/dir/baz/f2.kt']) + def test_pylint(self, mock_check, _mock_run): """Verify the pylint builtin hook.""" self._test_file_filter(mock_check, rh.hooks.check_pylint2, |