1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-31 17:08:22 +01:00

Return no results from grep repository queries on error

Summary: Fixes T7852. Although `1` could also indicate other kinds of problems, assume it means "no results".

Test Plan: Searched for nonsense strings in Git and Mercurial. Searched for valid strings in Git and Mercurial.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T7852

Differential Revision: https://secure.phabricator.com/D14943
This commit is contained in:
epriestley 2016-01-05 05:56:35 -08:00
parent 8b6edaa4e2
commit f1c298203a
3 changed files with 27 additions and 31 deletions

View file

@ -44,7 +44,6 @@ final class DiffusionQueryPathsConduitAPIMethod
$commit, $commit,
$path); $path);
$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0"); $lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");
return $this->filterResults($lines, $request); return $this->filterResults($lines, $request);
} }
@ -86,18 +85,17 @@ final class DiffusionQueryPathsConduitAPIMethod
$results = array(); $results = array();
$count = 0; $count = 0;
foreach ($lines as $line) { foreach ($lines as $line) {
if (!$pattern || preg_match($pattern, $line)) { if (strlen($pattern) && !preg_match($pattern, $line)) {
if ($count >= $offset) { continue;
$results[] = $line;
} }
$results[] = $line;
$count++; $count++;
if ($limit && ($count >= ($offset + $limit))) { if ($limit && ($count >= ($offset + $limit))) {
break; break;
} }
} }
}
return $results; return $results;
} }

View file

@ -25,18 +25,19 @@ final class DiffusionSearchQueryConduitAPIMethod
); );
} }
protected function defineCustomErrorTypes() {
return array(
'ERR-GREP-COMMAND' => pht('Grep command failed.'),
);
}
protected function getResult(ConduitAPIRequest $request) { protected function getResult(ConduitAPIRequest $request) {
try { try {
$results = parent::getResult($request); $results = parent::getResult($request);
} catch (CommandException $ex) { } catch (CommandException $ex) {
throw id(new ConduitException('ERR-GREP-COMMAND')) $err = $ex->getError();
->setErrorDescription($ex->getStderr());
if ($err === 1) {
// `git grep` and `hg grep` exit with 1 if there are no matches;
// assume we just didn't get any hits.
return array();
}
throw $ex;
} }
$offset = $request->getValue('offset'); $offset = $request->getValue('offset');
@ -63,6 +64,7 @@ final class DiffusionSearchQueryConduitAPIMethod
$binary_pattern = '/Binary file [^:]*:(.+) matches/'; $binary_pattern = '/Binary file [^:]*:(.+) matches/';
$lines = new LinesOfALargeExecFuture($future); $lines = new LinesOfALargeExecFuture($future);
foreach ($lines as $line) { foreach ($lines as $line) {
$result = null; $result = null;
if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) { if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) {

View file

@ -351,19 +351,16 @@ final class DiffusionBrowseController extends DiffusionController {
} }
private function renderSearchResults() { private function renderSearchResults() {
$request = $this->getRequest();
$drequest = $this->getDiffusionRequest(); $drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository(); $repository = $drequest->getRepository();
$results = array(); $results = array();
$limit = 100; $pager = id(new PHUIPagerView())
$page = $this->getRequest()->getInt('page', 0); ->readFromRequest($request);
$pager = new PHUIPagerView();
$pager->setPageSize($limit);
$pager->setOffset($page);
$pager->setURI($this->getRequest()->getRequestURI(), 'page');
$search_mode = null; $search_mode = null;
switch ($repository->getVersionControlSystem()) { switch ($repository->getVersionControlSystem()) {
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
$results = array(); $results = array();
@ -371,32 +368,31 @@ final class DiffusionBrowseController extends DiffusionController {
default: default:
if (strlen($this->getRequest()->getStr('grep'))) { if (strlen($this->getRequest()->getStr('grep'))) {
$search_mode = 'grep'; $search_mode = 'grep';
$query_string = $this->getRequest()->getStr('grep'); $query_string = $request->getStr('grep');
$results = $this->callConduitWithDiffusionRequest( $results = $this->callConduitWithDiffusionRequest(
'diffusion.searchquery', 'diffusion.searchquery',
array( array(
'grep' => $query_string, 'grep' => $query_string,
'commit' => $drequest->getStableCommit(), 'commit' => $drequest->getStableCommit(),
'path' => $drequest->getPath(), 'path' => $drequest->getPath(),
'limit' => $limit + 1, 'limit' => $pager->getPageSize() + 1,
'offset' => $page, 'offset' => $pager->getOffset(),
)); ));
} else { // Filename search. } else { // Filename search.
$search_mode = 'find'; $search_mode = 'find';
$query_string = $this->getRequest()->getStr('find'); $query_string = $request->getStr('find');
$results = $this->callConduitWithDiffusionRequest( $results = $this->callConduitWithDiffusionRequest(
'diffusion.querypaths', 'diffusion.querypaths',
array( array(
'pattern' => $query_string, 'pattern' => $query_string,
'commit' => $drequest->getStableCommit(), 'commit' => $drequest->getStableCommit(),
'path' => $drequest->getPath(), 'path' => $drequest->getPath(),
'limit' => $limit + 1, 'limit' => $pager->getPageSize() + 1,
'offset' => $page, 'offset' => $pager->getOffset(),
)); ));
} }
break; break;
} }
$results = $pager->sliceResults($results); $results = $pager->sliceResults($results);
if ($search_mode == 'grep') { if ($search_mode == 'grep') {