From 7148b224142a7c928c45e59049efb4d82d5677de Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 8 Sep 2012 01:51:33 +0200 Subject: [PATCH 1/8] Add github-domains config value for GitHub Enterprise setups, fixes #728 Signed-off-by: Gennady Feldman --- doc/04-schema.md | 2 ++ res/composer-schema.json | 7 +++++++ src/Composer/Config.php | 1 + src/Composer/Downloader/GitDownloader.php | 13 +++++++++---- .../Composer/Test/Downloader/GitDownloaderTest.php | 5 +---- 5 files changed, 20 insertions(+), 8 deletions(-) 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); From f8376a5b345a151c1701c83844fcabd9ef983195 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 12:23:06 -0400 Subject: [PATCH 2/8] Updating ConfigCommand so that we can set github-domains from the command line. --- src/Composer/Command/ConfigCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 7f92b2503..edc370a66 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -308,6 +308,18 @@ EOT return $vals; } ), + 'github-domains' => array( + function ($vals) { + if (!is_array($vals)) { + return 'array expected'; + } + + return true; + }, + function ($vals) { + return $vals; + } + ), ); foreach ($uniqueConfigValues as $name => $callbacks) { From e78499d28de4fd72d422fab3c3d6661cc4c88906 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 12:29:37 -0400 Subject: [PATCH 3/8] First working version of GitHub Enterprise API. --- src/Composer/Repository/Vcs/GitHubDriver.php | 36 ++++++++++++-------- src/Composer/Util/GitHub.php | 8 ++--- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 6345fa473..f6cdd16f8 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -45,10 +45,10 @@ class GitHubDriver extends VcsDriver */ public function initialize() { - preg_match('#^(?:(?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); - $this->owner = $match[1]; - $this->repository = $match[2]; - $this->originUrl = 'github.com'; + preg_match('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); + $this->owner = $match[3]; + $this->repository = $match[4]; + $this->originUrl = isset($match[1]) ? $match[1] : $match[2]; $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository); $this->fetchRootIdentifier(); @@ -75,7 +75,7 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getUrl(); } - return 'https://github.com/'.$this->owner.'/'.$this->repository.'.git'; + return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git'; } /** @@ -105,7 +105,8 @@ class GitHubDriver extends VcsDriver if ($this->gitDriver) { return $this->gitDriver->getDist($identifier); } - $url = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier; + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $url = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier; return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => ''); } @@ -127,7 +128,8 @@ class GitHubDriver extends VcsDriver $notFoundRetries = 2; while ($notFoundRetries) { try { - $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); $composer = JsonFile::parseJson($this->getContents($resource)); if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) { throw new \RuntimeException('Could not retrieve composer.json from '.$resource); @@ -149,16 +151,17 @@ class GitHubDriver extends VcsDriver $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { - $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); $commit = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $commit['commit']['committer']['date']; } if (!isset($composer['support']['source'])) { $label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier; - $composer['support']['source'] = sprintf('https://github.com/%s/%s/tree/%s', $this->owner, $this->repository, $label); + $composer['support']['source'] = sprintf('https://%s/%s/%s/tree/%s', $this->originUrl, $this->owner, $this->repository, $label); } if (!isset($composer['support']['issues']) && $this->hasIssues) { - $composer['support']['issues'] = sprintf('https://github.com/%s/%s/issues', $this->owner, $this->repository); + $composer['support']['issues'] = sprintf('https://%s/%s/%s/issues', $this->originUrl, $this->owner, $this->repository); } } @@ -181,7 +184,8 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getTags(); } if (null === $this->tags) { - $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'; + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/tags'; $tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $this->tags = array(); foreach ($tagsData as $tag) { @@ -201,7 +205,8 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getBranches(); } if (null === $this->branches) { - $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; $branchData = JsonFile::parseJson($this->getContents($resource), $resource); $this->branches = array(); foreach ($branchData as $branch) { @@ -218,7 +223,7 @@ class GitHubDriver extends VcsDriver */ public static function supports(IOInterface $io, $url, $deep = false) { - if (!preg_match('#^((?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $url)) { + if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $url)) { return false; } @@ -240,7 +245,7 @@ class GitHubDriver extends VcsDriver */ protected function generateSshUrl() { - return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git'; + return 'git@' . $this->originUrl . ':'.$this->owner.'/'.$this->repository.'.git'; } /** @@ -357,7 +362,8 @@ class GitHubDriver extends VcsDriver */ protected function fetchRootIdentifier() { - $repoDataUrl = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository; + $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; + $repoDataUrl = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository; $repoData = JsonFile::parseJson($this->getContents($repoDataUrl, true), $repoDataUrl); if (null === $repoData && null !== $this->gitDriver) { diff --git a/src/Composer/Util/GitHub.php b/src/Composer/Util/GitHub.php index 93894cc76..ed988894b 100644 --- a/src/Composer/Util/GitHub.php +++ b/src/Composer/Util/GitHub.php @@ -51,10 +51,6 @@ class GitHub */ public function authorizeOAuth($originUrl) { - if ('github.com' !== $originUrl) { - return false; - } - // if available use token from git config if (0 === $this->process->execute('git config github.accesstoken', $output)) { $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); @@ -78,6 +74,8 @@ class GitHub { $attemptCounter = 0; + $apiUrl = ('github.com' === $originUrl) ? 'api.github.com' : $originUrl . '/api/v3'; + if ($message) { $this->io->write($message); } @@ -95,7 +93,7 @@ class GitHub $appName .= ' on ' . trim($output); } - $contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://api.github.com/authorizations', false, array( + $contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/authorizations', false, array( 'http' => array( 'method' => 'POST', 'follow_location' => false, From a4d7fc138a02afa9cc22cb826f5785906f172885 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 13:08:18 -0400 Subject: [PATCH 4/8] Fixing broken unit test. --- src/Composer/Downloader/GitDownloader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index c8e5744bc..56b55629e 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -394,7 +394,7 @@ class GitDownloader extends VcsDownloader $protocols = $this->config->get('github-protocols'); $pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git'; if ($protocols[0] !== 'git') { - $pushUrl = 'https://github.com/'.$match[1].'/'.$match[2].'.git'; + $pushUrl = 'https://' . $match[1] . '/'.$match[2].'/'.$match[3].'.git'; } $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl)); $this->process->execute($cmd, $ignoredOutput, $path); From 6419266ea3a25332f2070bf5b6363f1d299a23fc Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 15:32:51 -0400 Subject: [PATCH 5/8] Validate the originUrl against the list of 'github-domains' from the config. --- src/Composer/Util/GitHub.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Composer/Util/GitHub.php b/src/Composer/Util/GitHub.php index ed988894b..49e56f8c9 100644 --- a/src/Composer/Util/GitHub.php +++ b/src/Composer/Util/GitHub.php @@ -51,6 +51,10 @@ class GitHub */ public function authorizeOAuth($originUrl) { + if (!in_array($originUrl, $this->config->get('github-domains'))) { + return false; + } + // if available use token from git config if (0 === $this->process->execute('git config github.accesstoken', $output)) { $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); From d8dbcab710e16a06d9908986260d81cda912f585 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 15:38:28 -0400 Subject: [PATCH 6/8] Refactoring my code to use a protected function to generate API Url. --- src/Composer/Repository/Vcs/GitHubDriver.php | 33 +++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index f6cdd16f8..f84eb74ca 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -78,6 +78,20 @@ class GitHubDriver extends VcsDriver return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git'; } + /** + * {@inheritDoc} + */ + protected function getApiUrl() + { + if ('github.com' === $this->originUrl) { + $apiUrl = 'api.github.com'; + } else { + $apiUrl = $this->originUrl . '/api/v3'; + } + + return 'https://' . $apiUrl; + } + /** * {@inheritDoc} */ @@ -105,8 +119,8 @@ class GitHubDriver extends VcsDriver if ($this->gitDriver) { return $this->gitDriver->getDist($identifier); } - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $url = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier; + + $url = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$identifier; return array('type' => 'zip', 'url' => $url, 'reference' => $identifier, 'shasum' => ''); } @@ -128,8 +142,7 @@ class GitHubDriver extends VcsDriver $notFoundRetries = 2; while ($notFoundRetries) { try { - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); + $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); $composer = JsonFile::parseJson($this->getContents($resource)); if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) { throw new \RuntimeException('Could not retrieve composer.json from '.$resource); @@ -151,8 +164,7 @@ class GitHubDriver extends VcsDriver $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); + $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); $commit = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $commit['commit']['committer']['date']; } @@ -184,8 +196,7 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getTags(); } if (null === $this->tags) { - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/tags'; + $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/tags'; $tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $this->tags = array(); foreach ($tagsData as $tag) { @@ -205,8 +216,7 @@ class GitHubDriver extends VcsDriver return $this->gitDriver->getBranches(); } if (null === $this->branches) { - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $resource = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; + $resource = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; $branchData = JsonFile::parseJson($this->getContents($resource), $resource); $this->branches = array(); foreach ($branchData as $branch) { @@ -362,8 +372,7 @@ class GitHubDriver extends VcsDriver */ protected function fetchRootIdentifier() { - $apiUrl = ('github.com' === $this->originUrl) ? 'api.github.com' : $this->originUrl . '/api/v3'; - $repoDataUrl = 'https://' . $apiUrl . '/repos/'.$this->owner.'/'.$this->repository; + $repoDataUrl = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository; $repoData = JsonFile::parseJson($this->getContents($repoDataUrl, true), $repoDataUrl); if (null === $repoData && null !== $this->gitDriver) { From 93ebfd54b13e3d2cedd55f3a767721e1fb60517f Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Mon, 28 Oct 2013 15:57:02 -0400 Subject: [PATCH 7/8] Adding Config as parameter to the Driver::supports(), updating all drivers, user and tests. --- src/Composer/Repository/Vcs/GitBitbucketDriver.php | 3 ++- src/Composer/Repository/Vcs/GitDriver.php | 3 ++- src/Composer/Repository/Vcs/GitHubDriver.php | 10 ++++++++-- src/Composer/Repository/Vcs/HgBitbucketDriver.php | 3 ++- src/Composer/Repository/Vcs/HgDriver.php | 3 ++- src/Composer/Repository/Vcs/PerforceDriver.php | 3 ++- src/Composer/Repository/Vcs/SvnDriver.php | 3 ++- src/Composer/Repository/Vcs/VcsDriverInterface.php | 10 ++++++---- src/Composer/Repository/VcsRepository.php | 4 ++-- .../Test/Repository/Vcs/PerforceDriverTest.php | 9 ++++----- tests/Composer/Test/Repository/Vcs/SvnDriverTest.php | 8 +++----- 11 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitBitbucketDriver.php b/src/Composer/Repository/Vcs/GitBitbucketDriver.php index 950115a79..c9a3c2fdf 100644 --- a/src/Composer/Repository/Vcs/GitBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/GitBitbucketDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\Json\JsonFile; use Composer\IO\IOInterface; @@ -140,7 +141,7 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url)) { return false; diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index e2a3a9eb0..2bcb79e23 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -18,6 +18,7 @@ use Composer\Util\Filesystem; use Composer\Util\Git as GitUtil; use Composer\IO\IOInterface; use Composer\Cache; +use Composer\Config; /** * @author Jordi Boggiano @@ -211,7 +212,7 @@ class GitDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { if (preg_match('#(^git://|\.git$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) { return true; diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index f84eb74ca..e84c1d012 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\Downloader\TransportException; use Composer\Json\JsonFile; use Composer\Cache; @@ -231,9 +232,14 @@ class GitHubDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { - if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $url)) { + if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $url, $matches)) { + return false; + } + + $originUrl = isset($matches[2]) ? $matches[2] : $matches[3]; + if (!in_array($originUrl, $config->get('github-domains'))) { return false; } diff --git a/src/Composer/Repository/Vcs/HgBitbucketDriver.php b/src/Composer/Repository/Vcs/HgBitbucketDriver.php index 80683a465..9c403e71d 100644 --- a/src/Composer/Repository/Vcs/HgBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/HgBitbucketDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\Json\JsonFile; use Composer\IO\IOInterface; @@ -150,7 +151,7 @@ class HgBitbucketDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url)) { return false; diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php index c3c06e38a..abfd0de51 100644 --- a/src/Composer/Repository/Vcs/HgDriver.php +++ b/src/Composer/Repository/Vcs/HgDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\Json\JsonFile; use Composer\Util\ProcessExecutor; use Composer\Util\Filesystem; @@ -189,7 +190,7 @@ class HgDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) { return true; diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 54e1d3e7d..6ff51125a 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\IO\IOInterface; use Composer\Util\ProcessExecutor; use Composer\Util\Perforce; @@ -158,7 +159,7 @@ class PerforceDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { if ($deep || preg_match('#\b(perforce|p4)\b#i', $url)) { return Perforce::checkServerExists($url, new ProcessExecutor); diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index c5a67b455..a2e345b07 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -13,6 +13,7 @@ namespace Composer\Repository\Vcs; use Composer\Cache; +use Composer\Config; use Composer\Json\JsonFile; use Composer\Util\ProcessExecutor; use Composer\Util\Filesystem; @@ -241,7 +242,7 @@ class SvnDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, $deep = false) + public static function supports(IOInterface $io, $url, Config $config, $deep = false) { $url = self::normalizeUrl($url); if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) { diff --git a/src/Composer/Repository/Vcs/VcsDriverInterface.php b/src/Composer/Repository/Vcs/VcsDriverInterface.php index b29841b68..741cf1dda 100644 --- a/src/Composer/Repository/Vcs/VcsDriverInterface.php +++ b/src/Composer/Repository/Vcs/VcsDriverInterface.php @@ -12,6 +12,7 @@ namespace Composer\Repository\Vcs; +use Composer\Config; use Composer\IO\IOInterface; /** @@ -90,10 +91,11 @@ interface VcsDriverInterface /** * Checks if this driver can handle a given url * - * @param IOInterface $io IO instance - * @param string $url - * @param bool $deep unless true, only shallow checks (url matching typically) should be done + * @param IOInterface $io IO instance + * @param string $url URL to validate/check + * @param Config $config current $config + * @param bool $deep unless true, only shallow checks (url matching typically) should be done * @return bool */ - public static function supports(IOInterface $io, $url, $deep = false); + public static function supports(IOInterface $io, $url, Config $config, $deep = false); } diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index 701db33bb..a18328725 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -80,7 +80,7 @@ class VcsRepository extends ArrayRepository } foreach ($this->drivers as $driver) { - if ($driver::supports($this->io, $this->url)) { + if ($driver::supports($this->io, $this->url, $this->config)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); @@ -89,7 +89,7 @@ class VcsRepository extends ArrayRepository } foreach ($this->drivers as $driver) { - if ($driver::supports($this->io, $this->url, true)) { + if ($driver::supports($this->io, $this->url, $this->config, true)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index d2c8167b9..ba418b2a2 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -104,7 +104,6 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase public function testHasComposerFile() { - $this->setUp(); $repoConfig = array( 'url' => 'TEST_PERFORCE_URL', 'depot' => 'TEST_DEPOT_CONFIG', @@ -131,17 +130,17 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase $result = $driver->hasComposerFile($identifier); $this->assertTrue($result); } - + /** * Test that supports() simply return false. - * + * * @covers \Composer\Repository\Vcs\PerforceDriver::supports - * + * * @return void */ public function testSupportsReturnsFalseNoDeepCheck() { $this->expectOutputString(''); - $this->assertFalse(PerforceDriver::supports($this->io, 'existing.url')); + $this->assertFalse(PerforceDriver::supports($this->io, 'existing.url', $this->config)); } } diff --git a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php index d9bd53321..1b4437a04 100644 --- a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php @@ -80,10 +80,8 @@ class SvnDriverTest extends \PHPUnit_Framework_TestCase */ public function testSupport($url, $assertion) { - if ($assertion === true) { - $this->assertTrue(SvnDriver::supports($this->getMock('Composer\IO\IOInterface'), $url)); - } else { - $this->assertFalse(SvnDriver::supports($this->getMock('Composer\IO\IOInterface'), $url)); - } + $config = new Config(); + $result = SvnDriver::supports($this->getMock('Composer\IO\IOInterface'), $url, $config); + $this->assertEquals($assertion, $result); } } From e5045ce2152c77fefd54f1e3c0c88dcb33af1e02 Mon Sep 17 00:00:00 2001 From: Gennady Feldman Date: Tue, 29 Oct 2013 11:00:00 -0400 Subject: [PATCH 8/8] Per request from Jordi Boggiano (Seldaek) making Config 2nd parameter in supports() --- src/Composer/Repository/Vcs/GitBitbucketDriver.php | 2 +- src/Composer/Repository/Vcs/GitDriver.php | 2 +- src/Composer/Repository/Vcs/GitHubDriver.php | 2 +- src/Composer/Repository/Vcs/HgBitbucketDriver.php | 2 +- src/Composer/Repository/Vcs/HgDriver.php | 2 +- src/Composer/Repository/Vcs/PerforceDriver.php | 2 +- src/Composer/Repository/Vcs/SvnDriver.php | 2 +- src/Composer/Repository/Vcs/VcsDriverInterface.php | 4 ++-- src/Composer/Repository/VcsRepository.php | 4 ++-- tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php | 2 +- tests/Composer/Test/Repository/Vcs/SvnDriverTest.php | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitBitbucketDriver.php b/src/Composer/Repository/Vcs/GitBitbucketDriver.php index c9a3c2fdf..12eba6d3a 100644 --- a/src/Composer/Repository/Vcs/GitBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/GitBitbucketDriver.php @@ -141,7 +141,7 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url)) { return false; diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index 2bcb79e23..ecfad77db 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -212,7 +212,7 @@ class GitDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if (preg_match('#(^git://|\.git$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) { return true; diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index e84c1d012..428c2fdf4 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -232,7 +232,7 @@ class GitHubDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if (!preg_match('#^((?:https?|git)://([^/]+)/|git@([^:]+):)([^/]+)/(.+?)(?:\.git)?$#', $url, $matches)) { return false; diff --git a/src/Composer/Repository/Vcs/HgBitbucketDriver.php b/src/Composer/Repository/Vcs/HgBitbucketDriver.php index 9c403e71d..c6eac73b9 100644 --- a/src/Composer/Repository/Vcs/HgBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/HgBitbucketDriver.php @@ -151,7 +151,7 @@ class HgBitbucketDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url)) { return false; diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php index abfd0de51..5de081cca 100644 --- a/src/Composer/Repository/Vcs/HgDriver.php +++ b/src/Composer/Repository/Vcs/HgDriver.php @@ -190,7 +190,7 @@ class HgDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) { return true; diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 6ff51125a..484f1e96f 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -159,7 +159,7 @@ class PerforceDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { if ($deep || preg_match('#\b(perforce|p4)\b#i', $url)) { return Perforce::checkServerExists($url, new ProcessExecutor); diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index a2e345b07..d69230cce 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -242,7 +242,7 @@ class SvnDriver extends VcsDriver /** * {@inheritDoc} */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false) + public static function supports(IOInterface $io, Config $config, $url, $deep = false) { $url = self::normalizeUrl($url); if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) { diff --git a/src/Composer/Repository/Vcs/VcsDriverInterface.php b/src/Composer/Repository/Vcs/VcsDriverInterface.php index 741cf1dda..dd30baacd 100644 --- a/src/Composer/Repository/Vcs/VcsDriverInterface.php +++ b/src/Composer/Repository/Vcs/VcsDriverInterface.php @@ -92,10 +92,10 @@ interface VcsDriverInterface * Checks if this driver can handle a given url * * @param IOInterface $io IO instance - * @param string $url URL to validate/check * @param Config $config current $config + * @param string $url URL to validate/check * @param bool $deep unless true, only shallow checks (url matching typically) should be done * @return bool */ - public static function supports(IOInterface $io, $url, Config $config, $deep = false); + public static function supports(IOInterface $io, Config $config, $url, $deep = false); } diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index a18328725..c7a543748 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -80,7 +80,7 @@ class VcsRepository extends ArrayRepository } foreach ($this->drivers as $driver) { - if ($driver::supports($this->io, $this->url, $this->config)) { + if ($driver::supports($this->io, $this->config, $this->url)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); @@ -89,7 +89,7 @@ class VcsRepository extends ArrayRepository } foreach ($this->drivers as $driver) { - if ($driver::supports($this->io, $this->url, $this->config, true)) { + if ($driver::supports($this->io, $this->config, $this->url, true)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); diff --git a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php index ba418b2a2..36cd69ebc 100644 --- a/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php @@ -141,6 +141,6 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase public function testSupportsReturnsFalseNoDeepCheck() { $this->expectOutputString(''); - $this->assertFalse(PerforceDriver::supports($this->io, 'existing.url', $this->config)); + $this->assertFalse(PerforceDriver::supports($this->io, $this->config, 'existing.url')); } } diff --git a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php index 1b4437a04..2f698565f 100644 --- a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php @@ -81,7 +81,7 @@ class SvnDriverTest extends \PHPUnit_Framework_TestCase public function testSupport($url, $assertion) { $config = new Config(); - $result = SvnDriver::supports($this->getMock('Composer\IO\IOInterface'), $url, $config); + $result = SvnDriver::supports($this->getMock('Composer\IO\IOInterface'), $config, $url); $this->assertEquals($assertion, $result); } }