Adding very simple pagination to commits, both repository and history. Fixes #4

This commit is contained in:
Klaus Silveira
2012-05-23 03:10:37 -03:00
parent 64c089ef87
commit 3f3c25bce8
4 changed files with 29 additions and 5 deletions

View File

@@ -2,7 +2,9 @@
$app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) { $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo); $repository = $app['git']->getRepository($app['git.repos'] . $repo);
$commits = $repository->getCommits(); $pagenumber = $app['request']->get('page');
$pagenumber = (empty($pagenumber)) ? 0 : $pagenumber;
$commits = $repository->getCommits(null, $pagenumber);
foreach ($commits as $commit) { foreach ($commits as $commit) {
$date = $commit->getDate(); $date = $commit->getDate();
@@ -13,6 +15,7 @@ $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) {
return $app['twig']->render('commits.twig', array( return $app['twig']->render('commits.twig', array(
'baseurl' => $app['baseurl'], 'baseurl' => $app['baseurl'],
'page' => 'commits', 'page' => 'commits',
'pagenumber' => $pagenumber,
'repo' => $repo, 'repo' => $repo,
'branch' => $branch, 'branch' => $branch,
'branches' => $repository->getBranches(), 'branches' => $repository->getBranches(),
@@ -25,7 +28,9 @@ $app->get('{repo}/commits/{branch}', function($repo, $branch) use($app) {
$app->get('{repo}/commits/{branch}/{file}/', function($repo, $branch, $file) use($app) { $app->get('{repo}/commits/{branch}/{file}/', function($repo, $branch, $file) use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo); $repository = $app['git']->getRepository($app['git.repos'] . $repo);
$commits = $repository->getCommits($file); $pagenumber = $app['request']->get('page');
$pagenumber = (empty($pagenumber)) ? 0 : $pagenumber;
$commits = $repository->getCommits($file, $pagenumber);
foreach ($commits as $commit) { foreach ($commits as $commit) {
$date = $commit->getDate(); $date = $commit->getDate();
@@ -36,6 +41,7 @@ $app->get('{repo}/commits/{branch}/{file}/', function($repo, $branch, $file) use
return $app['twig']->render('commits.twig', array( return $app['twig']->render('commits.twig', array(
'baseurl' => $app['baseurl'], 'baseurl' => $app['baseurl'],
'page' => 'commits', 'page' => 'commits',
'pagenumber' => $pagenumber,
'repo' => $repo, 'repo' => $repo,
'branch' => $branch, 'branch' => $branch,
'branches' => $repository->getBranches(), 'branches' => $repository->getBranches(),

View File

@@ -22,7 +22,7 @@ $app['autoloader']->registerNamespace('Application', __DIR__.'/lib');
$app->register(new Silex\Provider\TwigServiceProvider(), array( $app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views', 'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor', 'twig.class_path' => __DIR__.'/vendor',
'twig.options' => array('cache' => __DIR__.'/cache'), // 'twig.options' => array('cache' => __DIR__.'/cache'),
)); ));
$app->register(new Git\GitServiceProvider(), array( $app->register(new Git\GitServiceProvider(), array(
'git.client' => $config['git']['client'], 'git.client' => $config['git']['client'],

View File

@@ -222,15 +222,22 @@ class Repository
* @access public * @access public
* @return array Commit log * @return array Commit log
*/ */
public function getCommits($file = null) public function getCommits($file = null, $page = 0)
{ {
$command = 'log --pretty=format:\'"%h": {"hash": "%H", "short_hash": "%h", "tree": "%T", "parent": "%P", "author": "%an", "author_email": "%ae", "date": "%at", "commiter": "%cn", "commiter_email": "%ce", "commiter_date": "%ct", "message": "%f"}\''; $page = 15 * $page;
$pager = "--skip=$page --max-count=15";
$command = 'log ' . $pager . ' --pretty=format:\'"%h": {"hash": "%H", "short_hash": "%h", "tree": "%T", "parent": "%P", "author": "%an", "author_email": "%ae", "date": "%at", "commiter": "%cn", "commiter_email": "%ce", "commiter_date": "%ct", "message": "%f"}\'';
if ($file) { if ($file) {
$command .= " $file"; $command .= " $file";
} }
$logs = $this->getClient()->run($this, $command); $logs = $this->getClient()->run($this, $command);
if (empty($logs)) {
throw new \RuntimeException('No commit log available');
}
$logs = str_replace("\n", ',', $logs); $logs = str_replace("\n", ',', $logs);
$logs = json_decode("{ $logs }", true); $logs = json_decode("{ $logs }", true);

View File

@@ -39,6 +39,17 @@
</table> </table>
{% endfor %} {% endfor %}
<ul class="pager">
{% if pagenumber != 0 %}
<li class="previous">
<a href="?page={{ pagenumber - 1 }}">&larr; Older</a>
</li>
{% endif %}
<li class="next">
<a href="?page={{ pagenumber + 1 }}">Newer &rarr;</a>
</li>
</ul>
<hr> <hr>
{% include 'footer.twig' %} {% include 'footer.twig' %}