GitDriver.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 (Filesystem::isLocalPath($this->url)) {
  36. $this->repoDir = $this->url;
  37. $cacheUrl = realpath($this->url);
  38. } else {
  39. $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/';
  40. GitUtil::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. $gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs);
  50. // update the repo if it is a valid git repository
  51. if (is_dir($this->repoDir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $this->repoDir) && trim($output) === '.') {
  52. try {
  53. $commandCallable = function ($url) {
  54. return sprintf('git remote set-url origin %s && git remote update --prune origin', ProcessExecutor::escape($url));
  55. };
  56. $gitUtil->runCommand($commandCallable, $this->url, $this->repoDir);
  57. } catch (\Exception $e) {
  58. $this->io->write('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$e->getMessage().')</error>');
  59. }
  60. } else {
  61. // clean up directory and do a fresh clone into it
  62. $fs->removeDirectory($this->repoDir);
  63. $repoDir = $this->repoDir;
  64. $commandCallable = function ($url) use ($repoDir) {
  65. return sprintf('git clone --mirror %s %s', ProcessExecutor::escape($url), ProcessExecutor::escape($repoDir));
  66. };
  67. $gitUtil->runCommand($commandCallable, $this->url, $this->repoDir, true);
  68. }
  69. $cacheUrl = $this->url;
  70. }
  71. $this->getTags();
  72. $this->getBranches();
  73. $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $cacheUrl));
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function getRootIdentifier()
  79. {
  80. if (null === $this->rootIdentifier) {
  81. $this->rootIdentifier = 'master';
  82. // select currently checked out branch if master is not available
  83. $this->process->execute('git branch --no-color', $output, $this->repoDir);
  84. $branches = $this->process->splitLines($output);
  85. if (!in_array('* master', $branches)) {
  86. foreach ($branches as $branch) {
  87. if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) {
  88. $this->rootIdentifier = $match[1];
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. return $this->rootIdentifier;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function getUrl()
  100. {
  101. return $this->url;
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function getSource($identifier)
  107. {
  108. return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function getDist($identifier)
  114. {
  115. return null;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function getComposerInformation($identifier)
  121. {
  122. if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
  123. $this->infoCache[$identifier] = JsonFile::parseJson($res);
  124. }
  125. if (!isset($this->infoCache[$identifier])) {
  126. $resource = sprintf('%s:composer.json', ProcessExecutor::escape($identifier));
  127. $this->process->execute(sprintf('git show %s', $resource), $composer, $this->repoDir);
  128. if (!trim($composer)) {
  129. return;
  130. }
  131. $composer = JsonFile::parseJson($composer, $resource);
  132. if (empty($composer['time'])) {
  133. $this->process->execute(sprintf('git log -1 --format=%%at %s', ProcessExecutor::escape($identifier)), $output, $this->repoDir);
  134. $date = new \DateTime('@'.trim($output), new \DateTimeZone('UTC'));
  135. $composer['time'] = $date->format('Y-m-d H:i:s');
  136. }
  137. if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
  138. $this->cache->write($identifier, json_encode($composer));
  139. }
  140. $this->infoCache[$identifier] = $composer;
  141. }
  142. return $this->infoCache[$identifier];
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function getTags()
  148. {
  149. if (null === $this->tags) {
  150. $this->tags = array();
  151. $this->process->execute('git show-ref --tags', $output, $this->repoDir);
  152. foreach ($output = $this->process->splitLines($output) as $tag) {
  153. if ($tag && preg_match('{^([a-f0-9]{40}) refs/tags/(\S+)$}', $tag, $match)) {
  154. $this->tags[$match[2]] = $match[1];
  155. }
  156. }
  157. }
  158. return $this->tags;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public function getBranches()
  164. {
  165. if (null === $this->branches) {
  166. $branches = array();
  167. $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir);
  168. foreach ($this->process->splitLines($output) as $branch) {
  169. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  170. if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+)(?: .*)?$}', $branch, $match)) {
  171. $branches[$match[1]] = $match[2];
  172. }
  173. }
  174. }
  175. $this->branches = $branches;
  176. }
  177. return $this->branches;
  178. }
  179. /**
  180. * {@inheritDoc}
  181. */
  182. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  183. {
  184. if (preg_match('#(^git://|\.git$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) {
  185. return true;
  186. }
  187. // local filesystem
  188. if (Filesystem::isLocalPath($url)) {
  189. $url = Filesystem::getPlatformPath($url);
  190. if (!is_dir($url)) {
  191. return false;
  192. }
  193. $process = new ProcessExecutor($io);
  194. // check whether there is a git repo in that path
  195. if ($process->execute('git tag', $output, $url) === 0) {
  196. return true;
  197. }
  198. }
  199. if (!$deep) {
  200. return false;
  201. }
  202. $process = new ProcessExecutor($io);
  203. if($process->execute('git ls-remote --heads ' . ProcessExecutor::escape($url)) === 0) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. }