]> khamidou — GitHub mirrors - arcanist.git/commitdiff
[arc svn-hook-pre-commit] Access working copy
authorKunal Bhalla <kunalb@fb.com>
Wed, 21 Dec 2011 04:26:05 +0000 (20:26 -0800)
committerKunal Bhalla <kunalb@fb.com>
Thu, 29 Dec 2011 22:28:50 +0000 (14:28 -0800)
Summary:
Creates a new hook API that can be used to interface with
SVN/Git/Mercurial in the context of a commit hook. Currently only adds a
function to read the modified file data in a Subversion commit hook.

An object of this API is created in the SvnHookPreCommitWorkflow and
passed on the Lint Engine which then uses it to access current file
data, of the way the APIs seem to be structured); linters use the
getData function which is essentially a wrapper around the engine's
call, with another layer of caching.

Task ID: #770556

Blame Rev:

Test Plan:
- Create a local svn repository and add a minimal hook to run the local
  version of arc to test commits

(http://phabricator.com/docs/arcanist/article/Installing_Arcanist_SVN_Hooks.html)
- Create a temporary repository that can trigger any of the linters
  available, and test against a temporary linter by committing against
  the test repository: the linter should be able to access all required
  files by using loadData/getData in the LintEngine and Linter.

Revert Plan:

Tags: lint, svn-hook-pre-commit

Reviewers: jungejason, asukhachev, epriestley, aran

Reviewed By: epriestley

CC: aran, jungejason, epriestley, kunalb, asukhachev
Differential Revision: https://secure.phabricator.com/D1256

src/__phutil_library_map__.php
src/lint/engine/base/ArcanistLintEngine.php
src/lint/linter/base/ArcanistLinter.php
src/lint/linter/base/__init__.php
src/repository/hookapi/base/ArcanistHookAPI.php [new file with mode: 0644]
src/repository/hookapi/base/__init__.php [new file with mode: 0644]
src/repository/hookapi/subversion/ArcanistSubversionHookAPI.php [new file with mode: 0644]
src/repository/hookapi/subversion/__init__.php [new file with mode: 0644]
src/workflow/svn-hook-pre-commit/ArcanistSvnHookPreCommitWorkflow.php
src/workflow/svn-hook-pre-commit/__init__.php

index bf97f652c59e6250a9bb49cdabf6fea104ca20a3..7a620a74d6e2a66b1e579ae5dea99443f2e217f4 100644 (file)
@@ -42,6 +42,7 @@ phutil_register_library_map(array(
     'ArcanistGitAPI' => 'repository/api/git',
     'ArcanistGitHookPreReceiveWorkflow' => 'workflow/git-hook-pre-receive',
     'ArcanistHelpWorkflow' => 'workflow/help',
+    'ArcanistHookAPI' => 'repository/hookapi/base',
     'ArcanistInstallCertificateWorkflow' => 'workflow/install-certificate',
     'ArcanistLiberateLintEngine' => 'lint/engine/liberate',
     'ArcanistLiberateWorkflow' => 'workflow/liberate',
@@ -78,6 +79,7 @@ phutil_register_library_map(array(
     'ArcanistRepositoryAPI' => 'repository/api/base',
     'ArcanistShellCompleteWorkflow' => 'workflow/shell-complete',
     'ArcanistSubversionAPI' => 'repository/api/subversion',
+    'ArcanistSubversionHookAPI' => 'repository/hookapi/subversion',
     'ArcanistSvnHookPreCommitWorkflow' => 'workflow/svn-hook-pre-commit',
     'ArcanistTextLinter' => 'lint/linter/text',
     'ArcanistTextLinterTestCase' => 'lint/linter/text/__tests__',
@@ -145,6 +147,7 @@ phutil_register_library_map(array(
     'ArcanistPyLintLinter' => 'ArcanistLinter',
     'ArcanistShellCompleteWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistSubversionAPI' => 'ArcanistRepositoryAPI',
+    'ArcanistSubversionHookAPI' => 'ArcanistHookAPI',
     'ArcanistSvnHookPreCommitWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistTextLinter' => 'ArcanistLinter',
     'ArcanistTextLinterTestCase' => 'ArcanistLinterTestCase',
index 308cdd50f5dff5ad2d2fd3a14690519657ce2638..1db0007c87350758eae11b15c4d8e0e024532bb7 100644 (file)
@@ -69,6 +69,7 @@ abstract class ArcanistLintEngine {
 
   private $changedLines = array();
   private $commitHookMode = false;
+  private $hookAPI;
 
   public function __construct() {
 
@@ -115,10 +116,23 @@ abstract class ArcanistLintEngine {
     return $this;
   }
 
-  protected function loadData($path) {
+  public function setHookAPI(ArcanistHookAPI $hook_api) {
+    $this->hookAPI  = $hook_api;
+  }
+
+  public function getHookAPI() {
+    return $this->hookAPI;
+  }
+
+  public function loadData($path) {
     if (!isset($this->fileData[$path])) {
-      $disk_path = $this->getFilePathOnDisk($path);
-      $this->fileData[$path] = Filesystem::readFile($disk_path);
+      if ($this->getCommitHookMode()) {
+        $this->fileData[$path] = $this->getHookAPI()
+          ->getCurrentFileData($path);
+      } else {
+        $disk_path = $this->getFilePathOnDisk($path);
+        $this->fileData[$path] = Filesystem::readFile($disk_path);
+      }
     }
     return $this->fileData[$path];
   }
index 67bfd6c091fd58392aebda74fedb41a753862859..638085d84076a432b373fc2a4de2c27898b4b52c 100644 (file)
@@ -67,12 +67,7 @@ abstract class ArcanistLinter {
 
   protected function getData($path) {
     if (!array_key_exists($path, $this->data)) {
-      $disk_path = $this->getEngine()->getFilePathOnDisk($path);
-      if ($disk_path) {
-        $this->data[$path] = Filesystem::readFile($disk_path);
-      } else {
-        throw new Exception("Data is not provided for path '{$path}'!");
-      }
+      $this->data[$path] = $this->getEngine()->loadData($path);
     }
     return $this->data[$path];
   }
index 1b364bf590758e63ecc8801daafcc9172c1f8fe8..ade0834a16af86cb34d4173edb6ec589aaedfa6b 100644 (file)
@@ -9,7 +9,5 @@
 phutil_require_module('arcanist', 'lint/message');
 phutil_require_module('arcanist', 'lint/severity');
 
-phutil_require_module('phutil', 'filesystem');
-
 
 phutil_require_source('ArcanistLinter.php');
diff --git a/src/repository/hookapi/base/ArcanistHookAPI.php b/src/repository/hookapi/base/ArcanistHookAPI.php
new file mode 100644 (file)
index 0000000..0fcd2b6
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * API while running in the context of a commit hook
+ */
+abstract class ArcanistHookAPI {
+  abstract public function getCurrentFileData($path);
+}
diff --git a/src/repository/hookapi/base/__init__.php b/src/repository/hookapi/base/__init__.php
new file mode 100644 (file)
index 0000000..99b5e54
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+phutil_require_source('ArcanistHookAPI.php');
diff --git a/src/repository/hookapi/subversion/ArcanistSubversionHookAPI.php b/src/repository/hookapi/subversion/ArcanistSubversionHookAPI.php
new file mode 100644 (file)
index 0000000..92dc7a3
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Interfaces with Subversion while running as a commit hook.
+ */
+class ArcanistSubversionHookAPI extends ArcanistHookAPI {
+
+  protected $root;
+  protected $transaction;
+  protected $repository;
+
+  public function __construct($root, $transaction, $repository) {
+    $this->root        = $root;
+    $this->transaction = $transaction;
+    $this->repository  = $repository;
+  }
+
+  public function getCurrentFileData($path) {
+    list($err, $file) = exec_manual(
+      'svnlook cat --transaction %s %s %s',
+      $this->transaction,
+      $this->repository,
+      $this->root . "/$path");
+
+    return ($err? null : $file);
+  }
+}
diff --git a/src/repository/hookapi/subversion/__init__.php b/src/repository/hookapi/subversion/__init__.php
new file mode 100644 (file)
index 0000000..23bc535
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'repository/hookapi/base');
+
+phutil_require_module('phutil', 'future/exec');
+
+
+phutil_require_source('ArcanistSubversionHookAPI.php');
index 5325c922de3e8244dd59e6deda9c94bb36aa1122..1c3b6545af95616acec3cad3ebbeeaa9d51004ae 100644 (file)
@@ -131,7 +131,6 @@ EOTEXT
       }
     }
 
-
     if ($failed && $resolved) {
       $failed_paths = '        '.implode("\n        ", $failed);
       $resolved_paths = '        '.implode("\n        ", array_keys($resolved));
@@ -178,22 +177,16 @@ EOTEXT
       $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,
-        $repository,
-        $path);
-      $data[$path] = $err ? null : $filedata;
-    }
-
     $working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile(
       $project_root,
       $config,
       $config_file." (svnlook: {$transaction} {$repository})");
 
+    $repository_api = new ArcanistSubversionHookAPI(
+      $project_root,
+      $transaction,
+      $repository);
+
     $lint_engine = $working_copy->getConfig('lint_engine');
     if (!$lint_engine) {
       return 0;
@@ -204,9 +197,9 @@ EOTEXT
     $engine = newv($lint_engine, array());
     $engine->setWorkingCopy($working_copy);
     $engine->setMinimumSeverity(ArcanistLintSeverity::SEVERITY_ERROR);
-    $engine->setPaths(array_keys($data));
-    $engine->setFileData($data);
+    $engine->setPaths($paths);
     $engine->setCommitHookMode(true);
+    $engine->setHookAPI($repository_api);
 
     try {
       $results = $engine->run();
index f80fe11c0a0cc757838ff4fae3c9733cef08c16b..1dacc48cfadca5fd991168f5df7873c0a05cc5cb 100644 (file)
@@ -9,6 +9,7 @@
 phutil_require_module('arcanist', 'exception/usage');
 phutil_require_module('arcanist', 'lint/renderer');
 phutil_require_module('arcanist', 'lint/severity');
+phutil_require_module('arcanist', 'repository/hookapi/subversion');
 phutil_require_module('arcanist', 'workflow/base');
 phutil_require_module('arcanist', 'workingcopyidentity');