SvnDriver.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. $identifier = '/' . trim($identifier, '/') . '/';
  107. if ($res = $this->cache->read($identifier.'.json')) {
  108. $this->infoCache[$identifier] = JsonFile::parseJson($res);
  109. }
  110. if (!isset($this->infoCache[$identifier])) {
  111. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  112. if (!empty($match[2])) {
  113. $path = $match[1];
  114. $rev = $match[2];
  115. } else {
  116. $path = $identifier;
  117. $rev = '';
  118. }
  119. try {
  120. $resource = $path.'composer.json';
  121. $output = $this->execute('svn cat', $this->baseUrl . $resource . $rev);
  122. if (!trim($output)) {
  123. return;
  124. }
  125. } catch (\RuntimeException $e) {
  126. throw new TransportException($e->getMessage());
  127. }
  128. $composer = JsonFile::parseJson($output, $this->baseUrl . $resource . $rev);
  129. if (empty($composer['time'])) {
  130. $output = $this->execute('svn info', $this->baseUrl . $path . $rev);
  131. foreach ($this->process->splitLines($output) as $line) {
  132. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  133. $date = new \DateTime($match[1], new \DateTimeZone('UTC'));
  134. $composer['time'] = $date->format('Y-m-d H:i:s');
  135. break;
  136. }
  137. }
  138. }
  139. $this->cache->write($identifier.'.json', json_encode($composer));
  140. $this->infoCache[$identifier] = $composer;
  141. }
  142. return $this->infoCache[$identifier];
  143. }
  144. /**
  145. * @param string $file
  146. * @param string $identifier
  147. */
  148. public function getFileContent($file, $identifier)
  149. {
  150. $identifier = '/' . trim($identifier, '/') . '/';
  151. if ($res = $this->cache->read($identifier . ':' . $file)) {
  152. return $res;
  153. }
  154. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  155. if (!empty($match[2])) {
  156. $path = $match[1];
  157. $rev = $match[2];
  158. } else {
  159. $path = $identifier;
  160. $rev = '';
  161. }
  162. try {
  163. $resource = $path.$file;
  164. $output = $this->execute('svn cat', $this->baseUrl . $resource . $rev);
  165. if (!trim($output)) {
  166. return null;
  167. }
  168. } catch (\RuntimeException $e) {
  169. throw new TransportException($e->getMessage());
  170. }
  171. $this->cache->write($identifier . ':' . $file, $output);
  172. return $output;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getChangeDate($identifier) {
  178. $identifier = '/' . trim($identifier, '/') . '/';
  179. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  180. if (!empty($match[2])) {
  181. $path = $match[1];
  182. $rev = $match[2];
  183. } else {
  184. $path = $identifier;
  185. $rev = '';
  186. }
  187. $output = $this->execute('svn info', $this->baseUrl . $path . $rev);
  188. foreach ($this->process->splitLines($output) as $line) {
  189. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  190. return new \DateTime($match[1], new \DateTimeZone('UTC'));
  191. }
  192. }
  193. }
  194. /**
  195. * {@inheritDoc}
  196. */
  197. public function getTags()
  198. {
  199. if (null === $this->tags) {
  200. $this->tags = array();
  201. if ($this->tagsPath !== false) {
  202. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath);
  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->tags[rtrim($match[2], '/')] = $this->buildIdentifier(
  209. '/' . $this->tagsPath . '/' . $match[2],
  210. $match[1]
  211. );
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. return $this->tags;
  219. }
  220. /**
  221. * {@inheritDoc}
  222. */
  223. public function getBranches()
  224. {
  225. if (null === $this->branches) {
  226. $this->branches = array();
  227. if (false === $this->trunkPath) {
  228. $trunkParent = $this->baseUrl . '/';
  229. } else {
  230. $trunkParent = $this->baseUrl . '/' . $this->trunkPath;
  231. }
  232. $output = $this->execute('svn ls --verbose', $trunkParent);
  233. if ($output) {
  234. foreach ($this->process->splitLines($output) as $line) {
  235. $line = trim($line);
  236. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  237. if (isset($match[1]) && isset($match[2]) && $match[2] === './') {
  238. $this->branches['trunk'] = $this->buildIdentifier(
  239. '/' . $this->trunkPath,
  240. $match[1]
  241. );
  242. $this->rootIdentifier = $this->branches['trunk'];
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. unset($output);
  249. if ($this->branchesPath !== false) {
  250. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->branchesPath);
  251. if ($output) {
  252. foreach ($this->process->splitLines(trim($output)) as $line) {
  253. $line = trim($line);
  254. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  255. if (isset($match[1]) && isset($match[2]) && $match[2] !== './') {
  256. $this->branches[rtrim($match[2], '/')] = $this->buildIdentifier(
  257. '/' . $this->branchesPath . '/' . $match[2],
  258. $match[1]
  259. );
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. return $this->branches;
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  272. {
  273. $url = self::normalizeUrl($url);
  274. if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) {
  275. return true;
  276. }
  277. // proceed with deep check for local urls since they are fast to process
  278. if (!$deep && !Filesystem::isLocalPath($url)) {
  279. return false;
  280. }
  281. $processExecutor = new ProcessExecutor();
  282. $exit = $processExecutor->execute(
  283. "svn info --non-interactive {$url}",
  284. $ignoredOutput
  285. );
  286. if ($exit === 0) {
  287. // This is definitely a Subversion repository.
  288. return true;
  289. }
  290. if (false !== stripos($processExecutor->getErrorOutput(), 'authorization failed:')) {
  291. // This is likely a remote Subversion repository that requires
  292. // authentication. We will handle actual authentication later.
  293. return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * An absolute path (leading '/') is converted to a file:// url.
  299. *
  300. * @param string $url
  301. *
  302. * @return string
  303. */
  304. protected static function normalizeUrl($url)
  305. {
  306. $fs = new Filesystem();
  307. if ($fs->isAbsolutePath($url)) {
  308. return 'file://' . strtr($url, '\\', '/');
  309. }
  310. return $url;
  311. }
  312. /**
  313. * Execute an SVN command and try to fix up the process with credentials
  314. * if necessary.
  315. *
  316. * @param string $command The svn command to run.
  317. * @param string $url The SVN URL.
  318. * @throws \RuntimeException
  319. * @return string
  320. */
  321. protected function execute($command, $url)
  322. {
  323. if (null === $this->util) {
  324. $this->util = new SvnUtil($this->baseUrl, $this->io, $this->config, $this->process);
  325. $this->util->setCacheCredentials($this->cacheCredentials);
  326. }
  327. try {
  328. return $this->util->execute($command, $url);
  329. } catch (\RuntimeException $e) {
  330. if (0 !== $this->process->execute('svn --version', $ignoredOutput)) {
  331. 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());
  332. }
  333. throw new \RuntimeException(
  334. 'Repository '.$this->url.' could not be processed, '.$e->getMessage()
  335. );
  336. }
  337. }
  338. /**
  339. * Build the identifier respecting "package-path" config option
  340. *
  341. * @param string $baseDir The path to trunk/branch/tag
  342. * @param int $revision The revision mark to add to identifier
  343. *
  344. * @return string
  345. */
  346. protected function buildIdentifier($baseDir, $revision)
  347. {
  348. return rtrim($baseDir, '/') . $this->packagePath . '/@' . $revision;
  349. }
  350. }