SvnDownloader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Util\Svn as SvnUtil;
  14. /**
  15. * @author Ben Bieker <mail@ben-bieker.de>
  16. * @author Till Klampaeckel <till@php.net>
  17. */
  18. class SvnDownloader extends VcsDownloader
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function doDownload(PackageInterface $package, $path)
  24. {
  25. $url = $package->getSourceUrl();
  26. $ref = $package->getSourceReference();
  27. $this->io->write(" Checking out ".$package->getSourceReference());
  28. $this->execute($url, "svn co", sprintf("%s/%s", $url, $ref), null, $path);
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
  34. {
  35. $url = $target->getSourceUrl();
  36. $ref = $target->getSourceReference();
  37. $this->io->write(" Checking out " . $ref);
  38. $this->execute($url, "svn switch", sprintf("%s/%s", $url, $ref), $path);
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function getLocalChanges($path)
  44. {
  45. if (!is_dir($path.'/.svn')) {
  46. return;
  47. }
  48. $this->process->execute('svn status --ignore-externals', $output, $path);
  49. return preg_match('{^ *[^X ] +}m', $output) ? $output : null;
  50. }
  51. /**
  52. * Execute an SVN command and try to fix up the process with credentials
  53. * if necessary.
  54. *
  55. * @param string $baseUrl Base URL of the repository
  56. * @param string $command SVN command to run
  57. * @param string $url SVN url
  58. * @param string $cwd Working directory
  59. * @param string $path Target for a checkout
  60. *
  61. * @return string
  62. */
  63. protected function execute($baseUrl, $command, $url, $cwd = null, $path = null)
  64. {
  65. $util = new SvnUtil($baseUrl, $this->io);
  66. try {
  67. return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
  68. } catch (\RuntimeException $e) {
  69. throw new \RuntimeException(
  70. 'Package could not be downloaded, '.$e->getMessage()
  71. );
  72. }
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. protected function cleanChanges($path, $update)
  78. {
  79. if (!$changes = $this->getLocalChanges($path)) {
  80. return;
  81. }
  82. if (!$this->io->isInteractive()) {
  83. if (true === $this->config->get('discard-changes')) {
  84. return $this->discardChanges($path);
  85. }
  86. return parent::cleanChanges($path, $update);
  87. }
  88. $changes = array_map(function ($elem) {
  89. return ' '.$elem;
  90. }, preg_split('{\s*\r?\n\s*}', $changes));
  91. $this->io->write(' <error>The package has modified files:</error>');
  92. $this->io->write(array_slice($changes, 0, 10));
  93. if (count($changes) > 10) {
  94. $this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  95. }
  96. while (true) {
  97. switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
  98. case 'y':
  99. $this->discardChanges($path);
  100. break 2;
  101. case 'n':
  102. throw new \RuntimeException('Update aborted');
  103. case 'v':
  104. $this->io->write($changes);
  105. break;
  106. case '?':
  107. default:
  108. $this->io->write(array(
  109. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  110. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  111. ' v - view modified files',
  112. ' ? - print help',
  113. ));
  114. break;
  115. }
  116. }
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. protected function getCommitLogs($fromReference, $toReference, $path)
  122. {
  123. // strip paths from references and only keep the actual revision
  124. $fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference);
  125. $toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference);
  126. $command = sprintf('cd %s && svn log -r%s:%s --incremental', escapeshellarg($path), $fromRevision, $toRevision);
  127. if (0 !== $this->process->execute($command, $output)) {
  128. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  129. }
  130. return $output;
  131. }
  132. protected function discardChanges($path)
  133. {
  134. if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
  135. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  136. }
  137. }
  138. }