HgDriver.php 6.9 KB

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