GitDriver.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (!is_dir($this->repoDir)) {
  55. throw new \RuntimeException('Failed to clone '.$this->url.' to read package information from it');
  56. }
  57. $this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
  58. }
  59. $cacheUrl = $this->url;
  60. }
  61. $this->getTags();
  62. $this->getBranches();
  63. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getRootIdentifier()
  69. {
  70. if (null === $this->rootIdentifier) {
  71. $this->rootIdentifier = 'master';
  72. // select currently checked out branch if master is not available
  73. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  74. $branches = $this->process->splitLines($output);
  75. if (!in_array('* master', $branches)) {
  76. foreach ($branches as $branch) {
  77. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  78. $this->rootIdentifier = $match[1];
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. return $this->rootIdentifier;
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function getUrl()
  90. {
  91. return $this->url;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getSource($identifier)
  97. {
  98. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function getDist($identifier)
  104. {
  105. return null;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getFileContent($file, $identifier)
  111. {
  112. $resource = sprintf('%s:%s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
  113. $this->process->execute(sprintf('git show %s', $resource), $content, $this->repoDir);
  114. if (!trim($content)) {
  115. return null;
  116. }
  117. return $content;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getChangeDate($identifier)
  123. {
  124. $this->process->execute(sprintf(
  125. 'git -c log.showSignature=false log -1 --format=%%at %s',
  126. ProcessExecutor::escape($identifier)
  127. ), $output, $this->repoDir);
  128. return new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  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 --dereference', $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, Config $config, $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 (Filesystem::isLocalPath($url)) {
  175. $url = Filesystem::getPlatformPath($url);
  176. if (!is_dir($url)) {
  177. return false;
  178. }
  179. $process = new ProcessExecutor($io);
  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. $process = new ProcessExecutor($io);
  189. return $process->execute('git ls-remote --heads ' . ProcessExecutor::escape($url), $output) === 0;
  190. }
  191. }