@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Diffusion - move search (grep) system commands to happen over Conduit

Summary: Ref T2784.

Test Plan: loaded up my git and mercurial copies of Phabricator. Searched for "diff". Observed many results and pagination working correctly.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2784

Differential Revision: https://secure.phabricator.com/D5955

+131 -58
+2
src/__phutil_library_map__.php
··· 157 157 'ConduitAPI_diffusion_getrecentcommitsbypath_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_getrecentcommitsbypath_Method.php', 158 158 'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_lastmodifiedquery_Method.php', 159 159 'ConduitAPI_diffusion_rawdiffquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_rawdiffquery_Method.php', 160 + 'ConduitAPI_diffusion_searchquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_searchquery_Method.php', 160 161 'ConduitAPI_diffusion_tagsquery_Method' => 'applications/diffusion/conduit/ConduitAPI_diffusion_tagsquery_Method.php', 161 162 'ConduitAPI_feed_Method' => 'applications/feed/conduit/ConduitAPI_feed_Method.php', 162 163 'ConduitAPI_feed_publish_Method' => 'applications/feed/conduit/ConduitAPI_feed_publish_Method.php', ··· 1955 1956 'ConduitAPI_diffusion_getrecentcommitsbypath_Method' => 'ConduitAPI_diffusion_Method', 1956 1957 'ConduitAPI_diffusion_lastmodifiedquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 1957 1958 'ConduitAPI_diffusion_rawdiffquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 1959 + 'ConduitAPI_diffusion_searchquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 1958 1960 'ConduitAPI_diffusion_tagsquery_Method' => 'ConduitAPI_diffusion_abstractquery_Method', 1959 1961 'ConduitAPI_feed_Method' => 'ConduitAPIMethod', 1960 1962 'ConduitAPI_feed_publish_Method' => 'ConduitAPI_feed_Method',
+117
src/applications/diffusion/conduit/ConduitAPI_diffusion_searchquery_Method.php
··· 1 + <?php 2 + 3 + /** 4 + * @group conduit 5 + */ 6 + final class ConduitAPI_diffusion_searchquery_Method 7 + extends ConduitAPI_diffusion_abstractquery_Method { 8 + 9 + public function getMethodDescription() { 10 + return 'Search (grep) a repository at a specific path and commit.'; 11 + } 12 + 13 + public function defineReturnType() { 14 + return 'array'; 15 + } 16 + 17 + protected function defineCustomParamTypes() { 18 + return array( 19 + 'path' => 'required string', 20 + 'stableCommitName' => 'required string', 21 + 'grep' => 'required string', 22 + 'limit' => 'optional int', 23 + 'offset' => 'optional int', 24 + ); 25 + } 26 + 27 + protected function defineCustomErrorTypes() { 28 + return array( 29 + 'ERR-GREP-COMMAND' => 'Grep command failed.'); 30 + } 31 + 32 + protected function getResult(ConduitAPIRequest $request) { 33 + try { 34 + $results = parent::getResult($request); 35 + } catch (CommandException $ex) { 36 + throw id(new ConduitException('ERR-GREP-COMMAND')) 37 + ->setErrorDescription($ex->getStderr()); 38 + } 39 + 40 + $offset = $request->getValue('offset'); 41 + $results = array_slice($results, $offset); 42 + 43 + return $results; 44 + } 45 + 46 + protected function getGitResult(ConduitAPIRequest $request) { 47 + $drequest = $this->getDiffusionRequest(); 48 + $path = $drequest->getPath(); 49 + $stable_commit_name = $request->getValue('stableCommitName'); 50 + $grep = $request->getValue('grep'); 51 + $repository = $drequest->getRepository(); 52 + $limit = $request->getValue('limit'); 53 + $offset = $request->getValue('offset'); 54 + 55 + $results = array(); 56 + $future = $repository->getLocalCommandFuture( 57 + // NOTE: --perl-regexp is available only with libpcre compiled in. 58 + 'grep --extended-regexp --null -n --no-color -e %s %s -- %s', 59 + $grep, 60 + $stable_commit_name, 61 + $path); 62 + 63 + $binary_pattern = '/Binary file [^:]*:(.+) matches/'; 64 + $lines = new LinesOfALargeExecFuture($future); 65 + foreach ($lines as $line) { 66 + $result = null; 67 + if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) { 68 + $results[] = array_slice($result, 1); 69 + } else if (preg_match($binary_pattern, $line, $result)) { 70 + list(, $path) = $result; 71 + $results[] = array($path, null, pht('Binary file')); 72 + } else { 73 + $results[] = array(null, null, $line); 74 + } 75 + if (count($results) >= $offset + $limit) { 76 + break; 77 + } 78 + } 79 + unset($lines); 80 + 81 + return $results; 82 + } 83 + 84 + protected function getMercurialResult(ConduitAPIRequest $request) { 85 + $drequest = $this->getDiffusionRequest(); 86 + $path = $drequest->getPath(); 87 + $stable_commit_name = $request->getValue('stableCommitName'); 88 + $grep = $request->getValue('grep'); 89 + $repository = $drequest->getRepository(); 90 + $limit = $request->getValue('limit'); 91 + $offset = $request->getValue('offset'); 92 + 93 + $results = array(); 94 + $future = $repository->getLocalCommandFuture( 95 + 'grep --rev %s --print0 --line-number %s %s', 96 + hgsprintf('ancestors(%s)', $stable_commit_name), 97 + $grep, 98 + $path); 99 + 100 + $lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0"); 101 + $parts = array(); 102 + foreach ($lines as $line) { 103 + $parts[] = $line; 104 + if (count($parts) == 4) { 105 + list($path, $char_offset, $line, $string) = $parts; 106 + $results[] = array($path, $line, $string); 107 + if (count($results) >= $offset + $limit) { 108 + break; 109 + } 110 + $parts = array(); 111 + } 112 + } 113 + unset($lines); 114 + 115 + return $results; 116 + } 117 + }
+12 -58
src/applications/diffusion/controller/DiffusionBrowseController.php
··· 171 171 172 172 try { 173 173 174 - switch ($repository->getVersionControlSystem()) { 175 - case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: 176 - $future = $repository->getLocalCommandFuture( 177 - // NOTE: --perl-regexp is available only with libpcre compiled in. 178 - 'grep --extended-regexp --null -n --no-color -e %s %s -- %s', 179 - $this->getRequest()->getStr('grep'), 180 - $drequest->getStableCommitName(), 181 - $drequest->getPath()); 182 - 183 - $binary_pattern = '/Binary file [^:]*:(.+) matches/'; 184 - $lines = new LinesOfALargeExecFuture($future); 185 - foreach ($lines as $line) { 186 - $result = null; 187 - if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) { 188 - $results[] = array_slice($result, 1); 189 - } else if (preg_match($binary_pattern, $line, $result)) { 190 - list(, $path) = $result; 191 - $results[] = array($path, null, pht('Binary file')); 192 - } else { 193 - $results[] = array(null, null, $line); 194 - } 195 - if (count($results) > $page + $limit) { 196 - break; 197 - } 198 - } 199 - unset($lines); 200 - 201 - break; 202 - 203 - case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: 204 - $future = $repository->getLocalCommandFuture( 205 - 'grep --rev %s --print0 --line-number %s %s', 206 - hgsprintf('ancestors(%s)', $drequest->getStableCommitName()), 207 - $this->getRequest()->getStr('grep'), 208 - $drequest->getPath()); 174 + $results = $this->callConduitWithDiffusionRequest( 175 + 'diffusion.searchquery', 176 + array( 177 + 'grep' => $this->getRequest()->getStr('grep'), 178 + 'stableCommitName' => $drequest->getStableCommitName(), 179 + 'path' => $drequest->getPath(), 180 + 'limit' => $limit + 1, 181 + 'offset' => $page)); 209 182 210 - $lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0"); 211 - $parts = array(); 212 - foreach ($lines as $line) { 213 - $parts[] = $line; 214 - if (count($parts) == 4) { 215 - list($path, $offset, $line, $string) = $parts; 216 - $results[] = array($path, $line, $string); 217 - if (count($results) > $page + $limit) { 218 - break; 219 - } 220 - $parts = array(); 221 - } 222 - } 223 - unset($lines); 224 - 225 - break; 226 - } 227 - 228 - } catch (CommandException $ex) { 229 - $stderr = $ex->getStderr(); 230 - if ($stderr != '') { 183 + } catch (ConduitException $ex) { 184 + $err = $ex->getErrorDescription(); 185 + if ($err != '') { 231 186 return id(new AphrontErrorView()) 232 187 ->setTitle(pht('Search Error')) 233 - ->appendChild($stderr); 188 + ->appendChild($err); 234 189 } 235 190 } 236 191 237 - $results = array_slice($results, $page); 238 192 $results = $pager->sliceResults($results); 239 193 240 194 require_celerity_resource('syntax-highlighting-css');