From 0fa349476a20f2148cae401115b021b37345d606 Mon Sep 17 00:00:00 2001 From: Nate Eagleson Date: Thu, 13 Dec 2012 15:41:52 -0500 Subject: [PATCH 1/3] Use checked-out branch as default branch. Previously assumed 'master' was the default branch, when master may not exist at all. --- src/GitList/Controller/CommitController.php | 12 +++++++++--- src/GitList/Controller/MainController.php | 8 +++++++- src/GitList/Util/Repository.php | 5 ++++- tests/InterfaceTest.php | 18 ++++++++++++++++++ views/index.twig | 2 +- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/GitList/Controller/CommitController.php b/src/GitList/Controller/CommitController.php index 776f669..d215074 100644 --- a/src/GitList/Controller/CommitController.php +++ b/src/GitList/Controller/CommitController.php @@ -15,6 +15,10 @@ class CommitController implements ControllerProviderInterface $route->get('{repo}/commits/{branch}/{file}', function($repo, $branch, $file) use ($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); + if ($branch === null) { + $branch = $repository->getHead(); + } + list($branch, $file) = $app['util.repository']->extractRef($repository, $branch, $file); $type = $file ? "$branch -- \"$file\"" : $branch; @@ -41,12 +45,13 @@ class CommitController implements ControllerProviderInterface })->assert('repo', $app['util.routing']->getRepositoryRegex()) ->assert('branch', '[\w-._\/]+') ->assert('file', '.+') - ->value('branch', 'master') + ->value('branch', null) ->value('file', '') ->bind('commits'); $route->post('{repo}/commits/search', function(Request $request, $repo) use ($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); + $branch = $repository->getHead(); $commits = $repository->searchCommitLog($request->get('query')); foreach ($commits as $commit) { @@ -57,7 +62,7 @@ class CommitController implements ControllerProviderInterface return $app['twig']->render('searchcommits.twig', array( 'repo' => $repo, - 'branch' => 'master', + 'branch' => $branch, 'file' => '', 'commits' => $categorized, 'branches' => $repository->getBranches(), @@ -69,9 +74,10 @@ class CommitController implements ControllerProviderInterface $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(); return $app['twig']->render('commit.twig', array( - 'branch' => 'master', + 'branch' => $branch, 'repo' => $repo, 'commit' => $commit, )); diff --git a/src/GitList/Controller/MainController.php b/src/GitList/Controller/MainController.php index 89b6f62..3262b04 100644 --- a/src/GitList/Controller/MainController.php +++ b/src/GitList/Controller/MainController.php @@ -16,6 +16,9 @@ class MainController implements ControllerProviderInterface $repositories = array_map( function ($repo) use ($app) { $repo['relativePath'] = $app['util.routing']->getRelativePath($repo['path']); + $repository = $app['git']->getRepository($repo['path']); + $repo['branch'] = $repository->getHead(); + return $repo; }, $app['git']->getRepositories($app['git.repos']) @@ -28,6 +31,9 @@ class MainController implements ControllerProviderInterface $route->get('{repo}/stats/{branch}', function($repo, $branch) use ($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); + if ($branch === null) { + $branch = $repository->getHead(); + } $stats = $repository->getStatistics($branch); $authors = $repository->getAuthorStatistics(); @@ -41,7 +47,7 @@ class MainController implements ControllerProviderInterface )); })->assert('repo', $app['util.routing']->getRepositoryRegex()) ->assert('branch', '[\w-._\/]+') - ->value('branch', 'master') + ->value('branch', null) ->bind('stats'); $route->get('{repo}/{branch}/rss/', function($repo, $branch) use ($app) { diff --git a/src/GitList/Util/Repository.php b/src/GitList/Util/Repository.php index e158b95..169ea9c 100644 --- a/src/GitList/Util/Repository.php +++ b/src/GitList/Util/Repository.php @@ -160,9 +160,12 @@ class Repository return false; } - public function getReadme($repo, $branch = 'master') + public function getReadme($repo, $branch = null) { $repository = $this->app['git']->getRepository($this->app['git.repos'] . $repo); + if ($branch === null) { + $branch = $repository->getHead(); + } $files = $repository->getTree($branch)->output(); foreach ($files as $file) { diff --git a/tests/InterfaceTest.php b/tests/InterfaceTest.php index 38e437d..132d960 100644 --- a/tests/InterfaceTest.php +++ b/tests/InterfaceTest.php @@ -79,6 +79,16 @@ class InterfaceTest extends WebTestCase $repository->commit("Changing branch"); $repository->checkout("master"); + // master-less repository fixture + $git->createRepository(self::$tmpdir . 'masterless'); + $repository = $git->getRepository(self::$tmpdir . 'masterless'); + $repository = $repository->checkout('develop'); + file_put_contents(self::$tmpdir . 'masterless/README.md', "## masterless\nmasterless is a *test* repository!"); + file_put_contents(self::$tmpdir . 'masterless/test.php', "setConfig('user.name', 'Luke Skywalker'); + $repository->setConfig('user.email', 'luke@rebel.org'); + $repository->addAll(); + $repository->commit("Initial commit"); } public function createApplication() @@ -241,6 +251,14 @@ class InterfaceTest extends WebTestCase $this->assertRegexp('/NESTED TEST REPO README/', $client->getResponse()->getContent()); } + public function testMasterlessRepo() + { + $client = $this->createClient(); + + $crawler = $client->request('GET', '/masterless/'); + $this->assertTrue($client->getResponse()->isOk()); + } + public function testNestedRepoBranch() { $client = $this->createClient(); diff --git a/views/index.twig b/views/index.twig index 07093cd..b3f075d 100644 --- a/views/index.twig +++ b/views/index.twig @@ -10,7 +10,7 @@

{{ repository.description }}

From 9dfca14f31e04344296aca960ee5bf52817ba928 Mon Sep 17 00:00:00 2001 From: Nate Eagleson Date: Thu, 13 Dec 2012 23:21:30 -0500 Subject: [PATCH 2/3] Attempt to fix test for repo without master branch. Without a working test environment, it's hard to be confident it will succeed, but it's certainly a step closer to correct. --- tests/InterfaceTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/InterfaceTest.php b/tests/InterfaceTest.php index 132d960..0a600e8 100644 --- a/tests/InterfaceTest.php +++ b/tests/InterfaceTest.php @@ -82,6 +82,7 @@ class InterfaceTest extends WebTestCase // master-less repository fixture $git->createRepository(self::$tmpdir . 'masterless'); $repository = $git->getRepository(self::$tmpdir . 'masterless'); + $repository->createBranch("develop"); $repository = $repository->checkout('develop'); file_put_contents(self::$tmpdir . 'masterless/README.md', "## masterless\nmasterless is a *test* repository!"); file_put_contents(self::$tmpdir . 'masterless/test.php', " Date: Thu, 20 Dec 2012 22:22:47 -0500 Subject: [PATCH 3/3] Fix tests (and this time I tested it). Admittedly, I tested it by modifying composer.json to include phpunit but then did not commit that change, so it's vaguely possible an older phpunit would find a way to fail. I think we should be good, though. --- tests/InterfaceTest.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/InterfaceTest.php b/tests/InterfaceTest.php index 0a600e8..99a97b9 100644 --- a/tests/InterfaceTest.php +++ b/tests/InterfaceTest.php @@ -80,12 +80,17 @@ class InterfaceTest extends WebTestCase $repository->checkout("master"); // master-less repository fixture - $git->createRepository(self::$tmpdir . 'masterless'); - $repository = $git->getRepository(self::$tmpdir . 'masterless'); + $git->createRepository(self::$tmpdir . 'develop'); + $repository = $git->getRepository(self::$tmpdir . '/develop'); + $repository->setConfig('user.name', 'Luke Skywalker'); + $repository->setConfig('user.email', 'luke@rebel.org'); + file_put_contents(self::$tmpdir . 'develop/README.md', "## develop\ndevelop is a *test* repository!"); + $repository->addAll(); + $repository->commit("First commit"); $repository->createBranch("develop"); $repository = $repository->checkout('develop'); - file_put_contents(self::$tmpdir . 'masterless/README.md', "## masterless\nmasterless is a *test* repository!"); - file_put_contents(self::$tmpdir . 'masterless/test.php', "setConfig('user.name', 'Luke Skywalker'); $repository->setConfig('user.email', 'luke@rebel.org'); $repository->addAll(); @@ -121,8 +126,8 @@ class InterfaceTest extends WebTestCase $this->assertEquals('/nested/NestedRepo/master/rss/', $crawler->filter('.repository-header a')->eq(3)->attr('href')); $this->assertCount(1, $crawler->filter('div.repository-header:contains("foobar")')); $this->assertCount(1, $crawler->filter('div.repository-body:contains("This is a test repo!")')); - $this->assertEquals('/foobar/', $crawler->filter('.repository-header a')->eq(4)->attr('href')); - $this->assertEquals('/foobar/master/rss/', $crawler->filter('.repository-header a')->eq(5)->attr('href')); + $this->assertEquals('/foobar/', $crawler->filter('.repository-header a')->eq(6)->attr('href')); + $this->assertEquals('/foobar/master/rss/', $crawler->filter('.repository-header a')->eq(7)->attr('href')); } public function testRepositoryPage() @@ -252,11 +257,11 @@ class InterfaceTest extends WebTestCase $this->assertRegexp('/NESTED TEST REPO README/', $client->getResponse()->getContent()); } - public function testMasterlessRepo() + public function testDevelopRepo() { $client = $this->createClient(); - $crawler = $client->request('GET', '/masterless/'); + $crawler = $client->request('GET', '/develop/'); $this->assertTrue($client->getResponse()->isOk()); }