From 313e6b914d351aea334224b9bc7403039ed57b41 Mon Sep 17 00:00:00 2001 From: Nicole Cordes Date: Fri, 31 Mar 2017 15:02:50 +0200 Subject: [PATCH] Fix Git branch parsing for detached HEAD on a commit Current versions of Git output the commit hash as detached HEAD instead of FETCH_HEAD. The VersionGuesser should be able to handle commit hashes as well as FETCH_HEAD to detect the correct branch of a commit. --- src/Composer/Package/Version/VersionGuesser.php | 2 +- tests/Composer/Test/Package/Version/VersionGuesserTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index 9aca9ca53..f060a7455 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -98,7 +98,7 @@ class VersionGuesser // find current branch and collect all branch names foreach ($this->process->splitLines($output) as $branch) { - if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at FETCH_HEAD\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) { + if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) { if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ' || substr($match[1], 0, 17) === '(HEAD detached at') { $version = 'dev-' . $match[2]; $prettyVersion = $version; diff --git a/tests/Composer/Test/Package/Version/VersionGuesserTest.php b/tests/Composer/Test/Package/Version/VersionGuesserTest.php index 52488dacc..cf5377670 100644 --- a/tests/Composer/Test/Package/Version/VersionGuesserTest.php +++ b/tests/Composer/Test/Package/Version/VersionGuesserTest.php @@ -176,7 +176,7 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase ->method('execute') ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) { $self->assertEquals('git branch --no-color --no-abbrev -v', $command); - $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n"; + $output = "* (HEAD detached at " . substr($commitHash, 0, 9) . ") $commitHash Commit message\n"; return 0; })