Display diff on `git cl format --diff` when formatting Java files.

Google Java Format does not support `--diff` parameter, but we can simulate it by manually diffing the output of the tool with the current
file on disk.

All git cl-triggered format tools should support --diff output, because there're situations where a change in a file might be required, but it's unclear what exactly should be changed, because a developer might not have access to the host OS which only runs the check.

We at Brave want to run `git cl format --diff` as a part of CI run when a format check fails.

Change-Id: I2feee60219a54ad44b93bc10f5488a9c05715a4a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5351216
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Aleksey Khoroshilov <akhoroshilov@brave.com>
changes/16/5351216/4
Aleksey Khoroshilov 1 year ago committed by LUCI CQ
parent 3c79cfc485
commit 78bef266ac

@ -6032,7 +6032,7 @@ def BuildGitDiffCmd(diff_type, upstream_commit, args, allow_prefix=False):
if args:
for arg in args:
if os.path.isdir(arg) or os.path.isfile(arg):
if arg == '-' or os.path.isdir(arg) or os.path.isfile(arg):
diff_cmd.append(arg)
else:
DieWithError('Argument "%s" is not a file or a directory' % arg)
@ -6128,15 +6128,24 @@ def _RunGoogleJavaFormat(opts, paths, top_dir, upstream_commit):
return 0
base_cmd = [google_java_format, '--aosp']
if opts.dry_run or opts.diff:
if opts.dry_run:
base_cmd += ['--dry-run']
else:
elif not opts.diff:
base_cmd += ['--replace']
changed_lines_only = not (opts.full or settings.GetFormatFullByDefault())
if changed_lines_only:
line_diffs = _ComputeFormatDiffLineRanges(paths, upstream_commit)
def RunFormat(cmd, path, **kwds):
stdout = RunCommand(cmd + [path], **kwds)
# If --diff is passed, google-java-format will output formatted content.
# Diff it with the existing file in the checkout and output the result.
if opts.diff:
diff_cmd = BuildGitDiffCmd(['-U3'], '--no-index', [path, '-'])
stdout = RunGit(diff_cmd, stdin=stdout.encode(), **kwds)
return stdout
results = []
kwds = {'error_ok': True, 'cwd': top_dir}
with multiprocessing.pool.ThreadPool() as pool:
@ -6150,7 +6159,7 @@ def _RunGoogleJavaFormat(opts, paths, top_dir, upstream_commit):
cmd.extend('--lines={}:{}'.format(a, b) for a, b in ranges)
results.append(
pool.apply_async(RunCommand, args=[cmd + [path]], kwds=kwds))
pool.apply_async(RunFormat, args=[cmd, path], kwds=kwds))
return_value = 0
for result in results:

Loading…
Cancel
Save