1
0
Fork 0

Add some more version normalization and support for branch names

pull/48/head
Jordi Boggiano 2011-10-11 09:30:10 +02:00
parent 536c1e0f88
commit 19f89069a4
2 changed files with 56 additions and 1 deletions

View File

@ -34,6 +34,10 @@ class VersionParser
{
$version = trim($version);
if (in_array($version, array('master', 'trunk'))) {
return '9999999-dev';
}
// match classical versioning
if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.$this->modifierRegex.'$}i', $version, $matches)) {
$version = $matches[1]
@ -65,6 +69,31 @@ class VersionParser
throw new \UnexpectedValueException('Invalid version string '.$version);
}
/**
* Normalizes a branch name to be able to perform comparisons on it
*
* @param string $version
* @return array
*/
public function normalizeBranch($name)
{
$name = trim($name);
if (in_array($name, array('master', 'trunk'))) {
return $this->normalize($name);
}
if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) {
$version = '';
for ($i = 1; $i < 5; $i++) {
$version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x';
}
return str_replace('x', '9999999', $version).'-dev';
}
throw new \UnexpectedValueException('Invalid branch name '.$branch);
}
/**
* Parses as constraint string into LinkConstraint objects
*
@ -93,7 +122,7 @@ class VersionParser
private function parseConstraint($constraint)
{
if ('*' === $constraint || '*.*' === $constraint || '*.*.*' === $constraint) {
if ('*' === $constraint || '*.*' === $constraint || '*.*.*' === $constraint || '*.*.*.*' === $constraint) {
return array();
}

View File

@ -49,6 +49,8 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
'parses datetime' => array('20100102-203040', '20100102-203040'),
'parses dt+number' => array('20100102203040-10', '20100102203040-10'),
'parses dt+patch' => array('20100102-203040-p1', '20100102-203040-patch1'),
'parses master' => array('master', '9999999-dev'),
'parses trunk' => array('trunk', '9999999-dev'),
);
}
@ -72,6 +74,30 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
);
}
/**
* @dataProvider successfulNormalizedBranches
*/
public function testNormalizeBranch($input, $expected)
{
$parser = new VersionParser;
$this->assertSame((string) $expected, (string) $parser->normalizeBranch($input));
}
public function successfulNormalizedBranches()
{
return array(
'parses x' => array('v1.x', '1.9999999.9999999.9999999-dev'),
'parses *' => array('v1.*', '1.9999999.9999999.9999999-dev'),
'parses digits' => array('v1.0', '1.0.9999999.9999999-dev'),
'parses long x' => array('v1.0.x', '1.0.9999999.9999999-dev'),
'parses long *' => array('v1.0.3.*', '1.0.3.9999999-dev'),
'parses long digits' => array('v2.4.0', '2.4.0.9999999-dev'),
'parses long digits/2' => array('2.4.4', '2.4.4.9999999-dev'),
'parses master' => array('master', '9999999-dev'),
'parses trunk' => array('trunk', '9999999-dev'),
);
}
/**
* @dataProvider simpleConstraints
*/