SvnDriver.php 11 KB

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