SvnDriver.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace Composer\Repository\Vcs;
  3. use Composer\Json\JsonFile;
  4. use Composer\Util\ProcessExecutor;
  5. use Composer\Util\Svn as SvnUtil;
  6. use Composer\IO\IOInterface;
  7. /**
  8. * @author Jordi Boggiano <j.boggiano@seld.be>
  9. */
  10. class SvnDriver extends VcsDriver
  11. {
  12. protected $baseUrl;
  13. protected $tags;
  14. protected $branches;
  15. protected $infoCache = array();
  16. /**
  17. * @var boolean $useAuth Contains credentials, or not?
  18. */
  19. protected $useAuth = false;
  20. /**
  21. * @var boolean $useCache To determine if we should cache the credentials
  22. * supplied by the user. By default: no cache.
  23. * @see self::getSvnAuthCache()
  24. */
  25. protected $useCache = false;
  26. /**
  27. * @var string $svnUsername
  28. */
  29. protected $svnUsername = '';
  30. /**
  31. * @var string $svnPassword
  32. */
  33. protected $svnPassword = '';
  34. /**
  35. * @var Composer\Util\Svn $util
  36. */
  37. protected $util;
  38. /**
  39. * __construct
  40. *
  41. * @param string $url
  42. * @param IOInterface $io
  43. * @param ProcessExecutor $process
  44. *
  45. * @return $this
  46. * @uses self::detectSvnAuth()
  47. */
  48. public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
  49. {
  50. $url = self::fixSvnUrl($url);
  51. parent::__construct($this->baseUrl = rtrim($url, '/'), $io, $process);
  52. if (false !== ($pos = strrpos($url, '/trunk'))) {
  53. $this->baseUrl = substr($url, 0, $pos);
  54. }
  55. $this->util = new SvnUtil($this->baseUrl, $io);
  56. $this->useAuth = $this->util->hasAuth();
  57. }
  58. /**
  59. * Execute an SVN command and try to fix up the process with credentials
  60. * if necessary. The command is 'fixed up' with {@link self::getSvnCommand()}.
  61. *
  62. * @param string $command The svn command to run.
  63. * @param string $url The SVN URL.
  64. *
  65. * @return string
  66. * @uses Composer\Util\Svn::getCommand()
  67. * @uses parent::$process
  68. * @see ProcessExecutor::execute()
  69. */
  70. public function execute($command, $url)
  71. {
  72. $svnCommand = $this->util->getCommand($command, $url);
  73. $status = $this->process->execute(
  74. $svnCommand,
  75. $output
  76. );
  77. if ($status == 0) {
  78. return $output;
  79. }
  80. // this could be any failure, since SVN exits with 1 always
  81. if (!$this->io->isInteractive()) {
  82. return $output;
  83. }
  84. // the error is not auth-related
  85. if (strpos($output, 'authorization failed:') === false) {
  86. return $output;
  87. }
  88. // no authorization has been detected so far
  89. if (!$this->useAuth) {
  90. $this->useAuth = $this->util->doAuthDance()->hasAuth();
  91. // restart the process
  92. $output = $this->execute($command, $url);
  93. } else {
  94. $this->io->write("Authorization failed: {$svnCommand}");
  95. }
  96. return $output;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function initialize()
  102. {
  103. $this->getBranches();
  104. $this->getTags();
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function getRootIdentifier()
  110. {
  111. return 'trunk';
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function getUrl()
  117. {
  118. return $this->url;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getSource($identifier)
  124. {
  125. return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getDist($identifier)
  131. {
  132. return null;
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public function getComposerInformation($identifier)
  138. {
  139. $identifier = '/' . trim($identifier, '/') . '/';
  140. if (!isset($this->infoCache[$identifier])) {
  141. preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
  142. if (!empty($match[2])) {
  143. $identifier = $match[1];
  144. $rev = $match[2];
  145. } else {
  146. $rev = '';
  147. }
  148. $output = $this->execute('svn cat', $this->baseUrl . $identifier . 'composer.json' . $rev);
  149. if (!trim($output)) {
  150. return;
  151. }
  152. $composer = JsonFile::parseJson($output);
  153. if (!isset($composer['time'])) {
  154. $output = $this->execute('svn info', $this->baseUrl . $identifier . $rev);
  155. foreach ($this->process->splitLines($output) as $line) {
  156. if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
  157. $date = new \DateTime($match[1]);
  158. $composer['time'] = $date->format('Y-m-d H:i:s');
  159. break;
  160. }
  161. }
  162. }
  163. $this->infoCache[$identifier] = $composer;
  164. }
  165. return $this->infoCache[$identifier];
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function getTags()
  171. {
  172. if (null === $this->tags) {
  173. $output = $this->execute('svn ls', $this->baseUrl . '/tags');
  174. $this->tags = array();
  175. foreach ($this->process->splitLines($output) as $tag) {
  176. if ($tag) {
  177. $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
  178. }
  179. }
  180. }
  181. return $this->tags;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function getBranches()
  187. {
  188. if (null === $this->branches) {
  189. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/');
  190. $this->branches = array();
  191. foreach ($this->process->splitLines($output) as $line) {
  192. preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
  193. if ($match[2] === 'trunk/') {
  194. $this->branches['trunk'] = '/trunk/@'.$match[1];
  195. break;
  196. }
  197. }
  198. unset($output);
  199. $output = $this->execute('svn ls --verbose', $this->baseUrl . '/branches');
  200. foreach ($this->process->splitLines(trim($output)) as $line) {
  201. preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
  202. if ($match[2] === './') {
  203. continue;
  204. }
  205. $this->branches[rtrim($match[2], '/')] = '/branches/'.$match[2].'@'.$match[1];
  206. }
  207. }
  208. return $this->branches;
  209. }
  210. /**
  211. * {@inheritDoc}
  212. */
  213. public static function supports($url, $deep = false)
  214. {
  215. $url = self::fixSvnUrl($url);
  216. if (preg_match('#((^svn://)|(^svn\+ssh://)|(^file:///)|(^http)|(svn\.))#i', $url)) {
  217. return true;
  218. }
  219. if (!$deep) {
  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 (preg_match('/authorization failed/i', $processExecutor->getErrorOutput())) {
  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 fixSvnUrl($url)
  246. {
  247. if (strpos($url, '/', 0) === 0) {
  248. $url = 'file://' . $url;
  249. }
  250. return $url;
  251. }
  252. }