HgDriver.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Config;
  13. use Composer\Json\JsonFile;
  14. use Composer\Util\ProcessExecutor;
  15. use Composer\Util\Filesystem;
  16. use Composer\IO\IOInterface;
  17. use Composer\Downloader\TransportException;
  18. /**
  19. * @author Per Bernhardt <plb@webfactory.de>
  20. */
  21. class HgDriver extends VcsDriver
  22. {
  23. protected $tags;
  24. protected $branches;
  25. protected $rootIdentifier;
  26. protected $repoDir;
  27. protected $infoCache = array();
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function initialize()
  32. {
  33. if (Filesystem::isLocalPath($this->url)) {
  34. $this->repoDir = $this->url;
  35. } else {
  36. $cacheDir = $this->config->get('cache-vcs-dir');
  37. $this->repoDir = $cacheDir . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/';
  38. $fs = new Filesystem();
  39. $fs->ensureDirectoryExists($cacheDir);
  40. if (!is_writable(dirname($this->repoDir))) {
  41. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.$cacheDir.'" directory is not writable by the current user.');
  42. }
  43. // Ensure we are allowed to use this URL by config
  44. $this->config->prohibitUrlByConfig($this->url);
  45. // update the repo if it is a valid hg repository
  46. if (is_dir($this->repoDir) && 0 === $this->process->execute('hg summary', $output, $this->repoDir)) {
  47. if (0 !== $this->process->execute('hg pull', $output, $this->repoDir)) {
  48. $this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  49. }
  50. } else {
  51. // clean up directory and do a fresh clone into it
  52. $fs->removeDirectory($this->repoDir);
  53. if (0 !== $this->process->execute(sprintf('hg clone --noupdate %s %s', ProcessExecutor::escape($this->url), ProcessExecutor::escape($this->repoDir)), $output, $cacheDir)) {
  54. $output = $this->process->getErrorOutput();
  55. if (0 !== $this->process->execute('hg --version', $ignoredOutput)) {
  56. throw new \RuntimeException('Failed to clone '.$this->url.', hg was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  57. }
  58. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output);
  59. }
  60. }
  61. }
  62. $this->getTags();
  63. $this->getBranches();
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getRootIdentifier()
  69. {
  70. if (null === $this->rootIdentifier) {
  71. $this->process->execute(sprintf('hg tip --template "{node}"'), $output, $this->repoDir);
  72. $output = $this->process->splitLines($output);
  73. $this->rootIdentifier = $output[0];
  74. }
  75. return $this->rootIdentifier;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function getUrl()
  81. {
  82. return $this->url;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getSource($identifier)
  88. {
  89. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $identifier);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function getDist($identifier)
  95. {
  96. return null;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function getComposerInformation($identifier)
  102. {
  103. if (!isset($this->infoCache[$identifier])) {
  104. $this->process->execute(sprintf('hg cat -r %s composer.json', ProcessExecutor::escape($identifier)), $composer, $this->repoDir);
  105. if (!trim($composer)) {
  106. return;
  107. }
  108. $composer = JsonFile::parseJson($composer, $identifier);
  109. if (empty($composer['time'])) {
  110. $this->process->execute(sprintf('hg log --template "{date|rfc3339date}" -r %s', ProcessExecutor::escape($identifier)), $output, $this->repoDir);
  111. $date = new \DateTime(trim($output), new \DateTimeZone('UTC'));
  112. $composer['time'] = $date->format('Y-m-d H:i:s');
  113. }
  114. $this->infoCache[$identifier] = $composer;
  115. }
  116. return $this->infoCache[$identifier];
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getTags()
  122. {
  123. if (null === $this->tags) {
  124. $tags = array();
  125. $this->process->execute('hg tags', $output, $this->repoDir);
  126. foreach ($this->process->splitLines($output) as $tag) {
  127. if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
  128. $tags[$match[1]] = $match[2];
  129. }
  130. }
  131. unset($tags['tip']);
  132. $this->tags = $tags;
  133. }
  134. return $this->tags;
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public function getBranches()
  140. {
  141. if (null === $this->branches) {
  142. $branches = array();
  143. $bookmarks = array();
  144. $this->process->execute('hg branches', $output, $this->repoDir);
  145. foreach ($this->process->splitLines($output) as $branch) {
  146. if ($branch && preg_match('(^([^\s]+)\s+\d+:([a-f0-9]+))', $branch, $match)) {
  147. $branches[$match[1]] = $match[2];
  148. }
  149. }
  150. $this->process->execute('hg bookmarks', $output, $this->repoDir);
  151. foreach ($this->process->splitLines($output) as $branch) {
  152. if ($branch && preg_match('(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
  153. $bookmarks[$match[1]] = $match[2];
  154. }
  155. }
  156. // Branches will have preference over bookmarks
  157. $this->branches = array_merge($bookmarks, $branches);
  158. }
  159. return $this->branches;
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  165. {
  166. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) {
  167. return true;
  168. }
  169. // local filesystem
  170. if (Filesystem::isLocalPath($url)) {
  171. $url = Filesystem::getPlatformPath($url);
  172. if (!is_dir($url)) {
  173. return false;
  174. }
  175. $process = new ProcessExecutor();
  176. // check whether there is a hg repo in that path
  177. if ($process->execute('hg summary', $output, $url) === 0) {
  178. return true;
  179. }
  180. }
  181. if (!$deep) {
  182. return false;
  183. }
  184. $processExecutor = new ProcessExecutor();
  185. $exit = $processExecutor->execute(sprintf('hg identify %s', ProcessExecutor::escape($url)), $ignored);
  186. return $exit === 0;
  187. }
  188. }