Browse Source

Add github-domains config value for GitHub Enterprise setups, fixes #728

Signed-off-by: Gennady Feldman <gena01@gmail.com>
Jordi Boggiano 12 years ago
parent
commit
7148b22414

+ 2 - 0
doc/04-schema.md

@@ -655,6 +655,8 @@ The following options are supported:
 * **prepend-autoloader:** Defaults to `true`. If false, the composer autoloader
 * **prepend-autoloader:** Defaults to `true`. If false, the composer autoloader
   will not be prepended to existing autoloaders. This is sometimesrequired to fix
   will not be prepended to existing autoloaders. This is sometimesrequired to fix
   interoperability issues with other autoloaders.
   interoperability issues with other autoloaders.
+* **github-domains:** Defaults to `["github.com"]`. A list of domains to use in
+  github mode. This is used for GitHub Enterprise setups.
 * **notify-on-install:** Defaults to `true`. Composer allows repositories to
 * **notify-on-install:** Defaults to `true`. Composer allows repositories to
   define a notification URL, so that they get notified whenever a package from
   define a notification URL, so that they get notified whenever a package from
   that repository is installed. This option allows you to disable that behaviour.
   that repository is installed. This option allows you to disable that behaviour.

+ 7 - 0
res/composer-schema.json

@@ -179,6 +179,13 @@
                 "prepend-autoloader": {
                 "prepend-autoloader": {
                     "type": "boolean",
                     "type": "boolean",
                     "description": "If false, the composer autoloader will not be prepended to existing autoloaders, defaults to true."
                     "description": "If false, the composer autoloader will not be prepended to existing autoloaders, defaults to true."
+                },
+                "github-domains": {
+                    "type": "array",
+                    "description": "A list of domains to use in github mode. This is used for GitHub Enterprise setups, defaults to [\"github.com\"].",
+                    "items": {
+                        "type": "string"
+                    }
                 }
                 }
             }
             }
         },
         },

+ 1 - 0
src/Composer/Config.php

@@ -36,6 +36,7 @@ class Config
         'cache-files-maxsize' => '300MiB',
         'cache-files-maxsize' => '300MiB',
         'discard-changes' => false,
         'discard-changes' => false,
         'prepend-autoloader' => true,
         'prepend-autoloader' => true,
+        'github-domains' => array('github.com'),
     );
     );
 
 
     public static $defaultRepositories = array(
     public static $defaultRepositories = array(

+ 9 - 4
src/Composer/Downloader/GitDownloader.php

@@ -293,7 +293,7 @@ class GitDownloader extends VcsDownloader
         }
         }
 
 
         // public github, autoswitch protocols
         // public github, autoswitch protocols
-        if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
+        if (preg_match('{^(?:https?|git)(://'.$this->getGitHubDomainsRegex().'/.*)}', $url, $match)) {
             $protocols = $this->config->get('github-protocols');
             $protocols = $this->config->get('github-protocols');
             if (!is_array($protocols)) {
             if (!is_array($protocols)) {
                 throw new \RuntimeException('Config value "github-protocols" must be an array, got '.gettype($protocols));
                 throw new \RuntimeException('Config value "github-protocols" must be an array, got '.gettype($protocols));
@@ -317,7 +317,7 @@ class GitDownloader extends VcsDownloader
         $command = call_user_func($commandCallable, $url);
         $command = call_user_func($commandCallable, $url);
         if (0 !== $this->process->execute($command, $ignoredOutput, $cwd)) {
         if (0 !== $this->process->execute($command, $ignoredOutput, $cwd)) {
             // private github repository without git access, try https with auth
             // private github repository without git access, try https with auth
-            if (preg_match('{^git@(github.com):(.+?)\.git$}i', $url, $match)) {
+            if (preg_match('{^git@'.$this->getGitHubDomainsRegex().':(.+?)\.git$}i', $url, $match)) {
                 if (!$this->io->hasAuthentication($match[1])) {
                 if (!$this->io->hasAuthentication($match[1])) {
                     $gitHubUtil = new GitHub($this->io, $this->config, $this->process);
                     $gitHubUtil = new GitHub($this->io, $this->config, $this->process);
                     $message = 'Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos';
                     $message = 'Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos';
@@ -368,6 +368,11 @@ class GitDownloader extends VcsDownloader
         }
         }
     }
     }
 
 
+    protected function getGitHubDomainsRegex()
+    {
+        return '('.implode('|', array_map('preg_quote', $this->config->get('github-domains'))).')';
+    }
+
     protected function throwException($message, $url)
     protected function throwException($message, $url)
     {
     {
         if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
         if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
@@ -385,9 +390,9 @@ class GitDownloader extends VcsDownloader
     protected function setPushUrl(PackageInterface $package, $path)
     protected function setPushUrl(PackageInterface $package, $path)
     {
     {
         // set push url for github projects
         // set push url for github projects
-        if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
+        if (preg_match('{^(?:https?|git)://'.$this->getGitHubDomainsRegex().'/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
             $protocols = $this->config->get('github-protocols');
             $protocols = $this->config->get('github-protocols');
-            $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
+            $pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git';
             if ($protocols[0] !== 'git') {
             if ($protocols[0] !== 'git') {
                 $pushUrl = 'https://github.com/'.$match[1].'/'.$match[2].'.git';
                 $pushUrl = 'https://github.com/'.$match[1].'/'.$match[2].'.git';
             }
             }

+ 1 - 4
tests/Composer/Test/Downloader/GitDownloaderTest.php

@@ -24,10 +24,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
         $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
         $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
         $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
         $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
         if (!$config) {
         if (!$config) {
-            $config = $this->getMock('Composer\Config');
-            $config->expects($this->any())
-                ->method('has')
-                ->will($this->returnValue(false));
+            $config = new Config();
         }
         }
 
 
         return new GitDownloader($io, $config, $executor, $filesystem);
         return new GitDownloader($io, $config, $executor, $filesystem);