Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly support branch names with slashes #259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/GitList/Controller/BlobController.php
Expand Up @@ -12,8 +12,12 @@ public function connect(Application $app)
{
$route = $app['controllers_factory'];

$route->get('{repo}/blob/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$route->get('{repo}/blob/{commitish_path}', function($repo, $commitish_path) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);

list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);

list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

$blob = $repository->getBlob("$branch:\"$file\"");
Expand All @@ -38,14 +42,18 @@ public function connect(Application $app)
'branches' => $repository->getBranches(),
'tags' => $repository->getTags(),
));
})->assert('file', '.+')
->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('commitish_path', '.+')
->bind('blob');

$route->get('{repo}/raw/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$route->get('{repo}/raw/{commitish_path}', function($repo, $commitish_path) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);

list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);

list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

$blob = $repository->getBlob("$branch:\"$file\"")->output();

$headers = array();
Expand All @@ -58,9 +66,8 @@ public function connect(Application $app)
}

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

return $route;
Expand Down
25 changes: 14 additions & 11 deletions src/GitList/Controller/CommitController.php
Expand Up @@ -12,13 +12,16 @@ public function connect(Application $app)
{
$route = $app['controllers_factory'];

$route->get('{repo}/commits/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$route->get('{repo}/commits/{commitish_path}', function($repo, $commitish_path) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);

if ($branch === null) {
$branch = $repository->getHead();
if ($commitish_path === null) {
$commitish_path = $repository->getHead();
}

list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($commitish_path, $repo);

list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

$type = $file ? "$branch -- \"$file\"" : $branch;
Expand All @@ -45,10 +48,8 @@ public function connect(Application $app)
'file' => $file,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
->assert('file', '.+')
->value('branch', null)
->value('file', '')
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->value('commitish_path', null)
->bind('commits');

$route->post('{repo}/commits/{branch}/search', function(Request $request, $repo, $branch = '') use ($app) {
Expand All @@ -73,7 +74,7 @@ public function connect(Application $app)
'query' => $query
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
->assert('branch', $app['util.routing']->getBranchRegex())
->bind('searchcommits');

$route->get('{repo}/commit/{commit}', function($repo, $commit) use ($app) {
Expand All @@ -90,9 +91,12 @@ public function connect(Application $app)
->assert('commit', '[a-f0-9^]+')
->bind('commit');

$route->get('{repo}/blame/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$route->get('{repo}/blame/{branch_file}', function($repo, $branch_file) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);

list($branch, $file) = $app['util.routing']
->parseCommitishPathParam($branch_file, $repo);

list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);

$blames = $repository->getBlame("$branch -- \"$file\"");
Expand All @@ -106,8 +110,7 @@ public function connect(Application $app)
'blames' => $blames,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('file', '.+')
->assert('branch', '[\w-._\/]+')
->assert('branch_file', $app['util.routing']->getCommitishPathRegex())
->bind('blame');

return $route;
Expand Down
4 changes: 2 additions & 2 deletions src/GitList/Controller/MainController.php
Expand Up @@ -46,7 +46,7 @@ function ($repo) use ($app) {
'authors' => $authors,
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
->assert('branch', $app['util.routing']->getBranchRegex())
->value('branch', null)
->bind('stats');

Expand All @@ -62,7 +62,7 @@ function ($repo) use ($app) {

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

return $route;
Expand Down
11 changes: 6 additions & 5 deletions src/GitList/Controller/TreeController.php
Expand Up @@ -13,12 +13,14 @@ public function connect(Application $app)
{
$route = $app['controllers_factory'];

$route->get('{repo}/tree/{branch}/{tree}/', $treeController = function($repo, $branch = '', $tree = '') use ($app) {
$route->get('{repo}/tree/{commitish_path}/', $treeController = function($repo, $commitish_path = '') use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
if (!$branch) {
$branch = $repository->getHead();
if (!$commitish_path) {
$commitish_path = $repository->getHead();
}

list($branch, $tree) = $app['util.routing']->parseCommitishPathParam($commitish_path, $repo);

list($branch, $tree) = $app['util.repository']->extractRef($repository, $branch, $tree);
$files = $repository->getTree($tree ? "$branch:\"$tree\"/" : $branch);
$breadcrumbs = $app['util.view']->getBreadcrumbs($tree);
Expand All @@ -42,8 +44,7 @@ public function connect(Application $app)
'readme' => $app['util.repository']->getReadme($repo, $branch),
));
})->assert('repo', $app['util.routing']->getRepositoryRegex())
->assert('branch', '[\w-._\/]+')
->assert('tree', '.+')
->assert('commitish_path', $app['util.routing']->getCommitishPathRegex())
->bind('tree');

$route->post('{repo}/tree/{branch}/search', function(Request $request, $repo, $branch = '', $tree = '') use ($app) {
Expand Down
33 changes: 33 additions & 0 deletions src/GitList/Git/Repository.php
Expand Up @@ -10,6 +10,20 @@

class Repository extends BaseRepository
{
/**
* 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
*/
public function hasCommit($commitHash)
{
$logs = $this->getClient()->run($this, "show $commitHash");
$logs = explode("\n", $logs);

return strpos($logs[0], 'commit') === 0;
}

/**
* Show the data from a specific commit
*
Expand Down Expand Up @@ -313,4 +327,23 @@ public function createArchive($tree, $output, $format = 'zip')
$fs->mkdir(dirname($output));
$this->getClient()->run($this, "archive --format=$format --output=$output $tree");
}

/**
* 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.
*
* 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) {
$output = $this->getClient()->run($this, "ls-tree $commitish $path");

if (strlen($output) > 0) {
return TRUE;
}

return FALSE;
}
}
87 changes: 87 additions & 0 deletions src/GitList/Util/Routing.php
Expand Up @@ -13,6 +13,93 @@ public function __construct(Application $app)
$this->app = $app;
}

/* @brief Return $commitish, $path parsed from $commitish_path, 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) {
$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)) {
// We may have a commit hash as our commitish.
$hash = substr($commitish_path, 0, 40);
if ($repository->hasCommit($hash)) {
$commitish = $hash;
}
}

if ($commitish === null) {
// DEBUG Can you have a repo with no branches? How should we handle
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should fallback to the default branch. Take a look at: #61

// that?
$branches = $repository->getBranches();

$tags = $repository->getTags();
if ($tags !== null && count($tags) > 0) {
$branches = array_merge($branches, $tags);
}

$matched_branch = null;
$matched_branch_name_len = 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);
}
}

$commitish = $matched_branch;
}

if ($commitish === null) {
$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);
if (strpos($path, '/') === 0) {
$path = substr($path, 1);
}

$commit_has_path = $repository->pathExists($commitish, $path);
if ($commit_has_path !== TRUE) {
$app->abort(404, "\"$path\" does not exist in \"$commitish\".");
}

return array($commitish, $path);
}

public function getBranchRegex() {
static $branch_regex = null;

if ($branch_regex === null) {
$branch_regex = '[\w-._\/]+';
}

return $branch_regex;
}

public function getCommitishPathRegex() {
static $commitish_path_regex = null;

if ($commitish_path_regex === null) {
$commitish_path_regex = '.+';
}

return $commitish_path_regex;
}

public function getRepositoryRegex()
{
static $regex = null;
Expand Down
2 changes: 1 addition & 1 deletion views/breadcrumb.twig
Expand Up @@ -2,7 +2,7 @@
<li><a href="{{ path('repository', {repo: repo}) }}">{{ repo }}</a></li>
{% for breadcrumb in breadcrumbs %}
<span class="divider">/</span>
<li{% if loop.last %} class="active"{% endif %}>{% if not loop.last %}<a href="{{ path('tree', {repo: repo, branch: branch, tree: breadcrumb.path}) }}">{{ breadcrumb.dir }}</a>{% endif %}{% if loop.last %}{{ breadcrumb.dir }}{% endif %}</li>
<li{% if loop.last %} class="active"{% endif %}>{% if not loop.last %}<a href="{{ path('tree', {repo: repo, commitish_path: branch ~ '/' ~ breadcrumb.path}) }}">{{ breadcrumb.dir }}</a>{% endif %}{% if loop.last %}{{ breadcrumb.dir }}{% endif %}</li>
{% endfor %}

{% block extra %}{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions views/commit.twig
Expand Up @@ -30,8 +30,8 @@
<div class="meta"><a name="{{ loop.index }}">{{ diff.file }}</div>

<div class="btn-group pull-right">
<a href="{{ path('commits', {repo: repo, branch: commit.hash, file: diff.file}) }}" class="btn btn-small"><i class="icon-list-alt"></i> History</a>
<a href="{{ path('blob', {repo: repo, branch: commit.hash, file: diff.file}) }}" class="btn btn-small"><i class="icon-file"></i> View file @ {{ commit.shortHash }}</a>
<a href="{{ path('commits', {repo: repo, commitish_path: commit.hash ~ '/' ~ diff.file}) }}" class="btn btn-small"><i class="icon-list-alt"></i> History</a>
<a href="{{ path('blob', {repo: repo, commitish_path: commit.hash ~'/' ~ diff.file}) }}" class="btn btn-small"><i class="icon-file"></i> View file @ {{ commit.shortHash }}</a>
</div>
</div>

Expand Down
6 changes: 3 additions & 3 deletions views/file.twig
Expand Up @@ -12,9 +12,9 @@
<div class="meta"></div>

<div class="btn-group pull-right">
<a href="{{ path('blob_raw', {repo: repo, branch: branch, file: file}) }}" class="btn btn-small"><i class="icon-file"></i> Raw</a>
<a href="{{ path('blame', {repo: repo, branch: branch, file: file}) }}" class="btn btn-small"><i class="icon-bullhorn"></i> Blame</a>
<a href="{{ path('commits', {repo: repo, branch: branch, file: file}) }}" class="btn btn-small"><i class="icon-list-alt"></i> History</a>
<a href="{{ path('blob_raw', {repo: repo, commitish_path: branch ~ '/' ~ file}) }}" class="btn btn-small"><i class="icon-file"></i> Raw</a>
<a href="{{ path('blame', {repo: repo, branch_file: branch ~ '/' ~ file}) }}" class="btn btn-small"><i class="icon-bullhorn"></i> Blame</a>
<a href="{{ path('commits', {repo: repo, commitish_path: branch ~ '/' ~ file}) }}" class="btn btn-small"><i class="icon-list-alt"></i> History</a>
</div>
</div>
{% if fileType == 'image' %}
Expand Down
2 changes: 1 addition & 1 deletion views/menu.twig
@@ -1,5 +1,5 @@
<ul class="nav nav-tabs">
<li{% if page == 'files' %} class="active"{% endif %}><a href="{{ path('branch', {repo: repo, branch: branch}) }}">Files</a></li>
<li{% if page in ['commits', 'searchcommits'] %} class="active"{% endif %}><a href="{{ path('commits', {repo: repo, branch: branch}) }}">Commits</a></li>
<li{% if page in ['commits', 'searchcommits'] %} class="active"{% endif %}><a href="{{ path('commits', {repo: repo, commitish_path: branch}) }}">Commits</a></li>
<li{% if page == 'stats' %} class="active"{% endif %}><a href="{{ path('stats', {repo: repo, branch: branch}) }}">Stats</a></li>
</ul>
6 changes: 3 additions & 3 deletions views/tree.twig
Expand Up @@ -32,7 +32,7 @@
{% if not parent %}
<a href="{{ path('branch', {repo: repo, branch: branch}) }}">..</a>
{% else %}
<a href="{{ path('tree', {repo: repo, branch: branch, tree: parent}) }}">..</a>
<a href="{{ path('tree', {repo: repo, commitish_path: branch ~ '/' ~ parent}) }}">..</a>
{% endif %}
</td>
<td></td>
Expand All @@ -43,9 +43,9 @@
<tr>
<td><i class="{{ file.type == "folder" or file.type == "symlink" ? "icon-folder-open" : "icon-file" }} icon-spaced"></i> <a href="
{%- if file.type == "folder" or file.type == "symlink" -%}
{{ path('tree', {repo: repo, branch: branch, tree: path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{{ path('tree', {repo: repo, commitish_path: branch ~ '/' ~ path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{%- else -%}
{{ path('blob', {repo: repo, branch: branch, file: path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{{ path('blob', {repo: repo, commitish_path: branch ~ '/' ~ path ~ (file.type == "symlink" ? file.path : file.name)}) }}
{%- endif -%}
">{{ file.name }}</a></td>
<td>{{ file.mode }}</td>
Expand Down