From 55f122008b43b3a77a9e79692ef89e0ce97d391b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 9 Apr 2020 10:32:03 +0200 Subject: [PATCH] Make sure we avoid cleanup running more than once per package on VcsDownloader --- src/Composer/Downloader/GitDownloader.php | 16 ++++++++-------- src/Composer/Downloader/VcsDownloader.php | 7 +++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 483a3d364..5943f1e77 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/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(' Re-applying stashed changes'); 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; } /** diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 2236c4d88..da078e76f 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/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()]); } }