HgDriver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /**
  14. * @author Per Bernhardt <plb@webfactory.de>
  15. */
  16. class HgDriver implements VcsDriverInterface
  17. {
  18. protected $url;
  19. protected $tags;
  20. protected $branches;
  21. protected $rootIdentifier;
  22. protected $infoCache = array();
  23. public function __construct($url)
  24. {
  25. $this->url = $url;
  26. $this->tmpDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
  27. }
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function initialize()
  32. {
  33. $url = escapeshellarg($this->url);
  34. $tmpDir = escapeshellarg($this->tmpDir);
  35. if (is_dir($this->tmpDir)) {
  36. exec(sprintf('cd %s && hg pull -u', $tmpDir), $output);
  37. } else {
  38. exec(sprintf('hg clone %s %s', $url, $tmpDir), $output);
  39. }
  40. $this->getTags();
  41. $this->getBranches();
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getRootIdentifier()
  47. {
  48. $tmpDir = escapeshellarg($this->tmpDir);
  49. if (null === $this->rootIdentifier) {
  50. exec(sprintf('cd %s && hg tip --template "{rev}:{node|short}" --color never', $tmpDir), $output);
  51. $this->rootIdentifier = $output[0];
  52. }
  53. return $this->rootIdentifier;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function getUrl()
  59. {
  60. return $this->url;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function getSource($identifier)
  66. {
  67. $label = array_search($identifier, (array)$this->tags) ? : $identifier;
  68. return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label);
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function getDist($identifier)
  74. {
  75. return null;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function getComposerInformation($identifier)
  81. {
  82. if (!isset($this->infoCache[$identifier])) {
  83. exec(sprintf('cd %s && hg cat --color never -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  84. $composer = implode("\n", $output);
  85. unset($output);
  86. if (!$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. exec(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  92. $date = new \DateTime($output[0]);
  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. exec(sprintf('cd %s && hg tags --color never', escapeshellarg($this->tmpDir)), $output);
  106. foreach ($output as $tag) {
  107. preg_match('(^([^\s]+)[\s]+[\d+]:(.*)$)', $tag, $match);
  108. $tags[$match[1]] = $match[2];
  109. }
  110. unset($tags['tip']);
  111. $this->tags = $tags;
  112. }
  113. return $this->tags;
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function getBranches()
  119. {
  120. if (null === $this->branches) {
  121. $branches = array();
  122. exec(sprintf('cd %s && hg branches --color never', escapeshellarg($this->tmpDir)), $output);
  123. foreach ($output as $branch) {
  124. preg_match('(^([^\s]+)[\s]+[\d+]:(.*)$)', $branch, $match);
  125. $branches[$match[1]] = $match[2];
  126. }
  127. $this->branches = $branches;
  128. }
  129. return $this->branches;
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public function hasComposerFile($identifier)
  135. {
  136. try {
  137. $this->getComposerInformation($identifier);
  138. return true;
  139. } catch (\Exception $e) {
  140. }
  141. return false;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public static function supports($url, $deep = false)
  147. {
  148. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) {
  149. return true;
  150. }
  151. if (!$deep) {
  152. return false;
  153. }
  154. exec(sprintf('hg identify %s', escapeshellarg($url)), $output);
  155. return (boolean)$output;
  156. }
  157. }