SvnDownloader.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if (!is_dir($path.'/.svn')) {
  38. throw new \RuntimeException('The .svn directory is missing from '.$path.', see http://getcomposer.org/commit-deps for more information');
  39. }
  40. $this->io->write(" Checking out " . $ref);
  41. $this->execute($url, "svn switch", sprintf("%s/%s", $url, $ref), $path);
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getLocalChanges(PackageInterface $package, $path)
  47. {
  48. if (!is_dir($path.'/.svn')) {
  49. return;
  50. }
  51. $this->process->execute('svn status --ignore-externals', $output, $path);
  52. return preg_match('{^ *[^X ] +}m', $output) ? $output : null;
  53. }
  54. /**
  55. * Execute an SVN command and try to fix up the process with credentials
  56. * if necessary.
  57. *
  58. * @param string $baseUrl Base URL of the repository
  59. * @param string $command SVN command to run
  60. * @param string $url SVN url
  61. * @param string $cwd Working directory
  62. * @param string $path Target for a checkout
  63. * @throws \RuntimeException
  64. * @return string
  65. */
  66. protected function execute($baseUrl, $command, $url, $cwd = null, $path = null)
  67. {
  68. $util = new SvnUtil($baseUrl, $this->io);
  69. try {
  70. return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
  71. } catch (\RuntimeException $e) {
  72. throw new \RuntimeException(
  73. 'Package could not be downloaded, '.$e->getMessage()
  74. );
  75. }
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. protected function cleanChanges(PackageInterface $package, $path, $update)
  81. {
  82. if (!$changes = $this->getLocalChanges($package, $path)) {
  83. return;
  84. }
  85. if (!$this->io->isInteractive()) {
  86. if (true === $this->config->get('discard-changes')) {
  87. return $this->discardChanges($path);
  88. }
  89. return parent::cleanChanges($package, $path, $update);
  90. }
  91. $changes = array_map(function ($elem) {
  92. return ' '.$elem;
  93. }, preg_split('{\s*\r?\n\s*}', $changes));
  94. $this->io->write(' <error>The package has modified files:</error>');
  95. $this->io->write(array_slice($changes, 0, 10));
  96. if (count($changes) > 10) {
  97. $this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  98. }
  99. while (true) {
  100. switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
  101. case 'y':
  102. $this->discardChanges($path);
  103. break 2;
  104. case 'n':
  105. throw new \RuntimeException('Update aborted');
  106. case 'v':
  107. $this->io->write($changes);
  108. break;
  109. case '?':
  110. default:
  111. $this->io->write(array(
  112. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  113. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  114. ' v - view modified files',
  115. ' ? - print help',
  116. ));
  117. break;
  118. }
  119. }
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. protected function getCommitLogs($fromReference, $toReference, $path)
  125. {
  126. // strip paths from references and only keep the actual revision
  127. $fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference);
  128. $toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference);
  129. $command = sprintf('svn log -r%s:%s --incremental', $fromRevision, $toRevision);
  130. if (0 !== $this->process->execute($command, $output, $path)) {
  131. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  132. }
  133. return $output;
  134. }
  135. protected function discardChanges($path)
  136. {
  137. if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
  138. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  139. }
  140. }
  141. }