]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add lint support to 'arc svn-hook-pre-commit'.
authorepriestley <epriestley@phabricator.com>
Tue, 15 Feb 2011 22:57:24 +0000 (14:57 -0800)
committerepriestley <epriestley@phabricator.com>
Tue, 15 Feb 2011 22:57:24 +0000 (14:57 -0800)
Summary: The biggest blocker on getting rid of arc in trunk is that the lint
rules in the commit hooks are still running the old version. Push arc
commit hook support toward some reasonable state of approximately working.

Test Plan:

Reviewers:

CC:

src/lint/engine/base/ArcanistLintEngine.php
src/lint/engine/phutil/PhutilLintEngine.php
src/lint/engine/phutil/__init__.php
src/workflow/amend/ArcanistAmendWorkflow.php
src/workflow/svn-hook-pre-commit/ArcanistSvnHookPreCommitWorkflow.php
src/workflow/svn-hook-pre-commit/__init__.php
src/workingcopyidentity/ArcanistWorkingCopyIdentity.php

index d3291ab704001b0c98ce43323214f1385e135ed4..2f4071b31fe3de70bdc6de2902b4bb99f895bdc8 100644 (file)
@@ -27,6 +27,7 @@ abstract class ArcanistLintEngine {
   private $minimumSeverity = null;
 
   private $changedLines = array();
+  private $commitHookMode = false;
 
   public function __construct() {
 
@@ -59,6 +60,16 @@ abstract class ArcanistLintEngine {
     return idx($this->changedLines, $path);
   }
 
+  public function setFileData($data) {
+    $this->fileData = $data + $this->fileData;
+    return $this;
+  }
+
+  public function setCommitHookMode($mode) {
+    $this->commitHookMode = $mode;
+    return $this;
+  }
+
   protected function loadData($path) {
     if (!isset($this->fileData[$path])) {
       $disk_path = $this->getFilePathOnDisk($path);
@@ -67,6 +78,15 @@ abstract class ArcanistLintEngine {
     return $this->fileData[$path];
   }
 
+  public function pathExists($path) {
+    if ($this->getCommitHookMode()) {
+      return (idx($this->fileData, $path) !== null);
+    } else {
+      $disk_path = $this->getFilePathOnDisk($path);
+      return Filesystem::pathExists($disk_path);
+    }
+  }
+
   public function getFilePathOnDisk($path) {
     return Filesystem::resolvePath(
       $path,
@@ -79,7 +99,7 @@ abstract class ArcanistLintEngine {
   }
 
   public function getCommitHookMode() {
-    return false;
+    return $this->commitHookMode;
   }
 
   public function run() {
index 6d16e4ebf04a85bba72a52a220f40f8f443206d3..d393c8c900a11b7f9d171c3e04762152f7087202 100644 (file)
@@ -93,9 +93,4 @@ class PhutilLintEngine extends ArcanistLintEngine {
     return $linters;
   }
 
-  public function pathExists($path) {
-    $disk_path = $this->getFilePathOnDisk($path);
-    return Filesystem::pathExists($disk_path);
-  }
-
 }
index c9a4bd11ed503a123b4655ac495737dafbaf1563..77ce9ef998a7702edb77bfd579e9e3ab3f4d31f9 100644 (file)
@@ -7,14 +7,12 @@
 
 
 phutil_require_module('arcanist', 'lint/engine/base');
+phutil_require_module('arcanist', 'lint/linter/apachelicense');
 phutil_require_module('arcanist', 'lint/linter/filename');
 phutil_require_module('arcanist', 'lint/linter/generated');
 phutil_require_module('arcanist', 'lint/linter/phutilmodule');
 phutil_require_module('arcanist', 'lint/linter/text');
 phutil_require_module('arcanist', 'lint/linter/xhpast');
-phutil_require_module('arcanist', 'lint/linter/apachelicense');
-
-phutil_require_module('phutil', 'filesystem');
 
 
 phutil_require_source('PhutilLintEngine.php');
index e88010996014df659260dbfb6a57634304869261..afa5c60516470e838f9bd2496f12505c14cb6139 100644 (file)
@@ -95,6 +95,10 @@ EOTEXT
       }
     }
 
+    // TODO: The old 'arc amend' had a check here to see if you were running
+    // 'arc amend' with an explicit revision but HEAD already had another
+    // revision in it. Maybe this is worth restoring?
+
     $conduit = $this->getConduit();
     $message = $conduit->callMethodSynchronous(
       'differential.getcommitmessage',
index 02ccdcb64117779b0892583d4dbe091168953fde..13d14289a5d090c7d4cf43c505b08f4f8e1a16a3 100644 (file)
@@ -20,7 +20,7 @@ class ArcanistSvnHookPreCommitWorkflow extends ArcanistBaseWorkflow {
 
   public function getCommandHelp() {
     return phutil_console_format(<<<EOTEXT
-      **svn-hook-pre-receive** __repository__ __transaction__
+      **svn-hook-pre-commit** __repository__ __transaction__
           Supports: svn
           You can install this as an SVN pre-commit hook.
 EOTEXT
@@ -48,8 +48,12 @@ EOTEXT
       $transaction,
       $repository);
 
+    if (strpos($commit_message, '@bypass-lint') !== false) {
+      return 0;
+    }
+
+
     // TODO: Do stuff with commit message.
-    var_dump($commit_message);
 
     list($changed) = execx(
       'svnlook changed --transaction %s %s',
@@ -119,6 +123,7 @@ EOTEXT
       }
     }
 
+
     if ($failed && $resolved) {
       $failed_paths = '        '.implode("\n        ", $failed);
       $resolved_paths = '        '.implode("\n        ", array_keys($resolved));
@@ -134,7 +139,7 @@ EOTEXT
 
     if (!$resolved) {
       // None of the affected paths are beneath a .arcconfig file.
-      return 3;
+      return 0;
     }
 
     $groups = array();
@@ -155,11 +160,19 @@ EOTEXT
         $message);
     }
 
-    $project_root = key($groups);
+    $config_file = key($groups);
+    $project_root = dirname($config_file);
     $paths = reset($groups);
 
+    list($config) = execx(
+      'svnlook cat --transaction %s %s %s',
+      $transaction,
+      $repository,
+      $config_file);
+
     $data = array();
     foreach ($paths as $path) {
+      // TODO: This should be done in parallel.
       list($err, $filedata) = exec_manual(
         'svnlook cat --transaction %s %s %s',
         $transaction,
@@ -168,9 +181,51 @@ EOTEXT
       $data[$path] = $err ? null : $filedata;
     }
 
-    // TODO: Do stuff with data.
-    var_dump($data);
+    $working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile(
+      $project_root,
+      $config);
+
+    $lint_engine = $working_copy->getConfig('lint_engine');
+    if (!$lint_engine) {
+      return 0;
+    }
+
+    PhutilSymbolLoader::loadClass($lint_engine);
+
+    $engine = newv($lint_engine, array());
+    $engine->setWorkingCopy($working_copy);
+    $engine->setMinimumSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
+    $engine->setPaths(array_keys($data));
+    $engine->setFileData($data);
+    $engine->setCommitHookMode(true);
+
+    $results = $engine->run();
+
+    $renderer = new ArcanistLintRenderer();
+    $failures = array();
+    foreach ($results as $result) {
+      if (!$result->getMessages()) {
+        continue;
+      }
+      $failures[] = $result;
+    }
+
+    if ($failures) {
+      $at = "@";
+      $msg = phutil_console_format(
+        "\n**LINT ERRORS**\n\n".
+        "This changeset has lint errors. You must fix all lint errors before ".
+        "you can commit.\n\n".
+        "You can add '{$at}bypass-lint' to your commit message to disable ".
+        "lint checks for this commit, or '{$at}nolint' to the file with ".
+        "errors to disable lint for that file.\n\n");
+      echo phutil_console_wrap($msg);
+      foreach ($failures as $result) {
+        echo $renderer->renderLintResult($result);
+      }
+      return 1;
+    }
 
-    return 1;
+    return 99;
   }
 }
index e3f69614caac9594390b3a4a680517973fbde925..f80fe11c0a0cc757838ff4fae3c9733cef08c16b 100644 (file)
@@ -7,10 +7,15 @@
 
 
 phutil_require_module('arcanist', 'exception/usage');
+phutil_require_module('arcanist', 'lint/renderer');
+phutil_require_module('arcanist', 'lint/severity');
 phutil_require_module('arcanist', 'workflow/base');
+phutil_require_module('arcanist', 'workingcopyidentity');
 
 phutil_require_module('phutil', 'console');
 phutil_require_module('phutil', 'future/exec');
+phutil_require_module('phutil', 'symbols');
+phutil_require_module('phutil', 'utils');
 
 
 phutil_require_source('ArcanistSvnHookPreCommitWorkflow.php');
index 2d8fd2e9c1fabd27a0976428ded92357705c935a..fbde4ff699f38593f35047e883f5920810f61d7a 100644 (file)
@@ -31,29 +31,38 @@ class ArcanistWorkingCopyIdentity {
         continue;
       }
       $proj_raw = Filesystem::readFile($config_file);
-      $proj = json_decode($proj_raw, true);
-      if (!is_array($proj)) {
-        throw new Exception(
-          "Unable to parse '.arcconfig' file in '{$dir}'. The file contents ".
-          "should be valid JSON.");
-      }
-      $required_keys = array(
-        'project_id',
-      );
-      foreach ($required_keys as $key) {
-        if (!array_key_exists($key, $proj)) {
-          throw new Exception(
-            "Required key '{$key}' is missing from '.arcconfig' file in ".
-            "'{$dir}'.");
-        }
-      }
-      $config = $proj;
+      $config = self::parseRawConfigFile($proj_raw);
       $project_root = $dir;
       break;
     }
     return new ArcanistWorkingCopyIdentity($project_root, $config);
   }
 
+  public static function newFromRootAndConfigFile($root, $config_raw) {
+    $config = self::parseRawConfigFile($config_raw);
+    return new ArcanistWorkingCopyIdentity($root, $config);
+  }
+
+  private static function parseRawConfigFile($raw_config) {
+    $proj = json_decode($raw_config, true);
+    if (!is_array($proj)) {
+      throw new Exception(
+        "Unable to parse '.arcconfig' file in '{$dir}'. The file contents ".
+        "should be valid JSON.");
+    }
+    $required_keys = array(
+      'project_id',
+    );
+    foreach ($required_keys as $key) {
+      if (!array_key_exists($key, $proj)) {
+        throw new Exception(
+          "Required key '{$key}' is missing from '.arcconfig' file in ".
+          "'{$dir}'.");
+      }
+    }
+    return $proj;
+  }
+
   protected function __construct($root, array $config) {
     $this->projectRoot    = $root;
     $this->projectConfig  = $config;