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\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. $this->repoDir = $this->config->get('home') . '/cache.hg/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/';
  35. $fs = new Filesystem();
  36. $fs->ensureDirectoryExists(dirname($this->repoDir));
  37. if (!is_writable(dirname($this->repoDir))) {
  38. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
  39. }
  40. // update the repo if it is a valid hg repository
  41. if (is_dir($this->repoDir) && 0 === $this->process->execute('hg summary', $output, $this->repoDir)) {
  42. if (0 !== $this->process->execute('hg pull -u', $output, $this->repoDir)) {
  43. $this->io->write('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  44. }
  45. } else {
  46. // clean up directory and do a fresh clone into it
  47. $fs->removeDirectory($this->repoDir);
  48. if (0 !== $this->process->execute(sprintf('hg clone %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir)), $output, $cacheDir)) {
  49. $output = $this->process->getErrorOutput();
  50. if (0 !== $this->process->execute('hg --version', $ignoredOutput)) {
  51. 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());
  52. }
  53. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output);
  54. }
  55. }
  56. }
  57. $this->getTags();
  58. $this->getBranches();
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function getRootIdentifier()
  64. {
  65. if (null === $this->rootIdentifier) {
  66. $this->process->execute(sprintf('hg tip --template "{node}"'), $output, $this->repoDir);
  67. $output = $this->process->splitLines($output);
  68. $this->rootIdentifier = $output[0];
  69. }
  70. return $this->rootIdentifier;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function getUrl()
  76. {
  77. return $this->url;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function getSource($identifier)
  83. {
  84. $label = array_search($identifier, (array) $this->tags) ? : $identifier;
  85. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label);
  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|rfc822date}" -r %s', escapeshellarg($identifier)), $output, $this->repoDir);
  107. $date = new \DateTime(trim($output));
  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+:(.*)$)', $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. }