1
0
Fork 0

Merge pull request #7703 from glaubinix/t/composer-version-cache

Vcs Repository: add option to cache/reuse entire versions
pull/7762/head
Jordi Boggiano 2018-10-31 11:09:10 +01:00 committed by GitHub
commit 359d1318f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 10 deletions

View File

@ -41,8 +41,10 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
private $drivers; private $drivers;
/** @var VcsDriverInterface */ /** @var VcsDriverInterface */
private $driver; private $driver;
/** @var VersionCacheInterface */
private $versionCache;
public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $dispatcher = null, array $drivers = null) public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $dispatcher = null, array $drivers = null, VersionCacheInterface $versionCache = null)
{ {
parent::__construct(); parent::__construct();
$this->drivers = $drivers ?: array( $this->drivers = $drivers ?: array(
@ -64,6 +66,7 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
$this->verbose = $io->isVeryVerbose(); $this->verbose = $io->isVeryVerbose();
$this->config = $config; $this->config = $config;
$this->repoConfig = $repoConfig; $this->repoConfig = $repoConfig;
$this->versionCache = $versionCache;
} }
public function getRepoConfig() public function getRepoConfig()
@ -152,6 +155,13 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
// strip the release- prefix from tags if present // strip the release- prefix from tags if present
$tag = str_replace('release-', '', $tag); $tag = str_replace('release-', '', $tag);
$cachedPackage = $this->getCachedPackageVersion($tag, $identifier, $verbose);
if ($cachedPackage) {
$this->addPackage($cachedPackage);
continue;
}
if (!$parsedTag = $this->validateTag($tag)) { if (!$parsedTag = $this->validateTag($tag)) {
if ($verbose) { if ($verbose) {
$this->io->writeError('<warning>Skipped tag '.$tag.', invalid tag name</warning>'); $this->io->writeError('<warning>Skipped tag '.$tag.', invalid tag name</warning>');
@ -236,6 +246,21 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
continue; continue;
} }
// make sure branch packages have a dev flag
if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
$version = 'dev-' . $branch;
} else {
$prefix = substr($branch, 0, 1) === 'v' ? 'v' : '';
$version = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
}
$cachedPackage = $this->getCachedPackageVersion($version, $identifier, $verbose);
if ($cachedPackage) {
$this->addPackage($cachedPackage);
continue;
}
try { try {
if (!$data = $driver->getComposerInformation($identifier)) { if (!$data = $driver->getComposerInformation($identifier)) {
if ($verbose) { if ($verbose) {
@ -245,17 +270,9 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
} }
// branches are always auto-versioned, read value from branch name // branches are always auto-versioned, read value from branch name
$data['version'] = $branch; $data['version'] = $version;
$data['version_normalized'] = $parsedBranch; $data['version_normalized'] = $parsedBranch;
// make sure branch packages have a dev flag
if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
$data['version'] = 'dev-' . $data['version'];
} else {
$prefix = substr($branch, 0, 1) === 'v' ? 'v' : '';
$data['version'] = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
}
if ($verbose) { if ($verbose) {
$this->io->writeError('Importing branch '.$branch.' ('.$data['version'].')'); $this->io->writeError('Importing branch '.$branch.' ('.$data['version'].')');
} }
@ -327,4 +344,34 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt
return false; return false;
} }
private function getCachedPackageVersion($version, $identifier, $verbose)
{
if (!$this->versionCache) {
return;
}
$cachedPackage = $this->versionCache->getVersionPackage($version, $identifier);
if ($cachedPackage) {
$msg = 'Found cached composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $version . '</comment>)';
if ($verbose) {
$this->io->writeError($msg);
} else {
$this->io->overwriteError($msg, false);
}
if ($existingPackage = $this->findPackage($cachedPackage['name'], $cachedPackage['version_normalized'])) {
if ($verbose) {
$this->io->writeError('<warning>Skipped cached version '.$version.', it conflicts with an another tag ('.$existingPackage->getPrettyVersion().') as both resolve to '.$cachedPackage['version_normalized'].' internally</warning>');
}
$cachedPackage = null;
}
}
if ($cachedPackage) {
return $this->loader->load($cachedPackage);
}
return null;
}
} }

View File

@ -0,0 +1,23 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Repository;
interface VersionCacheInterface
{
/**
* @param string $version
* @param string $identifier
* @return array Package version data
*/
public function getVersionPackage($version, $identifier);
}