Browse Source

Always make sure we are on the right branch

Jordi Boggiano 12 years ago
parent
commit
f752b29fc6

+ 11 - 8
src/Composer/Downloader/GitDownloader.php

@@ -174,14 +174,17 @@ class GitDownloader extends VcsDownloader
             }
         }
 
-        // checkout branch by name if the current reference matches the tip of the branch
-        if (preg_match('{^[a-f0-9]{40}$}', $reference)
-            && 0 === $this->process->execute('git log '.escapeshellarg($branch).' -1 --format=format:%H', $output, $path)
-            && $reference === trim($output)
-        ) {
-            $command = sprintf('git checkout -B %s %s && git reset --hard %2$s', escapeshellarg($branch), escapeshellarg('composer/'.$branch));
-            if (0 === $this->process->execute($command, $output, $path)) {
-                return;
+        // try to checkout branch by name and then reset it so it's on the proper branch name
+        if (preg_match('{^[a-f0-9]{40}$}', $reference)) {
+            $command = sprintf('git checkout %s', escapeshellarg($branch));
+            $fallbackCommand = sprintf('git checkout -B %s %s', escapeshellarg($branch), escapeshellarg('composer/'.$branch));
+            if (0 === $this->process->execute($command, $output, $path)
+                || 0 === $this->process->execute($fallbackCommand, $output, $path)
+            ) {
+                $command = sprintf('git reset --hard %s', escapeshellarg($reference));
+                if (0 === $this->process->execute($command, $output, $path)) {
+                    return;
+                }
             }
         }
 

+ 2 - 2
tests/Composer/Test/Downloader/GitDownloaderTest.php

@@ -68,12 +68,12 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
 
         $processExecutor->expects($this->at(1))
             ->method('execute')
-            ->with($this->equalTo($this->getCmd("git log 'master' -1 --format=format:%H")), $this->equalTo(null), $this->equalTo('composerPath'))
+            ->with($this->equalTo($this->getCmd("git checkout 'master'")), $this->equalTo(null), $this->equalTo('composerPath'))
             ->will($this->returnValue(0));
 
         $processExecutor->expects($this->at(2))
             ->method('execute')
-            ->with($this->equalTo($this->getCmd("git checkout '1234567890123456789012345678901234567890' && git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo('composerPath'))
+            ->with($this->equalTo($this->getCmd("git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo('composerPath'))
             ->will($this->returnValue(0));
 
         $downloader = $this->getDownloaderMock(null, null, $processExecutor);