SvnDownloader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $discardChanges = $this->config->get('discard-changes');
  83. if (!$this->io->isInteractive()) {
  84. switch ($discardChanges) {
  85. case 'true':
  86. return $this->discardChanges($path);
  87. case 'false':
  88. case 'stash':
  89. default:
  90. return parent::cleanChanges($path, $update);
  91. }
  92. }
  93. $changes = array_map(function ($elem) {
  94. return ' '.$elem;
  95. }, preg_split('{\s*\r?\n\s*}', $changes));
  96. $this->io->write(' <error>The package has modified files:</error>');
  97. $this->io->write(array_slice($changes, 0, 10));
  98. if (count($changes) > 10) {
  99. $this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  100. }
  101. while (true) {
  102. switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
  103. case 'y':
  104. $this->discardChanges($path);
  105. break 2;
  106. case 'n':
  107. throw new \RuntimeException('Update aborted');
  108. case 'v':
  109. $this->io->write($changes);
  110. break;
  111. case '?':
  112. default:
  113. $this->io->write(array(
  114. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  115. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  116. ' v - view modified files',
  117. ' ? - print help',
  118. ));
  119. break;
  120. }
  121. }
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. protected function getCommitLogs($fromReference, $toReference, $path)
  127. {
  128. // strip paths from references and only keep the actual revision
  129. $fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference);
  130. $toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference);
  131. $command = sprintf('cd %s && svn log -r%s:%s --incremental', escapeshellarg($path), $fromRevision, $toRevision);
  132. if (0 !== $this->process->execute($command, $output)) {
  133. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  134. }
  135. return $output;
  136. }
  137. protected function discardChanges($path)
  138. {
  139. if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
  140. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  141. }
  142. }
  143. }