Merge pull request #225 from marcoscoelho/path-to-avoid-errors

Path to avoid errors
This commit is contained in:
Klaus Silveira
2012-12-20 09:00:45 -08:00
5 changed files with 64 additions and 13 deletions

View File

@@ -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');

View File

@@ -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,

View File

@@ -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:\"<item><hash>%H</hash><short_hash>%h</short_hash><tree>%T</tree><parents>%P</parents><author>%an</author><author_email>%ae</author_email><date>%at</date><commiter>%cn</commiter><commiter_email>%ce</commiter_email><commiter_date>%ct</commiter_date><message><![CDATA[%s]]></message></item>\" $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:'<item><hash>%H</hash><short_hash>%h</short_hash><tree>%T</tree><parent>%P</parent><author>%an</author><author_email>%ae</author_email><date>%at</date><commiter>%cn</commiter><commiter_email>%ce</commiter_email><commiter_date>%ct</commiter_date><message><![CDATA[%s]]></message></item>'";
$command = "log $pager --pretty=format:\"<item><hash>%H</hash><short_hash>%h</short_hash><tree>%T</tree><parent>%P</parent><author>%an</author><author_email>%ae</author_email><date>%at</date><commiter>%cn</commiter><commiter_email>%ce</commiter_email><commiter_date>%ct</commiter_date><message><![CDATA[%s]]></message></item>\"";
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:'<item><hash>%H</hash><short_hash>%h</short_hash><tree>%T</tree><parent>%P</parent><author>%an</author><author_email>%ae</author_email><date>%at</date><commiter>%cn</commiter><commiter_email>%ce</commiter_email><commiter_date>%ct</commiter_date><message><![CDATA[%s]]></message></item>'";
$query = escapeshellarg($query);
$command = "log --grep={$query} --pretty=format:\"<item><hash>%H</hash><short_hash>%h</short_hash><tree>%T</tree><parent>%P</parent><author>%an</author><author_email>%ae</author_email><date>%at</date><commiter>%cn</commiter><commiter_email>%ce</commiter_email><commiter_date>%ct</commiter_date><message><![CDATA[%s]]></message></item>\"";
$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;
}

View File

@@ -1,3 +1,4 @@
{% if commits %}
{% for date, commit in commits %}
<table class="table table-striped table-bordered">
<thead>
@@ -19,6 +20,9 @@
</tbody>
</table>
{% endfor %}
{% else %}
<p>No results found.</p>
{% endif %}
{% if page != 'searchcommits' %}
<ul class="pager">

View File

@@ -5,7 +5,7 @@
{% block title %}GitList{% endblock %}
{% block content %}
{% include 'breadcrumb.twig' with {breadcrumbs: [{dir: 'Commits search results', path:''}]} %}
{% include 'breadcrumb.twig' with {breadcrumbs: [{dir: 'Commits search results for: ' ~ query, path:''}]} %}
{% include 'commits_list.twig' %}