HgDriver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\IO\IOInterface;
  15. /**
  16. * @author Per Bernhardt <plb@webfactory.de>
  17. */
  18. class HgDriver extends VcsDriver
  19. {
  20. protected $tags;
  21. protected $branches;
  22. protected $rootIdentifier;
  23. protected $infoCache = array();
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function initialize()
  28. {
  29. $this->tmpDir = $this->config->get('home') . '/cache.hg/' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
  30. $url = escapeshellarg($this->url);
  31. if (is_dir($this->tmpDir)) {
  32. $this->process->execute(sprintf('cd %s && hg pull -u', escapeshellarg($this->tmpDir)), $output);
  33. } else {
  34. $this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg(dirname($this->tmpDir)), $url, escapeshellarg($this->tmpDir)), $output);
  35. }
  36. $this->getTags();
  37. $this->getBranches();
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getRootIdentifier()
  43. {
  44. $tmpDir = escapeshellarg($this->tmpDir);
  45. if (null === $this->rootIdentifier) {
  46. $this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
  47. $output = $this->process->splitLines($output);
  48. $this->rootIdentifier = $output[0];
  49. }
  50. return $this->rootIdentifier;
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function getUrl()
  56. {
  57. return $this->url;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function getSource($identifier)
  63. {
  64. $label = array_search($identifier, (array)$this->tags) ? : $identifier;
  65. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label);
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function getDist($identifier)
  71. {
  72. return null;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getComposerInformation($identifier)
  78. {
  79. if (!isset($this->infoCache[$identifier])) {
  80. $this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
  81. if (!trim($composer)) {
  82. return;
  83. }
  84. $composer = JsonFile::parseJson($composer);
  85. if (!isset($composer['time'])) {
  86. $this->process->execute(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  87. $date = new \DateTime(trim($output));
  88. $composer['time'] = $date->format('Y-m-d H:i:s');
  89. }
  90. $this->infoCache[$identifier] = $composer;
  91. }
  92. return $this->infoCache[$identifier];
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getTags()
  98. {
  99. if (null === $this->tags) {
  100. $tags = array();
  101. $this->process->execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
  102. foreach ($this->process->splitLines($output) as $tag) {
  103. if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
  104. $tags[$match[1]] = $match[2];
  105. }
  106. }
  107. unset($tags['tip']);
  108. $this->tags = $tags;
  109. }
  110. return $this->tags;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function getBranches()
  116. {
  117. if (null === $this->branches) {
  118. $branches = array();
  119. $this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output);
  120. foreach ($this->process->splitLines($output) as $branch) {
  121. if ($branch && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
  122. $branches[$match[1]] = $match[2];
  123. }
  124. }
  125. $this->branches = $branches;
  126. }
  127. return $this->branches;
  128. }
  129. /**
  130. * {@inheritDoc}
  131. */
  132. public static function supports(IOInterface $io, $url, $deep = false)
  133. {
  134. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) {
  135. return true;
  136. }
  137. if (!$deep) {
  138. return false;
  139. }
  140. $processExecutor = new ProcessExecutor();
  141. $exit = $processExecutor->execute(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored);
  142. return $exit === 0;
  143. }
  144. }