GitDownloader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 && git remote add composer %1$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 composer %s && git fetch composer && git fetch --tags composer && git checkout %3$s && git reset --hard %3$s';
  41. // TODO: BC for the composer remote that didn't exist, to be remove after May 18th.
  42. $this->process->execute(sprintf('cd %s && git remote add composer %s', escapeshellarg($path), escapeshellarg($initial->getSourceUrl())), $ignoredOutput);
  43. $commandCallable = function($url) use ($ref, $path, $command) {
  44. return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref));
  45. };
  46. $this->runCommand($commandCallable, $target->getSourceUrl());
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. protected function enforceCleanDirectory($path)
  52. {
  53. $command = sprintf('cd %s && git status --porcelain --untracked-files=no', escapeshellarg($path));
  54. if (0 !== $this->process->execute($command, $output)) {
  55. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  56. }
  57. if (trim($output)) {
  58. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes');
  59. }
  60. }
  61. /**
  62. * Runs a command doing attempts for each protocol supported by github.
  63. *
  64. * @param callable $commandCallable A callable building the command for the given url
  65. * @param string $url
  66. * @param string $path The directory to remove for each attempt (null if not needed)
  67. * @throws \RuntimeException
  68. */
  69. protected function runCommand($commandCallable, $url, $path = null)
  70. {
  71. $handler = array($this, 'outputHandler');
  72. // github, autoswitch protocols
  73. if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
  74. $protocols = array('git', 'https', 'http');
  75. foreach ($protocols as $protocol) {
  76. $url = $protocol . $match[1];
  77. if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) {
  78. return;
  79. }
  80. if (null !== $path) {
  81. $this->filesystem->removeDirectory($path);
  82. }
  83. }
  84. // failed to checkout, first check git accessibility
  85. $this->throwException('Failed to clone ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . $this->process->getErrorOutput(), $url);
  86. }
  87. $command = call_user_func($commandCallable, $url);
  88. if (0 !== $this->process->execute($command, $handler)) {
  89. $this->throwException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(), $url);
  90. }
  91. }
  92. public function outputHandler($type, $buffer)
  93. {
  94. if ($type !== 'out') {
  95. return;
  96. }
  97. if ($this->io->isVerbose()) {
  98. $this->io->write($buffer, false);
  99. }
  100. }
  101. protected function throwException($message, $url)
  102. {
  103. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  104. throw new \RuntimeException('Failed to clone '.$url.', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  105. }
  106. throw new \RuntimeException($message);
  107. }
  108. protected function setPushUrl(PackageInterface $package, $path)
  109. {
  110. // set push url for github projects
  111. if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
  112. $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
  113. $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl));
  114. $this->process->execute($cmd, $ignoredOutput, $path);
  115. }
  116. }
  117. }