Bladeren bron

Add support for extra.branch-alias config to remap master on 2.1.x for example

Jordi Boggiano 13 jaren geleden
bovenliggende
commit
1228c04984

+ 2 - 2
src/Composer/Command/InstallCommand.php

@@ -113,10 +113,10 @@ EOT
         }
         }
         foreach ($aliases as $alias) {
         foreach ($aliases as $alias) {
             foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) {
             foreach ($repoManager->findPackages($alias['package'], $alias['version']) as $package) {
-                $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias']));
+                $package->getRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
             }
             }
             foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
             foreach ($repoManager->getLocalRepository()->findPackages($alias['package'], $alias['version']) as $package) {
-                $repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias']));
+                $repoManager->getLocalRepository()->addPackage(new AliasPackage($package, $alias['alias_normalized'], $alias['alias']));
                 $repoManager->getLocalRepository()->removePackage($package);
                 $repoManager->getLocalRepository()->removePackage($package);
             }
             }
         }
         }

+ 31 - 8
src/Composer/Package/AliasPackage.php

@@ -23,6 +23,7 @@ use Composer\Repository\PlatformRepository;
 class AliasPackage extends BasePackage
 class AliasPackage extends BasePackage
 {
 {
     protected $version;
     protected $version;
+    protected $prettyVersion;
     protected $dev;
     protected $dev;
     protected $aliasOf;
     protected $aliasOf;
 
 
@@ -38,12 +39,14 @@ class AliasPackage extends BasePackage
      *
      *
      * @param PackageInterface $aliasOf The package this package is an alias of
      * @param PackageInterface $aliasOf The package this package is an alias of
      * @param string $version The version the alias must report
      * @param string $version The version the alias must report
+     * @param string $prettyVersion The alias's non-normalized version
      */
      */
-    public function __construct($aliasOf, $version)
+    public function __construct($aliasOf, $version, $prettyVersion)
     {
     {
         parent::__construct($aliasOf->getName());
         parent::__construct($aliasOf->getName());
 
 
         $this->version = $version;
         $this->version = $version;
+        $this->prettyVersion = $prettyVersion;
         $this->aliasOf = $aliasOf;
         $this->aliasOf = $aliasOf;
         $this->dev = 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4);
         $this->dev = 'dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4);
 
 
@@ -78,7 +81,7 @@ class AliasPackage extends BasePackage
      */
      */
     public function getPrettyVersion()
     public function getPrettyVersion()
     {
     {
-        return $this->version;
+        return $this->prettyVersion;
     }
     }
 
 
     /**
     /**
@@ -92,7 +95,7 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getRequires()
+    public function getRequires()
     {
     {
         return $this->requires;
         return $this->requires;
     }
     }
@@ -100,7 +103,7 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getConflicts()
+    public function getConflicts()
     {
     {
         return $this->conflicts;
         return $this->conflicts;
     }
     }
@@ -108,7 +111,7 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getProvides()
+    public function getProvides()
     {
     {
         return $this->provides;
         return $this->provides;
     }
     }
@@ -116,7 +119,7 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getReplaces()
+    public function getReplaces()
     {
     {
         return $this->replaces;
         return $this->replaces;
     }
     }
@@ -124,7 +127,7 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getRecommends()
+    public function getRecommends()
     {
     {
         return $this->recommends;
         return $this->recommends;
     }
     }
@@ -132,11 +135,27 @@ class AliasPackage extends BasePackage
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    function getSuggests()
+    public function getSuggests()
     {
     {
         return $this->suggests;
         return $this->suggests;
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
+    public function getAlias()
+    {
+        return '';
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getPrettyAlias()
+    {
+        return '';
+    }
+
     /***************************************
     /***************************************
      * Wrappers around the aliased package *
      * Wrappers around the aliased package *
      ***************************************/
      ***************************************/
@@ -173,6 +192,10 @@ class AliasPackage extends BasePackage
     {
     {
         return $this->aliasOf->getSourceReference();
         return $this->aliasOf->getSourceReference();
     }
     }
+    public function setSourceReference($reference)
+    {
+        return $this->aliasOf->setSourceReference($reference);
+    }
     public function getDistType()
     public function getDistType()
     {
     {
         return $this->aliasOf->getDistType();
         return $this->aliasOf->getDistType();

+ 6 - 0
src/Composer/Package/Dumper/ArrayDumper.php

@@ -42,6 +42,12 @@ class ArrayDumper
         $data['name'] = $package->getPrettyName();
         $data['name'] = $package->getPrettyName();
         $data['version'] = $package->getPrettyVersion();
         $data['version'] = $package->getPrettyVersion();
         $data['version_normalized'] = $package->getVersion();
         $data['version_normalized'] = $package->getVersion();
+
+        if ($package->getAlias()) {
+            $data['alias'] = $package->getPrettyAlias();
+            $data['alias_normalized'] = $package->getAlias();
+        }
+
         if ($package->getTargetDir()) {
         if ($package->getTargetDir()) {
             $data['target-dir'] = $package->getTargetDir();
             $data['target-dir'] = $package->getTargetDir();
         }
         }

+ 6 - 0
src/Composer/Package/Loader/ArrayLoader.php

@@ -134,6 +134,12 @@ class ArrayLoader
             $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
             $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
         }
         }
 
 
+        // load alias for named dev packages
+        if (!empty($config['alias']) && 'dev-' === substr($package->getPrettyVersion(), 0, 4)) {
+            $package->setAlias($config['alias_normalized']);
+            $package->setPrettyAlias($config['alias']);
+        }
+
         foreach (Package\BasePackage::$supportedLinkTypes as $type => $description) {
         foreach (Package\BasePackage::$supportedLinkTypes as $type => $description) {
             if (isset($config[$type])) {
             if (isset($config[$type])) {
                 $method = 'set'.ucfirst($description);
                 $method = 'set'.ucfirst($description);

+ 2 - 1
src/Composer/Package/Loader/RootPackageLoader.php

@@ -50,7 +50,8 @@ class RootPackageLoader extends ArrayLoader
                     $aliases[] = array(
                     $aliases[] = array(
                         'package' => strtolower($reqName),
                         'package' => strtolower($reqName),
                         'version' => $this->versionParser->normalize($match[1]),
                         'version' => $this->versionParser->normalize($match[1]),
-                        'alias' => $this->versionParser->normalize($match[2]),
+                        'alias' => $match[2],
+                        'alias_normalized' => $this->versionParser->normalize($match[2]),
                     );
                     );
                 }
                 }
             }
             }

+ 9 - 1
src/Composer/Package/Locker.php

@@ -14,6 +14,7 @@ namespace Composer\Package;
 
 
 use Composer\Json\JsonFile;
 use Composer\Json\JsonFile;
 use Composer\Repository\RepositoryManager;
 use Composer\Repository\RepositoryManager;
+use Composer\Package\AliasPackage;
 
 
 /**
 /**
  * Reads/writes project lockfile (composer.lock).
  * Reads/writes project lockfile (composer.lock).
@@ -72,10 +73,14 @@ class Locker
         $lockList = $this->getLockData();
         $lockList = $this->getLockData();
         $packages = array();
         $packages = array();
         foreach ($lockList['packages'] as $info) {
         foreach ($lockList['packages'] as $info) {
-            $package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $info['version']);
+            $resolvedVersion = !empty($info['alias']) ? $info['alias'] : $info['version'];
+            $package = $this->repositoryManager->getLocalRepository()->findPackage($info['package'], $resolvedVersion);
 
 
             if (!$package) {
             if (!$package) {
                 $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
                 $package = $this->repositoryManager->findPackage($info['package'], $info['version']);
+                if ($package && !empty($info['alias'])) {
+                    $package = new AliasPackage($package, $info['alias'], $info['alias']);
+                }
             }
             }
 
 
             if (!$package) {
             if (!$package) {
@@ -134,6 +139,9 @@ class Locker
             if ($package->isDev()) {
             if ($package->isDev()) {
                 $spec['source-reference'] = $package->getSourceReference();
                 $spec['source-reference'] = $package->getSourceReference();
             }
             }
+            if ($package->getAlias()) {
+                $spec['alias'] = $package->getAlias();
+            }
 
 
             $lock['packages'][] = $spec;
             $lock['packages'][] = $spec;
         }
         }

+ 34 - 0
src/Composer/Package/MemoryPackage.php

@@ -42,6 +42,8 @@ class MemoryPackage extends BasePackage
     protected $binaries = array();
     protected $binaries = array();
     protected $scripts = array();
     protected $scripts = array();
     protected $aliases = array();
     protected $aliases = array();
+    protected $alias;
+    protected $prettyAlias;
     protected $dev;
     protected $dev;
 
 
     protected $requires = array();
     protected $requires = array();
@@ -173,6 +175,38 @@ class MemoryPackage extends BasePackage
         return $this->aliases;
         return $this->aliases;
     }
     }
 
 
+    /**
+     * @param string $alias
+     */
+    public function setAlias($alias)
+    {
+        $this->alias = $alias;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getAlias()
+    {
+        return $this->alias;
+    }
+
+    /**
+     * @param string $prettyAlias
+     */
+    public function setPrettyAlias($prettyAlias)
+    {
+        $this->prettyAlias = $prettyAlias;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getPrettyAlias()
+    {
+        return $this->prettyAlias;
+    }
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */

+ 14 - 0
src/Composer/Package/PackageInterface.php

@@ -316,6 +316,20 @@ interface PackageInterface
      */
      */
     function getAuthors();
     function getAuthors();
 
 
+    /**
+     * Returns a version this package should be aliased to
+     *
+     * @return string
+     */
+    function getAlias();
+
+    /**
+     * Returns a non-normalized version this package should be aliased to
+     *
+     * @return string
+     */
+    function getPrettyAlias();
+
     /**
     /**
      * Returns package unique name, constructed from name and version.
      * Returns package unique name, constructed from name and version.
      *
      *

+ 6 - 0
src/Composer/Repository/ArrayRepository.php

@@ -12,6 +12,7 @@
 
 
 namespace Composer\Repository;
 namespace Composer\Repository;
 
 
+use Composer\Package\AliasPackage;
 use Composer\Package\PackageInterface;
 use Composer\Package\PackageInterface;
 use Composer\Package\Version\VersionParser;
 use Composer\Package\Version\VersionParser;
 
 
@@ -96,6 +97,11 @@ class ArrayRepository implements RepositoryInterface
         $this->packages[] = $package;
         $this->packages[] = $package;
     }
     }
 
 
+    protected function createAliasPackage(PackageInterface $package)
+    {
+        return new AliasPackage($package, $package->getAlias(), $package->getPrettyAlias());
+    }
+
     /**
     /**
      * Removes package from repository.
      * Removes package from repository.
      *
      *

+ 6 - 1
src/Composer/Repository/FilesystemRepository.php

@@ -55,7 +55,12 @@ class FilesystemRepository extends ArrayRepository implements WritableRepository
 
 
         $loader = new ArrayLoader();
         $loader = new ArrayLoader();
         foreach ($packages as $package) {
         foreach ($packages as $package) {
-            $this->addPackage($loader->load($package));
+            $package = $loader->load($package);
+            if ($package->getAlias()) {
+                $this->addPackage($this->createAliasPackage($package));
+            } else {
+                $this->addPackage($package);
+            }
         }
         }
     }
     }
 
 

+ 38 - 8
src/Composer/Repository/VcsRepository.php

@@ -4,6 +4,8 @@ namespace Composer\Repository;
 
 
 use Composer\Repository\Vcs\VcsDriverInterface;
 use Composer\Repository\Vcs\VcsDriverInterface;
 use Composer\Package\Version\VersionParser;
 use Composer\Package\Version\VersionParser;
+use Composer\Package\PackageInterface;
+use Composer\Package\AliasPackage;
 use Composer\Package\Loader\ArrayLoader;
 use Composer\Package\Loader\ArrayLoader;
 use Composer\IO\IOInterface;
 use Composer\IO\IOInterface;
 
 
@@ -16,6 +18,7 @@ class VcsRepository extends ArrayRepository
     protected $packageName;
     protected $packageName;
     protected $debug;
     protected $debug;
     protected $io;
     protected $io;
+    protected $versionParser;
 
 
     public function __construct(array $config, IOInterface $io, array $drivers = null)
     public function __construct(array $config, IOInterface $io, array $drivers = null)
     {
     {
@@ -67,7 +70,7 @@ class VcsRepository extends ArrayRepository
             throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
             throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
         }
         }
 
 
-        $versionParser = new VersionParser;
+        $this->versionParser = new VersionParser;
         $loader = new ArrayLoader();
         $loader = new ArrayLoader();
 
 
         if ($driver->hasComposerFile($driver->getRootIdentifier())) {
         if ($driver->hasComposerFile($driver->getRootIdentifier())) {
@@ -83,7 +86,7 @@ class VcsRepository extends ArrayRepository
                 $this->io->overwrite($msg, false);
                 $this->io->overwrite($msg, false);
             }
             }
 
 
-            $parsedTag = $this->validateTag($versionParser, $tag);
+            $parsedTag = $this->validateTag($tag);
             if ($parsedTag && $driver->hasComposerFile($identifier)) {
             if ($parsedTag && $driver->hasComposerFile($identifier)) {
                 try {
                 try {
                     $data = $driver->getComposerInformation($identifier);
                     $data = $driver->getComposerInformation($identifier);
@@ -96,7 +99,7 @@ class VcsRepository extends ArrayRepository
 
 
                 // manually versioned package
                 // manually versioned package
                 if (isset($data['version'])) {
                 if (isset($data['version'])) {
-                    $data['version_normalized'] = $versionParser->normalize($data['version']);
+                    $data['version_normalized'] = $this->versionParser->normalize($data['version']);
                 } else {
                 } else {
                     // auto-versionned package, read value from tag
                     // auto-versionned package, read value from tag
                     $data['version'] = $tag;
                     $data['version'] = $tag;
@@ -135,7 +138,7 @@ class VcsRepository extends ArrayRepository
                 $this->io->overwrite($msg, false);
                 $this->io->overwrite($msg, false);
             }
             }
 
 
-            $parsedBranch = $this->validateBranch($versionParser, $branch);
+            $parsedBranch = $this->validateBranch($branch);
             if ($driver->hasComposerFile($identifier)) {
             if ($driver->hasComposerFile($identifier)) {
                 $data = $driver->getComposerInformation($identifier);
                 $data = $driver->getComposerInformation($identifier);
 
 
@@ -182,23 +185,50 @@ class VcsRepository extends ArrayRepository
             $data['source'] = $driver->getSource($identifier);
             $data['source'] = $driver->getSource($identifier);
         }
         }
 
 
+        // check for a branch alias (dev-master => 1.0.x-dev for example) if this is a named branch
+        if ('dev-' === substr($data['version'], 0, 4) && isset($data['extra']['branch-alias']) && is_array($data['extra']['branch-alias'])) {
+            foreach ($data['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
+                // ensure it is an alias to a numeric branch that is parseable
+                if (!($validatedTargetBranch = $this->validateBranch($targetBranch)) || '-dev' !== substr($validatedTargetBranch, -4)) {
+                    continue;
+                }
+                // ensure that it is the current branch aliasing itself
+                if ($data['version'] !== $sourceBranch && substr($data['version'], 4) !== $sourceBranch) {
+                    continue;
+                }
+
+                $data['alias'] = $targetBranch.'-dev';
+                $data['alias_normalized'] = $validatedTargetBranch;
+                break;
+            }
+        }
+
         return $data;
         return $data;
     }
     }
 
 
-    private function validateBranch($versionParser, $branch)
+    public function addPackage(PackageInterface $package)
+    {
+        parent::addPackage($package);
+
+        if ($package->getAlias()) {
+            $this->addPackage($this->createAliasPackage($package));
+        }
+    }
+
+    private function validateBranch($branch)
     {
     {
         try {
         try {
-            return $versionParser->normalizeBranch($branch);
+            return $this->versionParser->normalizeBranch($branch);
         } catch (\Exception $e) {
         } catch (\Exception $e) {
         }
         }
 
 
         return false;
         return false;
     }
     }
 
 
-    private function validateTag($versionParser, $version)
+    private function validateTag($version)
     {
     {
         try {
         try {
-            return $versionParser->normalize($version);
+            return $this->versionParser->normalize($version);
         } catch (\Exception $e) {
         } catch (\Exception $e) {
         }
         }