]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a --browse flag to `arc diff`, to open objects in a browser after creating them
authorepriestley <git@epriestley.com>
Mon, 4 Aug 2014 19:03:03 +0000 (12:03 -0700)
committerepriestley <git@epriestley.com>
Mon, 4 Aug 2014 19:03:03 +0000 (12:03 -0700)
Summary: Ref T5781. Add a flag to optionally open a web browser after creating a diff or revision.

Test Plan: Created //this revision// with `arc diff --browse` // !!! //

Reviewers: csilvers, btrahan

Reviewed By: btrahan

Subscribers: epriestley, spicyj

Maniphest Tasks: T5781

Differential Revision: https://secure.phabricator.com/D10141

src/workflow/ArcanistBrowseWorkflow.php
src/workflow/ArcanistDiffWorkflow.php
src/workflow/ArcanistWorkflow.php

index 725f64974d0d828043f60be202713e331115a527..6d7e65fbf37118bac39b528c90c4f002599ae242 100644 (file)
@@ -144,16 +144,7 @@ EOTEXT
     }
 
     if ($uris) {
-      $browser = $this->getBrowserCommand();
-      foreach ($uris as $uri) {
-        $err = phutil_passthru('%s %s', $browser, $uri);
-        if ($err) {
-          throw new ArcanistUsageException(
-            pht(
-              "Failed to execute browser ('%s'). Check your 'browser' config ".
-              "option."));
-        }
-      }
+      $this->openURIsInBrowser($uris);
     }
 
     return 0;
@@ -174,33 +165,4 @@ EOTEXT
     return $repo_uri.'browse/'.$branch.'/';
   }
 
-  private function getBrowserCommand() {
-    $config = $this->getConfigFromAnySource('browser');
-    if ($config) {
-      return $config;
-    }
-
-    if (phutil_is_windows()) {
-      return 'start';
-    }
-
-    $candidates = array('sensible-browser', 'xdg-open', 'open');
-
-    // NOTE: The "open" command works well on OS X, but on many Linuxes "open"
-    // exists and is not a browser. For now, we're just looking for other
-    // commands first, but we might want to be smarter about selecting "open"
-    // only on OS X.
-
-    foreach ($candidates as $cmd) {
-      if (Filesystem::binaryExists($cmd)) {
-        return $cmd;
-      }
-    }
-
-    throw new ArcanistUsageException(
-      pht(
-        "Unable to find a browser command to run. Set 'browser' in your ".
-        "arc config to specify one."));
-  }
-
 }
index 1f688af3f827d5642607c3c6032215df76b1d2d7..9130a9eb22f44a7913b25eaf77d32d373ef33225 100644 (file)
@@ -392,6 +392,10 @@ EOTEXT
           'unit' => true,
         ),
       ),
+      'browse' => array(
+        'help' => pht(
+          'After creating a diff or revision, open it in a web browser.'),
+      ),
       '*' => 'paths',
       'head' => array(
         'param' => 'commit',
@@ -570,6 +574,10 @@ EOTEXT
         ))."\n";
         ob_start();
       }
+
+      if ($this->shouldOpenCreatedObjectsInBrowser()) {
+        $this->openURIsInBrowser(array($diff_info['uri']));
+      }
     } else {
       $revision['diffid'] = $this->getDiffID();
 
@@ -626,6 +634,10 @@ EOTEXT
           ));
         echo "Planned changes to the revision.\n";
       }
+
+      if ($this->shouldOpenCreatedObjectsInBrowser()) {
+        $this->openURIsInBrowser(array($uri));
+      }
     }
 
     echo "Included changes:\n";
@@ -2623,4 +2635,8 @@ EOTEXT
     return Filesystem::getMimeType($tmp);
   }
 
+  private function shouldOpenCreatedObjectsInBrowser() {
+    return $this->getArgument('browse');
+  }
+
 }
index ed9fc24ac5ae45afb5d1a5b75ab692be9e62df8e..8e7cc701620ecaf0eaf331c2e21a17825b4f142c 100644 (file)
@@ -1795,4 +1795,48 @@ abstract class ArcanistWorkflow extends Phobject {
     return $engine;
   }
 
+  protected function openURIsInBrowser(array $uris) {
+    $browser = $this->getBrowserCommand();
+    foreach ($uris as $uri) {
+      $err = phutil_passthru('%s %s', $browser, $uri);
+      if ($err) {
+        throw new ArcanistUsageException(
+          pht(
+            "Failed to open '%s' in browser ('%s'). ".
+            "Check your 'browser' config option.",
+            $uri,
+            $browser));
+      }
+    }
+  }
+
+  private function getBrowserCommand() {
+    $config = $this->getConfigFromAnySource('browser');
+    if ($config) {
+      return $config;
+    }
+
+    if (phutil_is_windows()) {
+      return 'start';
+    }
+
+    $candidates = array('sensible-browser', 'xdg-open', 'open');
+
+    // NOTE: The "open" command works well on OS X, but on many Linuxes "open"
+    // exists and is not a browser. For now, we're just looking for other
+    // commands first, but we might want to be smarter about selecting "open"
+    // only on OS X.
+
+    foreach ($candidates as $cmd) {
+      if (Filesystem::binaryExists($cmd)) {
+        return $cmd;
+      }
+    }
+
+    throw new ArcanistUsageException(
+      pht(
+        "Unable to find a browser command to run. Set 'browser' in your ".
+        "Arcanist config to specify a command to use."));
+  }
+
 }