SvnDriver.php 10 KB

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