Skip to content

Commit

Permalink
Merge remote-tracking branch 'hason/git_config'
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Sep 7, 2012
2 parents 3f4b6bf + 3b6bd76 commit 122f660
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/Composer/Command/CreateProjectCommand.php
Expand Up @@ -89,12 +89,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

public function installProject(IOInterface $io, $packageName, $directory = null, $packageVersion = null, $preferSource = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false)
{
$dm = $this->createDownloadManager($io);
$config = Factory::createConfig();

$dm = $this->createDownloadManager($io, $config);
if ($preferSource) {
$dm->setPreferSource(true);
}

$config = Factory::createConfig();
if (null === $repositoryUrl) {
$sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
} elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
Expand Down Expand Up @@ -183,10 +184,10 @@ public function installProject(IOInterface $io, $packageName, $directory = null,
$installer->run();
}

protected function createDownloadManager(IOInterface $io)
protected function createDownloadManager(IOInterface $io, Config $config)
{
$factory = new Factory();

return $factory->createDownloadManager($io);
return $factory->createDownloadManager($io, $config);
}
}
1 change: 1 addition & 0 deletions src/Composer/Config.php
Expand Up @@ -22,6 +22,7 @@ class Config
'vendor-dir' => 'vendor',
'bin-dir' => '{$vendor-dir}/bin',
'notify-on-install' => true,
'github-protocols' => array('git', 'https', 'http'),
);

public static $defaultRepositories = array(
Expand Down
16 changes: 14 additions & 2 deletions src/Composer/Downloader/GitDownloader.php
Expand Up @@ -12,13 +12,25 @@

namespace Composer\Downloader;

use Composer\IO\IOInterface;
use Composer\Config;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Composer\Package\PackageInterface;

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class GitDownloader extends VcsDownloader
{
private $config;

public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
{
parent::__construct($io, $process, $fs);
$this->config = $config;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -150,8 +162,8 @@ protected function runCommand($commandCallable, $url, $path = null)
$handler = array($this, 'outputHandler');

// public github, autoswitch protocols
if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
$protocols = array('git', 'https', 'http');
if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match) && $this->config->has('github-protocols')) {
$protocols = (array) $this->config->get('github-protocols');
$messages = array();
foreach ($protocols as $protocol) {
$url = $protocol . $match[1];
Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Factory.php
Expand Up @@ -155,7 +155,7 @@ public function createComposer(IOInterface $io, $localConfig = null)
$package = $loader->load($localConfig);

// initialize download manager
$dm = $this->createDownloadManager($io);
$dm = $this->createDownloadManager($io, $config);

// initialize installation manager
$im = $this->createInstallationManager($config);
Expand Down Expand Up @@ -219,10 +219,10 @@ protected function addLocalRepository(RepositoryManager $rm, $vendorDir)
* @param IO\IOInterface $io
* @return Downloader\DownloadManager
*/
public function createDownloadManager(IOInterface $io)
public function createDownloadManager(IOInterface $io, Config $config)
{
$dm = new Downloader\DownloadManager();
$dm->setDownloader('git', new Downloader\GitDownloader($io));
$dm->setDownloader('git', new Downloader\GitDownloader($io, $config));
$dm->setDownloader('svn', new Downloader\SvnDownloader($io));
$dm->setDownloader('hg', new Downloader\HgDownloader($io));
$dm->setDownloader('zip', new Downloader\ZipDownloader($io));
Expand Down
54 changes: 46 additions & 8 deletions tests/Composer/Test/Downloader/GitDownloaderTest.php
Expand Up @@ -13,16 +13,23 @@
namespace Composer\Test\Downloader;

use Composer\Downloader\GitDownloader;
use Composer\Config;

class GitDownloaderTest extends \PHPUnit_Framework_TestCase
{
protected function getDownloaderMock($io = null, $executor = null, $filesystem = null)
protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{
$io = $io ?: $this->getMock('Composer\IO\IOInterface');
$executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
$filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
if (!$config) {
$config = $this->getMock('Composer\Config');
$config->expects($this->any())
->method('has')
->will($this->returnValue(false));
}

return new GitDownloader($io, $executor, $filesystem);
return new GitDownloader($io, $config, $executor, $filesystem);
}

/**
Expand Down Expand Up @@ -64,7 +71,7 @@ public function testDownload()
->with($this->equalTo($this->getCmd("git checkout '1234567890123456789012345678901234567890' && git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo('composerPath'))
->will($this->returnValue(0));

$downloader = $this->getDownloaderMock(null, $processExecutor);
$downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->download($packageMock, 'composerPath');
}

Expand Down Expand Up @@ -116,7 +123,38 @@ public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
->will($this->returnValue(0));

$downloader = $this->getDownloaderMock(null, $processExecutor);
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->download($packageMock, 'composerPath');
}

public function testDownloadUsesCustomVariousProtocolsForGithub()
{
$packageMock = $this->getMock('Composer\Package\PackageInterface');
$packageMock->expects($this->any())
->method('getSourceReference')
->will($this->returnValue('ref'));
$packageMock->expects($this->any())
->method('getSourceUrl')
->will($this->returnValue('https://github.com/composer/composer'));
$packageMock->expects($this->any())
->method('getPrettyVersion')
->will($this->returnValue('1.0.0'));
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');

$expectedGitCommand = $this->getCmd("git clone 'http://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'http://github.com/composer/composer' && git fetch composer");
$processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedGitCommand))
->will($this->returnValue(0));

$processExecutor->expects($this->exactly(4))
->method('execute')
->will($this->returnValue(0));

$config = new Config();
$config->merge(array('config' => array('github-protocols' => 'http')));

$downloader = $this->getDownloaderMock(null, $config, $processExecutor);
$downloader->download($packageMock, 'composerPath');
}

Expand All @@ -139,7 +177,7 @@ public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
->with($this->equalTo($expectedGitCommand))
->will($this->returnValue(1));

$downloader = $this->getDownloaderMock(null, $processExecutor);
$downloader = $this->getDownloaderMock(null, null, $processExecutor);
$downloader->download($packageMock, 'composerPath');
}

Expand Down Expand Up @@ -195,7 +233,7 @@ public function testUpdate()
->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
->will($this->returnValue(0));

$downloader = $this->getDownloaderMock(null, $processExecutor);
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->update($packageMock, $packageMock, 'composerPath');
}

Expand Down Expand Up @@ -228,7 +266,7 @@ public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
->with($this->equalTo($expectedGitUpdateCommand))
->will($this->returnValue(1));

$downloader = $this->getDownloaderMock(null, $processExecutor);
$downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
$downloader->update($packageMock, $packageMock, 'composerPath');
}

Expand All @@ -248,7 +286,7 @@ public function testRemove()
->with($this->equalTo('composerPath'))
->will($this->returnValue(true));

$downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem);
$downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
$downloader->remove($packageMock, 'composerPath');
}

Expand Down

0 comments on commit 122f660

Please sign in to comment.