GitDriver.php 5.6 KB

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