Page MenuHomestyx hydra

No OneTemporary

diff --git a/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php b/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
index 7ff0f6725a..2e272d69a7 100644
--- a/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
+++ b/src/applications/diffusion/conduit/DiffusionBrowseQueryConduitAPIMethod.php
@@ -1,516 +1,546 @@
<?php
final class DiffusionBrowseQueryConduitAPIMethod
extends DiffusionQueryConduitAPIMethod {
public function getAPIMethodName() {
return 'diffusion.browsequery';
}
public function getMethodDescription() {
return pht(
'File(s) information for a repository at an (optional) path and '.
'(optional) commit.');
}
protected function defineReturnType() {
return 'array';
}
protected function defineCustomParamTypes() {
return array(
'path' => 'optional string',
'commit' => 'optional string',
'needValidityOnly' => 'optional bool',
'limit' => 'optional int',
'offset' => 'optional int',
);
}
protected function getResult(ConduitAPIRequest $request) {
$result = parent::getResult($request);
return $result->toDictionary();
}
protected function getGitResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$path = $request->getValue('path');
$commit = $request->getValue('commit');
$offset = (int)$request->getValue('offset');
$limit = (int)$request->getValue('limit');
$result = $this->getEmptyResultSet();
if ($path == '') {
// Fast path to improve the performance of the repository view; we know
// the root is always a tree at any commit and always exists.
$stdout = 'tree';
} else {
try {
list($stdout) = $repository->execxLocalCommand(
'cat-file -t %s:%s',
$commit,
$path);
} catch (CommandException $e) {
+ // The "cat-file" command may fail if the path legitimately does not
+ // exist, but it may also fail if the path is a submodule. This can
+ // produce either "Not a valid object name" or "could not get object
+ // info".
+
+ // To detect if we have a submodule, use `git ls-tree`. If the path
+ // is a submodule, we'll get a "160000" mode mask with type "commit".
+
+ list($sub_err, $sub_stdout) = $repository->execLocalCommand(
+ 'ls-tree %s -- %s',
+ $commit,
+ $path);
+ if (!$sub_err) {
+ // If the path failed "cat-file" but "ls-tree" worked, we assume it
+ // must be a submodule. If it is, the output will look something
+ // like this:
+ //
+ // 160000 commit <hash> <path>
+ //
+ // We make sure it has the 160000 mode mask to confirm that it's
+ // definitely a submodule.
+ $mode = (int)$sub_stdout;
+ if ($mode & 160000) {
+ $submodule_reason = DiffusionBrowseResultSet::REASON_IS_SUBMODULE;
+ $result
+ ->setReasonForEmptyResultSet($submodule_reason);
+ return $result;
+ }
+ }
+
$stderr = $e->getStderr();
if (preg_match('/^fatal: Not a valid object name/', $stderr)) {
// Grab two logs, since the first one is when the object was deleted.
list($stdout) = $repository->execxLocalCommand(
'log -n2 --format="%%H" %s -- %s',
$commit,
$path);
$stdout = trim($stdout);
if ($stdout) {
$commits = explode("\n", $stdout);
$result
->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_DELETED)
->setDeletedAtCommit(idx($commits, 0))
->setExistedAtCommit(idx($commits, 1));
return $result;
}
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_NONEXISTENT);
return $result;
} else {
throw $e;
}
}
}
if (trim($stdout) == 'blob') {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_FILE);
return $result;
}
$result->setIsValidResults(true);
if ($this->shouldOnlyTestValidity($request)) {
return $result;
}
list($stdout) = $repository->execxLocalCommand(
'ls-tree -z -l %s:%s',
$commit,
$path);
$submodules = array();
if (strlen($path)) {
$prefix = rtrim($path, '/').'/';
} else {
$prefix = '';
}
$count = 0;
$results = array();
$lines = empty($stdout)
? array()
: explode("\0", rtrim($stdout));
foreach ($lines as $line) {
// NOTE: Limit to 5 components so we parse filenames with spaces in them
// correctly.
// NOTE: The output uses a mixture of tabs and one-or-more spaces to
// delimit fields.
$parts = preg_split('/\s+/', $line, 5);
if (count($parts) < 5) {
throw new Exception(
pht(
'Expected "<mode> <type> <hash> <size>\t<name>", for ls-tree of '.
'"%s:%s", got: %s',
$commit,
$path,
$line));
}
list($mode, $type, $hash, $size, $name) = $parts;
$path_result = new DiffusionRepositoryPath();
if ($type == 'tree') {
$file_type = DifferentialChangeType::FILE_DIRECTORY;
} else if ($type == 'commit') {
$file_type = DifferentialChangeType::FILE_SUBMODULE;
$submodules[] = $path_result;
} else {
$mode = intval($mode, 8);
if (($mode & 0120000) == 0120000) {
$file_type = DifferentialChangeType::FILE_SYMLINK;
} else {
$file_type = DifferentialChangeType::FILE_NORMAL;
}
}
$path_result->setFullPath($prefix.$name);
$path_result->setPath($name);
$path_result->setHash($hash);
$path_result->setFileType($file_type);
$path_result->setFileSize($size);
if ($count >= $offset) {
$results[] = $path_result;
}
$count++;
if ($limit && $count >= ($offset + $limit)) {
break;
}
}
// If we identified submodules, lookup the module info at this commit to
// find their source URIs.
if ($submodules) {
// NOTE: We need to read the file out of git and write it to a temporary
// location because "git config -f" doesn't accept a "commit:path"-style
// argument.
// NOTE: This file may not exist, e.g. because the commit author removed
// it when they added the submodule. See T1448. If it's not present, just
// show the submodule without enriching it. If ".gitmodules" was removed
// it seems to partially break submodules, but the repository as a whole
// continues to work fine and we've seen at least two cases of this in
// the wild.
list($err, $contents) = $repository->execLocalCommand(
'cat-file blob %s:.gitmodules',
$commit);
if (!$err) {
$tmp = new TempFile();
Filesystem::writeFile($tmp, $contents);
list($module_info) = $repository->execxLocalCommand(
'config -l -f %s',
$tmp);
$dict = array();
$lines = explode("\n", trim($module_info));
foreach ($lines as $line) {
list($key, $value) = explode('=', $line, 2);
$parts = explode('.', $key);
$dict[$key] = $value;
}
foreach ($submodules as $path) {
$full_path = $path->getFullPath();
$key = 'submodule.'.$full_path.'.url';
if (isset($dict[$key])) {
$path->setExternalURI($dict[$key]);
}
}
}
}
return $result->setPaths($results);
}
protected function getMercurialResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$path = $request->getValue('path');
$commit = $request->getValue('commit');
$offset = (int)$request->getValue('offset');
$limit = (int)$request->getValue('limit');
$result = $this->getEmptyResultSet();
$entire_manifest = id(new DiffusionLowLevelMercurialPathsQuery())
->setRepository($repository)
->withCommit($commit)
->withPath($path)
->execute();
$results = array();
$match_against = trim($path, '/');
$match_len = strlen($match_against);
// For the root, don't trim. For other paths, trim the "/" after we match.
// We need this because Mercurial's canonical paths have no leading "/",
// but ours do.
$trim_len = $match_len ? $match_len + 1 : 0;
$count = 0;
foreach ($entire_manifest as $path) {
if (strncmp($path, $match_against, $match_len)) {
continue;
}
if (!strlen($path)) {
continue;
}
$remainder = substr($path, $trim_len);
if (!strlen($remainder)) {
// There is a file with this exact name in the manifest, so clearly
// it's a file.
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_FILE);
return $result;
}
$parts = explode('/', $remainder);
$name = reset($parts);
// If we've already seen this path component, we're looking at a file
// inside a directory we already processed. Just move on.
if (isset($results[$name])) {
continue;
}
if (count($parts) == 1) {
$type = DifferentialChangeType::FILE_NORMAL;
} else {
$type = DifferentialChangeType::FILE_DIRECTORY;
}
if ($count >= $offset) {
$results[$name] = $type;
}
$count++;
if ($limit && ($count >= ($offset + $limit))) {
break;
}
}
foreach ($results as $key => $type) {
$path_result = new DiffusionRepositoryPath();
$path_result->setPath($key);
$path_result->setFileType($type);
$path_result->setFullPath(ltrim($match_against.'/', '/').$key);
$results[$key] = $path_result;
}
$valid_results = true;
if (empty($results)) {
// TODO: Detect "deleted" by issuing "hg log"?
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_NONEXISTENT);
$valid_results = false;
}
return $result
->setPaths($results)
->setIsValidResults($valid_results);
}
protected function getSVNResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$path = $request->getValue('path');
$commit = $request->getValue('commit');
$offset = (int)$request->getValue('offset');
$limit = (int)$request->getValue('limit');
$result = $this->getEmptyResultSet();
$subpath = $repository->getDetail('svn-subpath');
if ($subpath && strncmp($subpath, $path, strlen($subpath))) {
// If we have a subpath and the path isn't a child of it, it (almost
// certainly) won't exist since we don't track commits which affect
// it. (Even if it exists, return a consistent result.)
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_UNTRACKED_PARENT);
return $result;
}
$conn_r = $repository->establishConnection('r');
$parent_path = DiffusionPathIDQuery::getParentPath($path);
$path_query = new DiffusionPathIDQuery(
array(
$path,
$parent_path,
));
$path_map = $path_query->loadPathIDs();
$path_id = $path_map[$path];
$parent_path_id = $path_map[$parent_path];
if (empty($path_id)) {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_NONEXISTENT);
return $result;
}
if ($commit) {
$slice_clause = 'AND svnCommit <= '.(int)$commit;
} else {
$slice_clause = '';
}
$index = queryfx_all(
$conn_r,
'SELECT pathID, max(svnCommit) maxCommit FROM %T WHERE
repositoryID = %d AND parentID = %d
%Q GROUP BY pathID',
PhabricatorRepository::TABLE_FILESYSTEM,
$repository->getID(),
$path_id,
$slice_clause);
if (!$index) {
if ($path == '/') {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_EMPTY);
} else {
// NOTE: The parent path ID is included so this query can take
// advantage of the table's primary key; it is uniquely determined by
// the pathID but if we don't do the lookup ourselves MySQL doesn't have
// the information it needs to avoid a table scan.
$reasons = queryfx_all(
$conn_r,
'SELECT * FROM %T WHERE repositoryID = %d
AND parentID = %d
AND pathID = %d
%Q ORDER BY svnCommit DESC LIMIT 2',
PhabricatorRepository::TABLE_FILESYSTEM,
$repository->getID(),
$parent_path_id,
$path_id,
$slice_clause);
$reason = reset($reasons);
if (!$reason) {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_NONEXISTENT);
} else {
$file_type = $reason['fileType'];
if (empty($reason['existed'])) {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_DELETED);
$result->setDeletedAtCommit($reason['svnCommit']);
if (!empty($reasons[1])) {
$result->setExistedAtCommit($reasons[1]['svnCommit']);
}
} else if ($file_type == DifferentialChangeType::FILE_DIRECTORY) {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_EMPTY);
} else {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_FILE);
}
}
}
return $result;
}
$result->setIsValidResults(true);
if ($this->shouldOnlyTestValidity($request)) {
return $result;
}
$sql = array();
foreach ($index as $row) {
$sql[] =
'(pathID = '.(int)$row['pathID'].' AND '.
'svnCommit = '.(int)$row['maxCommit'].')';
}
$browse = queryfx_all(
$conn_r,
'SELECT *, p.path pathName
FROM %T f JOIN %T p ON f.pathID = p.id
WHERE repositoryID = %d
AND parentID = %d
AND existed = 1
AND (%Q)
ORDER BY pathName',
PhabricatorRepository::TABLE_FILESYSTEM,
PhabricatorRepository::TABLE_PATH,
$repository->getID(),
$path_id,
implode(' OR ', $sql));
$loadable_commits = array();
foreach ($browse as $key => $file) {
// We need to strip out directories because we don't store last-modified
// in the filesystem table.
if ($file['fileType'] != DifferentialChangeType::FILE_DIRECTORY) {
$loadable_commits[] = $file['svnCommit'];
$browse[$key]['hasCommit'] = true;
}
}
$commits = array();
$commit_data = array();
if ($loadable_commits) {
// NOTE: Even though these are integers, use '%Ls' because MySQL doesn't
// use the second part of the key otherwise!
$commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
'repositoryID = %d AND commitIdentifier IN (%Ls)',
$repository->getID(),
$loadable_commits);
$commits = mpull($commits, null, 'getCommitIdentifier');
if ($commits) {
$commit_data = id(new PhabricatorRepositoryCommitData())->loadAllWhere(
'commitID in (%Ld)',
mpull($commits, 'getID'));
$commit_data = mpull($commit_data, null, 'getCommitID');
} else {
$commit_data = array();
}
}
$path_normal = DiffusionPathIDQuery::normalizePath($path);
$results = array();
$count = 0;
foreach ($browse as $file) {
$full_path = $file['pathName'];
$file_path = ltrim(substr($full_path, strlen($path_normal)), '/');
$full_path = ltrim($full_path, '/');
$result_path = new DiffusionRepositoryPath();
$result_path->setPath($file_path);
$result_path->setFullPath($full_path);
$result_path->setFileType($file['fileType']);
if (!empty($file['hasCommit'])) {
$commit = idx($commits, $file['svnCommit']);
if ($commit) {
$data = idx($commit_data, $commit->getID());
$result_path->setLastModifiedCommit($commit);
$result_path->setLastCommitData($data);
}
}
if ($count >= $offset) {
$results[] = $result_path;
}
$count++;
if ($limit && ($count >= ($offset + $limit))) {
break;
}
}
if (empty($results)) {
$result->setReasonForEmptyResultSet(
DiffusionBrowseResultSet::REASON_IS_EMPTY);
}
return $result->setPaths($results);
}
private function getEmptyResultSet() {
return id(new DiffusionBrowseResultSet())
->setPaths(array())
->setReasonForEmptyResultSet(null)
->setIsValidResults(false);
}
private function shouldOnlyTestValidity(ConduitAPIRequest $request) {
return $request->getValue('needValidityOnly', false);
}
}
diff --git a/src/applications/diffusion/data/DiffusionBrowseResultSet.php b/src/applications/diffusion/data/DiffusionBrowseResultSet.php
index 2208aca505..22d9e08251 100644
--- a/src/applications/diffusion/data/DiffusionBrowseResultSet.php
+++ b/src/applications/diffusion/data/DiffusionBrowseResultSet.php
@@ -1,162 +1,163 @@
<?php
final class DiffusionBrowseResultSet extends Phobject {
const REASON_IS_FILE = 'is-file';
+ const REASON_IS_SUBMODULE = 'is-submodule';
const REASON_IS_DELETED = 'is-deleted';
const REASON_IS_NONEXISTENT = 'nonexistent';
const REASON_BAD_COMMIT = 'bad-commit';
const REASON_IS_EMPTY = 'empty';
const REASON_IS_UNTRACKED_PARENT = 'untracked-parent';
private $paths;
private $isValidResults;
private $reasonForEmptyResultSet;
private $existedAtCommit;
private $deletedAtCommit;
public function setPaths(array $paths) {
assert_instances_of($paths, 'DiffusionRepositoryPath');
$this->paths = $paths;
return $this;
}
public function getPaths() {
return $this->paths;
}
public function setIsValidResults($is_valid) {
$this->isValidResults = $is_valid;
return $this;
}
public function isValidResults() {
return $this->isValidResults;
}
public function setReasonForEmptyResultSet($reason) {
$this->reasonForEmptyResultSet = $reason;
return $this;
}
public function getReasonForEmptyResultSet() {
return $this->reasonForEmptyResultSet;
}
public function setExistedAtCommit($existed_at_commit) {
$this->existedAtCommit = $existed_at_commit;
return $this;
}
public function getExistedAtCommit() {
return $this->existedAtCommit;
}
public function setDeletedAtCommit($deleted_at_commit) {
$this->deletedAtCommit = $deleted_at_commit;
return $this;
}
public function getDeletedAtCommit() {
return $this->deletedAtCommit;
}
public function toDictionary() {
$paths = $this->getPathDicts();
return array(
'paths' => $paths,
'isValidResults' => $this->isValidResults(),
'reasonForEmptyResultSet' => $this->getReasonForEmptyResultSet(),
'existedAtCommit' => $this->getExistedAtCommit(),
'deletedAtCommit' => $this->getDeletedAtCommit(),
);
}
public function getPathDicts() {
$paths = $this->getPaths();
if ($paths) {
return mpull($paths, 'toDictionary');
}
return array();
}
/**
* Get the best README file in this result set, if one exists.
*
* Callers should normally use `diffusion.filecontentquery` to pull README
* content.
*
* @return string|null Full path to best README, or null if one does not
* exist.
*/
public function getReadmePath() {
$allowed_types = array(
ArcanistDiffChangeType::FILE_NORMAL => true,
ArcanistDiffChangeType::FILE_TEXT => true,
);
$candidates = array();
foreach ($this->getPaths() as $path_object) {
if (empty($allowed_types[$path_object->getFileType()])) {
// Skip directories, images, etc.
continue;
}
$local_path = $path_object->getPath();
if (!preg_match('/^readme(\.|$)/i', $local_path)) {
// Skip files not named "README".
continue;
}
$full_path = $path_object->getFullPath();
$candidates[$full_path] = self::getReadmePriority($local_path);
}
if (!$candidates) {
return null;
}
arsort($candidates);
return head_key($candidates);
}
/**
* Get the priority of a README file.
*
* When a directory contains several README files, this function scores them
* so the caller can select a preferred file. See @{method:getReadmePath}.
*
* @param string Local README path, like "README.txt".
* @return int Priority score, with higher being more preferred.
*/
public static function getReadmePriority($path) {
$path = phutil_utf8_strtolower($path);
if ($path == 'readme') {
return 90;
}
$ext = last(explode('.', $path));
switch ($ext) {
case 'remarkup':
return 100;
case 'rainbow':
return 80;
case 'md':
return 70;
case 'txt':
return 60;
default:
return 50;
}
}
public static function newFromConduit(array $data) {
$paths = array();
$path_dicts = $data['paths'];
foreach ($path_dicts as $dict) {
$paths[] = DiffusionRepositoryPath::newFromDictionary($dict);
}
return id(new DiffusionBrowseResultSet())
->setPaths($paths)
->setIsValidResults($data['isValidResults'])
->setReasonForEmptyResultSet($data['reasonForEmptyResultSet'])
->setExistedAtCommit($data['existedAtCommit'])
->setDeletedAtCommit($data['deletedAtCommit']);
}
}
diff --git a/src/applications/diffusion/view/DiffusionEmptyResultView.php b/src/applications/diffusion/view/DiffusionEmptyResultView.php
index 12def83e76..c818267268 100644
--- a/src/applications/diffusion/view/DiffusionEmptyResultView.php
+++ b/src/applications/diffusion/view/DiffusionEmptyResultView.php
@@ -1,97 +1,104 @@
<?php
final class DiffusionEmptyResultView extends DiffusionView {
private $browseResultSet;
private $view;
public function setDiffusionBrowseResultSet(DiffusionBrowseResultSet $set) {
$this->browseResultSet = $set;
return $this;
}
public function setView($view) {
$this->view = $view;
return $this;
}
public function render() {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$commit = $drequest->getCommit();
if ($commit) {
$commit = $repository->formatCommitName($commit);
} else {
$commit = 'HEAD';
}
$reason = $this->browseResultSet->getReasonForEmptyResultSet();
switch ($reason) {
case DiffusionBrowseResultSet::REASON_IS_NONEXISTENT:
$title = pht('Path Does Not Exist');
// TODO: Under git, this error message should be more specific. It
// may exist on some other branch.
$body = pht('This path does not exist anywhere.');
$severity = PHUIInfoView::SEVERITY_ERROR;
break;
case DiffusionBrowseResultSet::REASON_IS_EMPTY:
$title = pht('Empty Directory');
$body = pht('This path was an empty directory at %s.', $commit);
$severity = PHUIInfoView::SEVERITY_NOTICE;
break;
+ case DiffusionBrowseResultSet::REASON_IS_SUBMODULE:
+ $title = pht('Submodule');
+ // TODO: We could improve this, but it is normally difficult to
+ // reach this page for a submodule.
+ $body = pht('This path was a submodule at %s.', $commit);
+ $severity = PHUIInfoView::SEVERITY_NOTICE;
+ break;
case DiffusionBrowseResultSet::REASON_IS_DELETED:
$deleted = $this->browseResultSet->getDeletedAtCommit();
$existed = $this->browseResultSet->getExistedAtCommit();
$existed_text = $repository->formatCommitName($existed);
$existed_href = $drequest->generateURI(
array(
'action' => 'browse',
'path' => $drequest->getPath(),
'commit' => $existed,
'params' => array(
'view' => $this->view,
),
));
$existed_link = phutil_tag(
'a',
array(
'href' => $existed_href,
),
$existed_text);
$title = pht('Path Was Deleted');
$body = pht(
'This path does not exist at %s. It was deleted in %s and last '.
'existed at %s.',
$commit,
self::linkCommit($drequest->getRepository(), $deleted),
$existed_link);
$severity = PHUIInfoView::SEVERITY_WARNING;
break;
case DiffusionBrowseResultSet::REASON_IS_UNTRACKED_PARENT:
$subdir = $drequest->getRepository()->getDetail('svn-subpath');
$title = pht('Directory Not Tracked');
$body =
pht(
"This repository is configured to track only one subdirectory ".
"of the entire repository ('%s'), but you aren't looking at ".
"something in that subdirectory, so no information is available.",
$subdir);
$severity = PHUIInfoView::SEVERITY_WARNING;
break;
default:
throw new Exception(pht('Unknown failure reason: %s', $reason));
}
$error_view = new PHUIInfoView();
$error_view->setSeverity($severity);
$error_view->setTitle($title);
$error_view->appendChild(phutil_tag('p', array(), $body));
return $error_view->render();
}
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Apr 30, 5:19 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
108774
Default Alt Text
(26 KB)

Event Timeline