HgDriver.php 6.6 KB

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