From 5ba6d7d4364b0d1898017436935b863d4d8fb14d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 30 Nov 2017 15:18:52 +0100 Subject: [PATCH] Fix pretty guessed versions to show nicely on numeric branches, refs #6845 --- .../Package/Version/VersionGuesser.php | 21 ++++++++++--- .../Package/Version/VersionGuesserTest.php | 31 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index 8b84ffc28..f8e43fb88 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -66,23 +66,36 @@ class VersionGuesser if (function_exists('proc_open')) { $versionData = $this->guessGitVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { - return $versionData; + return $this->postprocess($versionData); } $versionData = $this->guessHgVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { - return $versionData; + return $this->postprocess($versionData); } $versionData = $this->guessFossilVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { - return $versionData; + return $this->postprocess($versionData); } - return $this->guessSvnVersion($packageConfig, $path); + $versionData = $this->guessSvnVersion($packageConfig, $path); + if (null !== $versionData && null !== $versionData['version']) { + return $this->postprocess($versionData); + } } } + private function postprocess(array $versionData) + { + // make sure that e.g. dev-1.5 gets converted to 1.5.x-dev + if ('dev-' !== substr($versionData['version'], 0, 4)) { + $versionData['pretty_version'] = preg_replace('{(\.9{7})+}', '.x', $versionData['version']); + } + + return $versionData; + } + private function guessGitVersion(array $packageConfig, $path) { GitUtil::cleanEnv(); diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index 60681393c..fafb81c33 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -412,4 +412,35 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase $this->assertEquals("dev-foo", $versionData['version']); } + + public function testNumericBranchesShowNicely() + { + $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor') + ->setMethods(array('execute')) + ->disableArgumentCloning() + ->disableOriginalConstructor() + ->getMock() + ; + + $self = $this; + + $executor + ->expects($this->at(0)) + ->method('execute') + ->willReturnCallback(function ($command, &$output) use ($self) { + $self->assertEquals('git branch --no-color --no-abbrev -v', $command); + $output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n"; + + return 0; + }) + ; + + $config = new Config; + $config->merge(array('repositories' => array('packagist' => false))); + $guesser = new VersionGuesser($config, $executor, new VersionParser()); + $versionData = $guesser->guessVersion(array(), 'dummy/path'); + + $this->assertEquals("1.5.x-dev", $versionData['pretty_version']); + $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']); + } }