Revert lock file as well when a require command operation failed to complete

main
Jordi Boggiano 4 years ago
parent 379baa1560
commit 4e7ff690c6
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

@ -42,6 +42,10 @@ class RequireCommand extends InitCommand
private $json;
private $file;
private $composerBackup;
/** @var string file name */
private $lock;
/** @var ?string contents before modification if the lock file exists */
private $lockBackup;
protected function configure()
{
@ -118,7 +122,9 @@ EOT
}
$this->json = new JsonFile($this->file);
$this->lock = Factory::getLockFile($this->file);
$this->composerBackup = file_get_contents($this->json->getPath());
$this->lockBackup = file_exists($this->lock) ? file_get_contents($this->lock) : null;
// check for writability by writing to the file as is_writable can not be trusted on network-mounts
// see https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926
@ -325,9 +331,19 @@ EOT
if ($this->newlyCreated) {
$io->writeError("\n".'<error>Installation failed, deleting '.$this->file.'.</error>');
unlink($this->json->getPath());
if (file_exists($this->lock)) {
unlink($this->lock);
}
} else {
$io->writeError("\n".'<error>Installation failed, reverting '.$this->file.' to its original content.</error>');
$msg = ' to its ';
if ($this->lockBackup) {
$msg = ' and '.$this->lock.' to their ';
}
$io->writeError("\n".'<error>Installation failed, reverting '.$this->file.$msg.'original content.</error>');
file_put_contents($this->json->getPath(), $this->composerBackup);
if ($this->lockBackup) {
file_put_contents($this->lock, $this->lockBackup);
}
}
if ($hardExit) {

@ -223,6 +223,13 @@ class Factory
return trim(getenv('COMPOSER')) ?: './composer.json';
}
public static function getLockFile($composerFile)
{
return "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
? substr($composerFile, 0, -4).'lock'
: $composerFile . '.lock';
}
public static function createAdditionalStyles()
{
return array(
@ -388,9 +395,7 @@ class Factory
// init locker if possible
if ($fullLoad && isset($composerFile)) {
$lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
? substr($composerFile, 0, -4).'lock'
: $composerFile . '.lock';
$lockFile = self::getLockFile($composerFile);
$locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $im, file_get_contents($composerFile));
$composer->setLocker($locker);

Loading…
Cancel
Save