Path: blob/master/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php
12242 views
<?php12final class DiffusionBranchQueryConduitAPIMethod3extends DiffusionQueryConduitAPIMethod {45public function getAPIMethodName() {6return 'diffusion.branchquery';7}89public function getMethodDescription() {10return pht('Determine what branches exist for a repository.');11}1213protected function defineReturnType() {14return 'list<dict>';15}1617protected function defineCustomParamTypes() {18return array(19'closed' => 'optional bool',20'limit' => 'optional int',21'offset' => 'optional int',22'contains' => 'optional string',23'patterns' => 'optional list<string>',24);25}2627protected function getGitResult(ConduitAPIRequest $request) {28$drequest = $this->getDiffusionRequest();29$repository = $drequest->getRepository();3031$contains = $request->getValue('contains');32if ($contains !== null && strlen($contains)) {3334// See PHI958 (and, earlier, PHI720). If "patterns" are provided, pass35// them to "git branch ..." to let callers test for reachability from36// particular branch heads.37$patterns_argv = $request->getValue('patterns', array());38PhutilTypeSpec::checkMap(39array(40'patterns' => $patterns_argv,41),42array(43'patterns' => 'list<string>',44));4546// NOTE: We can't use DiffusionLowLevelGitRefQuery here because47// `git for-each-ref` does not support `--contains`.48list($stdout) = $repository->execxLocalCommand(49'branch --verbose --no-abbrev --contains %s -- %Ls',50$contains,51$patterns_argv);52$ref_map = DiffusionGitBranch::parseLocalBranchOutput(53$stdout);5455$refs = array();56foreach ($ref_map as $ref => $commit) {57$refs[] = id(new DiffusionRepositoryRef())58->setShortName($ref)59->setCommitIdentifier($commit);60}61} else {62$refs = id(new DiffusionLowLevelGitRefQuery())63->setRepository($repository)64->withRefTypes(65array(66PhabricatorRepositoryRefCursor::TYPE_BRANCH,67))68->execute();69}7071return $this->processBranchRefs($request, $refs);72}7374protected function getMercurialResult(ConduitAPIRequest $request) {75$drequest = $this->getDiffusionRequest();76$repository = $drequest->getRepository();7778$query = id(new DiffusionLowLevelMercurialBranchesQuery())79->setRepository($repository);8081$contains = $request->getValue('contains');82if ($contains !== null && strlen($contains)) {83$query->withContainsCommit($contains);84}8586$refs = $query->execute();8788return $this->processBranchRefs($request, $refs);89}9091protected function getSVNResult(ConduitAPIRequest $request) {92// Since SVN doesn't have meaningful branches, just return nothing for all93// queries.94return array();95}9697private function processBranchRefs(ConduitAPIRequest $request, array $refs) {98$drequest = $this->getDiffusionRequest();99$repository = $drequest->getRepository();100$offset = $request->getValue('offset');101$limit = $request->getValue('limit');102103foreach ($refs as $key => $ref) {104if (!$repository->shouldTrackBranch($ref->getShortName())) {105unset($refs[$key]);106}107}108109$with_closed = $request->getValue('closed');110if ($with_closed !== null) {111foreach ($refs as $key => $ref) {112$fields = $ref->getRawFields();113if (idx($fields, 'closed') != $with_closed) {114unset($refs[$key]);115}116}117}118119// NOTE: We can't apply the offset or limit until here, because we may have120// filtered untrackable branches out of the result set.121122if ($offset) {123$refs = array_slice($refs, $offset);124}125126if ($limit) {127$refs = array_slice($refs, 0, $limit);128}129130$refs = array_values($refs);131132return mpull($refs, 'toDictionary');133}134135}136137138