SvnDriver.php 8.1 KB

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