HgDriver.php 5.2 KB

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