GitDriver.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Util\Git as GitUtil;
  16. use Composer\IO\IOInterface;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class GitDriver extends VcsDriver
  21. {
  22. protected $tags;
  23. protected $branches;
  24. protected $rootIdentifier;
  25. protected $repoDir;
  26. protected $infoCache = array();
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function initialize()
  31. {
  32. if (static::isLocalUrl($this->url)) {
  33. $this->repoDir = str_replace('file://', '', $this->url);
  34. } else {
  35. $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
  36. $util = new GitUtil;
  37. $util->cleanEnv();
  38. $fs = new Filesystem();
  39. $fs->ensureDirectoryExists(dirname($this->repoDir));
  40. if (!is_writable(dirname($this->repoDir))) {
  41. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
  42. }
  43. if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
  44. throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.');
  45. }
  46. // update the repo if it is a valid git repository
  47. if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) {
  48. if (0 !== $this->process->execute('git remote update --prune origin', $output, $this->repoDir)) {
  49. $this->io->write('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  50. }
  51. } else {
  52. // clean up directory and do a fresh clone into it
  53. $fs->removeDirectory($this->repoDir);
  54. $command = sprintf('git clone --mirror %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir));
  55. if (0 !== $this->process->execute($command, $output)) {
  56. $output = $this->process->getErrorOutput();
  57. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  58. 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());
  59. }
  60. throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output);
  61. }
  62. }
  63. }
  64. $this->getTags();
  65. $this->getBranches();
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function getRootIdentifier()
  71. {
  72. if (null === $this->rootIdentifier) {
  73. $this->rootIdentifier = 'master';
  74. // select currently checked out branch if master is not available
  75. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  76. $branches = $this->process->splitLines($output);
  77. if (!in_array('* master', $branches)) {
  78. foreach ($branches as $branch) {
  79. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  80. $this->rootIdentifier = $match[1];
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. return $this->rootIdentifier;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getUrl()
  92. {
  93. return $this->url;
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getSource($identifier)
  99. {
  100. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function getDist($identifier)
  106. {
  107. return null;
  108. }
  109. /**
  110. * {@inheritDoc}
  111. */
  112. public function getComposerInformation($identifier)
  113. {
  114. if (!isset($this->infoCache[$identifier])) {
  115. $resource = sprintf('%s:composer.json', escapeshellarg($identifier));
  116. $this->process->execute(sprintf('git show %s', $resource), $composer, $this->repoDir);
  117. if (!trim($composer)) {
  118. return;
  119. }
  120. $composer = JsonFile::parseJson($composer, $resource);
  121. if (!isset($composer['time'])) {
  122. $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir);
  123. $date = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  124. $composer['time'] = $date->format('Y-m-d H:i:s');
  125. }
  126. $this->infoCache[$identifier] = $composer;
  127. }
  128. return $this->infoCache[$identifier];
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public function getTags()
  134. {
  135. if (null === $this->tags) {
  136. $this->tags = array();
  137. $this->process->execute('git show-ref --tags', $output, $this->repoDir);
  138. foreach ($output = $this->process->splitLines($output) as $tag) {
  139. if ($tag && preg_match('{^([a-f0-9]{40}) refs/tags/(\S+)$}', $tag, $match)) {
  140. $this->tags[$match[2]] = $match[1];
  141. }
  142. }
  143. }
  144. return $this->tags;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getBranches()
  150. {
  151. if (null === $this->branches) {
  152. $branches = array();
  153. $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir);
  154. foreach ($this->process->splitLines($output) as $branch) {
  155. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  156. if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  157. $branches[$match[1]] = $match[2];
  158. }
  159. }
  160. }
  161. $this->branches = $branches;
  162. }
  163. return $this->branches;
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public static function supports(IOInterface $io, $url, $deep = false)
  169. {
  170. if (preg_match('#(^git://|\.git$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) {
  171. return true;
  172. }
  173. // local filesystem
  174. if (static::isLocalUrl($url)) {
  175. if (!is_dir($url)) {
  176. throw new \RuntimeException('Directory does not exist: '.$url);
  177. }
  178. $process = new ProcessExecutor();
  179. $url = str_replace('file://', '', $url);
  180. // check whether there is a git repo in that path
  181. if ($process->execute('git tag', $output, $url) === 0) {
  182. return true;
  183. }
  184. }
  185. if (!$deep) {
  186. return false;
  187. }
  188. // TODO try to connect to the server
  189. return false;
  190. }
  191. }