SvnDriver.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 $modulePath = '/';
  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['module-path'])) {
  55. $this->modulePath = '/' . trim($this->repoConfig['module-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->buildModuleId(
  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. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/');
  169. if ($output) {
  170. foreach ($this->process->splitLines($output) as $line) {
  171. $line = trim($line);
  172. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  173. if (isset($match[1]) && isset($match[2]) && $match[2] === $this->trunkPath . '/') {
  174. $this->branches[$this->trunkPath] = $this->buildModuleId(
  175. '/' . $this->trunkPath,
  176. $match[1]
  177. );
  178. $this->rootIdentifier = $this->branches[$this->trunkPath];
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. unset($output);
  185. if ($this->branchesPath !== false) {
  186. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->branchesPath);
  187. if ($output) {
  188. foreach ($this->process->splitLines(trim($output)) as $line) {
  189. $line = trim($line);
  190. if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
  191. if (isset($match[1]) && isset($match[2]) && $match[2] !== './') {
  192. $this->branches[rtrim($match[2], '/')] = $this->buildModuleId(
  193. '/' . $this->branchesPath . '/' . $match[2],
  194. $match[1]
  195. );
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. return $this->branches;
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. public static function supports(IOInterface $io, $url, $deep = false)
  208. {
  209. $url = self::normalizeUrl($url);
  210. if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) {
  211. return true;
  212. }
  213. // proceed with deep check for local urls since they are fast to process
  214. if (!$deep && !static::isLocalUrl($url)) {
  215. return false;
  216. }
  217. $processExecutor = new ProcessExecutor();
  218. $exit = $processExecutor->execute(
  219. "svn info --non-interactive {$url}",
  220. $ignoredOutput
  221. );
  222. if ($exit === 0) {
  223. // This is definitely a Subversion repository.
  224. return true;
  225. }
  226. if (false !== stripos($processExecutor->getErrorOutput(), 'authorization failed:')) {
  227. // This is likely a remote Subversion repository that requires
  228. // authentication. We will handle actual authentication later.
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * An absolute path (leading '/') is converted to a file:// url.
  235. *
  236. * @param string $url
  237. *
  238. * @return string
  239. */
  240. protected static function normalizeUrl($url)
  241. {
  242. $fs = new Filesystem();
  243. if ($fs->isAbsolutePath($url)) {
  244. return 'file://' . strtr($url, '\\', '/');
  245. }
  246. return $url;
  247. }
  248. /**
  249. * Execute an SVN command and try to fix up the process with credentials
  250. * if necessary.
  251. *
  252. * @param string $command The svn command to run.
  253. * @param string $url The SVN URL.
  254. *
  255. * @return string
  256. */
  257. protected function execute($command, $url)
  258. {
  259. if (null === $this->util) {
  260. $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process);
  261. }
  262. try {
  263. return $this->util->execute($command, $url);
  264. } catch (\RuntimeException $e) {
  265. if (0 !== $this->process->execute('svn --version', $ignoredOutput)) {
  266. 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());
  267. }
  268. throw new \RuntimeException(
  269. 'Repository '.$this->url.' could not be processed, '.$e->getMessage()
  270. );
  271. }
  272. }
  273. /**
  274. * Build module identifier respecting the module-path config option
  275. *
  276. * @param string $baseDir The path to trunk/branch/tag
  277. * @param int $revision The revision mark to add to identifier
  278. *
  279. * @return string
  280. */
  281. protected function buildModuleId($baseDir, $revision)
  282. {
  283. return rtrim($baseDir, '/') . $this->modulePath . '/@' . $revision;
  284. }
  285. }