]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add arc browse workflow
authorAviv Eyal <avivey@gmail.com>
Sun, 9 Dec 2012 22:09:35 +0000 (14:09 -0800)
committerepriestley <git@epriestley.com>
Sun, 9 Dec 2012 22:10:21 +0000 (14:10 -0800)
Summary:
arc browse will open a browser to the Diffusion view of a file. Convenient if you like
Diffusion for reading source. Naturally, it fixes relative filenames.

Combined with git-grep it can be an easy replacement for server-side search functions.

Test Plan:
use feature with and without 'browser' configured.

I've only tested this on Linux, because that's all I have right now, but the principle is sound.

Reviewers: epriestley, vrana

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D4127

src/__phutil_library_map__.php
src/configuration/ArcanistSettings.php
src/workflow/ArcanistBrowseWorkflow.php [new file with mode: 0644]

index 53f297db59a251048370a2f79b01414f5072af8d..33e38b8da94522a685ead262a6883884b7362973 100644 (file)
@@ -21,6 +21,7 @@ phutil_register_library_map(array(
     'ArcanistBaseUnitTestEngine' => 'unit/engine/ArcanistBaseUnitTestEngine.php',
     'ArcanistBaseWorkflow' => 'workflow/ArcanistBaseWorkflow.php',
     'ArcanistBranchWorkflow' => 'workflow/ArcanistBranchWorkflow.php',
+    'ArcanistBrowseWorkflow' => 'workflow/ArcanistBrowseWorkflow.php',
     'ArcanistBundle' => 'parser/ArcanistBundle.php',
     'ArcanistBundleTestCase' => 'parser/__tests__/ArcanistBundleTestCase.php',
     'ArcanistCallConduitWorkflow' => 'workflow/ArcanistCallConduitWorkflow.php',
@@ -157,6 +158,7 @@ phutil_register_library_map(array(
     'ArcanistArcanistLinterTestCase' => 'ArcanistLinterTestCase',
     'ArcanistBaseCommitParserTestCase' => 'ArcanistTestCase',
     'ArcanistBranchWorkflow' => 'ArcanistBaseWorkflow',
+    'ArcanistBrowseWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistBundleTestCase' => 'ArcanistTestCase',
     'ArcanistCallConduitWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistCapabilityNotSupportedException' => 'Exception',
index e931c7e10d89695fa9ac2abcac7fa84ab9bf8602..c8ecb2e6c9696ec159b1117056f948a9425c474f 100644 (file)
@@ -71,6 +71,12 @@ final class ArcanistSettings {
           "'vim'. This setting overrides the EDITOR environmental variable.",
         'example' => '"nano"',
       ),
+      'browser' => array(
+        'type' => 'string',
+        'help' =>
+          "Command to use to invoke a web browser.",
+        'example' => '"gnome-www-browser"',
+      ),
       'events.listeners' => array(
         'type' => 'list',
         'help' => 'List of event listener classes to install at startup.',
diff --git a/src/workflow/ArcanistBrowseWorkflow.php b/src/workflow/ArcanistBrowseWorkflow.php
new file mode 100644 (file)
index 0000000..584d53f
--- /dev/null
@@ -0,0 +1,154 @@
+<?php
+
+/*
+ * Copyright 2012 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.
+ */
+
+/**
+ * Browse file in Diffusion.
+ *
+ * @group workflow
+ */
+final class ArcanistBrowseWorkflow extends ArcanistBaseWorkflow {
+
+  public function getWorkflowName() {
+    return 'browse';
+  }
+
+  public function getCommandSynopses() {
+    return phutil_console_format(<<<EOTEXT
+      **browse** [__options__] __path__
+EOTEXT
+      );
+  }
+
+  public function getCommandHelp() {
+    return phutil_console_format(<<<EOTEXT
+          Supports: git
+          Browse file in Diffusion (Web interface).
+
+          Set the 'browser' value using 'arc set-config' to select a browser. If
+          no browser is set, the command will try to guess which browser to use.
+EOTEXT
+      );
+  }
+
+  public function getArguments() {
+    return array(
+      'branch' => array(
+        'param' => 'branch_name',
+        'help' =>
+          "Select branch name to view (On server). Defaults to 'master'."
+      ),
+      '*' => 'paths',
+    );
+  }
+
+  public function requiresWorkingCopy() {
+    return true;
+  }
+
+  public function requiresConduit() {
+    return true;
+  }
+
+  public function requiresAuthentication() {
+    return true;
+  }
+
+  public function requiresRepositoryAPI() {
+    return true;
+  }
+
+  public function run() {
+    $repository_api = $this->getRepositoryAPI();
+    $project_root = $this->getWorkingCopy()->getProjectRoot();
+
+    $in_paths = $this->getArgument('paths');
+    $paths = array();
+    foreach ($in_paths as $key => $path) {
+      $full_path = Filesystem::resolvePath($path);
+
+      $paths[$key] = Filesystem::readablePath(
+        $full_path,
+        $project_root);
+    }
+
+    if (!$paths) {
+      throw new ArcanistUsageException("Specify a path to browse");
+    }
+
+    $base_uri = $this->getBaseURI();
+    $browser = $this->getBrowserCommand();
+
+    foreach ($paths as $path) {
+      $ret_code = phutil_passthru("%s %s", $browser, $base_uri . $path );
+      if ($ret_code) {
+        throw new ArcanistUsageException(
+          "It seems we failed to open the browser; Perhaps you should try to ".
+          "set the 'browser' config option. The command we tried to use was: ".
+          $browser);
+      }
+    }
+
+    return 0;
+  }
+
+  private function getBaseURI() {
+    $conduit = $this->getConduit();
+    $project_id = $this->getWorkingCopy()->getProjectID();
+    $project_info = $this->getConduit()->callMethodSynchronous(
+      'arcanist.projectinfo',
+      array(
+        'name' => $project_id,
+      ));
+
+    $repo_phid = $project_info['repositoryPHID'];
+    $repo_info = $this->getConduit()->callMethodSynchronous(
+      'phid.query',
+      array(
+        'phids' => array($repo_phid),
+      ));
+
+    $branch = $this->getArgument('branch', 'master');
+
+    return $repo_info[$repo_phid]['uri'] . 'browse/' . $branch .'/';
+  }
+
+  private function getBrowserCommand() {
+    $config = $this->getWorkingCopy()->getConfigFromAnySource('browser');
+    if ($config) {
+      return $config;
+    }
+
+    if (phutil_is_windows()) {
+      return "start";
+    }
+
+    $candidates = array("sensible-browser", "xdg-open", "open");
+    // on many Linuxes, "open" exists and is not the right program.
+
+    foreach ($candidates as $cmd) {
+      list($ret_code) = exec_manual("which %s", $cmd);
+      if ($ret_code == 0) {
+        return $cmd;
+      }
+    }
+
+    throw new ArcanistUsageException(
+      "Could not find a browser to run; Try setting the 'browser' option " .
+      "using arc set-config.");
+  }
+}