GitDownloader.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /**
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class GitDownloader extends VcsDownloader
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function doDownload(PackageInterface $package, $path)
  22. {
  23. $ref = $package->getSourceReference();
  24. $command = 'git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s && git remote add composer %1$s';
  25. $this->io->write(" Cloning ".$package->getSourceReference());
  26. $commandCallable = function($url) use ($ref, $path, $command) {
  27. return sprintf($command, escapeshellarg($url), escapeshellarg($path), escapeshellarg($ref));
  28. };
  29. $this->runCommand($commandCallable, $package->getSourceUrl(), $path);
  30. $this->setPushUrl($package, $path);
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
  36. {
  37. $ref = $target->getSourceReference();
  38. $this->io->write(" Checking out ".$target->getSourceReference());
  39. $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';
  40. // capture username/password from github URL if there is one
  41. $this->process->execute(sprintf('cd %s && git remote -v', escapeshellarg($path)), $output);
  42. if (preg_match('{^composer\s+https://(.+):(.+)@github.com/}im', $output, $match)) {
  43. $this->io->setAuthorization('github.com', $match[1], $match[2]);
  44. }
  45. // TODO: BC for the composer remote that didn't exist, to be remove after May 18th.
  46. $this->process->execute(sprintf('cd %s && git remote add composer %s', escapeshellarg($path), escapeshellarg($initial->getSourceUrl())), $ignoredOutput);
  47. $commandCallable = function($url) use ($ref, $path, $command) {
  48. return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref));
  49. };
  50. $this->runCommand($commandCallable, $target->getSourceUrl());
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. protected function enforceCleanDirectory($path)
  56. {
  57. $command = sprintf('cd %s && git status --porcelain --untracked-files=no', escapeshellarg($path));
  58. if (0 !== $this->process->execute($command, $output)) {
  59. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  60. }
  61. if (trim($output)) {
  62. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes');
  63. }
  64. }
  65. /**
  66. * Runs a command doing attempts for each protocol supported by github.
  67. *
  68. * @param callable $commandCallable A callable building the command for the given url
  69. * @param string $url
  70. * @param string $path The directory to remove for each attempt (null if not needed)
  71. * @throws \RuntimeException
  72. */
  73. protected function runCommand($commandCallable, $url, $path = null)
  74. {
  75. $handler = array($this, 'outputHandler');
  76. // github, autoswitch protocols
  77. if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
  78. $protocols = array('git', 'https', 'http');
  79. $messages = array();
  80. foreach ($protocols as $protocol) {
  81. $url = $protocol . $match[1];
  82. if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) {
  83. return;
  84. }
  85. $messages[] = '- ' . $url . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput());
  86. if (null !== $path) {
  87. $this->filesystem->removeDirectory($path);
  88. }
  89. }
  90. // failed to checkout, first check git accessibility
  91. $this->throwException('Failed to clone ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . implode("\n", $messages), $url);
  92. }
  93. $command = call_user_func($commandCallable, $url);
  94. if (0 !== $this->process->execute($command, $handler)) {
  95. if (preg_match('{^git@github.com:(.+?)\.git$}i', $url, $match) && $this->io->isInteractive()) {
  96. // private repository without git access, try https with auth
  97. $retries = 3;
  98. $retrying = false;
  99. do {
  100. if ($retrying) {
  101. $this->io->write('Invalid credentials');
  102. }
  103. if (!$this->io->hasAuthorization('github.com') || $retrying) {
  104. $username = $this->io->ask('Username: ');
  105. $password = $this->io->askAndHideAnswer('Password: ');
  106. $this->io->setAuthorization('github.com', $username, $password);
  107. }
  108. $auth = $this->io->getAuthorization('github.com');
  109. $url = 'https://'.$auth['username'] . ':' . $auth['password'] . '@github.com/'.$match[1].'.git';
  110. $command = call_user_func($commandCallable, $url);
  111. if (0 === $this->process->execute($command, $handler)) {
  112. return;
  113. }
  114. if (null !== $path) {
  115. $this->filesystem->removeDirectory($path);
  116. }
  117. $retrying = true;
  118. } while (--$retries);
  119. }
  120. if (null !== $path) {
  121. $this->filesystem->removeDirectory($path);
  122. }
  123. $this->throwException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(), $url);
  124. }
  125. }
  126. public function outputHandler($type, $buffer)
  127. {
  128. if ($type !== 'out') {
  129. return;
  130. }
  131. if ($this->io->isVerbose()) {
  132. $this->io->write($buffer, false);
  133. }
  134. }
  135. protected function throwException($message, $url)
  136. {
  137. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  138. 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());
  139. }
  140. throw new \RuntimeException($message);
  141. }
  142. protected function setPushUrl(PackageInterface $package, $path)
  143. {
  144. // set push url for github projects
  145. if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
  146. $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
  147. $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl));
  148. $this->process->execute($cmd, $ignoredOutput, $path);
  149. }
  150. }
  151. }