2012-12-09 23:09:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2014-08-04 21:02:54 +02:00
|
|
|
* Browse files or objects in the Phabricator web interface.
|
2012-12-09 23:09:35 +01:00
|
|
|
*/
|
2014-07-21 23:49:15 +02:00
|
|
|
final class ArcanistBrowseWorkflow extends ArcanistWorkflow {
|
2012-12-09 23:09:35 +01:00
|
|
|
|
|
|
|
public function getWorkflowName() {
|
|
|
|
return 'browse';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandSynopses() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2014-02-09 21:19:57 +01:00
|
|
|
**browse** [__options__] __path__ ...
|
2014-08-04 21:02:54 +02:00
|
|
|
**browse** [__options__] __object__ ...
|
2012-12-09 23:09:35 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2014-02-09 21:19:57 +01:00
|
|
|
Supports: git, hg, svn
|
2014-08-04 21:02:54 +02:00
|
|
|
Open a file or object (like a task or revision) in your web browser.
|
|
|
|
|
|
|
|
$ arc browse README # Open a file in Diffusion.
|
|
|
|
$ arc browse T123 # View a task.
|
2014-08-04 21:03:22 +02:00
|
|
|
$ arc browse HEAD # View a symbolic commit.
|
2012-12-09 23:09:35 +01:00
|
|
|
|
|
|
|
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',
|
2014-02-09 21:19:57 +01:00
|
|
|
'help' => pht(
|
2015-05-13 10:05:15 +02:00
|
|
|
'Default branch name to view on server. Defaults to "%s".',
|
|
|
|
'master'),
|
2012-12-09 23:09:35 +01:00
|
|
|
),
|
2014-08-04 21:02:54 +02:00
|
|
|
'force' => array(
|
|
|
|
'help' => pht(
|
|
|
|
'Open arguments as paths, even if they do not exist in the '.
|
|
|
|
'working copy.'),
|
|
|
|
),
|
2012-12-09 23:09:35 +01:00
|
|
|
'*' => 'paths',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
public function desiresWorkingCopy() {
|
2012-12-09 23:09:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresConduit() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresAuthentication() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
public function desiresRepositoryAPI() {
|
2012-12-09 23:09:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
2014-08-04 21:02:54 +02:00
|
|
|
$console = PhutilConsole::getConsole();
|
2012-12-09 23:09:35 +01:00
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
$is_force = $this->getArgument('force');
|
|
|
|
|
|
|
|
$things = $this->getArgument('paths');
|
|
|
|
if (!$things) {
|
2014-02-09 21:19:57 +01:00
|
|
|
throw new ArcanistUsageException(
|
|
|
|
pht(
|
2014-08-04 21:02:54 +02:00
|
|
|
'Specify one or more paths or objects to browse. Use the command '.
|
2015-05-13 10:05:15 +02:00
|
|
|
'"%s" if you want to browse this directory.',
|
|
|
|
'arc browse .'));
|
2014-02-09 21:19:57 +01:00
|
|
|
}
|
2014-08-04 21:02:54 +02:00
|
|
|
$things = array_fuse($things);
|
|
|
|
|
|
|
|
$objects = $this->getConduit()->callMethodSynchronous(
|
|
|
|
'phid.lookup',
|
|
|
|
array(
|
|
|
|
'names' => array_keys($things),
|
|
|
|
));
|
|
|
|
|
|
|
|
$uris = array();
|
|
|
|
foreach ($objects as $name => $object) {
|
|
|
|
$uris[] = $object['uri'];
|
|
|
|
|
|
|
|
$console->writeOut(
|
|
|
|
pht(
|
|
|
|
'Opening **%s** as an object.',
|
|
|
|
$name)."\n");
|
|
|
|
|
|
|
|
unset($things[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasRepositoryAPI()) {
|
|
|
|
$repository_api = $this->getRepositoryAPI();
|
|
|
|
$project_root = $this->getWorkingCopy()->getProjectRoot();
|
|
|
|
|
2014-08-04 21:03:22 +02:00
|
|
|
// First, try to resolve arguments as symbolic commits.
|
|
|
|
|
|
|
|
$commits = array();
|
|
|
|
foreach ($things as $key => $thing) {
|
2014-08-08 20:21:43 +02:00
|
|
|
if ($thing == '.') {
|
|
|
|
// Git resolves '.' like HEAD, but it should be interpreted to mean
|
|
|
|
// "the current directory". Just skip resolution and fall through.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$commit = $repository_api->getCanonicalRevisionName($thing);
|
|
|
|
if ($commit) {
|
|
|
|
$commits[$commit] = $key;
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
// Ignore.
|
2014-08-04 21:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($commits) {
|
|
|
|
$commit_info = $this->getConduit()->callMethodSynchronous(
|
|
|
|
'diffusion.querycommits',
|
|
|
|
array(
|
|
|
|
'repositoryPHID' => $this->getRepositoryPHID(),
|
|
|
|
'names' => array_keys($commits),
|
|
|
|
));
|
|
|
|
|
|
|
|
foreach ($commit_info['identifierMap'] as $ckey => $cphid) {
|
|
|
|
$thing = $commits[$ckey];
|
|
|
|
unset($things[$thing]);
|
|
|
|
|
|
|
|
$uris[] = $commit_info['data'][$cphid]['uri'];
|
|
|
|
|
|
|
|
$console->writeOut(
|
|
|
|
pht(
|
|
|
|
'Opening **%s** as a commit.',
|
|
|
|
$thing)."\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fail, try to resolve them as paths.
|
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
foreach ($things as $key => $path) {
|
2014-08-26 18:02:34 +02:00
|
|
|
$lines = null;
|
2014-08-25 21:45:49 +02:00
|
|
|
$parts = explode(':', $path);
|
|
|
|
if (count($parts) > 1) {
|
2014-08-26 18:02:34 +02:00
|
|
|
$lines = array_pop($parts);
|
2014-08-25 21:45:49 +02:00
|
|
|
}
|
|
|
|
$path = implode(':', $parts);
|
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
$full_path = Filesystem::resolvePath($path);
|
|
|
|
|
|
|
|
if (!$is_force && !Filesystem::pathExists($full_path)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$console->writeOut(
|
|
|
|
pht(
|
|
|
|
'Opening **%s** as a repository path.',
|
|
|
|
$key)."\n");
|
2014-02-09 21:19:57 +01:00
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
unset($things[$key]);
|
2012-12-09 23:09:35 +01:00
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
if ($full_path == $project_root) {
|
|
|
|
$path = '';
|
|
|
|
} else {
|
|
|
|
$path = Filesystem::readablePath($full_path, $project_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
$base_uri = $this->getBaseURI();
|
2014-08-25 21:45:49 +02:00
|
|
|
$uri = $base_uri.$path;
|
|
|
|
|
2014-08-26 18:02:34 +02:00
|
|
|
if ($lines) {
|
|
|
|
$uri = $uri.'$'.$lines;
|
2014-08-25 21:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$uris[] = $uri;
|
2014-08-04 21:02:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($things) {
|
|
|
|
$console->writeOut(
|
2015-05-13 10:05:15 +02:00
|
|
|
"%s\n",
|
2014-08-04 21:02:54 +02:00
|
|
|
pht(
|
|
|
|
"The current working directory is not a repository working ".
|
2014-08-04 21:03:22 +02:00
|
|
|
"copy, so remaining arguments can not be resolved as paths or ".
|
|
|
|
"commits. To browse paths or symbolic commits in Diffusion, run ".
|
2015-05-13 10:05:15 +02:00
|
|
|
"'%s' from inside a working copy.",
|
|
|
|
'arc browse'));
|
2014-02-09 18:00:54 +01:00
|
|
|
}
|
2012-12-09 23:09:35 +01:00
|
|
|
}
|
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
foreach ($things as $thing) {
|
|
|
|
$console->writeOut(
|
2015-05-13 10:05:15 +02:00
|
|
|
"%s\n",
|
2014-08-04 21:02:54 +02:00
|
|
|
pht(
|
2014-08-04 21:03:22 +02:00
|
|
|
'Unable to find an object named **%s**, no such commit exists in '.
|
|
|
|
'the remote, and no such path exists in the working copy. Use '.
|
2015-05-13 10:05:15 +02:00
|
|
|
'__%s__ to treat this as a path anyway.',
|
|
|
|
$thing,
|
|
|
|
'--force'));
|
2014-08-04 21:02:54 +02:00
|
|
|
}
|
2012-12-09 23:09:35 +01:00
|
|
|
|
2014-08-04 21:02:54 +02:00
|
|
|
if ($uris) {
|
2014-08-04 21:03:03 +02:00
|
|
|
$this->openURIsInBrowser($uris);
|
2012-12-09 23:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBaseURI() {
|
2014-02-09 17:55:16 +01:00
|
|
|
$repo_uri = $this->getRepositoryURI();
|
2014-02-09 21:19:57 +01:00
|
|
|
if ($repo_uri === null) {
|
|
|
|
throw new ArcanistUsageException(
|
|
|
|
pht(
|
|
|
|
'arc is unable to determine which repository in Diffusion '.
|
2015-05-13 10:05:15 +02:00
|
|
|
'this working copy belongs to. Use "%s" to understand how '.
|
|
|
|
'%s looks for a repository.',
|
|
|
|
'arc which',
|
|
|
|
'arc'));
|
2014-02-09 21:19:57 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 23:09:35 +01:00
|
|
|
$branch = $this->getArgument('branch', 'master');
|
2016-03-05 00:49:35 +01:00
|
|
|
$branch = phutil_escape_uri_path_component($branch);
|
2012-12-09 23:09:35 +01:00
|
|
|
|
2014-02-09 17:55:16 +01:00
|
|
|
return $repo_uri.'browse/'.$branch.'/';
|
2012-12-09 23:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|