Merge pull request #81 from GromNaN/zipball

Add zipball and tarball download (fix #66)
This commit is contained in:
Klaus Silveira
2012-07-14 07:50:21 -07:00
4 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<?php
use Symfony\Component\HttpFoundation\StreamedResponse;
$app->get('{repo}/{format}ball/{branch}', function($repo, $format, $branch) use($app) {
$repository = $app['git']->getRepository($app['git.repos'] . $repo);
$tree = $repository->getBranchTree($branch);
if (false === $tree) {
return $app->abort(404, 'Invalid commit or tree reference: '.$branch);
}
$file = $app['cache.archives'].DIRECTORY_SEPARATOR.$repo.DIRECTORY_SEPARATOR.substr($tree, 0, 2).DIRECTORY_SEPARATOR.substr($tree, 2).'.'.$format;
if (!file_exists($file)) {
$repository->createArchive($tree, $file, $format);
}
return new StreamedResponse(function () use($file) {
readfile($file);
}, 200, array(
'Content-type' => ('zip' === $format) ? 'application/zip' : 'application/x-tar',
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename="'.$repo.'-'.substr($tree, 0, 6).'.'.$format.'"',
'Content-Transfer-Encoding' => 'binary',
'Content-Length' => filesize($file),
));
})->assert('format', '(zip|tar)')
->assert('repo', '[\w-._]+')
->assert('branch', '[\w-._]+')
->value('format', 'zip')
->bind('archive');

View File

@@ -23,6 +23,8 @@ $app['filetypes'] = $config['filetypes'];
$app['hidden'] = isset($config['git']['hidden']) ? $config['git']['hidden'] : array();
$config['git']['repositories'] = rtrim($config['git']['repositories'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$app['cache.archives'] = __DIR__.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'archives';
// Register Git and Twig service providersclass_path
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
@@ -43,6 +45,7 @@ $app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
}));
// Load controllers
include 'controllers/archiveController.php';
include 'controllers/indexController.php';
include 'controllers/treeController.php';
include 'controllers/blobController.php';

View File

@@ -472,6 +472,37 @@ class Repository
return $data;
}
/**
* Extract the tree hash for a given branch or tree reference
*
* @param string $branch
* @return string
*/
public function getBranchTree($branch)
{
$hash = $this->getClient()->run($this, "log --pretty='%T' --max-count=1 $refspec");
$hash = trim($hash, "\r\n ");
return $hash ? : false;
}
/**
* Create a TAR or ZIP archive of a git tree
*
* @param string $tree Tree-ish reference
* @param string $output Output File name
* @param string $format Archive format
*/
public function createArchive($tree, $output, $format = 'zip')
{
if (!file_exists($dir = dirname($output)) && !@mkdir($dir, 0777, true)) {
throw new \RuntimeException("Unable to create directory $dir");
}
$this->getClient()->run($this, "archive --format=$format --output=$output $tree");
}
/**
* Get the Tree for the provided folder
*

View File

@@ -8,6 +8,11 @@
{% embed 'breadcrumb.twig' with {breadcrumbs: breadcrumbs} %}
{% block extra %}
<a href="{{ path('rss', {repo: repo, branch: branch}) }}"><i class="rss pull-right"></i></a>
<div class="btn-group pull-right">
<a href="{{ path('archive', {repo: repo, branch: branch, format: 'zip'}) }}" class="btn btn-mini" title="Download ZIP archive">ZIP</a>
<a href="{{ path('archive', {repo: repo, branch: branch, format: 'tar'}) }}" class="btn btn-mini" title="Download TAR archive">TAR</a>
&nbsp;
</div>
{% endblock %}
{% endembed %}