Skip to content

Commit

Permalink
Merge pull request #163 from languitar/bugfix/ktlint-fix-file-parsing
Browse files Browse the repository at this point in the history
Ensure only files are passed to ktlint for autofixing
  • Loading branch information
macisamuele committed May 30, 2023
2 parents 5f56679 + d174652 commit 4bc272d
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions language_formatters_pre_commit_hooks/pretty_format_kotlin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import argparse
import json
import subprocess # nosec B404
import sys
import typing

Expand Down Expand Up @@ -65,13 +67,28 @@ def pretty_format_kotlin(argv: typing.Optional[typing.List[str]] = None) -> int:
# To workaround this limitation we do run ktlint in check mode only,
# which provides the expected exit status and we run it again in format
# mode if autofix flag is enabled
check_status, check_output = run_command(
"java", *jvm_args, "-jar", ktlint_jar, "--log-level", "debug", "--relative", "--", *_fix_paths(args.filenames)
#
# Logging must be suppressed here as it goes to stdout. This would
# interfere with parsing the output JSON.
check_result = subprocess.run( # nosec: B603 B607
[
"java",
*jvm_args,
"-jar",
ktlint_jar,
"--log-level",
"none",
"--reporter=json",
"--relative",
"--",
*_fix_paths(args.filenames),
],
capture_output=True,
)

not_pretty_formatted_files: typing.Set[str] = set()
if check_status != 0:
not_pretty_formatted_files.update(line.split(":", 1)[0] for line in check_output.splitlines())
if check_result.returncode != 0:
not_pretty_formatted_files.update(item["file"] for item in json.loads(check_result.stdout))

if args.autofix:
print("Running ktlint format on {}".format(not_pretty_formatted_files))
Expand Down

0 comments on commit 4bc272d

Please sign in to comment.