Browse Source

better error handling when git command runs into a failure, fixes #340

mikey179 13 years ago
parent
commit
895d901bf9

+ 13 - 3
src/Composer/Downloader/GitDownloader.php

@@ -29,7 +29,10 @@ class GitDownloader extends VcsDownloader
         $ref = escapeshellarg($package->getSourceReference());
         $ref = escapeshellarg($package->getSourceReference());
         $path = escapeshellarg($path);
         $path = escapeshellarg($path);
         $this->io->write("    Cloning ".$package->getSourceReference());
         $this->io->write("    Cloning ".$package->getSourceReference());
-        $this->process->execute(sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref), $ignoredOutput);
+        $command = sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref);
+        if (0 !== $this->process->execute($command, $ignoredOutput)) {
+            throw new \RuntimeException('Failed to execute ' . $command);
+        }
     }
     }
 
 
     /**
     /**
@@ -40,7 +43,10 @@ class GitDownloader extends VcsDownloader
         $ref = escapeshellarg($target->getSourceReference());
         $ref = escapeshellarg($target->getSourceReference());
         $path = escapeshellarg($path);
         $path = escapeshellarg($path);
         $this->io->write("    Checking out ".$target->getSourceReference());
         $this->io->write("    Checking out ".$target->getSourceReference());
-        $this->process->execute(sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $ref), $ignoredOutput);
+        $command = sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $ref);
+        if (0 !== $this->process->execute($command, $ignoredOutput)) {
+            throw new \RuntimeException('Failed to execute ' . $command);
+        }
     }
     }
 
 
     /**
     /**
@@ -48,7 +54,11 @@ class GitDownloader extends VcsDownloader
      */
      */
     protected function enforceCleanDirectory($path)
     protected function enforceCleanDirectory($path)
     {
     {
-        $this->process->execute(sprintf('cd %s && git status --porcelain', escapeshellarg($path)), $output);
+        $command = sprintf('cd %s && git status --porcelain', escapeshellarg($path));
+        if (0 !== $this->process->execute($command, $output)) {
+            throw new \RuntimeException('Failed to execute ' . $command);
+        }
+
         if (trim($output)) {
         if (trim($output)) {
             throw new \RuntimeException('Source directory has uncommitted changes');
             throw new \RuntimeException('Source directory has uncommitted changes');
         }
         }

+ 8 - 4
tests/Composer/Test/Downloader/GitDownloaderTest.php

@@ -43,7 +43,8 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor->expects($this->once())
         $processExecutor->expects($this->once())
             ->method('execute')
             ->method('execute')
-            ->with($this->equalTo($expectedGitCommand));
+            ->with($this->equalTo($expectedGitCommand))
+            ->will($this->returnValue(0));
 
 
         $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
         $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
         $downloader->download($packageMock, 'composerPath');
         $downloader->download($packageMock, 'composerPath');
@@ -79,10 +80,12 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor->expects($this->at(0))
         $processExecutor->expects($this->at(0))
             ->method('execute')
             ->method('execute')
-            ->with($this->equalTo($expectedGitResetCommand));
+            ->with($this->equalTo($expectedGitResetCommand))
+            ->will($this->returnValue(0));
         $processExecutor->expects($this->at(1))
         $processExecutor->expects($this->at(1))
             ->method('execute')
             ->method('execute')
-            ->with($this->equalTo($expectedGitUpdateCommand));
+            ->with($this->equalTo($expectedGitUpdateCommand))
+            ->will($this->returnValue(0));
 
 
         $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
         $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
         $downloader->update($packageMock, $packageMock, 'composerPath');
         $downloader->update($packageMock, $packageMock, 'composerPath');
@@ -96,7 +99,8 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
         $processExecutor->expects($this->any())
         $processExecutor->expects($this->any())
             ->method('execute')
             ->method('execute')
-            ->with($this->equalTo($expectedGitResetCommand));
+            ->with($this->equalTo($expectedGitResetCommand))
+            ->will($this->returnValue(0));
         $filesystem = $this->getMock('Composer\Util\Filesystem');
         $filesystem = $this->getMock('Composer\Util\Filesystem');
         $filesystem->expects($this->any())
         $filesystem->expects($this->any())
             ->method('removeDirectory')
             ->method('removeDirectory')