SvnDriver.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 $rootIdentifier;
  30. protected $infoCache = array();
  31. protected $trunkPath = 'trunk';
  32. protected $branchesPath = 'branches';
  33. protected $tagsPath = 'tags';
  34. protected $packagePath = '';
  35. /**
  36. * @var \Composer\Util\Svn
  37. */
  38. private $util;
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function initialize()
  43. {
  44. $this->url = $this->baseUrl = rtrim(self::normalizeUrl($this->url), '/');
  45. if (isset($this->repoConfig['trunk-path'])) {
  46. $this->trunkPath = $this->repoConfig['trunk-path'];
  47. }
  48. if (isset($this->repoConfig['branches-path'])) {
  49. $this->branchesPath = $this->repoConfig['branches-path'];
  50. }
  51. if (isset($this->repoConfig['tags-path'])) {
  52. $this->tagsPath = $this->repoConfig['tags-path'];
  53. }
  54. if (isset($this->repoConfig['package-path'])) {
  55. $this->packagePath = '/' . trim($this->repoConfig['package-path'], '/');
  56. }
  57. if (false !== ($pos = strrpos($this->url, '/' . $this->trunkPath))) {
  58. $this->baseUrl = substr($this->url, 0, $pos);
  59. }
  60. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->baseUrl));
  61. $this->getBranches();
  62. $this->getTags();
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getRootIdentifier()
  68. {
  69. return $this->rootIdentifier ?: $this->trunkPath;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getUrl()
  75. {
  76. return $this->url;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function getSource($identifier)
  82. {
  83. return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function getDist($identifier)
  89. {
  90. return null;
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function getComposerInformation($identifier)
  96. {
  97. $identifier = '/' . trim($identifier, '/') . '/';
  98. if ($res = $this->cache->read($identifier.'.json')) {
  99. $this->infoCache[$identifier] = JsonFile::parseJson($res);
  100. }
  101. if (!isset($this->infoCache[$identifier])) {
  102. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  103. if (!empty($match[2])) {
  104. $path = $match[1];
  105. $rev = $match[2];
  106. } else {
  107. $path = $identifier;
  108. $rev = '';
  109. }
  110. try {
  111. $resource = $path.'composer.json';
  112. $output = $this->execute('svn cat', $this->baseUrl . $resource . $rev);
  113. if (!trim($output)) {
  114. return;
  115. }
  116. } catch (\RuntimeException $e) {
  117. throw new TransportException($e->getMessage());
  118. }
  119. $composer = JsonFile::parseJson($output, $this->baseUrl . $resource . $rev);
  120. if (!isset($composer['time'])) {
  121. $output = $this->execute('svn info', $this->baseUrl . $path . $rev);
  122. foreach ($this->process->splitLines($output) as $line) {
  123. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  124. $date = new \DateTime($match[1], new \DateTimeZone('UTC'));
  125. $composer['time'] = $date->format('Y-m-d H:i:s');
  126. break;
  127. }
  128. }
  129. }
  130. $this->cache->write($identifier.'.json', json_encode($composer));
  131. $this->infoCache[$identifier] = $composer;
  132. }
  133. return $this->infoCache[$identifier];
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function getTags()
  139. {
  140. if (null === $this->tags) {
  141. $this->tags = array();
  142. if ($this->tagsPath !== false) {
  143. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath);
  144. if ($output) {
  145. foreach ($this->process->splitLines($output) as $line) {
  146. $line = trim($line);
  147. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  148. if (isset($match[1]) && isset($match[2]) && $match[2] !== './') {
  149. $this->tags[rtrim($match[2], '/')] = $this->buildIdentifier(
  150. '/' . $this->tagsPath . '/' . $match[2],
  151. $match[1]
  152. );
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. return $this->tags;
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function getBranches()
  165. {
  166. if (null === $this->branches) {
  167. $this->branches = array();
  168. if(false === $this->trunkPath) {
  169. $trunkParent = $this->baseUrl . '/';
  170. } else {
  171. $trunkParent = $this->baseUrl . '/' . $this->trunkPath;
  172. }
  173. $output = $this->execute('svn ls --verbose', $trunkParent);
  174. if ($output) {
  175. foreach ($this->process->splitLines($output) as $line) {
  176. $line = trim($line);
  177. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  178. if (isset($match[1]) && isset($match[2]) && $match[2] === 'composer.json') {
  179. $this->branches['trunk'] = $this->buildIdentifier(
  180. '/' . $this->trunkPath,
  181. $match[1]
  182. );
  183. $this->rootIdentifier = $this->branches['trunk'];
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. unset($output);
  190. if ($this->branchesPath !== false) {
  191. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->branchesPath);
  192. if ($output) {
  193. foreach ($this->process->splitLines(trim($output)) as $line) {
  194. $line = trim($line);
  195. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  196. if (isset($match[1]) && isset($match[2]) && $match[2] !== './') {
  197. $this->branches[rtrim($match[2], '/')] = $this->buildIdentifier(
  198. '/' . $this->branchesPath . '/' . $match[2],
  199. $match[1]
  200. );
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. return $this->branches;
  208. }
  209. /**
  210. * {@inheritDoc}
  211. */
  212. public static function supports(IOInterface $io, $url, $deep = false)
  213. {
  214. $url = self::normalizeUrl($url);
  215. if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) {
  216. return true;
  217. }
  218. // proceed with deep check for local urls since they are fast to process
  219. if (!$deep && !static::isLocalUrl($url)) {
  220. return false;
  221. }
  222. $processExecutor = new ProcessExecutor();
  223. $exit = $processExecutor->execute(
  224. "svn info --non-interactive {$url}",
  225. $ignoredOutput
  226. );
  227. if ($exit === 0) {
  228. // This is definitely a Subversion repository.
  229. return true;
  230. }
  231. if (false !== stripos($processExecutor->getErrorOutput(), 'authorization failed:')) {
  232. // This is likely a remote Subversion repository that requires
  233. // authentication. We will handle actual authentication later.
  234. return true;
  235. }
  236. return false;
  237. }
  238. /**
  239. * An absolute path (leading '/') is converted to a file:// url.
  240. *
  241. * @param string $url
  242. *
  243. * @return string
  244. */
  245. protected static function normalizeUrl($url)
  246. {
  247. $fs = new Filesystem();
  248. if ($fs->isAbsolutePath($url)) {
  249. return 'file://' . strtr($url, '\\', '/');
  250. }
  251. return $url;
  252. }
  253. /**
  254. * Execute an SVN command and try to fix up the process with credentials
  255. * if necessary.
  256. *
  257. * @param string $command The svn command to run.
  258. * @param string $url The SVN URL.
  259. * @throws \RuntimeException
  260. * @return string
  261. */
  262. protected function execute($command, $url)
  263. {
  264. if (null === $this->util) {
  265. $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process);
  266. }
  267. try {
  268. return $this->util->execute($command, $url);
  269. } catch (\RuntimeException $e) {
  270. if (0 !== $this->process->execute('svn --version', $ignoredOutput)) {
  271. 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());
  272. }
  273. throw new \RuntimeException(
  274. 'Repository '.$this->url.' could not be processed, '.$e->getMessage()
  275. );
  276. }
  277. }
  278. /**
  279. * Build the identifier respecting "package-path" config option
  280. *
  281. * @param string $baseDir The path to trunk/branch/tag
  282. * @param int $revision The revision mark to add to identifier
  283. *
  284. * @return string
  285. */
  286. protected function buildIdentifier($baseDir, $revision)
  287. {
  288. return rtrim($baseDir, '/') . $this->packagePath . '/@' . $revision;
  289. }
  290. }