1
0
Fork 0

Merge remote-tracking branch 'GromNaN/git-dir'

pull/636/merge
Jordi Boggiano 2012-04-29 18:53:01 +02:00
commit 115dc407fa
12 changed files with 62 additions and 82 deletions

View File

@ -38,6 +38,11 @@ class Factory
} }
} }
// Protect directory against web access
if (!file_exists($home . '/.htaccess')) {
@mkdir($home, 0777, true) && @file_put_contents($home . '/.htaccess', 'deny from all');
}
$config = new Config(); $config = new Config();
$file = new JsonFile($home.'/config.json'); $file = new JsonFile($home.'/config.json');

View File

@ -27,20 +27,14 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
protected $rootIdentifier; protected $rootIdentifier;
protected $infoCache = array(); protected $infoCache = array();
public function __construct($url, IOInterface $io)
{
preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
parent::__construct($url, $io);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function initialize() public function initialize()
{ {
preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $this->url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
} }
/** /**

View File

@ -28,11 +28,6 @@ class GitDriver extends VcsDriver
protected $repoDir; protected $repoDir;
protected $infoCache = array(); protected $infoCache = array();
public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
{
parent::__construct($url, $io, $process);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -41,7 +36,7 @@ class GitDriver extends VcsDriver
if (static::isLocalUrl($this->url)) { if (static::isLocalUrl($this->url)) {
$this->repoDir = str_replace('file://', '', $this->url); $this->repoDir = str_replace('file://', '', $this->url);
} else { } else {
$this->repoDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; $this->repoDir = $this->config->get('home') . '/cache.git/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
// update the repo if it is a valid git repository // update the repo if it is a valid git repository
if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) { if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) {

View File

@ -38,28 +38,15 @@ class GitHubDriver extends VcsDriver
*/ */
protected $gitDriver; protected $gitDriver;
/**
* Constructor
*
* @param string $url
* @param IOInterface $io
* @param ProcessExecutor $process
* @param RemoteFilesystem $remoteFilesystem
*/
public function __construct($url, IOInterface $io, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
{
preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
parent::__construct($url, $io, $process, $remoteFilesystem);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function initialize() public function initialize()
{ {
preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
$this->fetchRootIdentifier(); $this->fetchRootIdentifier();
} }
@ -248,6 +235,7 @@ class GitHubDriver extends VcsDriver
$this->gitDriver = new GitDriver( $this->gitDriver = new GitDriver(
$this->generateSshUrl(), $this->generateSshUrl(),
$this->io, $this->io,
$this->config,
$this->process, $this->process,
$this->remoteFilesystem $this->remoteFilesystem
); );

View File

@ -27,20 +27,14 @@ class HgBitbucketDriver extends VcsDriver
protected $rootIdentifier; protected $rootIdentifier;
protected $infoCache = array(); protected $infoCache = array();
public function __construct($url, IOInterface $io)
{
preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
parent::__construct($url, $io);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function initialize() public function initialize()
{ {
preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $this->url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
} }
/** /**

View File

@ -26,24 +26,21 @@ class HgDriver extends VcsDriver
protected $rootIdentifier; protected $rootIdentifier;
protected $infoCache = array(); protected $infoCache = array();
public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
{
$this->tmpDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
parent::__construct($url, $io, $process);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function initialize() public function initialize()
{ {
$url = escapeshellarg($this->url); $this->tmpDir = $this->config->get('home') . '/cache.hg/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/';
$tmpDir = escapeshellarg($this->tmpDir);
if (is_dir($this->tmpDir)) { if (is_dir($this->tmpDir)) {
$this->process->execute(sprintf('cd %s && hg pull -u', $tmpDir), $output); $this->process->execute(sprintf('cd %s && hg pull -u', escapeshellarg($this->tmpDir)), $output);
} else { } else {
$this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output); $dir = dirname($this->tmpDir);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg($dir), escapeshellarg($this->url), escapeshellarg($this->tmpDir)), $output);
} }
$this->getTags(); $this->getTags();

View File

@ -33,25 +33,7 @@ class SvnDriver extends VcsDriver
/** /**
* @var \Composer\Util\Svn * @var \Composer\Util\Svn
*/ */
protected $util; private $util;
/**
* @param string $url
* @param IOInterface $io
* @param ProcessExecutor $process
*
* @return $this
*/
public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
{
$url = self::normalizeUrl($url);
parent::__construct($this->baseUrl = rtrim($url, '/'), $io, $process);
if (false !== ($pos = strrpos($url, '/trunk'))) {
$this->baseUrl = substr($url, 0, $pos);
}
$this->util = new SvnUtil($this->baseUrl, $io, $this->process);
}
/** /**
* Execute an SVN command and try to fix up the process with credentials * Execute an SVN command and try to fix up the process with credentials
@ -64,6 +46,10 @@ class SvnDriver extends VcsDriver
*/ */
protected function execute($command, $url) protected function execute($command, $url)
{ {
if (null === $this->util) {
$this->util = new SvnUtil($this->baseUrl, $this->io, $this->process);
}
try { try {
return $this->util->execute($command, $url); return $this->util->execute($command, $url);
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
@ -78,6 +64,12 @@ class SvnDriver extends VcsDriver
*/ */
public function initialize() public function initialize()
{ {
$this->url = rtrim(self::normalizeUrl($this->url), '/');
if (false !== ($pos = strrpos($this->url, '/trunk'))) {
$this->baseUrl = substr($this->url, 0, $pos);
}
$this->getBranches(); $this->getBranches();
$this->getTags(); $this->getTags();
} }

View File

@ -13,6 +13,7 @@
namespace Composer\Repository\Vcs; namespace Composer\Repository\Vcs;
use Composer\Downloader\TransportException; use Composer\Downloader\TransportException;
use Composer\Config;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem; use Composer\Util\RemoteFilesystem;
@ -26,6 +27,7 @@ abstract class VcsDriver implements VcsDriverInterface
{ {
protected $url; protected $url;
protected $io; protected $io;
protected $config;
protected $process; protected $process;
protected $remoteFilesystem; protected $remoteFilesystem;
@ -34,13 +36,15 @@ abstract class VcsDriver implements VcsDriverInterface
* *
* @param string $url The URL * @param string $url The URL
* @param IOInterface $io The IO instance * @param IOInterface $io The IO instance
* @param Config $config The composer configuration
* @param ProcessExecutor $process Process instance, injectable for mocking * @param ProcessExecutor $process Process instance, injectable for mocking
* @param callable $remoteFilesystem Remote Filesystem, injectable for mocking * @param callable $remoteFilesystem Remote Filesystem, injectable for mocking
*/ */
public function __construct($url, IOInterface $io, ProcessExecutor $process = null, $remoteFilesystem = null) final public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null, $remoteFilesystem = null)
{ {
$this->url = $url; $this->url = $url;
$this->io = $io; $this->io = $io;
$this->config = $config;
$this->process = $process ?: new ProcessExecutor; $this->process = $process ?: new ProcessExecutor;
$this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io); $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io);
} }
@ -58,7 +62,6 @@ abstract class VcsDriver implements VcsDriverInterface
return false; return false;
} }
/** /**
* Get the https or http protocol depending on SSL support. * Get the https or http protocol depending on SSL support.
* *

View File

@ -30,6 +30,7 @@ class VcsRepository extends ArrayRepository
protected $packageName; protected $packageName;
protected $verbose; protected $verbose;
protected $io; protected $io;
protected $config;
protected $versionParser; protected $versionParser;
protected $type; protected $type;
@ -48,20 +49,21 @@ class VcsRepository extends ArrayRepository
$this->io = $io; $this->io = $io;
$this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs'; $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs';
$this->verbose = $io->isVerbose(); $this->verbose = $io->isVerbose();
$this->config = $config;
} }
public function getDriver() public function getDriver()
{ {
if (isset($this->drivers[$this->type])) { if (isset($this->drivers[$this->type])) {
$class = $this->drivers[$this->type]; $class = $this->drivers[$this->type];
$driver = new $class($this->url, $this->io); $driver = new $class($this->url, $this->io, $this->config);
$driver->initialize(); $driver->initialize();
return $driver; return $driver;
} }
foreach ($this->drivers as $driver) { foreach ($this->drivers as $driver) {
if ($driver::supports($this->io, $this->url)) { if ($driver::supports($this->io, $this->url)) {
$driver = new $driver($this->url, $this->io); $driver = new $driver($this->url, $this->io, $this->config);
$driver->initialize(); $driver->initialize();
return $driver; return $driver;
} }
@ -69,7 +71,7 @@ class VcsRepository extends ArrayRepository
foreach ($this->drivers as $driver) { foreach ($this->drivers as $driver) {
if ($driver::supports($this->io, $this->url, true)) { if ($driver::supports($this->io, $this->url, true)) {
$driver = new $driver($this->url, $this->io); $driver = new $driver($this->url, $this->io, $this->config);
$driver->initialize(); $driver->initialize();
return $driver; return $driver;
} }

View File

@ -15,6 +15,7 @@ namespace Composer\Test\Repository\Vcs;
use Composer\Downloader\TransportException; use Composer\Downloader\TransportException;
use Composer\Repository\Vcs\GitHubDriver; use Composer\Repository\Vcs\GitHubDriver;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Config;
/** /**
* @author Beau Simensen <beau@dflydev.com> * @author Beau Simensen <beau@dflydev.com>
@ -64,7 +65,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->returnValue('{"master_branch": "test_master"}')); ->will($this->returnValue('{"master_branch": "test_master"}'));
$gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem); $gitHubDriver = new GitHubDriver($repoUrl, $io, new Config(), null, $remoteFilesystem);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
@ -114,7 +115,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase
->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
->will($this->returnValue('{"master_branch": "test_master"}')); ->will($this->returnValue('{"master_branch": "test_master"}'));
$gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem); $gitHubDriver = new GitHubDriver($repoUrl, $io, new Config(), null, $remoteFilesystem);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
@ -171,7 +172,14 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase
// clean local clone if present // clean local clone if present
$fs = new Filesystem(); $fs = new Filesystem();
$fs->removeDirectory(sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9.]}i', '-', $repoSshUrl) . '/'); $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
$config = new Config();
$config->merge(array(
'config' => array(
'home' => sys_get_temp_dir() . '/composer-test',
),
));
$process->expects($this->at(0)) $process->expects($this->at(0))
->method('execute') ->method('execute')
@ -202,7 +210,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase
->method('splitLines') ->method('splitLines')
->will($this->returnValue(array('* test_master'))); ->will($this->returnValue(array('* test_master')));
$gitHubDriver = new GitHubDriver($repoUrl, $io, $process, $remoteFilesystem); $gitHubDriver = new GitHubDriver($repoUrl, $io, $config, $process, $remoteFilesystem);
$gitHubDriver->initialize(); $gitHubDriver->initialize();
$this->assertEquals('test_master', $gitHubDriver->getRootIdentifier()); $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());

View File

@ -14,6 +14,7 @@ namespace Composer\Test\Repository\Vcs;
use Composer\Repository\Vcs\SvnDriver; use Composer\Repository\Vcs\SvnDriver;
use Composer\IO\NullIO; use Composer\IO\NullIO;
use Composer\Config;
class SvnDriverTest extends \PHPUnit_Framework_TestCase class SvnDriverTest extends \PHPUnit_Framework_TestCase
{ {
@ -39,7 +40,7 @@ class SvnDriverTest extends \PHPUnit_Framework_TestCase
->method('getErrorOutput') ->method('getErrorOutput')
->will($this->returnValue($output)); ->will($this->returnValue($output));
$svn = new SvnDriver('http://till:secret@corp.svn.local/repo', $console, $process); $svn = new SvnDriver('http://till:secret@corp.svn.local/repo', $console, new Config(), $process);
$svn->getTags(); $svn->getTags();
} }

View File

@ -19,6 +19,7 @@ use Composer\Repository\Vcs\GitDriver;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\IO\NullIO; use Composer\IO\NullIO;
use Composer\Config;
class VcsRepositoryTest extends \PHPUnit_Framework_TestCase class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
{ {
@ -123,7 +124,7 @@ class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
'dev-master' => true, 'dev-master' => true,
); );
$repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO); $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, new Config());
$packages = $repo->getPackages(); $packages = $repo->getPackages();
$dumper = new ArrayDumper(); $dumper = new ArrayDumper();