diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php
index 425481572..e32abdc1e 100644
--- a/src/Composer/Command/RequireCommand.php
+++ b/src/Composer/Command/RequireCommand.php
@@ -64,11 +64,17 @@ EOT
return 1;
}
+ if (!is_writable($file)) {
+ $output->writeln(''.$file.' is not writable.');
+
+ return 1;
+ }
$dialog = $this->getHelperSet()->get('dialog');
$json = new JsonFile($file);
$composer = $json->read();
+ $composerBackup = file_get_contents($json->getPath());
$requirements = $this->determineRequirements($input, $output, $input->getArgument('packages'));
@@ -106,7 +112,12 @@ EOT
->setUpdateWhitelist($requirements);
;
- return $install->run() ? 0 : 1;
+ if (!$install->run()) {
+ $output->writeln("\n".'Installation failed, reverting '.$file.' to its original content.');
+ file_put_contents($json->getPath(), $composerBackup);
+
+ return 1;
+ }
}
private function updateFileCleanly($json, array $base, array $new, $requireKey)