1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

Allow searching in repositories

Summary: We may later integrate it in the global search but I want to leave it here too for the case that you want to search just some repository or some part of it.

Test Plan: Browsed repo in SVN and Git, searched in Git.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin, chad

Differential Revision: https://secure.phabricator.com/D5738
This commit is contained in:
Jakub Vrana 2013-04-23 11:12:02 -07:00
parent 5c0333cef9
commit eff3089650
2 changed files with 219 additions and 45 deletions

View file

@ -4,11 +4,11 @@ final class DiffusionBrowseController extends DiffusionController {
public function processRequest() {
$drequest = $this->diffusionRequest;
$is_file = false;
if ($this->getRequest()->getStr('before')) {
$results = array();
$is_file = true;
} else {
} else if ($this->getRequest()->getStr('grep') == '') {
$browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
$browse_query->setViewer($this->getRequest()->getUser());
$results = $browse_query->loadPaths();
@ -16,6 +16,13 @@ final class DiffusionBrowseController extends DiffusionController {
$is_file = ($reason == DiffusionBrowseQuery::REASON_IS_FILE);
}
if ($is_file) {
$controller = new DiffusionBrowseFileController($this->getRequest());
$controller->setDiffusionRequest($drequest);
$controller->setCurrentApplication($this->getCurrentApplication());
return $this->delegateToController($controller);
}
$content = array();
if ($drequest->getTagContent()) {
@ -29,61 +36,59 @@ final class DiffusionBrowseController extends DiffusionController {
$content[] = $tag_view;
}
if (!$results) {
$content[] = $this->renderSearchForm();
if ($is_file) {
$controller = new DiffusionBrowseFileController($this->getRequest());
$controller->setDiffusionRequest($drequest);
$controller->setCurrentApplication($this->getCurrentApplication());
return $this->delegateToController($controller);
}
$empty_result = new DiffusionEmptyResultView();
$empty_result->setDiffusionRequest($drequest);
$empty_result->setBrowseQuery($browse_query);
$empty_result->setView($this->getRequest()->getStr('view'));
$content[] = $empty_result;
if ($this->getRequest()->getStr('grep') != '') {
$content[] = $this->renderSearchResults();
} else {
if (!$results) {
$empty_result = new DiffusionEmptyResultView();
$empty_result->setDiffusionRequest($drequest);
$empty_result->setBrowseQuery($browse_query);
$empty_result->setView($this->getRequest()->getStr('view'));
$content[] = $empty_result;
$phids = array();
foreach ($results as $result) {
$data = $result->getLastCommitData();
if ($data) {
if ($data->getCommitDetail('authorPHID')) {
$phids[$data->getCommitDetail('authorPHID')] = true;
} else {
$phids = array();
foreach ($results as $result) {
$data = $result->getLastCommitData();
if ($data) {
if ($data->getCommitDetail('authorPHID')) {
$phids[$data->getCommitDetail('authorPHID')] = true;
}
}
}
$phids = array_keys($phids);
$handles = $this->loadViewerHandles($phids);
$browse_table = new DiffusionBrowseTableView();
$browse_table->setDiffusionRequest($drequest);
$browse_table->setHandles($handles);
$browse_table->setPaths($results);
$browse_table->setUser($this->getRequest()->getUser());
$browse_panel = new AphrontPanelView();
$browse_panel->appendChild($browse_table);
$browse_panel->setNoBackground();
$content[] = $browse_panel;
}
$phids = array_keys($phids);
$handles = $this->loadViewerHandles($phids);
$content[] = $this->buildOpenRevisions();
$browse_table = new DiffusionBrowseTableView();
$browse_table->setDiffusionRequest($drequest);
$browse_table->setHandles($handles);
$browse_table->setPaths($results);
$browse_table->setUser($this->getRequest()->getUser());
$readme_content = $browse_query->renderReadme($results);
if ($readme_content) {
$readme_panel = new AphrontPanelView();
$readme_panel->setHeader('README');
$readme_panel->appendChild($readme_content);
$browse_panel = new AphrontPanelView();
$browse_panel->appendChild($browse_table);
$browse_panel->setNoBackground();
$content[] = $browse_panel;
$content[] = $readme_panel;
}
}
$content[] = $this->buildOpenRevisions();
$readme_content = $browse_query->renderReadme($results);
if ($readme_content) {
$readme_panel = new AphrontPanelView();
$readme_panel->setHeader('README');
$readme_panel->appendChild($readme_content);
$content[] = $readme_panel;
}
$nav = $this->buildSideNav('browse', false);
$nav->appendChild($content);
@ -105,6 +110,174 @@ final class DiffusionBrowseController extends DiffusionController {
));
}
private function renderSearchForm() {
$drequest = $this->getDiffusionRequest();
$form = id(new AphrontFormView())
->setUser($this->getRequest()->getUser())
->setMethod('GET');
switch ($drequest->getRepository()->getVersionControlSystem()) {
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
$form->appendChild(pht('Search is not available in Subversion.'));
break;
default:
$form
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Search Here'))
->setName('grep')
->setValue($this->getRequest()->getStr('grep'))
->setCaption(pht('Regular expression')))
->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Grep')));
break;
}
return $form;
}
private function renderSearchResults() {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$results = array();
$no_data = pht('No results found.');
$limit = 100;
$page = $this->getRequest()->getInt('page', 0);
$pager = new AphrontPagerView();
$pager->setPageSize($limit);
$pager->setOffset($page);
$pager->setURI($this->getRequest()->getRequestURI(), 'page');
try {
switch ($repository->getVersionControlSystem()) {
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
$future = $repository->getLocalCommandFuture(
// NOTE: --perl-regexp is available only with libpcre compiled in.
'grep --extended-regexp --null -n --no-color -e %s %s -- %s',
$this->getRequest()->getStr('grep'),
$drequest->getStableCommitName(),
$drequest->getPath());
$binary_pattern = '/Binary file [^:]*:(.+) matches/';
$lines = new LinesOfALargeExecFuture($future);
foreach ($lines as $line) {
$result = null;
if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) {
$results[] = array_slice($result, 1);
} else if (preg_match($binary_pattern, $line, $result)) {
list(, $path) = $result;
$results[] = array($path, null, pht('Binary file'));
} else {
$results[] = array(null, null, $line);
}
if (count($results) > $page + $limit) {
break;
}
}
unset($lines);
break;
case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
$future = $repository->getLocalCommandFuture(
'grep --rev %s --print0 --line-number %s %s',
hgsprintf('ancestors(%s)', $drequest->getStableCommitName()),
$this->getRequest()->getStr('grep'),
$drequest->getPath());
$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");
$parts = array();
foreach ($lines as $line) {
$parts[] = $line;
if (count($parts) == 4) {
list($path, $offset, $line, $string) = $parts;
$results[] = array($path, $line, $string);
if (count($results) > $page + $limit) {
break;
}
$parts = array();
}
}
unset($lines);
break;
}
} catch (CommandException $ex) {
return id(new AphrontErrorView())
->setTitle(pht('Search Error'))
->appendChild($ex->getStderr());
}
$results = array_slice($results, $page);
$results = $pager->sliceResults($results);
require_celerity_resource('syntax-highlighting-css');
// NOTE: This can be wrong because we may find the string inside the
// comment. But it's correct in most cases and highlighting the whole file
// would be too expensive.
$futures = array();
$engine = PhabricatorSyntaxHighlighter::newEngine();
foreach ($results as $result) {
list($path, $line, $string) = $result;
$futures["{$path}:{$line}"] = $engine->getHighlightFuture(
$engine->getLanguageFromFilename($path),
ltrim($string));
}
try {
Futures($futures)->limit(8)->resolveAll();
} catch (PhutilSyntaxHighlighterException $ex) {
}
$rows = array();
foreach ($results as $result) {
list($path, $line, $string) = $result;
$href = $drequest->generateURI(array(
'action' => 'browse',
'path' => $path,
'line' => $line,
));
try {
$string = $futures["{$path}:{$line}"]->resolve();
} catch (PhutilSyntaxHighlighterException $ex) {
}
$string = phutil_tag(
'pre',
array('class' => 'PhabricatorMonospaced'),
$string);
$path = Filesystem::readablePath($path, $drequest->getPath());
$rows[] = array(
phutil_tag('a', array('href' => $href), $path),
$line,
$string,
);
}
$table = id(new AphrontTableView($rows))
->setClassName('remarkup-code')
->setHeaders(array(pht('Path'), pht('Line'), pht('String')))
->setColumnClasses(array('', 'n', 'wide'))
->setNoDataString($no_data);
return id(new AphrontPanelView())
->setHeader(pht('Search Results'))
->appendChild($table)
->appendChild($pager);
}
private function markupText($text) {
$engine = PhabricatorMarkupEngine::newDiffusionMarkupEngine();
$engine->setConfig('viewer', $this->getRequest()->getUser());

View file

@ -72,6 +72,7 @@ abstract class DiffusionBrowseQuery {
}
final public function loadPaths() {
$this->reason = null;
return $this->executeQuery();
}