Skip to content

Commit

Permalink
Merge pull request klaussilveira#200 from cschorn/nested-repos
Browse files Browse the repository at this point in the history
Recursive scanning (should fix klaussilveira#2)
  • Loading branch information
klaussilveira committed Nov 6, 2012
2 parents 6a93bdd 11e662c commit 5664854
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 45 deletions.
6 changes: 4 additions & 2 deletions boot.php
Original file line number Diff line number Diff line change
@@ -1,7 1,9 @@
<?php

// Load configuration
$config = new GitList\Config('config.ini');
if (!isset($config)) {
die("No configuration object provided.");
}

$config->set('git', 'repositories', rtrim($config->get('git', 'repositories'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);

// Startup and configure Silex application
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@
"twig/twig": "1.9.*",
"symfony/twig-bridge": "2.1.*",
"symfony/filesystem": "2.1.*",
"klaussilveira/gitter": "0.1.2"
"klaussilveira/gitter": "dev-master"
},
"require-dev": {
"symfony/browser-kit": "2.1.*",
Expand Down
36 changes: 18 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 11,9 @@
}

require 'vendor/autoload.php';

// Load configuration
$config = GitList\Config::fromFile('config.ini');

$app = require 'boot.php';
$app->run();
$app->run();
2 changes: 2 additions & 0 deletions src/GitList/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,7 @@
use GitList\Provider\GitServiceProvider;
use GitList\Provider\RepositoryUtilServiceProvider;
use GitList\Provider\ViewUtilServiceProvider;
use GitList\Provider\RoutingUtilServiceProvider;

/**
* GitList application.
Expand Down Expand Up @@ -44,6 45,7 @@ public function __construct(Config $config, $root = null)
$this->register(new ViewUtilServiceProvider());
$this->register(new RepositoryUtilServiceProvider());
$this->register(new UrlGeneratorServiceProvider());
$this->register(new RoutingUtilServiceProvider());

$this['twig'] = $this->share($this->extend('twig', function($twig, $app) {
$twig->addFilter('md5', new \Twig_Filter_Function('md5'));
Expand Down
10 changes: 7 additions & 3 deletions src/GitList/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 6,17 @@ class Config
{
protected $data;

public function __construct($file)
{
public static function fromFile($file) {
if (!file_exists($file)) {
die(sprintf('Please, create the %1$s file.', $file));
}
$data = parse_ini_file($file, true);
return new static($data);
}

$this->data = parse_ini_file($file, true);
public function __construct($data)
{
$this->data = $data;
$this->validateOptions();
}

Expand Down
4 changes: 2 additions & 2 deletions src/GitList/Controller/BlobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 39,7 @@ public function connect(Application $app)
'tags' => $repository->getTags(),
));
})->assert('file', '. ')
->assert('repo', '[\w-._] ')
->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('blob');

Expand All @@ -59,7 59,7 @@ public function connect(Application $app)

return new Response($blob, 200, $headers);
})->assert('file', '. ')
->assert('repo', '[\w-._] ')
->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('blob_raw');

Expand Down
8 changes: 4 additions & 4 deletions src/GitList/Controller/CommitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 38,7 @@ public function connect(Application $app)
'commits' => $categorized,
'file' => $file,
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->assert('file', '. ')
->value('branch', 'master')
Expand All @@ -63,7 63,7 @@ public function connect(Application $app)
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->bind('searchcommits');

$route->get('{repo}/commit/{commit}/', function($repo, $commit) use ($app) {
Expand All @@ -75,7 75,7 @@ public function connect(Application $app)
'repo' => $repo,
'commit' => $commit,
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commit', '[a-f0-9^] ')
->bind('commit');

Expand All @@ -94,7 94,7 @@ public function connect(Application $app)
'tags' => $repository->getTags(),
'blames' => $blames,
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('file', '. ')
->assert('branch', '[\w-._\/] ')
->bind('blame');
Expand Down
12 changes: 9 additions & 3 deletions src/GitList/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,13 @@ public function connect(Application $app)
$route = $app['controllers_factory'];

$route->get('/', function() use ($app) {
$repositories = $app['git']->getRepositories($app['git.repos']);
$repositories = array_map(
function ($repo) use ($app) {
$repo['relativePath'] = $app['util.routing']->getRelativePath($repo['path']);
return $repo;
},
$app['git']->getRepositories($app['git.repos'])
);

return $app['twig']->render('index.twig', array(
'repositories' => $repositories,
Expand All @@ -33,7 39,7 @@ public function connect(Application $app)
'stats' => $stats,
'authors' => $authors,
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->value('branch', 'master')
->bind('stats');
Expand All @@ -49,7 55,7 @@ public function connect(Application $app)
));

return new Response($html, 200, array('Content-Type' => 'application/rss xml'));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('rss');

Expand Down
10 changes: 5 additions & 5 deletions src/GitList/Controller/TreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 41,7 @@ public function connect(Application $app)
'tags' => $repository->getTags(),
'readme' => $app['util.repository']->getReadme($repo, $branch),
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->assert('tree', '. ')
->bind('tree');
Expand All @@ -65,19 65,19 @@ public function connect(Application $app)
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
));
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('search');

$route->get('{repo}/{branch}/', function($repo, $branch) use ($app, $treeController) {
return $treeController($repo, $branch);
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('branch');

$route->get('{repo}/', function($repo) use ($app, $treeController) {
return $treeController($repo);
})->assert('repo', '[\w-._] ')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->bind('repository');

$route->get('{repo}/{format}ball/{branch}', function($repo, $format, $branch) use ($app) {
Expand Down Expand Up @@ -108,7 108,7 @@ public function connect(Application $app)
'Content-Transfer-Encoding' => 'binary',
));
})->assert('format', '(zip|tar)')
->assert('repo', '[\w-._] ')
->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/] ')
->bind('archive');

Expand Down
26 changes: 26 additions & 0 deletions src/GitList/Provider/RoutingUtilServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 1,26 @@
<?php

namespace GitList\Provider;

use GitList\Util\Routing;
use Silex\Application;
use Silex\ServiceProviderInterface;

class RoutingUtilServiceProvider implements ServiceProviderInterface
{
/**
* Register the Util\Repository class on the Application ServiceProvider
*
* @param Application $app Silex Application
*/
public function register(Application $app)
{
$app['util.routing'] = $app->share(function () use ($app) {
return new Routing($app);
});
}

public function boot(Application $app)
{
}
}
52 changes: 52 additions & 0 deletions src/GitList/Util/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 1,52 @@
<?php

namespace Gitlist\Util;

use Silex\Application;

class Routing
{
protected $app;

public function __construct(Application $app)
{
$this->app = $app;
}

public function getRepositoryRegex()
{
static $regex = null;

if ($regex === null) {
$app = $this->app;
$quotedPaths = array_map(
function ($repo) use ($app) {
return preg_quote($app['util.routing']->getRelativePath($repo['path']), '#');
},
$this->app['git']->getRepositories($this->app['git.repos'])
);
usort($quotedPaths, function ($a, $b) { return strlen($b) - strlen($a); });
$regex = implode('|', $quotedPaths);
}

return $regex;
}

/**
* Strips the base path from a full repository path
*
* @param string $repoPath Full path to the repository
* @return string Relative path to the repository from git.repositories
*/
public function getRelativePath($repoPath)
{
if (strpos($repoPath, $this->app['git.repos']) === 0) {
$relativePath = substr($repoPath, strlen($this->app['git.repos']));
return ltrim($relativePath, '/');
} else {
throw new \InvalidArgumentException(
sprintf("Path '%s' does not match configured repository directory", $repoPath)
);
}
}
}
Loading

0 comments on commit 5664854

Please sign in to comment.