diff --git a/CHANGELOG.md b/CHANGELOG.md index df976943e..65079f34f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,6 +150,11 @@ * Fixed suggest output being very spammy, it now is only one line long and shows more rarely * Fixed conflict rules like e.g. >=5 from matching dev-master, as it is not normalized to 9999999-dev internally anymore +### [1.10.20] 2021-01-27 + + * Fixed exclude-from-classmap causing regex issues when having too many paths + * Fixed compatibility issue with Symfony 4/5 + ### [1.10.19] 2020-12-04 * Fixed regression on PHP 8.0 @@ -1090,6 +1095,7 @@ [2.0.0-alpha3]: https://github.com/composer/composer/compare/2.0.0-alpha2...2.0.0-alpha3 [2.0.0-alpha2]: https://github.com/composer/composer/compare/2.0.0-alpha1...2.0.0-alpha2 [2.0.0-alpha1]: https://github.com/composer/composer/compare/1.10.7...2.0.0-alpha1 +[1.10.20]: https://github.com/composer/composer/compare/1.10.19...1.10.20 [1.10.19]: https://github.com/composer/composer/compare/1.10.18...1.10.19 [1.10.18]: https://github.com/composer/composer/compare/1.10.17...1.10.18 [1.10.17]: https://github.com/composer/composer/compare/1.10.16...1.10.17 diff --git a/doc/05-repositories.md b/doc/05-repositories.md index af0267368..7624c054e 100644 --- a/doc/05-repositories.md +++ b/doc/05-repositories.md @@ -576,14 +576,23 @@ the branch or tag that is currently checked out. Otherwise, the version should be explicitly defined in the package's `composer.json` file. If the version cannot be resolved by these means, it is assumed to be `dev-master`. -When the version cannot be inferred from the local VCS repository, you should use -the special `branch-version` entry under `extra` instead of `version`: +When the version cannot be inferred from the local VCS repository, or when you +want to override the version, you can use the `versions` option when declaring +the repository: ```json { - "extra": { - "branch-version": "4.2-dev" - } + "repositories": [ + { + "type": "path", + "url": "../../packages/my-package", + "options": { + "versions": { + "my/package": "4.2-dev" + } + } + } + ] } ``` diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 21dc88df0..fa5dd9cf2 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -275,7 +275,7 @@ EOF; $excluded = null; if (!empty($autoloads['exclude-from-classmap'])) { - $excluded = '{(' . implode('|', $autoloads['exclude-from-classmap']) . ')}'; + $excluded = $autoloads['exclude-from-classmap']; } $classMap = array(); @@ -398,8 +398,31 @@ EOF; return $classMap; } + /** + * @param ?array $excluded + */ private function generateClassMap($dir, $excluded, $namespaceFilter, $autoloadType, $showAmbiguousWarning, array &$scannedFiles) { + if ($excluded) { + // filter excluded patterns here to only use those matching $dir + // exclude-from-classmap patterns are all realpath'd so we can only filter them if $dir exists so that realpath($dir) will work + // if $dir does not exist, it should anyway not find anything there so no trouble + if (file_exists($dir)) { + // transform $dir in the same way that exclude-from-classmap patterns are transformed so we can match them against each other + $dirMatch = preg_quote(strtr(realpath($dir), '\\', '/')); + foreach ($excluded as $index => $pattern) { + // extract the constant string prefix of the pattern here, until we reach a non-escaped regex special character + $pattern = preg_replace('{^(([^.+*?\[^\]$(){}=!<>|:\\\\#-]+|\\\\[.+*?\[^\]$(){}=!<>|:#-])*).*}', '$1', $pattern); + // if the pattern is not a subset or superset of $dir, it is unrelated and we skip it + if (0 !== strpos($pattern, $dirMatch) && 0 !== strpos($dirMatch, $pattern)) { + unset($excluded[$index]); + } + } + } + + $excluded = $excluded ? '{(' . implode('|', $excluded) . ')}' : null; + } + return ClassMapGenerator::createMap($dir, $excluded, $showAmbiguousWarning ? $this->io : null, $namespaceFilter, $autoloadType, $scannedFiles); } @@ -513,7 +536,7 @@ EOF; if (isset($autoloads['classmap'])) { $excluded = null; if (!empty($autoloads['exclude-from-classmap'])) { - $excluded = '{(' . implode('|', $autoloads['exclude-from-classmap']) . ')}'; + $excluded = $autoloads['exclude-from-classmap']; } $scannedFiles = array(); diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index 60044e19c..92295f021 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -51,7 +51,7 @@ class ClassMapGenerator * Iterate over all files in the given directory searching for classes * * @param \Iterator|string $path The path to search in or an iterator - * @param string $excluded Regex that matches against the file path that exclude from the classmap. + * @param string $excluded Regex that matches file paths to be excluded from the classmap * @param IOInterface $io IO object * @param string $namespace Optional namespace prefix to filter by * @param string $autoloadType psr-0|psr-4 Optional autoload standard to use mapping rules diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index e807cfd45..843a94e69 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -74,10 +74,8 @@ class RootPackageLoader extends ArrayLoader if (!isset($config['version'])) { $commit = null; - if (isset($config['extra']['branch-version'])) { - $config['version'] = preg_replace('{(\.x)?(-dev)?$}', '', $config['extra']['branch-version']).'.x-dev'; - } elseif (getenv('COMPOSER_ROOT_VERSION')) { - // override with env var if available + // override with env var if available + if (getenv('COMPOSER_ROOT_VERSION')) { $config['version'] = getenv('COMPOSER_ROOT_VERSION'); } else { $versionData = $this->versionGuesser->guessVersion($config, $cwd ?: getcwd()); diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index c60f9ca9d..c1fc632da 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -170,10 +170,11 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn 'reference' => sha1($json . serialize($this->options)), ); $package['transport-options'] = $this->options; + unset($package['transport-options']['versions']); - // use the branch-version as the package version if available - if (!isset($package['version']) && isset($package['extra']['branch-version'])) { - $package['version'] = preg_replace('{(\.x)?(-dev)?$}', '', $package['extra']['branch-version']).'.x-dev'; + // use the version provided as option if available + if (isset($package['name'], $this->options['versions'][$package['name']])) { + $package['version'] = $this->options['versions'][$package['name']]; } // carry over the root package version if this path repo is in the same git repository as root package diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 0b8334464..134ebd6b8 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -1447,9 +1447,9 @@ EOF; $package->setAutoload(array( 'psr-0' => array('Foo' => '../path/../src'), 'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'), - 'classmap' => array('../classmap'), + 'classmap' => array('../classmap', '../classmap2/subdir', 'classmap3', 'classmap4'), 'files' => array('../test.php'), - 'exclude-from-classmap' => array('./../classmap/excluded'), + 'exclude-from-classmap' => array('./../classmap/excluded', '../classmap2', 'classmap3/classes.php', 'classmap4/*/classes.php'), )); $this->repository->expects($this->once()) @@ -1458,9 +1458,15 @@ EOF; $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo'); $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded'); + $this->fs->ensureDirectoryExists($this->workingDir.'/classmap2/subdir'); + $this->fs->ensureDirectoryExists($this->workingDir.'/working-dir/classmap3'); + $this->fs->ensureDirectoryExists($this->workingDir.'/working-dir/classmap4/foo/'); file_put_contents($this->workingDir.'/src/Foo/Bar.php', 'workingDir.'/classmap/classes.php', 'workingDir.'/classmap/excluded/classes.php', 'workingDir.'/classmap2/subdir/classes.php', 'workingDir.'/working-dir/classmap3/classes.php', 'workingDir.'/working-dir/classmap4/foo/classes.php', 'workingDir.'/test.php', 'generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14'); diff --git a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php index ade220950..880b6f766 100644 --- a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php @@ -202,28 +202,4 @@ class RootPackageLoaderTest extends TestCase $this->assertEquals("dev-latest-production", $package->getPrettyVersion()); } - - /** - * @dataProvider provideExtraBranchVersion - */ - public function testLoadExtraBranchVersion($branchVersion) - { - $package = $this->loadPackage(array( - 'extra' => array( - 'branch-version' => $branchVersion, - ), - )); - - $this->assertEquals('1.2.x-dev', $package->getPrettyVersion()); - } - - public function provideExtraBranchVersion() - { - return array( - array('1.2'), - array('1.2.x'), - array('1.2-dev'), - array('1.2.x-dev'), - ); - } } diff --git a/tests/Composer/Test/Repository/Fixtures/path/with-branch-version/composer.json b/tests/Composer/Test/Repository/Fixtures/path/with-branch-version/composer.json deleted file mode 100644 index 75545d5f9..000000000 --- a/tests/Composer/Test/Repository/Fixtures/path/with-branch-version/composer.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "test/path-branch-versioned", - "extra": { - "branch-version": "1.2" - } -} diff --git a/tests/Composer/Test/Repository/PathRepositoryTest.php b/tests/Composer/Test/Repository/PathRepositoryTest.php index 1d654c02f..326434d26 100644 --- a/tests/Composer/Test/Repository/PathRepositoryTest.php +++ b/tests/Composer/Test/Repository/PathRepositoryTest.php @@ -67,23 +67,6 @@ class PathRepositoryTest extends TestCase $this->assertNotEmpty($packageVersion); } - public function testLoadPackageFromFileSystemWithExtraBranchVersion() - { - $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface') - ->getMock(); - - $config = new \Composer\Config(); - $versionGuesser = null; - - $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', 'with-branch-version')); - $repository = new PathRepository(array('url' => $repositoryUrl), $ioInterface, $config); - $packages = $repository->getPackages(); - - $this->assertEquals(1, $repository->count()); - - $this->assertTrue($repository->hasPackage($this->getPackage('test/path-branch-versioned', '1.2.x-dev'))); - } - public function testLoadPackageFromFileSystemWithWildcard() { $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface') @@ -95,16 +78,50 @@ class PathRepositoryTest extends TestCase $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', '*')); $repository = new PathRepository(array('url' => $repositoryUrl), $ioInterface, $config); $packages = $repository->getPackages(); - $result = array(); + $names = array(); - $this->assertGreaterThanOrEqual(3, $repository->count()); + $this->assertEquals(2, $repository->count()); - foreach ($packages as $package) { - $result[$package->getName()] = $package->getPrettyVersion(); - } + $package = $packages[0]; + $names[] = $package->getName(); - ksort($result); - $this->assertSame(array('test/path-branch-versioned' => '1.2.x-dev', 'test/path-unversioned' => $result['test/path-unversioned'], 'test/path-versioned' => '0.0.2'), $result); + $package = $packages[1]; + $names[] = $package->getName(); + + sort($names); + $this->assertEquals(array('test/path-unversioned', 'test/path-versioned'), $names); + } + + public function testLoadPackageWithExplicitVersions() + { + $ioInterface = $this->getMockBuilder('Composer\IO\IOInterface') + ->getMock(); + + $config = new \Composer\Config(); + $versionGuesser = null; + + $options = array( + 'versions' => array( + 'test/path-unversioned' => '4.3.2.1', + 'test/path-versioned' => '3.2.1.0', + ), + ); + $repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', '*')); + $repository = new PathRepository(array('url' => $repositoryUrl, 'options' => $options), $ioInterface, $config); + $packages = $repository->getPackages(); + + $versions = array(); + + $this->assertEquals(2, $repository->count()); + + $package = $packages[0]; + $versions[$package->getName()] = $package->getVersion(); + + $package = $packages[1]; + $versions[$package->getName()] = $package->getVersion(); + + ksort($versions); + $this->assertSame(array('test/path-unversioned' => '4.3.2.1', 'test/path-versioned' => '3.2.1.0'), $versions); } /**