[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
[email protected]

Change-Id: Ia86d45c7c56a9f89db34ca7175ef686589084f85
Reviewed-on: https://chromium-review.googlesource.com/569105
Commit-Queue: Ilya Sherman <[email protected]>
Reviewed-by: Dirk Pranke <[email protected]>
diff --git a/git_cl.py b/git_cl.py
index 200fa00..58605c8 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -5819,6 +5819,8 @@
                     help='Format javascript code with clang-format.')
   parser.add_option('--diff', action='store_true',
                     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)
 
   # Normalize any remaining args against the current path, so paths relative to
@@ -5961,16 +5963,21 @@
         DieWithError("gn format failed on " + gn_diff_file +
                      "\nTry running 'gn format' on this file manually.")
 
-  for xml_dir in GetDirtyMetricsDirs(diff_files):
-    tool_dir = os.path.join(top_dir, xml_dir)
-    cmd = [os.path.join(tool_dir, 'pretty_print.py'), '--non-interactive']
-    if opts.dry_run or opts.diff:
-      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.
+  # Skip the metrics formatting from the global presubmit hook. These files have
+  # a separate presubmit hook that issues an error if the files need formatting,
+  # whereas the top-level presubmit script merely issues a warning. Formatting
+  # these files is somewhat slow, so it's important not to duplicate the work.
+  if not opts.presubmit:
+    for xml_dir in GetDirtyMetricsDirs(diff_files):
+      tool_dir = os.path.join(top_dir, xml_dir)
+      cmd = [os.path.join(tool_dir, 'pretty_print.py'), '--non-interactive']
+      if opts.dry_run or opts.diff:
+        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
 
diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py
index 99aab04..de4ab43 100644
--- a/presubmit_canned_checks.py
+++ b/presubmit_canned_checks.py
@@ -1088,7 +1088,7 @@
 
 def CheckPatchFormatted(input_api, output_api, check_js=False):
   import git_cl
-  cmd = ['cl', 'format', '--dry-run']
+  cmd = ['cl', 'format', '--dry-run', '--presubmit']
   if check_js:
     cmd.append('--js')
   cmd.append(input_api.PresubmitLocalPath())