HgDriver.php 6.9 KB

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