Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added suport for branches with slashes '/' in name #197

Merged
merged 1 commit into from

2 participants

@chmeliuk

reason: application throws an error if branch name contain slash /
was done:

  • extended route pattern for branch
  • added extractRef function to the util.repository service. it returns array with extracted branch name and tree from given parameters.
@chmeliuk

it seems like related to #128

@klaussilveira klaussilveira merged commit 8fd9196 into klaussilveira:master
@cschorn cschorn referenced this pull request
Closed

Recursive scanning #2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 26, 2012
  1. Added suport for branches with slashes '/' in name

    Denys Chmeliuk authored
This page is out of date. Refresh to see the latest.
View
10 src/GitList/Controller/BlobController.php
@@ -15,6 +15,9 @@ public function connect(Application $app)
$route->get('{repo}/blob/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
+
+ list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
+
$blob = $repository->getBlob("$branch:\"$file\"");
$breadcrumbs = $app['util.view']->getBreadcrumbs($file);
$fileType = $app['util.repository']->getFileType($file);
@@ -39,11 +42,14 @@ public function connect(Application $app)
));
})->assert('file', '.+')
->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('blob');
$route->get('{repo}/raw/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
+
+ list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
+
$blob = $repository->getBlob("$branch:\"$file\"")->output();
$headers = array();
@@ -59,7 +65,7 @@ public function connect(Application $app)
return new Response($blob, 200, $headers);
})->assert('file', '.+')
->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('blob_raw');
return $route;
View
10 src/GitList/Controller/CommitController.php
@@ -16,6 +16,9 @@ public function connect(Application $app)
$route->get('{repo}/commits/{branch}/{file}', function($repo, $branch, $file) use ($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
+
+ list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file);
+
$type = $file ? "$branch -- \"$file\"" : $branch;
$pager = $app['util.view']->getPager($app['request']->get('page'), $repository->getTotalCommits($type));
$commits = $repository->getPaginatedCommits($type, $pager['current']);
@@ -38,7 +41,7 @@ public function connect(Application $app)
'file' => $file,
));
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->assert('file', '.+')
->value('branch', 'master')
->value('file', '')
@@ -80,6 +83,9 @@ public function connect(Application $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.repository']->extractRef($repository, $branch, $file);
+
$blames = $repository->getBlame("$branch -- \"$file\"");
return $app['twig']->render('blame.twig', array(
@@ -92,7 +98,7 @@ public function connect(Application $app)
));
})->assert('repo', '[\w-._]+')
->assert('file', '.+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('blame');
return $route;
View
4 src/GitList/Controller/MainController.php
@@ -35,7 +35,7 @@ public function connect(Application $app)
'authors' => $authors,
));
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->value('branch', 'master')
->bind('stats');
@@ -51,7 +51,7 @@ public function connect(Application $app)
return new Response($html, 200, array('Content-Type' => 'application/rss+xml'));
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('rss');
View
11 src/GitList/Controller/TreeController.php
@@ -20,6 +20,9 @@ public function connect(Application $app)
if (!$branch) {
$branch = $repository->getHead();
}
+
+ list($branch, $tree) = $app['util.repository']->extractRef($repository, $branch, $tree);
+
$files = $repository->getTree($tree ? "$branch:\"$tree\"/" : $branch);
$breadcrumbs = $app['util.view']->getBreadcrumbs($tree);
@@ -42,7 +45,7 @@ public function connect(Application $app)
'readme' => $app['util.repository']->getReadme($repo, $branch),
));
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->assert('tree', '.+')
->bind('tree');
@@ -66,13 +69,13 @@ public function connect(Application $app)
'tags' => $repository->getTags(),
));
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('search');
$route->get('{repo}/{branch}/', function($repo, $branch) use ($app, $treeController) {
return $treeController($repo, $branch);
})->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('branch');
$route->get('{repo}/', function($repo) use ($app, $treeController) {
@@ -109,7 +112,7 @@ public function connect(Application $app)
));
})->assert('format', '(zip|tar)')
->assert('repo', '[\w-._]+')
- ->assert('branch', '[\w-._]+')
+ ->assert('branch', '[\w-._\/]+')
->bind('archive');
return $route;
View
44 src/GitList/Util/Repository.php
@@ -114,7 +114,7 @@ public function __construct(Application $app)
*/
public function getFileType($file)
{
- if (($pos = strrpos($file, '.')) !== FALSE) {
+ if (($pos = strrpos($file, '.')) !== false) {
$fileType = substr($file, $pos + 1);
} else {
return 'text';
@@ -175,4 +175,46 @@ public function getReadme($repo, $branch = 'master')
return array();
}
+
+ /**
+ * Returns an Array where the first value is the tree-ish and the second is the path
+ *
+ * @param \GitList\Git\Repository $repository
+ * @param string $branch
+ * @param string $tree
+ * @return array
+ */
+ public function extractRef($repository, $branch='', $tree='')
+ {
+ $branch = trim($branch, '/');
+ $tree = trim($tree, '/');
+ $input = $branch . '/' . $tree;
+
+ //If the ref appears to be a SHA, just split the string
+ if (preg_match("/^([[:alnum:]]{40})(.+)/", $input, $matches)) {
+ $branch = $matches[1];
+ } else {
+ //Otherwise, attempt to detect the ref using a list of the project's branches and tags
+ $valid_refs = array_merge((array)$repository->getBranches(), (array)$repository->getTags());
+ foreach ($valid_refs as $k => $v) {
+ if (!preg_match("#{$v}/#", $input)) {
+ unset($valid_refs[$k]);
+ }
+ }
+
+ //No exact ref match, so just try our best
+ if (count($valid_refs) > 1) {
+ preg_match('/([^\/]+)(.*)/', $input, $matches);
+ $branch = preg_replace('/^\/|\/$/', '', $matches[0]);
+ }
+ else {
+ //extract branch name
+ $branch = array_shift($valid_refs);
+ }
+ }
+ $tree = trim(str_replace($branch, "", $input), "/");
+
+ return array($branch, $tree);
+ }
+
}
View
9 tests/InterfaceTest.php
@@ -42,6 +42,7 @@ public static function setUpBeforeClass()
$repository->commit("Initial commit");
$repository->createBranch('issue12');
$repository->createBranch('issue42');
+ $repository->createBranch('branch/name/wiith/slashes');
// foobar repository fixture
$git->createRepository(self::$tmpdir . 'foobar');
@@ -94,9 +95,11 @@ public function testRepositoryPage()
$this->assertEquals("## GitTest\nGitTest is a *test* repository!", $crawler->filter('#readme-content')->eq(0)->text());
$this->assertEquals('/GitTest/blob/master/README.md', $crawler->filter('.tree tr td')->eq(0)->filter('a')->eq(0)->attr('href'));
$this->assertEquals('/GitTest/blob/master/test.php', $crawler->filter('.tree tr td')->eq(3)->filter('a')->eq(0)->attr('href'));
- $this->assertEquals('issue12', $crawler->filter('.dropdown-menu li')->eq(1)->text());
- $this->assertEquals('issue42', $crawler->filter('.dropdown-menu li')->eq(2)->text());
- $this->assertEquals('master', $crawler->filter('.dropdown-menu li')->eq(3)->text());
+
+ $this->assertEquals('branch/name/wiith/slashes', $crawler->filter('.dropdown-menu li')->eq(1)->text());
+ $this->assertEquals('issue12', $crawler->filter('.dropdown-menu li')->eq(2)->text());
+ $this->assertEquals('issue42', $crawler->filter('.dropdown-menu li')->eq(3)->text());
+ $this->assertEquals('master', $crawler->filter('.dropdown-menu li')->eq(4)->text());
$crawler = $client->request('GET', '/foobar/');
$this->assertTrue($client->getResponse()->isOk());
Something went wrong with that request. Please try again.