Browse Source

Merge remote-tracking branch 'hason/git_config'

Jordi Boggiano 12 years ago
parent
commit
122f66098e

+ 5 - 4
src/Composer/Command/CreateProjectCommand.php

@@ -89,12 +89,13 @@ EOT
 
     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)) {
@@ -183,10 +184,10 @@ EOT
         $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 - 0
src/Composer/Config.php

@@ -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(

+ 14 - 2
src/Composer/Downloader/GitDownloader.php

@@ -12,6 +12,10 @@
 
 namespace Composer\Downloader;
 
+use Composer\IO\IOInterface;
+use Composer\Config;
+use Composer\Util\Filesystem;
+use Composer\Util\ProcessExecutor;
 use Composer\Package\PackageInterface;
 
 /**
@@ -19,6 +23,14 @@ use Composer\Package\PackageInterface;
  */
 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}
      */
@@ -150,8 +162,8 @@ class GitDownloader extends VcsDownloader
         $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];

+ 3 - 3
src/Composer/Factory.php

@@ -155,7 +155,7 @@ class Factory
         $package = $loader->load($localConfig);
 
         // initialize download manager
-        $dm = $this->createDownloadManager($io);
+        $dm = $this->createDownloadManager($io, $config);
 
         // initialize installation manager
         $im = $this->createInstallationManager($config);
@@ -219,10 +219,10 @@ class Factory
      * @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));

+ 46 - 8
tests/Composer/Test/Downloader/GitDownloaderTest.php

@@ -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);
     }
 
     /**
@@ -64,7 +71,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->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');
     }
 
@@ -116,7 +123,38 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->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');
     }
 
@@ -139,7 +177,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->with($this->equalTo($expectedGitCommand))
             ->will($this->returnValue(1));
 
-        $downloader = $this->getDownloaderMock(null, $processExecutor);
+        $downloader = $this->getDownloaderMock(null, null, $processExecutor);
         $downloader->download($packageMock, 'composerPath');
     }
 
@@ -195,7 +233,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->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');
     }
 
@@ -228,7 +266,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->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');
     }
 
@@ -248,7 +286,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
             ->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');
     }