1
0
Fork 0

Merge remote-tracking branch 'naderman/version-guess-remotes' into 1.10

pull/9276/head
Jordi Boggiano 2020-10-08 14:01:25 +02:00
commit 6698e0bafa
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 67 additions and 21 deletions

View File

@ -118,7 +118,7 @@ class VersionGuesser
$isDetached = false; $isDetached = false;
// try to fetch current version from git branch // try to fetch current version from git branch
if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) { if (0 === $this->process->execute('git branch -a --no-color --no-abbrev -v', $output, $path)) {
$branches = array(); $branches = array();
$isFeatureBranch = false; $isFeatureBranch = false;
@ -141,8 +141,8 @@ class VersionGuesser
} }
} }
if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { if ($branch && !preg_match('{^ *.+/HEAD }', $branch)) {
if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) { if (preg_match('{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}', $branch, $match)) {
$branches[] = $match[1]; $branches[] = $match[1];
} }
} }
@ -245,13 +245,14 @@ class VersionGuesser
} }
foreach ($branches as $candidate) { foreach ($branches as $candidate) {
$candidateVersion = preg_replace('{^remotes/\S+/}', '', $candidate);
// return directly, if branch is configured to be non-feature branch // return directly, if branch is configured to be non-feature branch
if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidate)) { if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidateVersion)) {
break; break;
} }
// do not compare against itself or other feature branches // do not compare against itself or other feature branches
if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . '|master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) { if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . '|master|trunk|default|develop|\d+\..+)$}', $candidateVersion, $match)) {
continue; continue;
} }
@ -262,8 +263,11 @@ class VersionGuesser
if (strlen($output) < $length) { if (strlen($output) < $length) {
$length = strlen($output); $length = strlen($output);
$version = $this->versionParser->normalizeBranch($candidate); $version = $this->versionParser->normalizeBranch($candidateVersion);
$prettyVersion = 'dev-' . $match[1]; $prettyVersion = 'dev-' . $candidateVersion;
if ($length === 0) {
break;
}
} }
} }
} }

View File

@ -137,7 +137,7 @@ class RootPackageLoaderTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n"; $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
return 0; return 0;
@ -187,7 +187,7 @@ class RootPackageLoaderTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n"; $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
return 0; return 0;

View File

@ -48,7 +48,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at($step)) ->expects($this->at($step))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
return 128; return 128;
}) })
@ -116,7 +116,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n"; $output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n";
return 0; return 0;
@ -153,7 +153,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n"; $output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
return 0; return 0;
@ -200,7 +200,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = " latest-testing $commitHash Commit message\n* current $anotherCommitHash Another message\n"; $output = " latest-testing $commitHash Commit message\n* current $anotherCommitHash Another message\n";
return 0; return 0;
@ -247,7 +247,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n"; $output = "* latest-testing $commitHash Commit message\n current $anotherCommitHash Another message\n master $anotherCommitHash Another message\n";
return 0; return 0;
@ -282,7 +282,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (no branch) $commitHash Commit message\n"; $output = "* (no branch) $commitHash Commit message\n";
return 0; return 0;
@ -313,7 +313,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n"; $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
return 0; return 0;
@ -343,7 +343,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self, $commitHash) { ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n"; $output = "* (HEAD detached at 03a15d220) $commitHash Commit message\n";
return 0; return 0;
@ -372,7 +372,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n"; $output = "* (HEAD detached at v2.0.5-alpha2) 433b98d4218c181bae01865901aac045585e8a1a Commit message\n";
return 0; return 0;
@ -413,7 +413,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n"; $output = "* (HEAD detached at 1.0.0) c006f0c12bbbf197b5c071ffb1c0e9812bb14a4d Commit message\n";
return 0; return 0;
@ -455,7 +455,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n"; $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0; return 0;
@ -485,7 +485,7 @@ class VersionGuesserTest extends TestCase
->expects($this->at(0)) ->expects($this->at(0))
->method('execute') ->method('execute')
->willReturnCallback(function ($command, &$output) use ($self) { ->willReturnCallback(function ($command, &$output) use ($self) {
$self->assertEquals('git branch --no-color --no-abbrev -v', $command); $self->assertEquals('git branch -a --no-color --no-abbrev -v', $command);
$output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n"; $output = "* 1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0; return 0;
@ -500,4 +500,46 @@ class VersionGuesserTest extends TestCase
$this->assertEquals("1.5.x-dev", $versionData['pretty_version']); $this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
$this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']); $this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
} }
public function testRemoteBranchesAreSelected()
{
$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 -a --no-color --no-abbrev -v', $command);
$output = "* feature-branch 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n".
"remotes/origin/1.5 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
return 0;
})
;
$executor
->expects($this->at(1))
->method('execute')
->willReturnCallback(function ($command, &$output, $path) use ($self) {
$self->assertEquals('git rev-list remotes/origin/1.5..feature-branch', $command);
$output = "\n";
return 0;
})
;
$config = new Config;
$config->merge(array('repositories' => array('packagist' => false)));
$guesser = new VersionGuesser($config, $executor, new VersionParser());
$versionData = $guesser->guessVersion(array('version' => 'self.version'), 'dummy/path');
$this->assertEquals("1.5.x-dev", $versionData['pretty_version']);
$this->assertEquals("1.5.9999999.9999999-dev", $versionData['version']);
}
} }