From 472a62152d3386dce33df3935d647cb6e508bc81 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 16:09:38 +0200 Subject: [PATCH] Store default branch info inside metadata --- src/Composer/Package/AliasPackage.php | 5 +++++ src/Composer/Package/Dumper/ArrayDumper.php | 4 ++++ src/Composer/Package/Loader/ArrayLoader.php | 7 +++++++ src/Composer/Package/Package.php | 17 +++++++++++++++++ src/Composer/Package/PackageInterface.php | 5 +++++ src/Composer/Repository/VcsRepository.php | 20 +++++++++++++++++++- 6 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index ee93ec497..b0649ded0 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -392,6 +392,11 @@ class AliasPackage extends BasePackage implements CompletePackageInterface return $this->aliasOf->getArchiveExcludes(); } + public function isDefaultBranch() + { + return $this->aliasOf->isDefaultBranch(); + } + public function isAbandoned() { return $this->aliasOf->isAbandoned(); diff --git a/src/Composer/Package/Dumper/ArrayDumper.php b/src/Composer/Package/Dumper/ArrayDumper.php index dece598f1..ce8d0c215 100644 --- a/src/Composer/Package/Dumper/ArrayDumper.php +++ b/src/Composer/Package/Dumper/ArrayDumper.php @@ -92,6 +92,10 @@ class ArrayDumper $data['time'] = $package->getReleaseDate()->format(DATE_RFC3339); } + if ($package->isDefaultBranch()) { + $data['default_branch'] = true; + } + $data = $this->dumpValues($package, $keys, $data); if ($package instanceof CompletePackageInterface) { diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index 228632b42..d24d34d36 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/src/Composer/Package/Loader/ArrayLoader.php @@ -53,6 +53,9 @@ class ArrayLoader implements LoaderInterface } else { $version = $this->versionParser->normalize($config['version']); } + if (isset($config['default_branch']) && $config['default_branch'] === true) { + $version = '9999999-dev'; + } $package = new $class($config['name'], $version, $config['version']); $package->setType(isset($config['type']) ? strtolower($config['type']) : 'library'); @@ -75,6 +78,10 @@ class ArrayLoader implements LoaderInterface $package->setInstallationSource($config['installation-source']); } + if (isset($config['default_branch']) && $config['default_branch'] === true) { + $package->setIsDefaultBranch(true); + } + if (isset($config['source'])) { if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) { throw new \UnexpectedValueException(sprintf( diff --git a/src/Composer/Package/Package.php b/src/Composer/Package/Package.php index 6c7b426e7..7ce40a507 100644 --- a/src/Composer/Package/Package.php +++ b/src/Composer/Package/Package.php @@ -58,6 +58,7 @@ class Package extends BasePackage protected $devAutoload = array(); protected $includePaths = array(); protected $archiveExcludes = array(); + protected $isDefaultBranch = false; /** * Creates a new in memory package. @@ -569,6 +570,22 @@ class Package extends BasePackage return $this->archiveExcludes; } + /** + * @param bool $defaultBranch + */ + public function setIsDefaultBranch($defaultBranch) + { + $this->isDefaultBranch = $defaultBranch; + } + + /** + * {@inheritDoc} + */ + public function isDefaultBranch() + { + return $this->isDefaultBranch; + } + /** * Replaces current version and pretty version with passed values. * It also sets stability. diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index cb16efa7e..c09c9e7ba 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -352,6 +352,11 @@ interface PackageInterface */ public function getArchiveExcludes(); + /** + * @return bool + */ + public function isDefaultBranch(); + /** * Returns a list of options to download package dist files * diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index ccc49396a..dd3aa83d1 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -149,8 +149,10 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt $this->loader = new ArrayLoader($this->versionParser); } + $hasRootIdentifierComposerJson = false; try { - if ($driver->hasComposerFile($driver->getRootIdentifier())) { + $hasRootIdentifierComposerJson = $driver->hasComposerFile($driver->getRootIdentifier()); + if ($hasRootIdentifierComposerJson) { $data = $driver->getComposerInformation($driver->getRootIdentifier()); $this->packageName = !empty($data['name']) ? $data['name'] : null; } @@ -211,6 +213,9 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']); $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']); + // make sure tag do not contain the default_branch marker + unset($data['default_branch']); + // broken package, version doesn't match tag if ($data['version_normalized'] !== $parsedTag) { if ($isVeryVerbose) { @@ -251,6 +256,11 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt } $branches = $driver->getBranches(); + // make sure the root identifier branch gets loaded first + if ($hasRootIdentifierComposerJson && isset($branches[$driver->getRootIdentifier()])) { + $branches = array($driver->getRootIdentifier() => $branches[$driver->getRootIdentifier()]) + $branches; + } + foreach ($branches as $branch => $identifier) { $msg = 'Reading composer.json of ' . ($this->packageName ?: $this->url) . ' (' . $branch . ')'; if ($isVeryVerbose) { @@ -280,6 +290,9 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt $prefix = substr($branch, 0, 1) === 'v' ? 'v' : ''; $version = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch); } + if ($driver->getRootIdentifier() === $branch) { + $parsedBranch = '9999999-dev'; + } $cachedPackage = $this->getCachedPackageVersion($version, $identifier, $isVerbose, $isVeryVerbose); if ($cachedPackage) { @@ -305,6 +318,11 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt $data['version'] = $version; $data['version_normalized'] = $parsedBranch; + unset($data['default_branch']); + if ($driver->getRootIdentifier() === $branch) { + $data['default_branch'] = true; + } + if ($isVeryVerbose) { $this->io->writeError('Importing branch '.$branch.' ('.$data['version'].')'); }