1
0
Fork 0

Merge branch 'interactive-dirty-updates'

pull/1162/merge
Jordi Boggiano 2012-10-10 11:11:47 +02:00
commit c492476e88
3 changed files with 159 additions and 6 deletions

View File

@ -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(' <error>The package has modified files:</error>');
$this->io->write(array_slice($changes, 0, 10));
if (count($changes) > 10) {
$this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
}
while (true) {
switch ($this->io->ask(' <info>Discard changes [y,n,v,'.($update ? 's,' : '').'?]?</info> ', '?')) {
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(' <info>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';

View File

@ -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(' <error>The package has modified files:</error>');
$this->io->write(array_slice($changes, 0, 10));
if (count($changes) > 10) {
$this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
}
while (true) {
switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
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}
*/

View File

@ -86,8 +86,16 @@ abstract class VcsDownloader implements DownloaderInterface
$this->io->write(" - Updating <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
$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 <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)");
$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.
*