Commit e081cbe5 authored by Ilya Sherman's avatar Ilya Sherman Committed by Commit Bot

[git cl format] Pass --presubmit flag when formatting from presubmit.

This allows us to skip 'git cl format' for histograms.xml, which in
turn allows us to issue a presubmit error rather than a warning when
that file is malformatted, without running the formatting code twice.
The formatter is somewhat slow for this file, so it's helpful to avoid
duplicating that work.

BUG=none
TEST=none
R=iannucci@chromium.org

Change-Id: Ia86d45c7c56a9f89db34ca7175ef686589084f85
Reviewed-on: https://chromium-review.googlesource.com/569105
Commit-Queue: Ilya Sherman <isherman@chromium.org>
Reviewed-by: 's avatarDirk Pranke <dpranke@chromium.org>
parent 9ff0982e
...@@ -5819,6 +5819,8 @@ def CMDformat(parser, args): ...@@ -5819,6 +5819,8 @@ def CMDformat(parser, args):
help='Format javascript code with clang-format.') help='Format javascript code with clang-format.')
parser.add_option('--diff', action='store_true', parser.add_option('--diff', action='store_true',
help='Print diff to stdout rather than modifying files.') help='Print diff to stdout rather than modifying files.')
parser.add_option('--presubmit', action='store_true',
help='Used when running the script from a presubmit.')
opts, args = parser.parse_args(args) opts, args = parser.parse_args(args)
# Normalize any remaining args against the current path, so paths relative to # Normalize any remaining args against the current path, so paths relative to
...@@ -5961,16 +5963,21 @@ def CMDformat(parser, args): ...@@ -5961,16 +5963,21 @@ def CMDformat(parser, args):
DieWithError("gn format failed on " + gn_diff_file + DieWithError("gn format failed on " + gn_diff_file +
"\nTry running 'gn format' on this file manually.") "\nTry running 'gn format' on this file manually.")
for xml_dir in GetDirtyMetricsDirs(diff_files): # Skip the metrics formatting from the global presubmit hook. These files have
tool_dir = os.path.join(top_dir, xml_dir) # a separate presubmit hook that issues an error if the files need formatting,
cmd = [os.path.join(tool_dir, 'pretty_print.py'), '--non-interactive'] # whereas the top-level presubmit script merely issues a warning. Formatting
if opts.dry_run or opts.diff: # these files is somewhat slow, so it's important not to duplicate the work.
cmd.append('--diff') if not opts.presubmit:
stdout = RunCommand(cmd, cwd=top_dir) for xml_dir in GetDirtyMetricsDirs(diff_files):
if opts.diff: tool_dir = os.path.join(top_dir, xml_dir)
sys.stdout.write(stdout) cmd = [os.path.join(tool_dir, 'pretty_print.py'), '--non-interactive']
if opts.dry_run and stdout: if opts.dry_run or opts.diff:
return_value = 2 # Not formatted. cmd.append('--diff')
stdout = RunCommand(cmd, cwd=top_dir)
if opts.diff:
sys.stdout.write(stdout)
if opts.dry_run and stdout:
return_value = 2 # Not formatted.
return return_value return return_value
......
...@@ -1088,7 +1088,7 @@ def PanProjectChecks(input_api, output_api, ...@@ -1088,7 +1088,7 @@ def PanProjectChecks(input_api, output_api,
def CheckPatchFormatted(input_api, output_api, check_js=False): def CheckPatchFormatted(input_api, output_api, check_js=False):
import git_cl import git_cl
cmd = ['cl', 'format', '--dry-run'] cmd = ['cl', 'format', '--dry-run', '--presubmit']
if check_js: if check_js:
cmd.append('--js') cmd.append('--js')
cmd.append(input_api.PresubmitLocalPath()) cmd.append(input_api.PresubmitLocalPath())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment