Browse Source

Attempt at fixing support for git 2.11, refs #5942

Jordi Boggiano 8 years ago
parent
commit
e54c7478ee

+ 2 - 2
src/Composer/Package/Version/VersionGuesser.php

@@ -97,8 +97,8 @@ 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+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
-                    if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ') {
+                if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at FETCH_HEAD\)|\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;
                         $isFeatureBranch = true;

+ 32 - 0
tests/Composer/Test/Package/Version/VersionGuesserTest.php

@@ -91,6 +91,38 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals("dev-$commitHash", $versionData['version']);
     }
 
+    public function testDetachedHeadBecomesDevHashGit2()
+    {
+        $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
+
+        $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, $commitHash) {
+                $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
+                $output = "* (HEAD detached at FETCH_HEAD) $commitHash Commit message\n";
+
+                return 0;
+            })
+        ;
+
+        $config = new Config;
+        $config->merge(array('repositories' => array('packagist' => false)));
+        $guesser = new VersionGuesser($config, $executor, new VersionParser());
+        $versionData = $guesser->guessVersion(array(), 'dummy/path');
+
+        $this->assertEquals("dev-$commitHash", $versionData['version']);
+    }
+
     public function testTagBecomesVersion()
     {
         $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')