GitDownloader.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. $commandCallable = function($url) use ($ref, $path, $command) {
  46. return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref));
  47. };
  48. $this->runCommand($commandCallable, $target->getSourceUrl());
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. protected function enforceCleanDirectory($path)
  54. {
  55. $command = sprintf('cd %s && git status --porcelain --untracked-files=no', escapeshellarg($path));
  56. if (0 !== $this->process->execute($command, $output)) {
  57. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  58. }
  59. if (trim($output)) {
  60. throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes');
  61. }
  62. }
  63. /**
  64. * Runs a command doing attempts for each protocol supported by github.
  65. *
  66. * @param callable $commandCallable A callable building the command for the given url
  67. * @param string $url
  68. * @param string $path The directory to remove for each attempt (null if not needed)
  69. * @throws \RuntimeException
  70. */
  71. protected function runCommand($commandCallable, $url, $path = null)
  72. {
  73. $handler = array($this, 'outputHandler');
  74. // github, autoswitch protocols
  75. if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
  76. $protocols = array('git', 'https', 'http');
  77. $messages = array();
  78. foreach ($protocols as $protocol) {
  79. $url = $protocol . $match[1];
  80. if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) {
  81. return;
  82. }
  83. $messages[] = '- ' . $url . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput());
  84. if (null !== $path) {
  85. $this->filesystem->removeDirectory($path);
  86. }
  87. }
  88. // failed to checkout, first check git accessibility
  89. $this->throwException('Failed to clone ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . implode("\n", $messages), $url);
  90. }
  91. $command = call_user_func($commandCallable, $url);
  92. if (0 !== $this->process->execute($command, $handler)) {
  93. if (preg_match('{^git@github.com:(.+?)\.git$}i', $url, $match) && $this->io->isInteractive()) {
  94. // private repository without git access, try https with auth
  95. $retries = 3;
  96. $retrying = false;
  97. do {
  98. if ($retrying) {
  99. $this->io->write('Invalid credentials');
  100. }
  101. if (!$this->io->hasAuthorization('github.com') || $retrying) {
  102. $username = $this->io->ask('Username: ');
  103. $password = $this->io->askAndHideAnswer('Password: ');
  104. $this->io->setAuthorization('github.com', $username, $password);
  105. }
  106. $auth = $this->io->getAuthorization('github.com');
  107. $url = 'https://'.$auth['username'] . ':' . $auth['password'] . '@github.com/'.$match[1].'.git';
  108. $command = call_user_func($commandCallable, $url);
  109. if (0 === $this->process->execute($command, $handler)) {
  110. return;
  111. }
  112. if (null !== $path) {
  113. $this->filesystem->removeDirectory($path);
  114. }
  115. $retrying = true;
  116. } while (--$retries);
  117. }
  118. if (null !== $path) {
  119. $this->filesystem->removeDirectory($path);
  120. }
  121. $this->throwException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(), $url);
  122. }
  123. }
  124. public function outputHandler($type, $buffer)
  125. {
  126. if ($type !== 'out') {
  127. return;
  128. }
  129. if ($this->io->isVerbose()) {
  130. $this->io->write($buffer, false);
  131. }
  132. }
  133. protected function throwException($message, $url)
  134. {
  135. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  136. 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());
  137. }
  138. throw new \RuntimeException($message);
  139. }
  140. protected function setPushUrl(PackageInterface $package, $path)
  141. {
  142. // set push url for github projects
  143. if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
  144. $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
  145. $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl));
  146. $this->process->execute($cmd, $ignoredOutput, $path);
  147. }
  148. }
  149. }