GitDriver.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\Util\Filesystem;
  15. use Composer\IO\IOInterface;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class GitDriver extends VcsDriver
  20. {
  21. protected $tags;
  22. protected $branches;
  23. protected $rootIdentifier;
  24. protected $repoDir;
  25. protected $infoCache = array();
  26. public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
  27. {
  28. parent::__construct($url, $io, $process);
  29. }
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function initialize()
  34. {
  35. if (static::isLocalUrl($this->url)) {
  36. $this->repoDir = $this->url;
  37. } else {
  38. $this->repoDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
  39. // update the repo if it is a valid git repository
  40. if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) {
  41. $this->process->execute('git remote update', $output, $this->repoDir);
  42. } else {
  43. // clean up directory and do a fresh clone into it
  44. $fs = new Filesystem();
  45. $fs->removeDirectory($this->repoDir);
  46. $command = sprintf('git clone --mirror %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir));
  47. if (0 !== $this->process->execute($command, $output)) {
  48. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it ('.$this->process->getErrorOutput().')');
  49. }
  50. }
  51. }
  52. $this->getTags();
  53. $this->getBranches();
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function getRootIdentifier()
  59. {
  60. if (null === $this->rootIdentifier) {
  61. $this->rootIdentifier = 'master';
  62. // select currently checked out branch if master is not available
  63. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  64. $branches = $this->process->splitLines($output);
  65. if (!in_array('* master', $branches)) {
  66. foreach ($branches as $branch) {
  67. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  68. $this->rootIdentifier = $match[1];
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. return $this->rootIdentifier;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function getUrl()
  80. {
  81. return $this->url;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function getSource($identifier)
  87. {
  88. $label = array_search($identifier, (array) $this->tags) ?: $identifier;
  89. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function getDist($identifier)
  95. {
  96. return null;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function getComposerInformation($identifier)
  102. {
  103. if (!isset($this->infoCache[$identifier])) {
  104. $this->process->execute(sprintf('git show %s:composer.json', escapeshellarg($identifier)), $composer, $this->repoDir);
  105. if (!trim($composer)) {
  106. return;
  107. }
  108. $composer = JsonFile::parseJson($composer);
  109. if (!isset($composer['time'])) {
  110. $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir);
  111. $date = new \DateTime('@'.trim($output));
  112. $composer['time'] = $date->format('Y-m-d H:i:s');
  113. }
  114. $this->infoCache[$identifier] = $composer;
  115. }
  116. return $this->infoCache[$identifier];
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getTags()
  122. {
  123. if (null === $this->tags) {
  124. $this->process->execute('git tag', $output, $this->repoDir);
  125. $output = $this->process->splitLines($output);
  126. $this->tags = $output ? array_combine($output, $output) : array();
  127. }
  128. return $this->tags;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public function getBranches()
  134. {
  135. if (null === $this->branches) {
  136. $branches = array();
  137. $this->process->execute('git branch --no-color --no-abbrev -v', $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(IOInterface $io, $url, $deep = false)
  152. {
  153. if (preg_match('#(^git://|\.git$|git@|//git\.|//github.com/)#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. }