GitDriver.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Composer\Repository\Vcs;
  3. use Composer\Json\JsonFile;
  4. use Composer\IO\IOInterface;
  5. /**
  6. * @author Jordi Boggiano <j.boggiano@seld.be>
  7. */
  8. class GitDriver extends VcsDriver implements VcsDriverInterface
  9. {
  10. protected $tags;
  11. protected $branches;
  12. protected $rootIdentifier;
  13. protected $infoCache = array();
  14. public function __construct($url, IOInterface $io)
  15. {
  16. $this->tmpDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
  17. parent::__construct($url, $io);
  18. }
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function initialize()
  23. {
  24. $url = escapeshellarg($this->url);
  25. $tmpDir = escapeshellarg($this->tmpDir);
  26. if (is_dir($this->tmpDir)) {
  27. exec(sprintf('cd %s && git fetch origin', $tmpDir), $output);
  28. } else {
  29. exec(sprintf('git clone %s %s', $url, $tmpDir), $output);
  30. }
  31. $this->getTags();
  32. $this->getBranches();
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function getRootIdentifier()
  38. {
  39. if (null === $this->rootIdentifier) {
  40. $this->rootIdentifier = 'master';
  41. exec(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output);
  42. foreach ($output as $branch) {
  43. if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) {
  44. $this->rootIdentifier = $match[1];
  45. break;
  46. }
  47. }
  48. }
  49. return $this->rootIdentifier;
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function getUrl()
  55. {
  56. return $this->url;
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public function getSource($identifier)
  62. {
  63. $label = array_search($identifier, (array) $this->tags) ?: $identifier;
  64. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label);
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getDist($identifier)
  70. {
  71. return null;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getComposerInformation($identifier)
  77. {
  78. if (!isset($this->infoCache[$identifier])) {
  79. exec(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  80. $composer = implode("\n", $output);
  81. unset($output);
  82. if (!$composer) {
  83. throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
  84. }
  85. $composer = JsonFile::parseJson($composer);
  86. if (!isset($composer['time'])) {
  87. exec(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
  88. $date = new \DateTime('@'.$output[0]);
  89. $composer['time'] = $date->format('Y-m-d H:i:s');
  90. }
  91. $this->infoCache[$identifier] = $composer;
  92. }
  93. return $this->infoCache[$identifier];
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getTags()
  99. {
  100. if (null === $this->tags) {
  101. exec(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output);
  102. $this->tags = $output ? array_combine($output, $output) : array();
  103. }
  104. return $this->tags;
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function getBranches()
  110. {
  111. if (null === $this->branches) {
  112. $branches = array();
  113. exec(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output);
  114. foreach ($output as $branch) {
  115. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  116. preg_match('{^ *[^/]+/(\S+) *([a-f0-9]+) .*$}', $branch, $match);
  117. $branches[$match[1]] = $match[2];
  118. }
  119. }
  120. $this->branches = $branches;
  121. }
  122. return $this->branches;
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. public function hasComposerFile($identifier)
  128. {
  129. try {
  130. $this->getComposerInformation($identifier);
  131. return true;
  132. } catch (\Exception $e) {
  133. }
  134. return false;
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public static function supports($url, $deep = false)
  140. {
  141. if (preg_match('#(^git://|\.git$|git@|//git\.)#i', $url)) {
  142. return true;
  143. }
  144. if (!$deep) {
  145. return false;
  146. }
  147. // TODO try to connect to the server
  148. return false;
  149. }
  150. }