From abc9d60fcc7a53b9baa633fc7f61de5d3355df9e Mon Sep 17 00:00:00 2001 From: rkerner Date: Mon, 2 Feb 2015 14:36:50 +0100 Subject: [PATCH] added non-feature-branches to handle non-numeric branches matching configured patterns not been handled as feature branches --- res/composer-schema.json | 7 ++ .../Package/Loader/RootPackageLoader.php | 11 +++ .../Package/Loader/RootPackageLoaderTest.php | 76 +++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/res/composer-schema.json b/res/composer-schema.json index 905199247..3f3f595cd 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -367,6 +367,13 @@ "format": "uri" } } + }, + "non-feature-branches": { + "type": ["array"], + "description": "A set of string or regex patterns for non-numeric branch names that will not be handles as feature branches.", + "items": { + "type": "string" + } } } } diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index a067fa6a8..face114af 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -272,7 +272,18 @@ class RootPackageLoader extends ArrayLoader ) { $branch = preg_replace('{^dev-}', '', $version); $length = PHP_INT_MAX; + + $nonFeatureBranches = ''; + if(!empty($config['non-feature-branches'])) { + $nonFeatureBranches = implode('|', $config['non-feature-branches']); + } + foreach ($branches as $candidate) { + // return directly, if branch is configured to be non-feature branch + if($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidate)) { + return $version; + } + // do not compare against other feature branches if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) { continue; diff --git a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php index 1a6a3bf78..d3bab647f 100644 --- a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php @@ -153,4 +153,80 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase 'qux/quux' => BasePackage::STABILITY_RC, ), $package->getStabilityFlags()); } + + public function testFeatureBranchPrettyVersion() + { + if (!function_exists('proc_open')) { + $this->markTestSkipped('proc_open() is not available'); + } + + $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') + ->disableOriginalConstructor() + ->getMock(); + + $self = $this; + + /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */ + $processExecutor = new ProcessExecutorMock(function($command, &$output = null, $cwd = null) use ($self) { + if (0 === strpos($command, 'git rev-list')) { + $output = ""; + return 0; + } + + if ('git branch --no-color --no-abbrev -v' !== $command) { + return 1; //0; + } + + $self->assertEquals('git branch --no-color --no-abbrev -v', $command); + + $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n"; + + return 0; + }); + + $config = new Config; + $config->merge(array('repositories' => array('packagist' => false))); + $loader = new RootPackageLoader($manager, $config, null, $processExecutor); + $package = $loader->load(array('require' => array('foo/bar' => 'self.version'))); + + $this->assertEquals("dev-master", $package->getPrettyVersion()); + } + + public function testNonFeatureBranchPrettyVersion() + { + if (!function_exists('proc_open')) { + $this->markTestSkipped('proc_open() is not available'); + } + + $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager') + ->disableOriginalConstructor() + ->getMock(); + + $self = $this; + + /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */ + $processExecutor = new ProcessExecutorMock(function($command, &$output = null, $cwd = null) use ($self) { + if (0 === strpos($command, 'git rev-list')) { + $output = ""; + return 0; + } + + if ('git branch --no-color --no-abbrev -v' !== $command) { + return 1; //0; + } + + $self->assertEquals('git branch --no-color --no-abbrev -v', $command); + + $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n"; + + return 0; + }); + + $config = new Config; + $config->merge(array('repositories' => array('packagist' => false))); + $loader = new RootPackageLoader($manager, $config, null, $processExecutor); + $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*"))); + + $this->assertEquals("dev-latest-production", $package->getPrettyVersion()); + } }