Add git cl presubmit --files

Chromium's presubmits contain a lot of latent errors - presubmit errors
that will trigger the next time a file is modified, but have been
waiting for years. Flushing those out with "git cl presubmit --all"
works poorly because it exercises all presubmits simultaneously, which
is too slow and triggers too many failures. Adding a --files option lets
small areas of the tree or specific file types to be exercised in a
controlled manner.

Bug: 1311697
Change-Id: I36ec6a759a80000d6ed4a8cc218ece327d45f8d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3559174
Auto-Submit: Bruce Dawson <brucedawson@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
diff --git a/git_cl.py b/git_cl.py
index a3a9481..5751929 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1333,8 +1333,17 @@
 
     return args
 
-  def RunHook(self, committing, may_prompt, verbose, parallel, upstream,
-              description, all_files, resultdb=False, realm=None):
+  def RunHook(self,
+              committing,
+              may_prompt,
+              verbose,
+              parallel,
+              upstream,
+              description,
+              all_files,
+              files=None,
+              resultdb=False,
+              realm=None):
     """Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
     args = self._GetCommonPresubmitArgs(verbose, upstream)
     args.append('--commit' if committing else '--upload')
@@ -1344,6 +1353,9 @@
       args.append('--parallel')
     if all_files:
       args.append('--all_files')
+    if files:
+      args.extend(files.split(';'))
+      args.append('--source_controlled_only')
 
     if resultdb and not realm:
       # TODO (crbug.com/1113463): store realm somewhere and look it up so
@@ -4064,6 +4076,11 @@
                     help='Run checks even if tree is dirty')
   parser.add_option('--all', action='store_true',
                     help='Run checks against all files, not just modified ones')
+  parser.add_option('--files',
+                    nargs=1,
+                    help='Semicolon-separated list of files to be marked as '
+                    'modified when executing presubmit or post-upload hooks. '
+                    'fnmatch wildcards can also be used.')
   parser.add_option('--parallel', action='store_true',
                     help='Run all tests specified by input_api.RunTests in all '
                          'PRESUBMIT files in parallel.')
@@ -4089,16 +4106,16 @@
   else:
     description = _create_description_from_log([base_branch])
 
-  cl.RunHook(
-      committing=not options.upload,
-      may_prompt=False,
-      verbose=options.verbose,
-      parallel=options.parallel,
-      upstream=base_branch,
-      description=description,
-      all_files=options.all,
-      resultdb=options.resultdb,
-      realm=options.realm)
+  cl.RunHook(committing=not options.upload,
+             may_prompt=False,
+             verbose=options.verbose,
+             parallel=options.parallel,
+             upstream=base_branch,
+             description=description,
+             all_files=options.all,
+             files=options.files,
+             resultdb=options.resultdb,
+             realm=options.realm)
   return 0