mirror of
https://github.com/klaussilveira/gitlist.git
synced 2026-05-05 13:55:34 +02:00
Merge pull request #81 from GromNaN/zipball
Add zipball and tarball download (fix #66)
This commit is contained in:
32
controllers/archiveController.php
Normal file
32
controllers/archiveController.php
Normal 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');
|
||||
@@ -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';
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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>
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% endembed %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user