瀏覽代碼

Make sure we avoid cleanup running more than once per package on VcsDownloader

Jordi Boggiano 5 年之前
父節點
當前提交
55f122008b
共有 2 個文件被更改,包括 13 次插入10 次删除
  1. 8 8
      src/Composer/Downloader/GitDownloader.php
  2. 5 2
      src/Composer/Downloader/VcsDownloader.php

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

@@ -27,8 +27,8 @@ use Composer\Cache;
  */
 class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
 {
-    private $hasStashedChanges = false;
-    private $hasDiscardedChanges = false;
+    private $hasStashedChanges = array();
+    private $hasDiscardedChanges = array();
     private $gitUtil;
     private $cachedPackages = array();
 
@@ -356,15 +356,15 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
     protected function reapplyChanges($path)
     {
         $path = $this->normalizePath($path);
-        if ($this->hasStashedChanges) {
-            $this->hasStashedChanges = false;
+        if (!empty($this->hasStashedChanges[$path])) {
+            unset($this->hasStashedChanges[$path]);
             $this->io->writeError('    <info>Re-applying stashed changes</info>');
             if (0 !== $this->process->execute('git stash pop', $output, $path)) {
                 throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput());
             }
         }
 
-        $this->hasDiscardedChanges = false;
+        unset($this->hasDiscardedChanges[$path]);
     }
 
     /**
@@ -379,7 +379,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
      */
     protected function updateToCommit($path, $reference, $branch, $date)
     {
-        $force = $this->hasDiscardedChanges || $this->hasStashedChanges ? '-f ' : '';
+        $force = !empty($this->hasDiscardedChanges[$path]) || !empty($this->hasStashedChanges[$path]) ? '-f ' : '';
 
         // This uses the "--" sequence to separate branch from file parameters.
         //
@@ -484,7 +484,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
             throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
         }
 
-        $this->hasDiscardedChanges = true;
+        $this->hasDiscardedChanges[$path] = true;
     }
 
     /**
@@ -498,7 +498,7 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
             throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
         }
 
-        $this->hasStashedChanges = true;
+        $this->hasStashedChanges[$path] = true;
     }
 
     /**

+ 5 - 2
src/Composer/Downloader/VcsDownloader.php

@@ -35,6 +35,8 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
     protected $process;
     /** @var Filesystem */
     protected $filesystem;
+    /** @var array */
+    protected $hasCleanedChanges = array();
 
     public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
     {
@@ -90,6 +92,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
     {
         if ($type === 'update') {
             $this->cleanChanges($prevPackage, $path, true);
+            $this->hasCleanedChanges[$prevPackage->getUniqueName()] = true;
         } elseif ($type === 'install') {
             $this->filesystem->emptyDirectory($path);
         } elseif ($type === 'uninstall') {
@@ -102,9 +105,9 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
      */
     public function cleanup($type, PackageInterface $package, $path, PackageInterface $prevPackage = null)
     {
-        if ($type === 'update') {
-            // TODO keep track of whether prepare was called for this package
+        if ($type === 'update' && isset($this->hasCleanedChanges[$prevPackage->getUniqueName()])) {
             $this->reapplyChanges($path);
+            unset($this->hasCleanedChanges[$prevPackage->getUniqueName()]);
         }
     }