From 0a549efd0e3d33a1ea4e63068d944a59ea0727e5 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 7 Oct 2012 15:19:55 +0200 Subject: [PATCH 1/2] Allow interactive resets or stash/apply cycles when updating dirty packages instead of failing hard --- src/Composer/Downloader/GitDownloader.php | 82 +++++++++++++++++++++++ src/Composer/Downloader/SvnDownloader.php | 51 ++++++++++++++ src/Composer/Downloader/VcsDownloader.php | 34 ++++++++-- 3 files changed, 161 insertions(+), 6 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index a882002ff..4ddbe953a 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,86 @@ 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'); + break; + + 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..2d7818da1 100644 --- a/src/Composer/Downloader/SvnDownloader.php +++ b/src/Composer/Downloader/SvnDownloader.php @@ -79,6 +79,57 @@ 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'); + break; + + 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. * From 3a9c5b367899691e9f380e84f3c19e3dbf77582c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 8 Oct 2012 18:45:41 +0200 Subject: [PATCH 2/2] Remove dead break statements --- src/Composer/Downloader/GitDownloader.php | 1 - src/Composer/Downloader/SvnDownloader.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 4ddbe953a..08653d709 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -122,7 +122,6 @@ class GitDownloader extends VcsDownloader case 'n': throw new \RuntimeException('Update aborted'); - break; case 'v': $this->io->write($changes); diff --git a/src/Composer/Downloader/SvnDownloader.php b/src/Composer/Downloader/SvnDownloader.php index 2d7818da1..55a54e9b1 100644 --- a/src/Composer/Downloader/SvnDownloader.php +++ b/src/Composer/Downloader/SvnDownloader.php @@ -111,7 +111,6 @@ class SvnDownloader extends VcsDownloader case 'n': throw new \RuntimeException('Update aborted'); - break; case 'v': $this->io->write($changes);