GitDriver.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Util\ProcessExecutor;
  13. use Composer\Util\Filesystem;
  14. use Composer\Util\Git as GitUtil;
  15. use Composer\IO\IOInterface;
  16. use Composer\Cache;
  17. use Composer\Config;
  18. /**
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class GitDriver extends VcsDriver
  22. {
  23. protected $cache;
  24. protected $tags;
  25. protected $branches;
  26. protected $rootIdentifier;
  27. protected $repoDir;
  28. protected $infoCache = array();
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function initialize()
  33. {
  34. if (Filesystem::isLocalPath($this->url)) {
  35. $this->url = preg_replace('{[\\/]\.git/?$}', '', $this->url);
  36. $this->repoDir = $this->url;
  37. $cacheUrl = realpath($this->url);
  38. } else {
  39. if (!Cache::isUsable($this->config->get('cache-vcs-dir'))) {
  40. throw new \RuntimeException('GitDriver requires a usable cache directory, and it looks like you set it to be disabled');
  41. }
  42. $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
  43. GitUtil::cleanEnv();
  44. $fs = new Filesystem();
  45. $fs->ensureDirectoryExists(dirname($this->repoDir));
  46. if (!is_writable(dirname($this->repoDir))) {
  47. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.');
  48. }
  49. if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) {
  50. 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.');
  51. }
  52. $gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
  53. if (!$gitUtil->syncMirror($this->url, $this->repoDir)) {
  54. $this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
  55. }
  56. $cacheUrl = $this->url;
  57. }
  58. $this->getTags();
  59. $this->getBranches();
  60. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function getRootIdentifier()
  66. {
  67. if (null === $this->rootIdentifier) {
  68. $this->rootIdentifier = 'master';
  69. // select currently checked out branch if master is not available
  70. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  71. $branches = $this->process->splitLines($output);
  72. if (!in_array('* master', $branches)) {
  73. foreach ($branches as $branch) {
  74. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  75. $this->rootIdentifier = $match[1];
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. return $this->rootIdentifier;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function getUrl()
  87. {
  88. return $this->url;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. public function getSource($identifier)
  94. {
  95. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function getDist($identifier)
  101. {
  102. return null;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getFileContent($file, $identifier)
  108. {
  109. $resource = sprintf('%s:%s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
  110. $this->process->execute(sprintf('git show %s', $resource), $content, $this->repoDir);
  111. if (!trim($content)) {
  112. return null;
  113. }
  114. return $content;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getChangeDate($identifier)
  120. {
  121. $this->process->execute(sprintf(
  122. 'git -c log.showSignature=false log -1 --format=%%at %s',
  123. ProcessExecutor::escape($identifier)
  124. ), $output, $this->repoDir);
  125. return new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getTags()
  131. {
  132. if (null === $this->tags) {
  133. $this->tags = array();
  134. $this->process->execute('git show-ref --tags --dereference', $output, $this->repoDir);
  135. foreach ($output = $this->process->splitLines($output) as $tag) {
  136. if ($tag && preg_match('{^([a-f0-9]{40}) refs/tags/(\S+?)(\^\{\})?$}', $tag, $match)) {
  137. $this->tags[$match[2]] = $match[1];
  138. }
  139. }
  140. }
  141. return $this->tags;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function getBranches()
  147. {
  148. if (null === $this->branches) {
  149. $branches = array();
  150. $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir);
  151. foreach ($this->process->splitLines($output) as $branch) {
  152. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  153. if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+)(?: .*)?$}', $branch, $match)) {
  154. $branches[$match[1]] = $match[2];
  155. }
  156. }
  157. }
  158. $this->branches = $branches;
  159. }
  160. return $this->branches;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  166. {
  167. if (preg_match('#(^git://|\.git/?$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) {
  168. return true;
  169. }
  170. // local filesystem
  171. if (Filesystem::isLocalPath($url)) {
  172. $url = Filesystem::getPlatformPath($url);
  173. if (!is_dir($url)) {
  174. return false;
  175. }
  176. $process = new ProcessExecutor($io);
  177. // check whether there is a git repo in that path
  178. if ($process->execute('git tag', $output, $url) === 0) {
  179. return true;
  180. }
  181. }
  182. if (!$deep) {
  183. return false;
  184. }
  185. $process = new ProcessExecutor($io);
  186. return $process->execute('git ls-remote --heads ' . ProcessExecutor::escape($url), $output) === 0;
  187. }
  188. }