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

Basic filename search support for Diffusion

Summary:
Ref T156. Adds basic filename search support for Diffusion,
currently only for Git repositories.

This is preliminary, and it's up for discussion:
  - is the UI in the right place;
  - what should the search query syntax be (e.g. whether
    to put `*`s in the beginning and end of it);
  - how to best approach it for Mercurial and/or SVN;
  - what's the cleanest result format for `lsquery` (I went
    for the minimum necessary change to `DiffusionBrowseSearchController`).

Test Plan:
Browse to a repository in Diffusion, and use both
`Search File Names` and `Search File Content`.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley

CC: Korvin, epriestley, aran

Maniphest Tasks: T156

Differential Revision: https://secure.phabricator.com/D8093
This commit is contained in:
Vlad Albulescu 2014-02-01 08:32:23 -08:00 committed by epriestley
parent 99676e8e33
commit 2d27324bef
5 changed files with 116 additions and 12 deletions

View file

@ -169,6 +169,7 @@ phutil_register_library_map(array(
'ConduitAPI_diffusion_looksoon_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_looksoon_Method.php',
'ConduitAPI_diffusion_mergedcommitsquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_mergedcommitsquery_Method.php',
'ConduitAPI_diffusion_querycommits_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_querycommits_Method.php',
'ConduitAPI_diffusion_querypaths_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_querypaths_Method.php',
'ConduitAPI_diffusion_rawdiffquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_rawdiffquery_Method.php',
'ConduitAPI_diffusion_readmequery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_readmequery_Method.php',
'ConduitAPI_diffusion_refsquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_refsquery_Method.php',
@ -2657,6 +2658,7 @@ phutil_register_library_map(array(
'ConduitAPI_diffusion_looksoon_Method' => 'ConduitAPI_diffusion_Method',
'ConduitAPI_diffusion_mergedcommitsquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_querycommits_Method' => 'ConduitAPI_diffusion_Method',
'ConduitAPI_diffusion_querypaths_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_rawdiffquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_readmequery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',
'ConduitAPI_diffusion_refsquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method',

View file

@ -0,0 +1,87 @@
<?php
final class ConduitAPI_diffusion_querypaths_Method
extends ConduitAPI_diffusion_abstractquery_Method {
public function getMethodDescription() {
return pht('Filename search on a repository.');
}
public function defineReturnType() {
return 'list<string>';
}
protected function defineCustomParamTypes() {
return array(
'path' => 'required string',
'commit' => 'required string',
'pattern' => 'required string',
'limit' => 'optional int',
'offset' => 'optional int',
);
}
protected function getResult(ConduitAPIRequest $request) {
$results = parent::getResult($request);
$offset = $request->getValue('offset');
return array_slice($results, $offset);
}
protected function getGitResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$path = $drequest->getPath();
$commit = $request->getValue('commit');
$repository = $drequest->getRepository();
// http://comments.gmane.org/gmane.comp.version-control.git/197735
$future = $repository->getLocalCommandFuture(
'ls-tree --name-only -r -z %s -- %s',
$commit,
$path);
$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");
return $this->filterResults($lines, $request);
}
protected function getMercurialResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$path = $request->getValue('path');
$commit = $request->getValue('commit');
// Adapted from diffusion.browsequery.
list($entire_manifest) = $repository->execxLocalCommand(
'manifest --rev %s',
hgsprintf('%s', $commit));
$entire_manifest = explode("\n", $entire_manifest);
$match_against = trim($path, '/');
$match_len = strlen($match_against);
$lines = array();
foreach ($entire_manifest as $path) {
if (strlen($path) && !strncmp($path, $match_against, $match_len)) {
$lines[] = $path;
}
}
return $this->filterResults($lines, $request);
}
protected function filterResults($lines, ConduitAPIRequest $request) {
$pattern = $request->getValue('pattern');
$limit = $request->getValue('limit');
$offset = $request->getValue('offset');
$results = array();
foreach ($lines as $line) {
if (preg_match('#'.str_replace('#', '\#', $pattern).'#', $line)) {
$results[] = $line;
if (count($results) >= $offset + $limit) {
break;
}
}
}
return $results;
}
}

View file

@ -26,8 +26,9 @@ abstract class DiffusionBrowseController extends DiffusionController {
->setValue($this->getRequest()->getStr('grep'))
->setCaption(pht('Enter a regular expression.')))
->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Search File Content')));
id(new PHUIFormMultiSubmitControl())
->addButton('__ls__', pht('Search File Names'))
->addButton('__grep__', pht('Search File Content')));
break;
}

View file

@ -53,16 +53,29 @@ final class DiffusionBrowseSearchController extends DiffusionBrowseController {
$pager->setURI($this->getRequest()->getRequestURI(), 'page');
try {
$results = $this->callConduitWithDiffusionRequest(
'diffusion.searchquery',
array(
'grep' => $this->getRequest()->getStr('grep'),
'stableCommitName' => $drequest->getStableCommitName(),
'path' => $drequest->getPath(),
'limit' => $limit + 1,
'offset' => $page));
if ($this->getRequest()->getStr('__grep__')) {
$results = $this->callConduitWithDiffusionRequest(
'diffusion.searchquery',
array(
'grep' => $this->getRequest()->getStr('grep'),
'stableCommitName' => $drequest->getStableCommitName(),
'path' => $drequest->getPath(),
'limit' => $limit + 1,
'offset' => $page));
} else { // Filename search.
$results_raw = $this->callConduitWithDiffusionRequest(
'diffusion.querypaths',
array(
'pattern' => $this->getRequest()->getStr('grep'),
'commit' => $drequest->getStableCommitName(),
'path' => $drequest->getPath(),
'limit' => $limit + 1,
'offset' => $page));
$results = [];
foreach ($results_raw as $result) {
$results[] = array($result, null, null);
}
}
} catch (ConduitException $ex) {
$err = $ex->getErrorDescription();
if ($err != '') {

View file

@ -41,6 +41,7 @@ final class PHUIFormMultiSubmitControl extends AphrontFormControl {
'class' => $class,
'disabled' => $this->getDisabled() ? 'disabled' : null,
));
return $this;
}
protected function getCustomControlClass() {