SvnDownloader.php 7.5 KB

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