GitDownloader.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Downloader;
  12. use Composer\Package\PackageInterface;
  13. use Composer\Util\ProcessExecutor;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class GitDownloader extends VcsDownloader
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function doDownload(PackageInterface $package, $path)
  23. {
  24. $ref = $package->getSourceReference();
  25. $command = 'git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s';
  26. $this->io->write(" Cloning ".$package->getSourceReference());
  27. $commandCallable = function($url) use ($ref, $path, $command) {
  28. return sprintf($command, escapeshellarg($url), escapeshellarg($path), escapeshellarg($ref));
  29. };
  30. $this->runCommand($commandCallable, $package->getSourceUrl(), $path);
  31. $this->setPushUrl($package, $path);
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
  37. {
  38. $ref = $target->getSourceReference();
  39. $this->io->write(" Checking out ".$target->getSourceReference());
  40. $command = 'cd %s && git remote set-url origin %s && git fetch origin && git fetch --tags origin && git checkout %3$s && git reset --hard %3$s';
  41. $commandCallable = function($url) use ($ref, $path, $command) {
  42. return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref));
  43. };
  44. $this->runCommand($commandCallable, $target->getSourceUrl());
  45. $this->setPushUrl($target, $path);
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. protected function enforceCleanDirectory($path)
  51. {
  52. $command = sprintf('cd %s && git status --porcelain', escapeshellarg($path));
  53. if (0 !== $this->process->execute($command, $output)) {
  54. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  55. }
  56. if (trim($output)) {
  57. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes');
  58. }
  59. }
  60. /**
  61. * Runs a command doing attempts for each protocol supported by github.
  62. *
  63. * @param callable $commandCallable A callable building the command for the given url
  64. * @param string $url
  65. * @param string $path The directory to remove for each attempt (null if not needed)
  66. * @throws \RuntimeException
  67. */
  68. protected function runCommand($commandCallable, $url, $path = null)
  69. {
  70. $handler = array($this, 'outputHandler');
  71. // github, autoswitch protocols
  72. if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
  73. $protocols = array('git', 'https', 'http');
  74. foreach ($protocols as $protocol) {
  75. $url = $protocol . $match[1];
  76. if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) {
  77. return;
  78. }
  79. if (null !== $path) {
  80. $this->filesystem->removeDirectory($path);
  81. }
  82. }
  83. // failed to checkout, first check git accessibility
  84. $output = $this->process->getErrorOutput();
  85. if (false === strpos($this->process->execute('git --version', $handler), 'git version')) {
  86. throw new \RuntimeException('It looks like git isn\'t accessible through the console, please check your installation and your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  87. } else {
  88. throw new \RuntimeException('Failed to checkout ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . $output);
  89. }
  90. }
  91. $command = call_user_func($commandCallable, $url);
  92. if (0 !== $this->process->execute($command, $handler)) {
  93. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  94. }
  95. }
  96. public function outputHandler($type, $buffer)
  97. {
  98. if ($type !== 'out') {
  99. return;
  100. }
  101. if ($this->io->isVerbose()) {
  102. $this->io->write($buffer, false);
  103. }
  104. }
  105. protected function setPushUrl(PackageInterface $package, $path)
  106. {
  107. // set push url for github projects
  108. if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
  109. $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
  110. $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl));
  111. $this->process->execute($cmd, $ignoredOutput, $path);
  112. }
  113. }
  114. }