3 final class DiffusionBrowseResultSet
extends Phobject
{
5 const REASON_IS_FILE
= 'is-file';
6 const REASON_IS_SUBMODULE
= 'is-submodule';
7 const REASON_IS_DELETED
= 'is-deleted';
8 const REASON_IS_NONEXISTENT
= 'nonexistent';
9 const REASON_BAD_COMMIT
= 'bad-commit';
10 const REASON_IS_EMPTY
= 'empty';
11 const REASON_IS_UNTRACKED_PARENT
= 'untracked-parent';
14 private $isValidResults;
15 private $reasonForEmptyResultSet;
16 private $existedAtCommit;
17 private $deletedAtCommit;
19 public function setPaths(array $paths) {
20 assert_instances_of($paths, 'DiffusionRepositoryPath');
21 $this->paths
= $paths;
24 public function getPaths() {
28 public function setIsValidResults($is_valid) {
29 $this->isValidResults
= $is_valid;
32 public function isValidResults() {
33 return $this->isValidResults
;
36 public function setReasonForEmptyResultSet($reason) {
37 $this->reasonForEmptyResultSet
= $reason;
40 public function getReasonForEmptyResultSet() {
41 return $this->reasonForEmptyResultSet
;
44 public function setExistedAtCommit($existed_at_commit) {
45 $this->existedAtCommit
= $existed_at_commit;
48 public function getExistedAtCommit() {
49 return $this->existedAtCommit
;
52 public function setDeletedAtCommit($deleted_at_commit) {
53 $this->deletedAtCommit
= $deleted_at_commit;
56 public function getDeletedAtCommit() {
57 return $this->deletedAtCommit
;
60 public function toDictionary() {
61 $paths = $this->getPathDicts();
65 'isValidResults' => $this->isValidResults(),
66 'reasonForEmptyResultSet' => $this->getReasonForEmptyResultSet(),
67 'existedAtCommit' => $this->getExistedAtCommit(),
68 'deletedAtCommit' => $this->getDeletedAtCommit(),
72 public function getPathDicts() {
73 $paths = $this->getPaths();
75 return mpull($paths, 'toDictionary');
81 * Get the best README file in this result set, if one exists.
83 * Callers should normally use `diffusion.filecontentquery` to pull README
86 * @return string|null Full path to best README, or null if one does not
89 public function getReadmePath() {
90 $allowed_types = array(
91 ArcanistDiffChangeType
::FILE_NORMAL
=> true,
92 ArcanistDiffChangeType
::FILE_TEXT
=> true,
95 $candidates = array();
96 foreach ($this->getPaths() as $path_object) {
97 if (empty($allowed_types[$path_object->getFileType()])) {
98 // Skip directories, images, etc.
102 $local_path = $path_object->getPath();
103 if (!preg_match('/^readme(\.|$)/i', $local_path)) {
104 // Skip files not named "README".
108 $full_path = $path_object->getFullPath();
109 $candidates[$full_path] = self
::getReadmePriority($local_path);
117 return head_key($candidates);
121 * Get the priority of a README file.
123 * When a directory contains several README files, this function scores them
124 * so the caller can select a preferred file. See @{method:getReadmePath}.
126 * @param string Local README path, like "README.txt".
127 * @return int Priority score, with higher being more preferred.
129 public static function getReadmePriority($path) {
130 $path = phutil_utf8_strtolower($path);
131 if ($path == 'readme') {
135 $ext = last(explode('.', $path));
150 public static function newFromConduit(array $data) {
152 $path_dicts = $data['paths'];
153 foreach ($path_dicts as $dict) {
154 $paths[] = DiffusionRepositoryPath
::newFromDictionary($dict);
156 return id(new DiffusionBrowseResultSet())
158 ->setIsValidResults($data['isValidResults'])
159 ->setReasonForEmptyResultSet($data['reasonForEmptyResultSet'])
160 ->setExistedAtCommit($data['existedAtCommit'])
161 ->setDeletedAtCommit($data['deletedAtCommit']);