SvnDriver.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\Repository\Vcs;
  12. use Composer\Json\JsonFile;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\Util\Filesystem;
  15. use Composer\Util\Svn as SvnUtil;
  16. use Composer\IO\IOInterface;
  17. use Composer\Downloader\TransportException;
  18. /**
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. * @author Till Klampaeckel <till@php.net>
  21. */
  22. class SvnDriver extends VcsDriver
  23. {
  24. protected $baseUrl;
  25. protected $tags;
  26. protected $branches;
  27. protected $infoCache = array();
  28. /**
  29. * @var \Composer\Util\Svn
  30. */
  31. private $util;
  32. /**
  33. * Execute an SVN command and try to fix up the process with credentials
  34. * if necessary.
  35. *
  36. * @param string $command The svn command to run.
  37. * @param string $url The SVN URL.
  38. *
  39. * @return string
  40. */
  41. protected function execute($command, $url)
  42. {
  43. if (null === $this->util) {
  44. $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process);
  45. }
  46. try {
  47. return $this->util->execute($command, $url);
  48. } catch (\RuntimeException $e) {
  49. throw new \RuntimeException(
  50. 'Repository '.$this->url.' could not be processed, '.$e->getMessage()
  51. );
  52. }
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function initialize()
  58. {
  59. $this->url = rtrim(self::normalizeUrl($this->url), '/');
  60. if (false !== ($pos = strrpos($this->url, '/trunk'))) {
  61. $this->baseUrl = substr($this->url, 0, $pos);
  62. }
  63. $this->getBranches();
  64. $this->getTags();
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getRootIdentifier()
  70. {
  71. return 'trunk';
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getUrl()
  77. {
  78. return $this->url;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function getSource($identifier)
  84. {
  85. return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getDist($identifier)
  91. {
  92. return null;
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getComposerInformation($identifier)
  98. {
  99. $identifier = '/' . trim($identifier, '/') . '/';
  100. if (!isset($this->infoCache[$identifier])) {
  101. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  102. if (!empty($match[2])) {
  103. $identifier = $match[1];
  104. $rev = $match[2];
  105. } else {
  106. $rev = '';
  107. }
  108. try {
  109. $output = $this->execute('svn cat', $this->baseUrl . $identifier . 'composer.json' . $rev);
  110. if (!trim($output)) {
  111. return;
  112. }
  113. } catch (\RuntimeException $e) {
  114. throw new TransportException($e->getMessage());
  115. }
  116. $composer = JsonFile::parseJson($output);
  117. if (!isset($composer['time'])) {
  118. $output = $this->execute('svn info', $this->baseUrl . $identifier . $rev);
  119. foreach ($this->process->splitLines($output) as $line) {
  120. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  121. $date = new \DateTime($match[1]);
  122. $composer['time'] = $date->format('Y-m-d H:i:s');
  123. break;
  124. }
  125. }
  126. }
  127. $this->infoCache[$identifier] = $composer;
  128. }
  129. return $this->infoCache[$identifier];
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public function getTags()
  135. {
  136. if (null === $this->tags) {
  137. $this->tags = array();
  138. $output = $this->execute('svn ls', $this->baseUrl . '/tags');
  139. if ($output) {
  140. foreach ($this->process->splitLines($output) as $tag) {
  141. if ($tag) {
  142. $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
  143. }
  144. }
  145. }
  146. }
  147. return $this->tags;
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function getBranches()
  153. {
  154. if (null === $this->branches) {
  155. $this->branches = array();
  156. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/');
  157. if ($output) {
  158. foreach ($this->process->splitLines($output) as $line) {
  159. $line = trim($line);
  160. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  161. if (isset($match[1]) && isset($match[2]) && $match[2] === 'trunk/') {
  162. $this->branches['trunk'] = '/trunk/@'.$match[1];
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. unset($output);
  169. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/branches');
  170. if ($output) {
  171. foreach ($this->process->splitLines(trim($output)) as $line) {
  172. $line = trim($line);
  173. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  174. if (isset($match[1]) && isset($match[2]) && $match[2] !== './') {
  175. $this->branches[rtrim($match[2], '/')] = '/branches/'.$match[2].'@'.$match[1];
  176. }
  177. }
  178. }
  179. }
  180. }
  181. return $this->branches;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public static function supports(IOInterface $io, $url, $deep = false)
  187. {
  188. $url = self::normalizeUrl($url);
  189. if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) {
  190. return true;
  191. }
  192. // proceed with deep check for local urls since they are fast to process
  193. if (!$deep && !static::isLocalUrl($url)) {
  194. return false;
  195. }
  196. $processExecutor = new ProcessExecutor();
  197. $exit = $processExecutor->execute(
  198. "svn info --non-interactive {$url}",
  199. $ignoredOutput
  200. );
  201. if ($exit === 0) {
  202. // This is definitely a Subversion repository.
  203. return true;
  204. }
  205. if (false !== stripos($processExecutor->getErrorOutput(), 'authorization failed:')) {
  206. // This is likely a remote Subversion repository that requires
  207. // authentication. We will handle actual authentication later.
  208. return true;
  209. }
  210. return false;
  211. }
  212. /**
  213. * An absolute path (leading '/') is converted to a file:// url.
  214. *
  215. * @param string $url
  216. *
  217. * @return string
  218. */
  219. protected static function normalizeUrl($url)
  220. {
  221. $fs = new Filesystem();
  222. if ($fs->isAbsolutePath($url)) {
  223. return 'file://' . strtr($url, '\\', '/');
  224. }
  225. return $url;
  226. }
  227. }