GitDriver.php 4.7 KB

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