]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add ArcanistLintSeverity::SEVERITY_AUTOFIX.
authorBen Gertzfield <beng@fb.com>
Fri, 6 Apr 2012 19:23:19 +0000 (12:23 -0700)
committerBen Gertzfield <beng@fb.com>
Tue, 10 Apr 2012 19:42:09 +0000 (12:42 -0700)
Summary:
Xcode (a popular code editor on Mac OS X) has no facility
to trim trailing whitespace automatically.

This adds a new lint severity "AUTOFIX" that's between
WARNING and ERROR. When running the linter, any lint message
whose severity is AUTOFIX will automatically be patched.

Furthermore, if all lint messages returned from the engine are
AUTOFIX, we'll automatically amend HEAD with the patch.

Test Plan:
arc lint on files with and without trailing whitespace,
with and without UTF-8 contents to confirm those still error

Reviewers: epriestley, jungejason

Reviewed By: epriestley

CC: aran
Differential Revision: https://secure.phabricator.com/D2125

src/lint/linter/text/ArcanistTextLinter.php
src/lint/message/ArcanistLintMessage.php
src/lint/renderer/ArcanistLintRenderer.php
src/lint/result/ArcanistLintResult.php
src/lint/severity/ArcanistLintSeverity.php
src/workflow/diff/ArcanistDiffWorkflow.php
src/workflow/lint/ArcanistLintWorkflow.php

index 8326af264c52240ea342c7ada4dd7192becf0c72..882a14be87e7d3b805be82d21a84e1252ae395d2 100644 (file)
@@ -49,6 +49,7 @@ final class ArcanistTextLinter extends ArcanistLinter {
   public function getLintSeverityMap() {
     return array(
       self::LINT_LINE_WRAP => ArcanistLintSeverity::SEVERITY_WARNING,
+      self::LINT_TRAILING_WHITESPACE => ArcanistLintSeverity::SEVERITY_AUTOFIX,
     );
   }
 
index bd25f1015b614f4eac2fe44007a6b2d75696618d..4b997e09063b4188a9e81566461e8528a426e703 100644 (file)
@@ -144,6 +144,10 @@ final class ArcanistLintMessage {
     return $this->getSeverity() == ArcanistLintSeverity::SEVERITY_WARNING;
   }
 
+  public function isAutofix() {
+    return $this->getSeverity() == ArcanistLintSeverity::SEVERITY_AUTOFIX;
+  }
+
   public function hasFileContext() {
     return ($this->getLine() !== null);
   }
index baa1e11677418a0a074e27940ddfd27ff6a3b82a..c4a7d702b9775c302e6e1fd73813ee6cb3fee077 100644 (file)
  * @group lint
  */
 final class ArcanistLintRenderer {
+  private $showAutofixPatches = false;
+
+  public function setShowAutofixPatches($show_autofix_patches) {
+    $this->showAutofixPatches = $show_autofix_patches;
+    return $this;
+  }
+
   public function renderLintResult(ArcanistLintResult $result) {
     $messages = $result->getMessages();
     $path = $result->getPath();
@@ -29,9 +36,12 @@ final class ArcanistLintRenderer {
     $lines = explode("\n", $result->getData());
 
     $text = array();
-    $text[] = phutil_console_format('**>>>** Lint for __%s__:', $path);
-    $text[] = null;
+
     foreach ($messages as $message) {
+      if (!$this->showAutofixPatches && $message->isAutofix()) {
+        continue;
+      }
+
       if ($message->isError()) {
         $color = 'red';
       } else {
@@ -55,10 +65,13 @@ final class ArcanistLintRenderer {
         $text[] = $this->renderContext($message, $lines);
       }
     }
-    $text[] = null;
-    $text[] = null;
 
-    return implode("\n", $text);
+    if ($text) {
+      $prefix = phutil_console_format("**>>>** Lint for __%s__:\n\n\n", $path);
+      return $prefix . implode("\n", $text);
+    } else {
+      return null;
+    }
   }
 
   protected function renderContext(
index 61e0e2b916c6b163a3776ca59d4ca6d980ce48db..5f40ffa4e7d0fdca5d53414c3134914b240f17d9 100644 (file)
@@ -79,6 +79,15 @@ final class ArcanistLintResult {
     return false;
   }
 
+  public function isAllAutofix() {
+    foreach ($this->messages as $message) {
+      if (!$message->isAutofix()) {
+        return false;
+      }
+    }
+    return true;
+  }
+
   private function sortAndFilterMessages() {
     $messages = $this->messages;
 
index f4d3191b83519c23f174be9f6525d61315fe4b50..83ba9c5be42f3438131c4ed564a42ab596774ad4 100644 (file)
@@ -24,6 +24,7 @@
 final class ArcanistLintSeverity {
 
   const SEVERITY_ADVICE       = 'advice';
+  const SEVERITY_AUTOFIX      = 'autofix';
   const SEVERITY_WARNING      = 'warning';
   const SEVERITY_ERROR        = 'error';
   const SEVERITY_DISABLED     = 'disabled';
@@ -31,6 +32,7 @@ final class ArcanistLintSeverity {
   public static function getStringForSeverity($severity_code) {
     static $map = array(
       self::SEVERITY_ADVICE   => 'Advice',
+      self::SEVERITY_AUTOFIX  => 'Auto-Fix',
       self::SEVERITY_WARNING  => 'Warning',
       self::SEVERITY_ERROR    => 'Error',
       self::SEVERITY_DISABLED => 'Disabled',
@@ -50,6 +52,7 @@ final class ArcanistLintSeverity {
     static $map = array(
       self::SEVERITY_DISABLED => 10,
       self::SEVERITY_ADVICE   => 20,
+      self::SEVERITY_AUTOFIX  => 25,
       self::SEVERITY_WARNING  => 30,
       self::SEVERITY_ERROR    => 40,
     );
index 67dddc604eb8f85b0ff78f58c5b24dce5372f084..89de96fd41b2e76b29e7fd05549fd4b0b69424c4 100644 (file)
@@ -276,6 +276,22 @@ EOTEXT
           'lint' => true,
         ),
       ),
+      'amend-all' => array(
+        'help' =>
+          'When linting git repositories, amend HEAD with all patches '.
+          'suggested by lint without prompting.',
+        'passthru' => array(
+          'lint' => true,
+        ),
+      ),
+      'amend-autofixes' => array(
+        'help' =>
+          'When linting git repositories, amend HEAD with autofix '.
+          'patches suggested by lint without prompting.',
+        'passthru' => array(
+          'lint' => true,
+        ),
+      ),
       'json' => array(
         'help' =>
           'Emit machine-readable JSON. EXPERIMENTAL! Probably does not work!',
index b53c87ba82d28cc01945c2babd5f74b6c65171ae..2ebc656d24ecda510050fb8465bfbf12b20cfb4d 100644 (file)
@@ -30,12 +30,24 @@ class ArcanistLintWorkflow extends ArcanistBaseWorkflow {
 
   private $unresolvedMessages;
   private $shouldAmendChanges = false;
+  private $shouldAmendWithoutPrompt = false;
+  private $shouldAmendAutofixesWithoutPrompt = false;
 
   public function setShouldAmendChanges($should_amend) {
     $this->shouldAmendChanges = $should_amend;
     return $this;
   }
 
+  public function setShouldAmendWithoutPrompt($should_amend) {
+    $this->shouldAmendWithoutPrompt = $should_amend;
+    return $this;
+  }
+
+  public function setShouldAmendAutofixesWithoutPrompt($should_amend) {
+    $this->shouldAmendAutofixesWithoutPrompt = $should_amend;
+    return $this;
+  }
+
   public function getCommandSynopses() {
     return phutil_console_format(<<<EOTEXT
       **lint** [__options__] [__paths__]
@@ -100,6 +112,16 @@ EOTEXT
           'apply-patches' => true,
         ),
       ),
+      'amend-all' => array(
+        'help' =>
+        'When linting git repositories, amend HEAD with all patches '.
+        'suggested by lint without prompting.',
+      ),
+      'amend-autofixes' => array(
+        'help' =>
+        'When linting git repositories, amend HEAD with autofix '.
+        'patches suggested by lint without prompting.',
+      ),
       '*' => 'paths',
     );
   }
@@ -155,7 +177,7 @@ EOTEXT
     if ($this->getArgument('advice')) {
       $engine->setMinimumSeverity(ArcanistLintSeverity::SEVERITY_ADVICE);
     } else {
-      $engine->setMinimumSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
+      $engine->setMinimumSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX);
     }
 
     // Propagate information about which lines changed to the lint engine.
@@ -186,6 +208,19 @@ EOTEXT
       $prompt_patches = true;
     }
 
+    if ($this->getArgument('amend-all')) {
+      $this->shouldAmendChanges = true;
+      $this->shouldAmendWithoutPrompt = true;
+    }
+
+    if ($this->getArgument('amend-autofixes')) {
+      $prompt_autofix_patches = false;
+      $this->shouldAmendChanges = true;
+      $this->shouldAmendAutofixesWithoutPrompt = true;
+    } else {
+      $prompt_autofix_patches = true;
+    }
+
     $wrote_to_disk = false;
 
     switch ($this->getArgument('output')) {
@@ -204,22 +239,35 @@ EOTEXT
         break;
       default:
         $renderer = new ArcanistLintRenderer();
+        $renderer->setShowAutofixPatches($prompt_autofix_patches);
         break;
     }
 
+    $all_autofix = true;
+
     foreach ($results as $result) {
-      if (!$result->getMessages()) {
+      $result_all_autofix = $result->isAllAutofix();
+
+      if (!$result->getMessages() && !$result_all_autofix) {
         continue;
       }
 
-      echo $renderer->renderLintResult($result);
+      if (!$result_all_autofix) {
+        $all_autofix = false;
+      }
+
+      $lint_result = $renderer->renderLintResult($result);
+      if ($lint_result) {
+        echo $lint_result;
+      }
 
       if ($apply_patches && $result->isPatchable()) {
         $patcher = ArcanistLintPatcher::newFromArcanistLintResult($result);
         $old = $patcher->getUnmodifiedFileContent();
         $new = $patcher->getModifiedFileContent();
 
-        if ($prompt_patches) {
+        if ($prompt_patches &&
+            !($result_all_autofix && !$prompt_autofix_patches)) {
           $old_file = $result->getFilePathOnDisk();
           if (!Filesystem::pathExists($old_file)) {
             $old_file = '/dev/null';
@@ -248,7 +296,17 @@ EOTEXT
     if ($wrote_to_disk &&
         ($repository_api instanceof ArcanistGitAPI) &&
         $this->shouldAmendChanges) {
-      $amend = phutil_console_confirm("Amend HEAD with lint patches?");
+
+      if ($this->shouldAmendWithoutPrompt ||
+          ($this->shouldAmendAutofixesWithoutPrompt && $all_autofix)) {
+        echo phutil_console_format(
+          "<bg:yellow>** LINT NOTICE **</bg> Automatically amending HEAD ".
+          "with lint patches.\n");
+        $amend = true;
+      } else {
+        $amend = phutil_console_confirm("Amend HEAD with lint patches?");
+      }
+
       if ($amend) {
         execx(
           '(cd %s; git commit -a --amend -C HEAD)',