diff --git a/src/GitList/Controller/CommitController.php b/src/GitList/Controller/CommitController.php index 8d67687..c2c68d0 100644 --- a/src/GitList/Controller/CommitController.php +++ b/src/GitList/Controller/CommitController.php @@ -20,6 +20,7 @@ class CommitController implements ControllerProviderInterface $type = $file ? "$branch -- \"$file\"" : $branch; $pager = $app['util.view']->getPager($app['request']->get('page'), $repository->getTotalCommits($type)); $commits = $repository->getPaginatedCommits($type, $pager['current']); + $categorized = array(); foreach ($commits as $commit) { $date = $commit->getDate(); @@ -48,7 +49,9 @@ class CommitController implements ControllerProviderInterface $route->post('{repo}/commits/search', function(Request $request, $repo) use ($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); - $commits = $repository->searchCommitLog($request->get('query')); + $query = $request->get('query'); + $commits = $repository->searchCommitLog($query); + $categorized = array(); foreach ($commits as $commit) { $date = $commit->getDate(); @@ -63,6 +66,7 @@ class CommitController implements ControllerProviderInterface 'commits' => $categorized, 'branches' => $repository->getBranches(), 'tags' => $repository->getTags(), + 'query' => $query )); })->assert('repo', $app['util.routing']->getRepositoryRegex()) ->bind('searchcommits'); diff --git a/src/GitList/Controller/TreeController.php b/src/GitList/Controller/TreeController.php index fe463b7..86be94b 100644 --- a/src/GitList/Controller/TreeController.php +++ b/src/GitList/Controller/TreeController.php @@ -53,8 +53,9 @@ class TreeController implements ControllerProviderInterface $branch = $repository->getHead(); } - $breadcrumbs = $app['util.view']->getBreadcrumbs($tree); - $results = $repository->searchTree($request->get('query'), $branch); + $query = $request->get('query'); + $breadcrumbs = array(array('dir' => 'Search results for: ' . $query, 'path' => '')); + $results = $repository->searchTree($query, $branch); return $app['twig']->render('search.twig', array( 'results' => $results, diff --git a/src/GitList/Git/Repository.php b/src/GitList/Git/Repository.php index 4c0cf91..50d1f37 100644 --- a/src/GitList/Git/Repository.php +++ b/src/GitList/Git/Repository.php @@ -4,10 +4,39 @@ namespace GitList\Git; use Gitter\Repository as BaseRepository; use Gitter\Model\Commit\Commit; +use Gitter\PrettyFormat; use Symfony\Component\Filesystem\Filesystem; class Repository extends BaseRepository { + + /** + * Show the data from a specific commit + * + * @param string $commitHash Hash of the specific commit to read data + * @return array Commit data + */ + public function getCommit($commitHash) + { + $logs = $this->getClient()->run($this, "show --pretty=format:\"%H%h%T%P%an%ae%at%cn%ce%ct\" $commitHash"); + $logs = explode("\n", $logs); + + // Read commit metadata + $format = new PrettyFormat; + $data = $format->parse($logs[0]); + $commit = new Commit; + $commit->importData($data[0]); + unset($logs[0]); + + if (empty($logs[1])) { + $logs = explode("\n", $this->getClient()->run($this, 'diff ' . $commitHash . '~1..' . $commitHash)); + } + + $commit->setDiffs($this->readDiffLogs($logs)); + + return $commit; + } + /** * Show the repository commit log with pagination * @@ -18,13 +47,17 @@ class Repository extends BaseRepository { $page = 15 * $page; $pager = "--skip=$page --max-count=15"; - $command = "log $pager --pretty=format:'%H%h%T%P%an%ae%at%cn%ce%ct'"; + $command = "log $pager --pretty=format:\"%H%h%T%P%an%ae%at%cn%ce%ct\""; if ($file) { $command .= " $file"; } - $logs = $this->getPrettyFormat($command); + try { + $logs = $this->getPrettyFormat($command); + } catch (\RuntimeException $e) { + return array(); + } foreach ($logs as $log) { $commit = new Commit; @@ -37,9 +70,14 @@ class Repository extends BaseRepository public function searchCommitLog($query) { - $command = "log --grep='$query' --pretty=format:'%H%h%T%P%an%ae%at%cn%ce%ct'"; + $query = escapeshellarg($query); + $command = "log --grep={$query} --pretty=format:\"%H%h%T%P%an%ae%at%cn%ce%ct\""; - $logs = $this->getPrettyFormat($command); + try { + $logs = $this->getPrettyFormat($command); + } catch (\RuntimeException $e) { + return array(); + } foreach ($logs as $log) { $commit = new Commit; @@ -52,8 +90,10 @@ class Repository extends BaseRepository public function searchTree($query, $branch) { + $query = escapeshellarg($query); + try { - $results = $this->getClient()->run($this, "grep -I --line-number '$query' $branch"); + $results = $this->getClient()->run($this, "grep -I --line-number {$query} $branch"); } catch (\RuntimeException $e) { return false; } @@ -65,11 +105,13 @@ class Repository extends BaseRepository continue; } - preg_match_all('/([\w-._]+):(.+):([0-9]+):(.+)/', $result, $matches, PREG_SET_ORDER); + preg_match_all('/([\w-._]+):([^:]+):([0-9]+):(.+)/', $result, $matches, PREG_SET_ORDER); + $data['branch'] = $matches[0][1]; - $data['file'] = $matches[0][2]; - $data['line'] = $matches[0][3]; - $data['match'] = $matches[0][4]; + $data['file'] = $matches[0][2]; + $data['line'] = $matches[0][3]; + $data['match'] = $matches[0][4]; + $searchResults[] = $data; } diff --git a/views/commits_list.twig b/views/commits_list.twig index 959d9ff..cdab179 100644 --- a/views/commits_list.twig +++ b/views/commits_list.twig @@ -1,3 +1,4 @@ +{% if commits %} {% for date, commit in commits %} @@ -19,6 +20,9 @@
{% endfor %} +{% else %} +

No results found.

+{% endif %} {% if page != 'searchcommits' %}