Bug 1281899 - [mozlint] Add --rev option to lint files touched by changesets draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 23 Jun 2016 10:46:41 -0400
changeset 380945 9d2ca28dba34d0afa3124ed7f0e95700bfb34fbd
parent 379746 5f95858f8ddf21ea2271a12810332efd09eff138
child 523852 9ee05a8d5bcd21e0f8cefae5e6c62ae4c37b2ce6
push id21361
push userahalberstadt@mozilla.com
push dateThu, 23 Jun 2016 19:24:11 +0000
bugs1281899
milestone50.0a1
Bug 1281899 - [mozlint] Add --rev option to lint files touched by changesets MozReview-Commit-ID: H6LEZhYPJFg
tools/lint/mach_commands.py
--- a/tools/lint/mach_commands.py
+++ b/tools/lint/mach_commands.py
@@ -11,16 +11,17 @@ import os
 import platform
 import subprocess
 import sys
 import which
 from distutils.version import LooseVersion
 
 from mozbuild.base import (
     MachCommandBase,
+    MachCommandConditions as conditions,
 )
 
 
 from mach.decorators import (
     CommandArgument,
     CommandProvider,
     Command,
     SubCommand,
@@ -65,36 +66,45 @@ Valid installation paths:
 
 @CommandProvider
 class MachCommands(MachCommandBase):
 
     @Command(
         'lint', category='devenv',
         description='Run linters.')
     @CommandArgument(
-        'paths', nargs='*', default=None,
+        'paths', nargs='*', default=[],
         help="Paths to file or directories to lint, like "
              "'browser/components/loop' or 'mobile/android'. "
              "Defaults to the current directory if not given.")
     @CommandArgument(
         '-l', '--linter', dest='linters', default=None, action='append',
         help="Linters to run, e.g 'eslint'. By default all linters are run "
              "for all the appropriate files.")
     @CommandArgument(
         '-f', '--format', dest='fmt', default='stylish',
         help="Formatter to use. Defaults to 'stylish'.")
     @CommandArgument(
         '-n', '--no-filter', dest='use_filters', default=True, action='store_false',
         help="Ignore all filtering. This is useful for quickly testing a "
              "directory that otherwise wouldn't be run, without needing to "
              "modify the config file.")
-    def lint(self, paths, linters=None, fmt='stylish', **lintargs):
+    @CommandArgument(
+        '-r', '--rev', dest='rev', default=None,
+        help="Only lint files touched by the given revision(s). This is either "
+             "forwarded to `hg log -T {files} -r` or `git diff --name-only`.")
+    def lint(self, paths, linters, fmt, rev, **lintargs):
         """Run linters."""
         from mozlint import LintRoller, formatters
 
+        if rev and conditions.is_hg(self):
+            paths.extend(subprocess.check_output(['hg', 'log', '-T={files}', '-r', rev]).split())
+        elif rev and conditions.is_git(self):
+            paths.extend(subprocess.check_output(['git', 'status', '--names-only', rev]).split())
+            pass
         paths = paths or ['.']
 
         lint_files = self.find_linters(linters)
 
         lintargs['exclude'] = ['obj*']
         lint = LintRoller(**lintargs)
         lint.read(lint_files)