1
0
Fork 0

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

pull/8754/head
Jordi Boggiano 2020-04-09 10:32:03 +02:00
parent 0aaa815268
commit 55f122008b
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 13 additions and 10 deletions

View File

@ -27,8 +27,8 @@ use Composer\Cache;
*/ */
class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
{ {
private $hasStashedChanges = false; private $hasStashedChanges = array();
private $hasDiscardedChanges = false; private $hasDiscardedChanges = array();
private $gitUtil; private $gitUtil;
private $cachedPackages = array(); private $cachedPackages = array();
@ -356,15 +356,15 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
protected function reapplyChanges($path) protected function reapplyChanges($path)
{ {
$path = $this->normalizePath($path); $path = $this->normalizePath($path);
if ($this->hasStashedChanges) { if (!empty($this->hasStashedChanges[$path])) {
$this->hasStashedChanges = false; unset($this->hasStashedChanges[$path]);
$this->io->writeError(' <info>Re-applying stashed changes</info>'); $this->io->writeError(' <info>Re-applying stashed changes</info>');
if (0 !== $this->process->execute('git stash pop', $output, $path)) { if (0 !== $this->process->execute('git stash pop', $output, $path)) {
throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput()); 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) 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. // 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()); 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()); throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
} }
$this->hasStashedChanges = true; $this->hasStashedChanges[$path] = true;
} }
/** /**

View File

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