HgDriver.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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', '-', $this->url) . '/';
  30. if (is_dir($this->tmpDir)) {
  31. $this->process->execute(sprintf('cd %s && hg pull -u', escapeshellarg($this->tmpDir)), $output);
  32. } else {
  33. $dir = dirname($this->tmpDir);
  34. if (!is_dir($dir)) {
  35. mkdir($dir, 0777, true);
  36. }
  37. $this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg($dir), escapeshellarg($this->url), escapeshellarg($this->tmpDir)), $output);
  38. }
  39. $this->getTags();
  40. $this->getBranches();
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function getRootIdentifier()
  46. {
  47. $tmpDir = escapeshellarg($this->tmpDir);
  48. if (null === $this->rootIdentifier) {
  49. $this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
  50. $output = $this->process->splitLines($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. $this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
  84. if (!trim($composer)) {
  85. return;
  86. }
  87. $composer = JsonFile::parseJson($composer);
  88. if (!isset($composer['time'])) {
  89. $this->process->execute(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  90. $date = new \DateTime(trim($output));
  91. $composer['time'] = $date->format('Y-m-d H:i:s');
  92. }
  93. $this->infoCache[$identifier] = $composer;
  94. }
  95. return $this->infoCache[$identifier];
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function getTags()
  101. {
  102. if (null === $this->tags) {
  103. $tags = array();
  104. $this->process->execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
  105. foreach ($this->process->splitLines($output) as $tag) {
  106. if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
  107. $tags[$match[1]] = $match[2];
  108. }
  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. $this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output);
  123. foreach ($this->process->splitLines($output) as $branch) {
  124. if ($branch && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
  125. $branches[$match[1]] = $match[2];
  126. }
  127. }
  128. $this->branches = $branches;
  129. }
  130. return $this->branches;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public static function supports(IOInterface $io, $url, $deep = false)
  136. {
  137. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) {
  138. return true;
  139. }
  140. if (!$deep) {
  141. return false;
  142. }
  143. $processExecutor = new ProcessExecutor();
  144. $exit = $processExecutor->execute(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored);
  145. return $exit === 0;
  146. }
  147. }