This commit is contained in:
Klaus Silveira
2013-03-28 23:49:18 -03:00
parent 1f103627c3
commit 132ba42616
9 changed files with 89 additions and 77 deletions

View File

@@ -48,7 +48,7 @@ class Application extends SilexApplication
$this->register(new UrlGeneratorServiceProvider());
$this->register(new RoutingUtilServiceProvider());
$this['twig'] = $this->share($this->extend('twig', function($twig, $app) {
$this['twig'] = $this->share($this->extend('twig', function ($twig, $app) {
$twig->addFilter('htmlentities', new \Twig_Filter_Function('htmlentities'));
$twig->addFilter('md5', new \Twig_Filter_Function('md5'));

View File

@@ -6,11 +6,13 @@ class Config
{
protected $data;
public static function fromFile($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);
}

View File

@@ -12,11 +12,11 @@ class BlobController implements ControllerProviderInterface
{
$route = $app['controllers_factory'];
$route->get('{repo}/blob/{commitish_path}', function($repo, $commitish_path) use ($app) {
$route->get('{repo}/blob/{commitishPath}', function ($repo, $commitishPath) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);
->parseCommitishPathParam($commitishPath, $repo);
list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
@@ -43,14 +43,14 @@ class BlobController implements ControllerProviderInterface
'tags' => $repository->getTags(),
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', '.+')
->assert('commitishPath', '.+')
->bind('blob');
$route->get('{repo}/raw/{commitish_path}', function($repo, $commitish_path) use ($app) {
$route->get('{repo}/raw/{commitishPath}', function ($repo, $commitishPath) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);
->parseCommitishPathParam($commitishPath, $repo);
list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
@@ -67,7 +67,7 @@ class BlobController implements ControllerProviderInterface
return new Response($blob, 200, $headers);
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
->bind('blob_raw');
return $route;

View File

@@ -12,15 +12,15 @@ class CommitController implements ControllerProviderInterface
{
$route = $app['controllers_factory'];
$route->get('{repo}/commits/{commitish_path}', function($repo, $commitish_path) use ($app) {
$route->get('{repo}/commits/{commitishPath}', function ($repo, $commitishPath) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if ($commitish_path === null) {
$commitish_path = $repository->getHead();
if ($commitishPath === null) {
$commitishPath = $repository->getHead();
}
list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);
->parseCommitishPathParam($commitishPath, $repo);
list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
@@ -48,11 +48,11 @@ class CommitController implements ControllerProviderInterface
'file' => $file,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->value('commitish_path', null)
->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
->value('commitishPath', null)
->bind('commits');
$route->post('{repo}/commits/{branch}/search', function(Request $request, $repo, $branch = '') use ($app) {
$route->post('{repo}/commits/{branch}/search', function (Request $request, $repo, $branch = '') use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$query = $request->get('query');
$commits = $repository->searchCommitLog($query);
@@ -77,7 +77,7 @@ class CommitController implements ControllerProviderInterface
->assert('branch', $app['util.routing']->getBranchRegex())
->bind('searchcommits');
$route->get('{repo}/commit/{commit}', function($repo, $commit) use ($app) {
$route->get('{repo}/commit/{commit}', function ($repo, $commit) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$commit = $repository->getCommit($commit);
$branch = $repository->getHead();
@@ -91,11 +91,11 @@ class CommitController implements ControllerProviderInterface
->assert('commit', '[a-f0-9^]+')
->bind('commit');
$route->get('{repo}/blame/{commitish_path}', function($repo, $commitish_path) use ($app) {
$route->get('{repo}/blame/{commitishPath}', function ($repo, $commitishPath) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);
->parseCommitishPathParam($commitishPath, $repo);
list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
@@ -110,7 +110,7 @@ class CommitController implements ControllerProviderInterface
'blames' => $blames,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
->bind('blame');
return $route;

View File

@@ -12,7 +12,7 @@ class MainController implements ControllerProviderInterface
{
$route = $app['controllers_factory'];
$route->get('/', function() use ($app) {
$route->get('/', function () use ($app) {
$repositories = array_map(
function ($repo) use ($app) {
$repo['relativePath'] = $app['util.routing']->getRelativePath($repo['path']);
@@ -29,7 +29,7 @@ class MainController implements ControllerProviderInterface
));
})->bind('homepage');
$route->get('{repo}/stats/{branch}', function($repo, $branch) use ($app) {
$route->get('{repo}/stats/{branch}', function ($repo, $branch) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if ($branch === null) {
$branch = $repository->getHead();
@@ -43,14 +43,14 @@ class MainController implements ControllerProviderInterface
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
'stats' => $stats,
'authors' => $authors,
'authors' => $authors,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', $app['util.routing']->getBranchRegex())
->value('branch', null)
->bind('stats');
$route->get('{repo}/{branch}/rss/', function($repo, $branch) use ($app) {
$route->get('{repo}/{branch}/rss/', function ($repo, $branch) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if ($branch === null) {

View File

@@ -13,13 +13,13 @@ class TreeController implements ControllerProviderInterface
{
$route = $app['controllers_factory'];
$route->get('{repo}/tree/{commitish_path}/', $treeController = function($repo, $commitish_path = '') use ($app) {
$route->get('{repo}/tree/{commitishPath}/', $treeController = function ($repo, $commitishPath = '') use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if (!$commitish_path) {
$commitish_path = $repository->getHead();
if (!$commitishPath) {
$commitishPath = $repository->getHead();
}
list($branch, $tree) = $app['util.routing']->parseCommitishPathParam($commitish_path, $repo);
list($branch, $tree) = $app['util.routing']->parseCommitishPathParam($commitishPath, $repo);
list($branch, $tree) = $app['util.repository']->extractRef($repository, $branch, $tree);
$files = $repository->getTree($tree ? "$branch:\"$tree\"/" : $branch);
@@ -44,10 +44,10 @@ class TreeController implements ControllerProviderInterface
'readme' => $app['util.repository']->getReadme($repo, $branch),
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
->bind('tree');
$route->post('{repo}/tree/{branch}/search', function(Request $request, $repo, $branch = '', $tree = '') use ($app) {
$route->post('{repo}/tree/{branch}/search', function (Request $request, $repo, $branch = '', $tree = '') use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if (!$branch) {
@@ -71,18 +71,18 @@ class TreeController implements ControllerProviderInterface
->assert('branch', '[\w-._\/]+')
->bind('search');
$route->get('{repo}/{branch}/', function($repo, $branch) use ($app, $treeController) {
$route->get('{repo}/{branch}/', function ($repo, $branch) use ($app, $treeController) {
return $treeController($repo, $branch);
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
->bind('branch');
$route->get('{repo}/', function($repo) use ($app, $treeController) {
$route->get('{repo}/', function ($repo) use ($app, $treeController) {
return $treeController($repo);
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->bind('repository');
$route->get('{repo}/{format}ball/{branch}', function($repo, $format, $branch) use ($app) {
$route->get('{repo}/{format}ball/{branch}', function ($repo, $format, $branch) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$tree = $repository->getBranchTree($branch);

View File

@@ -11,10 +11,10 @@ use Symfony\Component\Filesystem\Filesystem;
class Repository extends BaseRepository
{
/**
* Return TRUE if the repo contains this commit.
* Return true if the repo contains this commit.
*
* @param $commitHash Hash of commit whose existence we want to check
* @return boolean Whether or not the commit exists in this repo
* @return boolean Whether or not the commit exists in this repo
*/
public function hasCommit($commitHash)
{
@@ -104,8 +104,8 @@ class Repository extends BaseRepository
/**
* Read diff logs and generate a collection of diffs
*
* @param array $logs Array of log rows
* @return array Array of diffs
* @param array $logs Array of log rows
* @return array Array of diffs
*/
public function readDiffLogs(array $logs)
{
@@ -313,7 +313,7 @@ class Repository extends BaseRepository
$data['size'] += $file[3];
}
if (($pos = strrpos($file[4], '.')) !== FALSE) {
if (($pos = strrpos($file[4], '.')) !== false) {
$data['extensions'][] = substr($file[4], $pos);
}
}
@@ -339,21 +339,22 @@ class Repository extends BaseRepository
}
/**
* Return TRUE if $path exists in $branch; return FALSE otherwise.
* Return true if $path exists in $branch; return false otherwise.
*
* @param string $commitish Commitish reference; branch, tag, SHA1, etc.
* @param string $path Path whose existence we want to verify.
* @param string $path Path whose existence we want to verify.
*
* GRIPE Arguably belongs in Gitter, as it's generally useful functionality.
* Also, this really may not be the best way to do this.
*/
public function pathExists($commitish, $path) {
public function pathExists($commitish, $path)
{
$output = $this->getClient()->run($this, "ls-tree $commitish $path");
if (strlen($output) > 0) {
return TRUE;
return true;
}
return FALSE;
return false;
}
}

View File

@@ -189,7 +189,7 @@ class Repository
* @param string $tree
* @return array
*/
public function extractRef($repository, $branch='', $tree='')
public function extractRef($repository, $branch = '', $tree = '')
{
$branch = trim($branch, '/');
$tree = trim($tree, '/');
@@ -219,5 +219,4 @@ class Repository
return array($branch, $tree);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Gitlist\Util;
namespace GitList\Util;
use Silex\Application;
@@ -13,26 +13,27 @@ class Routing
$this->app = $app;
}
/* @brief Return $commitish, $path parsed from $commitish_path, based on
/* @brief Return $commitish, $path parsed from $commitishPath, based on
* what's in $repo. Raise a 404 if $branchpath does not represent a
* valid branch and path.
*
* A helper for parsing routes that use commit-ish names and paths
* separated by /, since route regexes are not enough to get that right.
*/
public function parseCommitishPathParam($commitish_path, $repo) {
public function parseCommitishPathParam($commitishPath, $repo)
{
$app = $this->app;
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$commitish = null;
$path = null;
$slash_pos = strpos($commitish_path, '/');
if (strlen($commitish_path) >= 40 &&
($slash_pos === FALSE ||
$slash_pos === 40)) {
$slashPosition = strpos($commitishPath, '/');
if (strlen($commitishPath) >= 40 &&
($slashPosition === false ||
$slashPosition === 40)) {
// We may have a commit hash as our commitish.
$hash = substr($commitish_path, 0, 40);
$hash = substr($commitishPath, 0, 40);
if ($repository->hasCommit($hash)) {
$commitish = $hash;
}
@@ -48,56 +49,57 @@ class Routing
$branches = array_merge($branches, $tags);
}
$matched_branch = null;
$matched_branch_name_len = 0;
$matchedBranch = null;
$matchedBranchLength = 0;
foreach ($branches as $branch) {
if (strpos($commitish_path, $branch) === 0 &&
strlen($branch) > $matched_branch_name_len) {
$matched_branch = $branch;
$matched_branch_name_len = strlen($matched_branch);
if (strpos($commitishPath, $branch) === 0 &&
strlen($branch) > $matchedBranchLength) {
$matchedBranch = $branch;
$matchedBranchLength = strlen($matchedBranch);
}
}
$commitish = $matched_branch;
$commitish = $matchedBranch;
}
if ($commitish === null) {
$app->abort(404, "'$branch_path' does not appear to contain a " .
"commit-ish for '$repo.'");
$app->abort(404, "'$branch_path' does not appear to contain a commit-ish for '$repo'.");
}
$commitish_len = strlen($commitish);
$path = substr($commitish_path, $commitish_len);
$commitishLength = strlen($commitish);
$path = substr($commitishPath, $commitishLength);
if (strpos($path, '/') === 0) {
$path = substr($path, 1);
}
$commit_has_path = $repository->pathExists($commitish, $path);
if ($commit_has_path !== TRUE) {
$commitHasPath = $repository->pathExists($commitish, $path);
if ($commitHasPath !== true) {
$app->abort(404, "\"$path\" does not exist in \"$commitish\".");
}
return array($commitish, $path);
}
public function getBranchRegex() {
static $branch_regex = null;
public function getBranchRegex()
{
static $branchRegex = null;
if ($branch_regex === null) {
$branch_regex = '[\w-._\/]+';
if ($branchRegex === null) {
$branchRegex = '[\w-._\/]+';
}
return $branch_regex;
return $branchRegex;
}
public function getCommitishPathRegex() {
static $commitish_path_regex = null;
public function getCommitishPathRegex()
{
static $commitishPathRegex = null;
if ($commitish_path_regex === null) {
$commitish_path_regex = '.+';
if ($commitishPathRegex === null) {
$commitishPathRegex = '.+';
}
return $commitish_path_regex;
return $commitishPathRegex;
}
public function getRepositoryRegex()
@@ -112,7 +114,14 @@ class Routing
},
$this->app['git']->getRepositories($this->app['git.repos'])
);
usort($quotedPaths, function ($a, $b) { return strlen($b) - strlen($a); });
usort(
$quotedPaths,
function ($a, $b) {
return strlen($b) - strlen($a);
}
);
$regex = implode('|', $quotedPaths);
}
@@ -122,13 +131,14 @@ class Routing
/**
* Strips the base path from a full repository path
*
* @param string $repoPath Full path to the repository
* @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(strtr($relativePath, '\\', '/'), '/');
} else {
throw new \InvalidArgumentException(