Skip to content

Commit

Permalink
Java-format: format multiples files in parallel to improve speed
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 620100015
  • Loading branch information
java-team-github-bot authored and google-java-format Team committed Mar 29, 2024
1 parent 71a755b commit 3ee6e2a
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions scripts/google-java-format-diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,34 @@
import subprocess
import io
import sys
from concurrent.futures import ThreadPoolExecutor,wait,FIRST_EXCEPTION
from shutil import which

def _apply_format(filename, lines, base_command, args):
"""Apply format on filename."""
if args.i and args.verbose:
print('Formatting', filename)

command = base_command[:]
command.extend(lines)
command.append(filename)
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=None, stdin=subprocess.PIPE)
stdout, _ = p.communicate()
if p.returncode != 0:
sys.exit(p.returncode)

if not args.i:
with open(filename) as f:
code = f.readlines()
formatted_code = io.StringIO(stdout.decode('utf-8')).readlines()
diff = difflib.unified_diff(code, formatted_code,
filename, filename,
'(before formatting)', '(after formatting)')
diff_string = ''.join(diff)
if len(diff_string) > 0:
sys.stdout.write(diff_string)

def main():
parser = argparse.ArgumentParser(description=
'Reformat changed lines in diff. Without -i '
Expand Down Expand Up @@ -108,39 +134,29 @@ def main():
binary = which('google-java-format') or '/usr/bin/google-java-format'
base_command = [binary]

# Reformat files containing changes in place.
for filename, lines in lines_by_file.items():
if args.i and args.verbose:
print('Formatting', filename)
command = base_command[:]
if args.i:
command.append('-i')
if args.aosp:
command.append('--aosp')
if args.skip_sorting_imports:
command.append('--skip-sorting-imports')
if args.skip_removing_unused_imports:
command.append('--skip-removing-unused-imports')
if args.skip_javadoc_formatting:
command.append('--skip-javadoc-formatting')
command.extend(lines)
command.append(filename)
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=None, stdin=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
sys.exit(p.returncode);

if not args.i:
with open(filename) as f:
code = f.readlines()
formatted_code = io.StringIO(stdout.decode('utf-8')).readlines()
diff = difflib.unified_diff(code, formatted_code,
filename, filename,
'(before formatting)', '(after formatting)')
diff_string = ''.join(diff)
if len(diff_string) > 0:
sys.stdout.write(diff_string)
if args.i:
base_command.append('-i')
if args.aosp:
base_command.append('--aosp')
if args.skip_sorting_imports:
base_command.append('--skip-sorting-imports')
if args.skip_removing_unused_imports:
base_command.append('--skip-removing-unused-imports')
if args.skip_javadoc_formatting:
base_command.append('--skip-javadoc-formatting')

with ThreadPoolExecutor() as executor:

This comment has been minimized.

Copy link
@hei-teacher

hei-teacher Sep 3, 2024

Nice 👍

format_futures = []
for filename, lines in lines_by_file.items():
format_futures.append(
executor.submit(_apply_format, filename, lines, base_command, args)
)

done, _ = wait(format_futures, return_when=FIRST_EXCEPTION)
for future in done:
if exception := future.exception():
executor.shutdown(wait=True, cancel_futures=True)
sys.exit(exception.args[0])

if __name__ == '__main__':
main()

0 comments on commit 3ee6e2a

Please sign in to comment.