GitDriver.php 7.8 KB

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