SvnDownloader.php 5.9 KB

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