GitDriver.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /**
  27. * {@inheritDoc}
  28. */
  29. public function initialize()
  30. {
  31. if (static::isLocalUrl($this->url)) {
  32. $this->repoDir = str_replace('file://', '', $this->url);
  33. } else {
  34. $this->repoDir = $this->config->get('home') . '/cache.git/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
  35. // update the repo if it is a valid git repository
  36. if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) {
  37. if (0 !== $this->process->execute('git remote update --prune origin', $output, $this->repoDir)) {
  38. $this->io->write('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  39. }
  40. } else {
  41. // clean up directory and do a fresh clone into it
  42. $fs = new Filesystem();
  43. $fs->removeDirectory($this->repoDir);
  44. $command = sprintf('git clone --mirror %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir));
  45. if (0 !== $this->process->execute($command, $output)) {
  46. $output = $this->process->getErrorOutput();
  47. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  48. throw new \RuntimeException('Failed to clone '.$this->url.', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  49. }
  50. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output);
  51. }
  52. }
  53. }
  54. $this->getTags();
  55. $this->getBranches();
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getRootIdentifier()
  61. {
  62. if (null === $this->rootIdentifier) {
  63. $this->rootIdentifier = 'master';
  64. // select currently checked out branch if master is not available
  65. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  66. $branches = $this->process->splitLines($output);
  67. if (!in_array('* master', $branches)) {
  68. foreach ($branches as $branch) {
  69. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  70. $this->rootIdentifier = $match[1];
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. return $this->rootIdentifier;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function getUrl()
  82. {
  83. return $this->url;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function getSource($identifier)
  89. {
  90. $label = array_search($identifier, (array) $this->tags) ?: $identifier;
  91. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getDist($identifier)
  97. {
  98. return null;
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function getComposerInformation($identifier)
  104. {
  105. if (!isset($this->infoCache[$identifier])) {
  106. $this->process->execute(sprintf('git show %s:composer.json', escapeshellarg($identifier)), $composer, $this->repoDir);
  107. if (!trim($composer)) {
  108. return;
  109. }
  110. $composer = JsonFile::parseJson($composer);
  111. if (!isset($composer['time'])) {
  112. $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir);
  113. $date = new \DateTime('@'.trim($output));
  114. $composer['time'] = $date->format('Y-m-d H:i:s');
  115. }
  116. $this->infoCache[$identifier] = $composer;
  117. }
  118. return $this->infoCache[$identifier];
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getTags()
  124. {
  125. if (null === $this->tags) {
  126. $this->process->execute('git tag', $output, $this->repoDir);
  127. $output = $this->process->splitLines($output);
  128. $this->tags = $output ? array_combine($output, $output) : array();
  129. }
  130. return $this->tags;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getBranches()
  136. {
  137. if (null === $this->branches) {
  138. $branches = array();
  139. $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir);
  140. foreach ($this->process->splitLines($output) as $branch) {
  141. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  142. preg_match('{^(?:\* )? *(?:[^/ ]+?/)?(\S+) *([a-f0-9]+) .*$}', $branch, $match);
  143. $branches[$match[1]] = $match[2];
  144. }
  145. }
  146. $this->branches = $branches;
  147. }
  148. return $this->branches;
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public static function supports(IOInterface $io, $url, $deep = false)
  154. {
  155. if (preg_match('#(^git://|\.git$|git@|//git\.|//github.com/)#i', $url)) {
  156. return true;
  157. }
  158. // local filesystem
  159. if (static::isLocalUrl($url)) {
  160. $process = new ProcessExecutor();
  161. $url = str_replace('file://', '', $url);
  162. // check whether there is a git repo in that path
  163. if ($process->execute('git tag', $output, $url) === 0) {
  164. return true;
  165. }
  166. }
  167. if (!$deep) {
  168. return false;
  169. }
  170. // TODO try to connect to the server
  171. return false;
  172. }
  173. }