diff --git a/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php b/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php index 345e63fc3b..0286b49ee4 100644 --- a/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php +++ b/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php @@ -1,136 +1,136 @@ <?php final class DiffusionBranchQueryConduitAPIMethod extends DiffusionQueryConduitAPIMethod { public function getAPIMethodName() { return 'diffusion.branchquery'; } public function getMethodDescription() { return pht('Determine what branches exist for a repository.'); } protected function defineReturnType() { return 'list<dict>'; } protected function defineCustomParamTypes() { return array( 'closed' => 'optional bool', 'limit' => 'optional int', 'offset' => 'optional int', 'contains' => 'optional string', 'patterns' => 'optional list<string>', ); } protected function getGitResult(ConduitAPIRequest $request) { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $contains = $request->getValue('contains'); - if (strlen($contains)) { + if (phutil_nonempty_string($contains)) { // See PHI958 (and, earlier, PHI720). If "patterns" are provided, pass // them to "git branch ..." to let callers test for reachability from // particular branch heads. $patterns_argv = $request->getValue('patterns', array()); PhutilTypeSpec::checkMap( array( 'patterns' => $patterns_argv, ), array( 'patterns' => 'list<string>', )); // NOTE: We can't use DiffusionLowLevelGitRefQuery here because // `git for-each-ref` does not support `--contains`. list($stdout) = $repository->execxLocalCommand( 'branch --verbose --no-abbrev --contains %s -- %Ls', $contains, $patterns_argv); $ref_map = DiffusionGitBranch::parseLocalBranchOutput( $stdout); $refs = array(); foreach ($ref_map as $ref => $commit) { $refs[] = id(new DiffusionRepositoryRef()) ->setShortName($ref) ->setCommitIdentifier($commit); } } else { $refs = id(new DiffusionLowLevelGitRefQuery()) ->setRepository($repository) ->withRefTypes( array( PhabricatorRepositoryRefCursor::TYPE_BRANCH, )) ->execute(); } return $this->processBranchRefs($request, $refs); } protected function getMercurialResult(ConduitAPIRequest $request) { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $query = id(new DiffusionLowLevelMercurialBranchesQuery()) ->setRepository($repository); $contains = $request->getValue('contains'); if (strlen($contains)) { $query->withContainsCommit($contains); } $refs = $query->execute(); return $this->processBranchRefs($request, $refs); } protected function getSVNResult(ConduitAPIRequest $request) { // Since SVN doesn't have meaningful branches, just return nothing for all // queries. return array(); } private function processBranchRefs(ConduitAPIRequest $request, array $refs) { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $offset = $request->getValue('offset'); $limit = $request->getValue('limit'); foreach ($refs as $key => $ref) { if (!$repository->shouldTrackBranch($ref->getShortName())) { unset($refs[$key]); } } $with_closed = $request->getValue('closed'); if ($with_closed !== null) { foreach ($refs as $key => $ref) { $fields = $ref->getRawFields(); if (idx($fields, 'closed') != $with_closed) { unset($refs[$key]); } } } // NOTE: We can't apply the offset or limit until here, because we may have // filtered untrackable branches out of the result set. if ($offset) { $refs = array_slice($refs, $offset); } if ($limit) { $refs = array_slice($refs, 0, $limit); } $refs = array_values($refs); return mpull($refs, 'toDictionary'); } } diff --git a/src/applications/diffusion/controller/DiffusionBranchTableController.php b/src/applications/diffusion/controller/DiffusionBranchTableController.php index 13f566a57b..63b1dbc592 100644 --- a/src/applications/diffusion/controller/DiffusionBranchTableController.php +++ b/src/applications/diffusion/controller/DiffusionBranchTableController.php @@ -1,140 +1,140 @@ <?php final class DiffusionBranchTableController extends DiffusionController { public function shouldAllowPublic() { return true; } public function handleRequest(AphrontRequest $request) { $response = $this->loadDiffusionContext(); if ($response) { return $response; } $viewer = $this->getViewer(); $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $pager = id(new PHUIPagerView()) ->readFromRequest($request); $params = array( 'offset' => $pager->getOffset(), 'limit' => $pager->getPageSize() + 1, 'branch' => null, ); $contains = $drequest->getSymbolicCommit(); - if (strlen($contains)) { + if (phutil_nonempty_string($contains)) { $params['contains'] = $contains; } $branches = $this->callConduitWithDiffusionRequest( 'diffusion.branchquery', $params); $branches = $pager->sliceResults($branches); $branches = DiffusionRepositoryRef::loadAllFromDictionaries($branches); // If there is one page of results or fewer, sort branches so the default // branch is on top and permanent branches are below it. if (!$pager->getOffset() && !$pager->getHasMorePages()) { $branches = $this->sortBranches($repository, $branches); } $content = null; if (!$branches) { $content = $this->renderStatusMessage( pht('No Branches'), pht('This repository has no branches.')); } else { $commits = id(new DiffusionCommitQuery()) ->setViewer($viewer) ->withIdentifiers(mpull($branches, 'getCommitIdentifier')) ->withRepository($repository) ->execute(); $list = id(new DiffusionBranchListView()) ->setUser($viewer) ->setBranches($branches) ->setCommits($commits) ->setDiffusionRequest($drequest); $content = id(new PHUIObjectBoxView()) ->setHeaderText($repository->getName()) ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) ->addClass('diffusion-mobile-view') ->setTable($list) ->setPager($pager); } $crumbs = $this->buildCrumbs( array( 'branches' => true, )); $crumbs->setBorder(true); $header = id(new PHUIHeaderView()) ->setHeader(pht('Branches')) ->setHeaderIcon('fa-code-fork'); if (!$repository->isSVN()) { $branch_tag = $this->renderBranchTag($drequest); $header->addTag($branch_tag); } $tabs = $this->buildTabsView('branch'); $view = id(new PHUITwoColumnView()) ->setHeader($header) ->setTabs($tabs) ->setFooter(array( $content, )); return $this->newPage() ->setTitle( array( pht('Branches'), $repository->getDisplayName(), )) ->setCrumbs($crumbs) ->appendChild($view); } private function sortBranches( PhabricatorRepository $repository, array $branches) { $publisher = $repository->newPublisher(); $default_branch = $repository->getDefaultBranch(); $vectors = array(); foreach ($branches as $key => $branch) { $short_name = $branch->getShortName(); if ($short_name === $default_branch) { $order_default = 0; } else { $order_default = 1; } if ($publisher->shouldPublishRef($branch)) { $order_permanent = 0; } else { $order_permanent = 1; } $vectors[$key] = id(new PhutilSortVector()) ->addInt($order_default) ->addInt($order_permanent) ->addString($short_name); } $vectors = msortv($vectors, 'getSelf'); return array_select_keys($branches, array_keys($vectors)); } }