From 472a62152d3386dce33df3935d647cb6e508bc81 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 16:09:38 +0200 Subject: [PATCH 1/6] 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'].')'); } From 93d4cf6f918a6743dfd60e764de633215a3d34fa Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 16:37:06 +0200 Subject: [PATCH 2/6] Add --no-show-signature where git supports it, fixes #8966 --- src/Composer/Downloader/GitDownloader.php | 4 +-- src/Composer/Package/Locker.php | 2 +- .../Package/Version/VersionGuesser.php | 4 +-- src/Composer/Repository/PathRepository.php | 3 +- src/Composer/Util/Git.php | 32 +++++++++++++------ .../Package/Version/VersionGuesserTest.php | 14 +++++++- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index edeaa7686..54e023354 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -48,7 +48,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface $flag = Platform::isWindows() ? '/D ' : ''; // --dissociate option is only available since git 2.3.0-rc0 - $gitVersion = $this->gitUtil->getVersion(); + $gitVersion = GitUtil::getVersion($this->process); $msg = "Cloning ".$this->getShortHash($ref); $command = 'git clone --no-checkout %url% %path% && cd '.$flag.'%path% && git remote add composer %url% && git fetch composer && git remote set-url origin %sanitizedUrl% && git remote set-url composer %sanitizedUrl%'; @@ -435,7 +435,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface protected function getCommitLogs($fromReference, $toReference, $path) { $path = $this->normalizePath($path); - $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', ProcessExecutor::escape($fromReference), ProcessExecutor::escape($toReference)); + $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"'.GitUtil::getNoShowSignatureFlag($this->process), ProcessExecutor::escape($fromReference), ProcessExecutor::escape($toReference)); if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); diff --git a/src/Composer/Package/Locker.php b/src/Composer/Package/Locker.php index 8a500d837..1adc45dc6 100644 --- a/src/Composer/Package/Locker.php +++ b/src/Composer/Package/Locker.php @@ -422,7 +422,7 @@ class Locker case 'git': GitUtil::cleanEnv(); - if (0 === $this->process->execute('git log -n1 --pretty=%ct '.ProcessExecutor::escape($sourceRef), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) { + if (0 === $this->process->execute('git log -n1 --pretty=%ct '.ProcessExecutor::escape($sourceRef).GitUtil::getNoShowSignatureFlag($this->process), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) { $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC')); } break; diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index 721887787..fb1ee712b 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -86,7 +86,7 @@ class VersionGuesser if (null !== $versionData && null !== $versionData['version']) { return $this->postprocess($versionData); } - + return null; } @@ -169,7 +169,7 @@ class VersionGuesser } if (!$commit) { - $command = 'git log --pretty="%H" -n1 HEAD'; + $command = 'git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($this->process); if (0 === $this->process->execute($command, $output, $path)) { $commit = trim($output) ?: null; } diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 4e860658f..a0a6835b8 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -21,6 +21,7 @@ use Composer\Package\Version\VersionParser; use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\Filesystem; +use Composer\Util\Git as GitUtil; /** * This repository allows installing local packages that are not necessarily under their own VCS. @@ -176,7 +177,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn } $output = ''; - if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) { + if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H'.GitUtil::getNoShowSignatureFlag($this->process), $output, $path)) { $package['dist']['reference'] = trim($output); } diff --git a/src/Composer/Util/Git.php b/src/Composer/Util/Git.php index 70f62bde2..cdb559712 100644 --- a/src/Composer/Util/Git.php +++ b/src/Composer/Util/Git.php @@ -20,7 +20,7 @@ use Composer\IO\IOInterface; */ class Git { - private static $version; + private static $version = false; /** @var IOInterface */ protected $io; @@ -291,6 +291,16 @@ class Git return false; } + public static function getNoShowSignatureFlag(ProcessExecutor $process) + { + $gitVersion = self::getVersion($process); + if ($gitVersion && version_compare($gitVersion, '2.10.0-rc0', '>=')) { + return ' --no-show-signature'; + } + + return ''; + } + private function checkRefIsInMirror($url, $dir, $ref) { if (is_dir($dir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $dir) && trim($output) === '.') { @@ -398,16 +408,18 @@ class Git * * @return string|null The git version number. */ - public function getVersion() + public static function getVersion(ProcessExecutor $process = null) { - if (isset(self::$version)) { - return self::$version; - } - if (0 !== $this->process->execute('git --version', $output)) { - return; - } - if (preg_match('/^git version (\d+(?:\.\d+)+)/m', $output, $matches)) { - return self::$version = $matches[1]; + if (false === self::$version) { + self::$version = null; + if (!$process) { + $process = new ProcessExecutor; + } + if (0 === $process->execute('git --version', $output) && preg_match('/^git version (\d+(?:\.\d+)+)/m', $output, $matches)) { + self::$version = $matches[1]; + } } + + return self::$version; } } diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index 31c47d72b..1ccea20ee 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -16,6 +16,7 @@ use Composer\Config; use Composer\Package\Version\VersionGuesser; use Composer\Semver\VersionParser; use Composer\Test\TestCase; +use Composer\Util\Git as GitUtil; class VersionGuesserTest extends TestCase { @@ -66,7 +67,18 @@ class VersionGuesserTest extends TestCase ->expects($this->at($step)) ->method('execute') ->willReturnCallback(function ($command, &$output) use ($self) { - $self->assertEquals('git log --pretty="%H" -n1 HEAD', $command); + $self->assertEquals('git --version', $command); + + return 0; + }) + ; + + ++$step; + $executor + ->expects($this->at($step)) + ->method('execute') + ->willReturnCallback(function ($command, &$output) use ($self, $executor) { + $self->assertEquals('git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($executor), $command); return 128; }) From 8c0ecf733768ad651d4e50c1f58af51521971d6c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 16:41:33 +0200 Subject: [PATCH 3/6] Clarify why a dev tag was ignored, fixes #8951 --- src/Composer/Repository/VcsRepository.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index dd3aa83d1..be8ed7782 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -219,7 +219,11 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt // broken package, version doesn't match tag if ($data['version_normalized'] !== $parsedTag) { if ($isVeryVerbose) { - $this->io->writeError('Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json'); + if (preg_match('{(^dev-|[.-]?dev$)}i', $parsedTag)) { + $this->io->writeError('Skipped tag '.$tag.', invalid tag name, tags can not use dev prefixes or suffixes'); + } else { + $this->io->writeError('Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json'); + } } continue; } From fb7fc4a4ca47211651a9370c17cdaaf753cd825c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 16:59:43 +0200 Subject: [PATCH 4/6] Fix git tests --- .../Test/Downloader/GitDownloaderTest.php | 97 ++++++------------- .../Package/Version/VersionGuesserTest.php | 16 +-- 2 files changed, 36 insertions(+), 77 deletions(-) diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 393ecfc5f..5bf75d4f6 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -17,6 +17,8 @@ use Composer\Config; use Composer\Test\TestCase; use Composer\Util\Filesystem; use Composer\Util\Platform; +use Composer\Util\ProcessExecutor; +use Composer\Util\Git as GitUtil; class GitDownloaderTest extends TestCase { @@ -29,6 +31,8 @@ class GitDownloaderTest extends TestCase { $this->skipIfNotExecutable('git'); + $this->initGitVersion('1.0.0'); + $this->fs = new Filesystem; $this->workingDir = $this->getUniqueTmpDirectory(); } @@ -39,10 +43,15 @@ class GitDownloaderTest extends TestCase $this->fs->removeDirectory($this->workingDir); } + $this->initGitVersion(false); + } + + private function initGitVersion($version) + { // reset the static version cache $refl = new \ReflectionProperty('Composer\Util\Git', 'version'); $refl->setAccessible(true); - $refl->setValue(null, null); + $refl->setValue(null, $version); } protected function setupConfig($config = null) @@ -99,32 +108,23 @@ class GitDownloaderTest extends TestCase ->will($this->returnValue('dev-master')); $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); - $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($this->winCompat('git --version'))) - ->will($this->returnCallback(function ($command, &$output = null) { - $output = 'git version 1.0.0'; - - return 0; - })); - $expectedGitCommand = $this->winCompat("git clone --no-checkout 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://example.com/composer/composer' && git fetch composer && git remote set-url origin 'https://example.com/composer/composer' && git remote set-url composer 'https://example.com/composer/composer'"); - $processExecutor->expects($this->at(1)) + $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(2)) + $processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(3)) + $processExecutor->expects($this->at(2)) ->method('execute') ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(4)) + $processExecutor->expects($this->at(3)) ->method('execute') ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); @@ -150,14 +150,7 @@ class GitDownloaderTest extends TestCase ->will($this->returnValue('dev-master')); $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); - $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($this->winCompat('git --version'))) - ->will($this->returnCallback(function ($command, &$output = null) { - $output = 'git version 2.3.1'; - - return 0; - })); + $this->initGitVersion('2.17.0'); $config = new Config; $this->setupConfig($config); @@ -167,7 +160,7 @@ class GitDownloaderTest extends TestCase $filesystem->removeDirectory($cachePath); $expectedGitCommand = $this->winCompat(sprintf("git clone --mirror 'https://example.com/composer/composer' '%s'", $cachePath)); - $processExecutor->expects($this->at(1)) + $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnCallback(function () use ($cachePath) { @@ -175,7 +168,7 @@ class GitDownloaderTest extends TestCase return 0; })); - $processExecutor->expects($this->at(2)) + $processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo('git rev-parse --git-dir'), $this->anything(), $this->equalTo($this->winCompat($cachePath))) ->will($this->returnCallback(function ($command, &$output = null) { @@ -183,28 +176,28 @@ class GitDownloaderTest extends TestCase return 0; })); - $processExecutor->expects($this->at(3)) + $processExecutor->expects($this->at(2)) ->method('execute') ->with($this->equalTo($this->winCompat('git rev-parse --quiet --verify \'1234567890123456789012345678901234567890^{commit}\'')), $this->equalTo(null), $this->equalTo($this->winCompat($cachePath))) ->will($this->returnValue(0)); $expectedGitCommand = $this->winCompat(sprintf("git clone --no-checkout '%1\$s' 'composerPath' --dissociate --reference '%1\$s' && cd 'composerPath' && git remote set-url origin 'https://example.com/composer/composer' && git remote add composer 'https://example.com/composer/composer'", $cachePath)); - $processExecutor->expects($this->at(4)) + $processExecutor->expects($this->at(3)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(5)) + $processExecutor->expects($this->at(4)) ->method('execute') ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(6)) + $processExecutor->expects($this->at(5)) ->method('execute') ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(7)) + $processExecutor->expects($this->at(6)) ->method('execute') ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); @@ -231,50 +224,41 @@ class GitDownloaderTest extends TestCase ->will($this->returnValue('1.0.0')); $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); - $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($this->winCompat('git --version'))) - ->will($this->returnCallback(function ($command, &$output = null) { - $output = 'git version 1.0.0'; - - return 0; - })); - $expectedGitCommand = $this->winCompat("git clone --no-checkout 'https://github.com/mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://github.com/mirrors/composer' && git fetch composer && git remote set-url origin 'https://github.com/mirrors/composer' && git remote set-url composer 'https://github.com/mirrors/composer'"); - $processExecutor->expects($this->at(1)) + $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(1)); - $processExecutor->expects($this->at(2)) + $processExecutor->expects($this->at(1)) ->method('getErrorOutput') ->with() ->will($this->returnValue('Error1')); $expectedGitCommand = $this->winCompat("git clone --no-checkout 'git@github.com:mirrors/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'git@github.com:mirrors/composer' && git fetch composer && git remote set-url origin 'git@github.com:mirrors/composer' && git remote set-url composer 'git@github.com:mirrors/composer'"); - $processExecutor->expects($this->at(3)) + $processExecutor->expects($this->at(2)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'"); - $processExecutor->expects($this->at(4)) + $processExecutor->expects($this->at(3)) ->method('execute') ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'"); - $processExecutor->expects($this->at(5)) + $processExecutor->expects($this->at(4)) ->method('execute') ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(6)) + $processExecutor->expects($this->at(5)) ->method('execute') ->with($this->equalTo('git branch -r')) ->will($this->returnValue(0)); - $processExecutor->expects($this->at(7)) + $processExecutor->expects($this->at(6)) ->method('execute') ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); @@ -315,28 +299,19 @@ class GitDownloaderTest extends TestCase ->will($this->returnValue('1.0.0')); $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); - $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($this->winCompat('git --version'))) - ->will($this->returnCallback(function ($command, &$output = null) { - $output = 'git version 1.0.0'; - - return 0; - })); - $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer && git remote set-url origin '{$url}' && git remote set-url composer '{$url}'"); - $processExecutor->expects($this->at(1)) + $processExecutor->expects($this->at(0)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'"); - $processExecutor->expects($this->at(2)) + $processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath'))) ->will($this->returnValue(0)); - $processExecutor->expects($this->exactly(5)) + $processExecutor->expects($this->exactly(4)) ->method('execute') ->will($this->returnValue(0)); @@ -362,14 +337,6 @@ class GitDownloaderTest extends TestCase ->will($this->returnValue(array('https://example.com/composer/composer'))); $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); $processExecutor->expects($this->at(0)) - ->method('execute') - ->with($this->equalTo($this->winCompat('git --version'))) - ->will($this->returnCallback(function ($command, &$output = null) { - $output = 'git version 1.0.0'; - - return 0; - })); - $processExecutor->expects($this->at(1)) ->method('execute') ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(1)); diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index 1ccea20ee..5d8d4f5eb 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -17,6 +17,7 @@ use Composer\Package\Version\VersionGuesser; use Composer\Semver\VersionParser; use Composer\Test\TestCase; use Composer\Util\Git as GitUtil; +use Composer\Util\ProcessExecutor; class VersionGuesserTest extends TestCase { @@ -31,7 +32,7 @@ class VersionGuesserTest extends TestCase { $branch = 'default'; - $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') + $executor = $this->getMockBuilder('Composer\\Util\\ProcessExecutor') ->setMethods(array('execute')) ->disableArgumentCloning() ->disableOriginalConstructor() @@ -41,6 +42,8 @@ class VersionGuesserTest extends TestCase $self = $this; $step = 0; + GitUtil::getVersion(new ProcessExecutor); + $executor ->expects($this->at($step)) ->method('execute') @@ -62,17 +65,6 @@ class VersionGuesserTest extends TestCase }) ; - ++$step; - $executor - ->expects($this->at($step)) - ->method('execute') - ->willReturnCallback(function ($command, &$output) use ($self) { - $self->assertEquals('git --version', $command); - - return 0; - }) - ; - ++$step; $executor ->expects($this->at($step)) From 0278e7453dc6026b20788b3ee28c0a754d59452b Mon Sep 17 00:00:00 2001 From: Michael Stucki Date: Mon, 15 Jun 2020 21:42:41 +0200 Subject: [PATCH 5/6] Clean Git repos during discard --- 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 54e023354..ad8503cea 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -451,7 +451,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface protected function discardChanges($path) { $path = $this->normalizePath($path); - if (0 !== $this->process->execute('git reset --hard', $output, $path)) { + if (0 !== $this->process->execute('git clean -df && git reset --hard', $output, $path)) { throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput()); } From cb1f3899bbef0a0686b03a9702abaf980cff2d15 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 17 Jun 2020 17:32:26 +0200 Subject: [PATCH 6/6] Revert "Store default branch info inside metadata" This reverts commit 472a62152d3386dce33df3935d647cb6e508bc81. --- 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, 1 insertion(+), 57 deletions(-) diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index b0649ded0..ee93ec497 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -392,11 +392,6 @@ 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 ce8d0c215..dece598f1 100644 --- a/src/Composer/Package/Dumper/ArrayDumper.php +++ b/src/Composer/Package/Dumper/ArrayDumper.php @@ -92,10 +92,6 @@ 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 d24d34d36..228632b42 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/src/Composer/Package/Loader/ArrayLoader.php @@ -53,9 +53,6 @@ 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'); @@ -78,10 +75,6 @@ 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 7ce40a507..6c7b426e7 100644 --- a/src/Composer/Package/Package.php +++ b/src/Composer/Package/Package.php @@ -58,7 +58,6 @@ class Package extends BasePackage protected $devAutoload = array(); protected $includePaths = array(); protected $archiveExcludes = array(); - protected $isDefaultBranch = false; /** * Creates a new in memory package. @@ -570,22 +569,6 @@ 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 c09c9e7ba..cb16efa7e 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -352,11 +352,6 @@ 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 be8ed7782..f248420b0 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -149,10 +149,8 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt $this->loader = new ArrayLoader($this->versionParser); } - $hasRootIdentifierComposerJson = false; try { - $hasRootIdentifierComposerJson = $driver->hasComposerFile($driver->getRootIdentifier()); - if ($hasRootIdentifierComposerJson) { + if ($driver->hasComposerFile($driver->getRootIdentifier())) { $data = $driver->getComposerInformation($driver->getRootIdentifier()); $this->packageName = !empty($data['name']) ? $data['name'] : null; } @@ -213,9 +211,6 @@ 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) { @@ -260,11 +255,6 @@ 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) { @@ -294,9 +284,6 @@ 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) { @@ -322,11 +309,6 @@ 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'].')'); }