diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 728e0ba5d..3162e62cb 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/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; + } } } diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 20305a5fe..feb009b1e 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/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);