Path: blob/master/src/applications/diffusion/conduit/DiffusionSearchQueryConduitAPIMethod.php
12242 views
<?php12final class DiffusionSearchQueryConduitAPIMethod3extends DiffusionQueryConduitAPIMethod {45public function getAPIMethodName() {6return 'diffusion.searchquery';7}89public function getMethodDescription() {10return pht('Search (grep) a repository at a specific path and commit.');11}1213protected function defineReturnType() {14return 'array';15}1617protected function defineCustomParamTypes() {18return array(19'path' => 'required string',20'commit' => 'optional string',21'grep' => 'required string',22'limit' => 'optional int',23'offset' => 'optional int',24);25}2627protected function getResult(ConduitAPIRequest $request) {28try {29$results = parent::getResult($request);30} catch (CommandException $ex) {31$err = $ex->getError();3233if ($err === 1) {34// `git grep` and `hg grep` exit with 1 if there are no matches;35// assume we just didn't get any hits.36return array();37}3839throw $ex;40}4142$offset = $request->getValue('offset');43$results = array_slice($results, $offset);4445return $results;46}4748protected function getGitResult(ConduitAPIRequest $request) {49$drequest = $this->getDiffusionRequest();50$path = $drequest->getPath();51$grep = $request->getValue('grep');52$repository = $drequest->getRepository();53$limit = $request->getValue('limit');54$offset = $request->getValue('offset');5556// Starting with Git 2.16.0, Git assumes passing an empty argument is57// an error and recommends you pass "." instead.58if (!strlen($path)) {59$path = '.';60}6162$results = array();63$future = $repository->getLocalCommandFuture(64// NOTE: --perl-regexp is available only with libpcre compiled in.65'grep --extended-regexp --null -n --no-color -f - %s -- %s',66gitsprintf('%s', $drequest->getStableCommit()),67$path);6869// NOTE: We're writing the pattern on stdin to avoid issues with UTF870// being mangled by the shell. See T12807.71$future->write($grep);7273$binary_pattern = '/Binary file [^:]*:(.+) matches/';74$lines = new LinesOfALargeExecFuture($future);7576foreach ($lines as $line) {77$result = null;78if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) {79$results[] = array_slice($result, 1);80} else if (preg_match($binary_pattern, $line, $result)) {81list(, $path) = $result;82$results[] = array($path, null, pht('Binary file'));83} else {84$results[] = array(null, null, $line);85}86if (count($results) >= $offset + $limit) {87break;88}89}90unset($lines);9192return $results;93}9495protected function getMercurialResult(ConduitAPIRequest $request) {96$drequest = $this->getDiffusionRequest();97$path = $drequest->getPath();98$grep = $request->getValue('grep');99$repository = $drequest->getRepository();100$limit = $request->getValue('limit');101$offset = $request->getValue('offset');102103$results = array();104$future = $repository->getLocalCommandFuture(105'grep --rev %s --print0 --line-number -- %s %s',106hgsprintf('ancestors(%s)', $drequest->getStableCommit()),107$grep,108$path);109110$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");111$parts = array();112foreach ($lines as $line) {113$parts[] = $line;114if (count($parts) == 4) {115list($path, $char_offset, $line, $string) = $parts;116$results[] = array($path, $line, $string);117if (count($results) >= $offset + $limit) {118break;119}120$parts = array();121}122}123unset($lines);124125return $results;126}127128}129130131