From a2dadb91bf87a0d5cccb98fdca1e4fd66459a896 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 14 Jan 2020 12:52:20 +0100 Subject: [PATCH] Return two packages in PathRepository when on a feature branch, one for feature branch and one for the source branch it came from, fixes #8498, fixes #8477 --- .../Package/Version/VersionGuesser.php | 22 ++++++++++++++++++- src/Composer/Repository/PathRepository.php | 15 +++++++++---- .../Package/Version/VersionGuesserTest.php | 14 +++++++++--- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index e6ff84965..f35aabeb2 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -59,7 +59,7 @@ class VersionGuesser * @param array $packageConfig * @param string $path Path to guess into * - * @return null|array versionData, 'version', 'pretty_version' and 'commit' keys + * @return null|array versionData, 'version', 'pretty_version' and 'commit' keys, if the version is a feature branch, 'feature_version' and 'feature_pretty_version' keys may also be returned */ public function guessVersion(array $packageConfig, $path) { @@ -88,10 +88,18 @@ class VersionGuesser private function postprocess(array $versionData) { + if (!empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['feature_pretty_version']) { + unset($versionData['feature_version'], $versionData['feature_pretty_version']); + } + if ('-dev' === substr($versionData['version'], -4) && preg_match('{\.9{7}}', $versionData['version'])) { $versionData['pretty_version'] = preg_replace('{(\.9{7})+}', '.x', $versionData['version']); } + if (!empty($versionData['feature_version']) && '-dev' === substr($versionData['feature_version'], -4) && preg_match('{\.9{7}}', $versionData['feature_version'])) { + $versionData['feature_pretty_version'] = preg_replace('{(\.9{7})+}', '.x', $versionData['feature_version']); + } + return $versionData; } @@ -101,6 +109,8 @@ class VersionGuesser $commit = null; $version = null; $prettyVersion = null; + $featureVersion = null; + $featurePrettyVersion = null; $isDetached = false; // try to fetch current version from git branch @@ -135,6 +145,8 @@ class VersionGuesser } if ($isFeatureBranch) { + $featureVersion = $version; + $featurePrettyVersion = $prettyVersion; // try to find the best (nearest) version branch to assume this feature's version $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path); $version = $result['version']; @@ -147,6 +159,8 @@ class VersionGuesser if ($result) { $version = $result['version']; $prettyVersion = $result['pretty_version']; + $featureVersion = null; + $featurePrettyVersion = null; } } @@ -157,6 +171,10 @@ class VersionGuesser } } + if ($featureVersion) { + return array('version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion, 'feature_version' => $featureVersion, 'feature_pretty_version' => $featurePrettyVersion); + } + return array('version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion); } @@ -198,6 +216,8 @@ class VersionGuesser // try to find the best (nearest) version branch to assume this feature's version $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path); $result['commit'] = ''; + $result['feature_version'] = $version; + $result['feature_pretty_version'] = $version; return $result; } diff --git a/src/Composer/Repository/PathRepository.php b/src/Composer/Repository/PathRepository.php index 6e218f159..4e6bb05b7 100644 --- a/src/Composer/Repository/PathRepository.php +++ b/src/Composer/Repository/PathRepository.php @@ -153,19 +153,26 @@ 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)) { + $package['dist']['reference'] = trim($output); + } + if (!isset($package['version'])) { $versionData = $this->versionGuesser->guessVersion($package, $path); if (is_array($versionData) && $versionData['pretty_version']) { + // if there is a feature branch detected, we add a second packages with the feature branch version + if (!empty($versionData['feature_pretty_version'])) { + $package['version'] = $versionData['feature_pretty_version']; + $this->addPackage($this->loader->load($package)); + } + $package['version'] = $versionData['pretty_version']; } else { $package['version'] = 'dev-master'; } } - $output = ''; - if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H', $output, $path)) { - $package['dist']['reference'] = trim($output); - } $package = $this->loader->load($package); $this->addPackage($package); } diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index fe229d679..6eeafd122 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -126,13 +126,15 @@ class VersionGuesserTest extends TestCase $this->assertEquals("9999999-dev", $versionArray['version']); $this->assertEquals("dev-master", $versionArray['pretty_version']); + $this->assertArrayNotHasKey('feature_version', $versionArray); + $this->assertArrayNotHasKey('feature_pretty_version', $versionArray); $this->assertEquals($commitHash, $versionArray['commit']); } public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNaming() { $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; - $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; + $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') ->setMethods(array('execute')) @@ -172,12 +174,14 @@ class VersionGuesserTest extends TestCase $this->assertEquals("dev-arbitrary", $versionArray['version']); $this->assertEquals($anotherCommitHash, $versionArray['commit']); + $this->assertEquals("dev-current", $versionArray['feature_version']); + $this->assertEquals("dev-current", $versionArray['feature_pretty_version']); } public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingRegex() { $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; - $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; + $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') ->setMethods(array('execute')) @@ -217,12 +221,14 @@ class VersionGuesserTest extends TestCase $this->assertEquals("dev-latest-testing", $versionArray['version']); $this->assertEquals($anotherCommitHash, $versionArray['commit']); + $this->assertEquals("dev-current", $versionArray['feature_version']); + $this->assertEquals("dev-current", $versionArray['feature_pretty_version']); } public function testGuessVersionReadsAndRespectsNonFeatureBranchesConfigurationForArbitraryNamingWhenOnNonFeatureBranch() { $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; - $anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea'; + $anotherCommitHash = '13a15d220da53c52eddd5f32ffca64a7b3801bea'; $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') ->setMethods(array('execute')) @@ -251,6 +257,8 @@ class VersionGuesserTest extends TestCase $this->assertEquals("dev-latest-testing", $versionArray['version']); $this->assertEquals($commitHash, $versionArray['commit']); + $this->assertArrayNotHasKey('feature_version', $versionArray); + $this->assertArrayNotHasKey('feature_pretty_version', $versionArray); } public function testDetachedHeadBecomesDevHash()