SvnDownloader.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. use Composer\Repository\VcsRepository;
  15. /**
  16. * @author Ben Bieker <mail@ben-bieker.de>
  17. * @author Till Klampaeckel <till@php.net>
  18. */
  19. class SvnDownloader extends VcsDownloader
  20. {
  21. protected $cacheCredentials = true;
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function doDownload(PackageInterface $package, $path, $url)
  26. {
  27. SvnUtil::cleanEnv();
  28. $ref = $package->getSourceReference();
  29. $repo = $package->getRepository();
  30. if ($repo instanceof VcsRepository) {
  31. $repoConfig = $repo->getRepoConfig();
  32. if (array_key_exists('svn-cache-credentials', $repoConfig)) {
  33. $this->cacheCredentials = (bool) $repoConfig['svn-cache-credentials'];
  34. }
  35. }
  36. $this->io->writeError(" Checking out ".$package->getSourceReference());
  37. $this->execute($url, "svn co", sprintf("%s/%s", $url, $ref), null, $path);
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  43. {
  44. SvnUtil::cleanEnv();
  45. $ref = $target->getSourceReference();
  46. if (!$this->hasMetadataRepository($path)) {
  47. throw new \RuntimeException('The .svn directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  48. }
  49. $flags = "";
  50. if (0 === $this->process->execute('svn --version', $output)) {
  51. if (preg_match('{(\d+(?:\.\d+)+)}', $output, $match) && version_compare($match[1], '1.7.0', '>=')) {
  52. $flags .= ' --ignore-ancestry';
  53. }
  54. }
  55. $this->io->writeError(" Checking out " . $ref);
  56. $this->execute($url, "svn switch" . $flags, sprintf("%s/%s", $url, $ref), $path);
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function getLocalChanges(PackageInterface $package, $path)
  62. {
  63. if (!$this->hasMetadataRepository($path)) {
  64. return null;
  65. }
  66. $this->process->execute('svn status --ignore-externals', $output, $path);
  67. return preg_match('{^ *[^X ] +}m', $output) ? $output : null;
  68. }
  69. /**
  70. * Execute an SVN command and try to fix up the process with credentials
  71. * if necessary.
  72. *
  73. * @param string $baseUrl Base URL of the repository
  74. * @param string $command SVN command to run
  75. * @param string $url SVN url
  76. * @param string $cwd Working directory
  77. * @param string $path Target for a checkout
  78. * @throws \RuntimeException
  79. * @return string
  80. */
  81. protected function execute($baseUrl, $command, $url, $cwd = null, $path = null)
  82. {
  83. $util = new SvnUtil($baseUrl, $this->io, $this->config);
  84. $util->setCacheCredentials($this->cacheCredentials);
  85. try {
  86. return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
  87. } catch (\RuntimeException $e) {
  88. throw new \RuntimeException(
  89. 'Package could not be downloaded, '.$e->getMessage()
  90. );
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. protected function cleanChanges(PackageInterface $package, $path, $update)
  97. {
  98. if (!$changes = $this->getLocalChanges($package, $path)) {
  99. return;
  100. }
  101. if (!$this->io->isInteractive()) {
  102. if (true === $this->config->get('discard-changes')) {
  103. return $this->discardChanges($path);
  104. }
  105. return parent::cleanChanges($package, $path, $update);
  106. }
  107. $changes = array_map(function ($elem) {
  108. return ' '.$elem;
  109. }, preg_split('{\s*\r?\n\s*}', $changes));
  110. $this->io->writeError(' <error>The package has modified files:</error>');
  111. $this->io->writeError(array_slice($changes, 0, 10));
  112. if (count($changes) > 10) {
  113. $this->io->writeError(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  114. }
  115. while (true) {
  116. switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
  117. case 'y':
  118. $this->discardChanges($path);
  119. break 2;
  120. case 'n':
  121. throw new \RuntimeException('Update aborted');
  122. case 'v':
  123. $this->io->writeError($changes);
  124. break;
  125. case '?':
  126. default:
  127. $this->io->writeError(array(
  128. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  129. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  130. ' v - view modified files',
  131. ' ? - print help',
  132. ));
  133. break;
  134. }
  135. }
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. protected function getCommitLogs($fromReference, $toReference, $path)
  141. {
  142. if (preg_match('{.*@(\d+)$}', $fromReference) && preg_match('{.*@(\d+)$}', $toReference)) {
  143. // strip paths from references and only keep the actual revision
  144. $fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference);
  145. $toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference);
  146. $command = sprintf('svn log -r%s:%s --incremental', $fromRevision, $toRevision);
  147. if (0 !== $this->process->execute($command, $output, $path)) {
  148. throw new \RuntimeException(
  149. 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()
  150. );
  151. }
  152. } else {
  153. $output = "Could not retrieve changes between $fromReference and $toReference due to missing revision information";
  154. }
  155. return $output;
  156. }
  157. protected function discardChanges($path)
  158. {
  159. if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
  160. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  161. }
  162. }
  163. /**
  164. * {@inheritDoc}
  165. */
  166. protected function hasMetadataRepository($path)
  167. {
  168. return is_dir($path.'/.svn');
  169. }
  170. }