Browse Source

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.
Nicole Cordes 8 years ago
parent
commit
313e6b914d

+ 1 - 1
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;

+ 1 - 1
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;
             })