HgDriver.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\Json\JsonFile;
  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 (static::isLocalUrl($this->url)) {
  32. $this->repoDir = str_replace('file://', '', $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. // update the repo if it is a valid hg repository
  42. if (is_dir($this->repoDir) && 0 === $this->process->execute('hg summary', $output, $this->repoDir)) {
  43. if (0 !== $this->process->execute('hg pull -u', $output, $this->repoDir)) {
  44. $this->io->write('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  45. }
  46. } else {
  47. // clean up directory and do a fresh clone into it
  48. $fs->removeDirectory($this->repoDir);
  49. if (0 !== $this->process->execute(sprintf('hg clone %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir)), $output, $cacheDir)) {
  50. $output = $this->process->getErrorOutput();
  51. if (0 !== $this->process->execute('hg --version', $ignoredOutput)) {
  52. 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());
  53. }
  54. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output);
  55. }
  56. }
  57. }
  58. $this->getTags();
  59. $this->getBranches();
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getRootIdentifier()
  65. {
  66. if (null === $this->rootIdentifier) {
  67. $this->process->execute(sprintf('hg tip --template "{node}"'), $output, $this->repoDir);
  68. $output = $this->process->splitLines($output);
  69. $this->rootIdentifier = $output[0];
  70. }
  71. return $this->rootIdentifier;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getUrl()
  77. {
  78. return $this->url;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function getSource($identifier)
  84. {
  85. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $identifier);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getDist($identifier)
  91. {
  92. return null;
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getComposerInformation($identifier)
  98. {
  99. if (!isset($this->infoCache[$identifier])) {
  100. $this->process->execute(sprintf('hg cat -r %s composer.json', escapeshellarg($identifier)), $composer, $this->repoDir);
  101. if (!trim($composer)) {
  102. return;
  103. }
  104. $composer = JsonFile::parseJson($composer, $identifier);
  105. if (!isset($composer['time'])) {
  106. $this->process->execute(sprintf('hg log --template "{date|rfc3339date}" -r %s', escapeshellarg($identifier)), $output, $this->repoDir);
  107. $date = new \DateTime(trim($output), new \DateTimeZone('UTC'));
  108. $composer['time'] = $date->format('Y-m-d H:i:s');
  109. }
  110. $this->infoCache[$identifier] = $composer;
  111. }
  112. return $this->infoCache[$identifier];
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function getTags()
  118. {
  119. if (null === $this->tags) {
  120. $tags = array();
  121. $this->process->execute('hg tags', $output, $this->repoDir);
  122. foreach ($this->process->splitLines($output) as $tag) {
  123. if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
  124. $tags[$match[1]] = $match[2];
  125. }
  126. }
  127. unset($tags['tip']);
  128. $this->tags = $tags;
  129. }
  130. return $this->tags;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getBranches()
  136. {
  137. if (null === $this->branches) {
  138. $branches = array();
  139. $bookmarks = array();
  140. $this->process->execute('hg branches', $output, $this->repoDir);
  141. foreach ($this->process->splitLines($output) as $branch) {
  142. if ($branch && preg_match('(^([^\s]+)\s+\d+:([a-f0-9]+))', $branch, $match)) {
  143. $branches[$match[1]] = $match[2];
  144. }
  145. }
  146. $this->process->execute('hg bookmarks', $output, $this->repoDir);
  147. foreach ($this->process->splitLines($output) as $branch) {
  148. if ($branch && preg_match('(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
  149. $bookmarks[$match[1]] = $match[2];
  150. }
  151. }
  152. // Branches will have preference over bookmarks
  153. $this->branches = array_merge($bookmarks, $branches);
  154. }
  155. return $this->branches;
  156. }
  157. /**
  158. * {@inheritDoc}
  159. */
  160. public static function supports(IOInterface $io, $url, $deep = false)
  161. {
  162. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) {
  163. return true;
  164. }
  165. // local filesystem
  166. if (static::isLocalUrl($url)) {
  167. if (!is_dir($url)) {
  168. throw new \RuntimeException('Directory does not exist: '.$url);
  169. }
  170. $process = new ProcessExecutor();
  171. $url = str_replace('file://', '', $url);
  172. // check whether there is a hg repo in that path
  173. if ($process->execute('hg summary', $output, $url) === 0) {
  174. return true;
  175. }
  176. }
  177. if (!$deep) {
  178. return false;
  179. }
  180. $processExecutor = new ProcessExecutor();
  181. $exit = $processExecutor->execute(sprintf('hg identify %s', escapeshellarg($url)), $ignored);
  182. return $exit === 0;
  183. }
  184. }