diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php
index a882002ff..08653d709 100644
--- a/src/Composer/Downloader/GitDownloader.php
+++ b/src/Composer/Downloader/GitDownloader.php
@@ -19,6 +19,8 @@ use Composer\Package\PackageInterface;
*/
class GitDownloader extends VcsDownloader
{
+ private $hasStashedChanges = false;
+
/**
* {@inheritDoc}
*/
@@ -76,6 +78,85 @@ class GitDownloader extends VcsDownloader
return trim($output) ?: null;
}
+ /**
+ * {@inhertiDoc}
+ */
+ protected function cleanChanges($path, $update)
+ {
+ if (!$this->io->isInteractive()) {
+ return parent::cleanChanges($path, $update);
+ }
+
+ if (!$changes = $this->getLocalChanges($path)) {
+ return;
+ }
+
+ $changes = array_map(function ($elem) {
+ return ' '.$elem;
+ }, preg_split('{\s*\r?\n\s*}', $changes));
+ $this->io->write(' The package has modified files:');
+ $this->io->write(array_slice($changes, 0, 10));
+ if (count($changes) > 10) {
+ $this->io->write(' '.count($changes) - 10 . ' more files modified, choose "v" to view the full list');
+ }
+
+ while (true) {
+ switch ($this->io->ask(' Discard changes [y,n,v,'.($update ? 's,' : '').'?]? ', '?')) {
+ case 'y':
+ if (0 !== $this->process->execute('git reset --hard', $output, $path)) {
+ throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
+ }
+ break 2;
+
+ case 's':
+ if (!$update) {
+ goto help;
+ }
+
+ if (0 !== $this->process->execute('git stash', $output, $path)) {
+ throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
+ }
+
+ $this->hasStashedChanges = true;
+ break 2;
+
+ case 'n':
+ throw new \RuntimeException('Update aborted');
+
+ case 'v':
+ $this->io->write($changes);
+ break;
+
+ case '?':
+ default:
+ help:
+ $this->io->write(array(
+ ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
+ ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
+ ' v - view modified files',
+ ));
+ if ($update) {
+ $this->io->write(' s - stash changes and try to reapply them after the update');
+ }
+ $this->io->write(' ? - print help');
+ break;
+ }
+ }
+ }
+
+ /**
+ * {@inhertiDoc}
+ */
+ protected function reapplyChanges($path)
+ {
+ if ($this->hasStashedChanges) {
+ $this->io->write(' 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());
+ }
+ }
+ }
+
protected function updateToCommit($path, $reference, $branch, $date)
{
$template = 'git checkout %s && git reset --hard %1$s';
diff --git a/src/Composer/Downloader/SvnDownloader.php b/src/Composer/Downloader/SvnDownloader.php
index 47322d9f5..55a54e9b1 100644
--- a/src/Composer/Downloader/SvnDownloader.php
+++ b/src/Composer/Downloader/SvnDownloader.php
@@ -79,6 +79,56 @@ class SvnDownloader extends VcsDownloader
}
}
+ /**
+ * {@inhertiDoc}
+ */
+ protected function cleanChanges($path, $update)
+ {
+ if (!$this->io->isInteractive()) {
+ return parent::cleanChanges($path, $update);
+ }
+
+ if (!$changes = $this->getLocalChanges($path)) {
+ return;
+ }
+
+ $changes = array_map(function ($elem) {
+ return ' '.$elem;
+ }, preg_split('{\s*\r?\n\s*}', $changes));
+ $this->io->write(' The package has modified files:');
+ $this->io->write(array_slice($changes, 0, 10));
+ if (count($changes) > 10) {
+ $this->io->write(' '.count($changes) - 10 . ' more files modified, choose "v" to view the full list');
+ }
+
+ while (true) {
+ switch ($this->io->ask(' Discard changes [y,n,v,?]? ', '?')) {
+ case 'y':
+ if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
+ throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
+ }
+ break 2;
+
+ case 'n':
+ throw new \RuntimeException('Update aborted');
+
+ case 'v':
+ $this->io->write($changes);
+ break;
+
+ case '?':
+ default:
+ $this->io->write(array(
+ ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
+ ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
+ ' v - view modified files',
+ ' ? - print help',
+ ));
+ break;
+ }
+ }
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php
index 3c2f58a67..997705147 100644
--- a/src/Composer/Downloader/VcsDownloader.php
+++ b/src/Composer/Downloader/VcsDownloader.php
@@ -86,8 +86,16 @@ abstract class VcsDownloader implements DownloaderInterface
$this->io->write(" - Updating " . $name . " (" . $from . " => " . $to . ")");
- $this->enforceCleanDirectory($path);
- $this->doUpdate($initial, $target, $path);
+ $this->cleanChanges($path, true);
+ try {
+ $this->doUpdate($initial, $target, $path);
+ } catch (\Exception $e) {
+ // in case of failed update, try to reapply the changes before aborting
+ $this->reapplyChanges($path);
+
+ throw $e;
+ }
+ $this->reapplyChanges($path);
//print the commit logs if in verbose mode
if ($this->io->isVerbose()) {
@@ -117,25 +125,39 @@ abstract class VcsDownloader implements DownloaderInterface
*/
public function remove(PackageInterface $package, $path)
{
- $this->enforceCleanDirectory($path);
$this->io->write(" - Removing " . $package->getName() . " (" . $package->getPrettyVersion() . ")");
+ $this->cleanChanges($path, false);
if (!$this->filesystem->removeDirectory($path)) {
throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
}
}
/**
- * Guarantee that no changes have been made to the local copy
+ * Prompt the user to check if changes should be stashed/removed or the operation aborted
*
- * @throws \RuntimeException if the directory is not clean
+ * @param string $path
+ * @param bool $stash if true (update) the changes can be stashed and reapplied after an update,
+ * if false (remove) the changes should be assumed to be lost if the operation is not aborted
+ * @throws \RuntimeException in case the operation must be aborted
*/
- protected function enforceCleanDirectory($path)
+ protected function cleanChanges($path, $update)
{
+ // the default implementation just fails if there are any changes, override in child classes to provide stash-ability
if (null !== $this->getLocalChanges($path)) {
throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.');
}
}
+ /**
+ * Guarantee that no changes have been made to the local copy
+ *
+ * @param string $path
+ * @throws \RuntimeException in case the operation must be aborted or the patch does not apply cleanly
+ */
+ protected function reapplyChanges($path)
+ {
+ }
+
/**
* Downloads specific package into specific folder.
*