diff --git a/doc/04-schema.md b/doc/04-schema.md index ec885bf85..c4de65150 100644 --- a/doc/04-schema.md +++ b/doc/04-schema.md @@ -655,6 +655,8 @@ The following options are supported: * **prepend-autoloader:** Defaults to `true`. If false, the composer autoloader will not be prepended to existing autoloaders. This is sometimesrequired to fix 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 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. diff --git a/res/composer-schema.json b/res/composer-schema.json index 1f68c8b43..7b52d7733 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -179,6 +179,13 @@ "prepend-autoloader": { "type": "boolean", "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" + } } } }, diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 6151b4f81..c893091c6 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -36,6 +36,7 @@ class Config 'cache-files-maxsize' => '300MiB', 'discard-changes' => false, 'prepend-autoloader' => true, + 'github-domains' => array('github.com'), ); public static $defaultRepositories = array( diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index f320cf931..c8e5744bc 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -293,7 +293,7 @@ class GitDownloader extends VcsDownloader } // 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'); if (!is_array($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); if (0 !== $this->process->execute($command, $ignoredOutput, $cwd)) { // 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])) { $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'; @@ -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) { if (0 !== $this->process->execute('git --version', $ignoredOutput)) { @@ -385,9 +390,9 @@ class GitDownloader extends VcsDownloader protected function setPushUrl(PackageInterface $package, $path) { // 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'); - $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git'; + $pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git'; if ($protocols[0] !== 'git') { $pushUrl = 'https://github.com/'.$match[1].'/'.$match[2].'.git'; } diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 1d093574a..7a0816eaf 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -24,10 +24,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase $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)); + $config = new Config(); } return new GitDownloader($io, $config, $executor, $filesystem);