GitDownloader.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. private $hasStashedChanges = false;
  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 remote add composer %1$s && git fetch composer';
  26. $this->io->write(" Cloning ".$ref);
  27. // added in git 1.7.1, prevents prompting the user
  28. putenv('GIT_ASKPASS=echo');
  29. $commandCallable = function($url) use ($ref, $path, $command) {
  30. return sprintf($command, escapeshellarg($url), escapeshellarg($path), escapeshellarg($ref));
  31. };
  32. $this->runCommand($commandCallable, $package->getSourceUrl(), $path);
  33. $this->setPushUrl($package, $path);
  34. $this->updateToCommit($path, $ref, $package->getPrettyVersion(), $package->getReleaseDate());
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
  40. {
  41. $ref = $target->getSourceReference();
  42. $this->io->write(" Checking out ".$ref);
  43. $command = 'cd %s && git remote set-url composer %s && git fetch composer && git fetch --tags composer';
  44. if (!$this->io->hasAuthorization('github.com')) {
  45. // capture username/password from github URL if there is one
  46. $this->process->execute(sprintf('cd %s && git remote -v', escapeshellarg($path)), $output);
  47. if (preg_match('{^composer\s+https://(.+):(.+)@github.com/}im', $output, $match)) {
  48. $this->io->setAuthorization('github.com', $match[1], $match[2]);
  49. }
  50. }
  51. $commandCallable = function($url) use ($ref, $path, $command) {
  52. return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref));
  53. };
  54. $this->runCommand($commandCallable, $target->getSourceUrl());
  55. $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate());
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getLocalChanges($path)
  61. {
  62. $command = sprintf('cd %s && git status --porcelain --untracked-files=no', escapeshellarg($path));
  63. if (0 !== $this->process->execute($command, $output)) {
  64. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  65. }
  66. return trim($output) ?: null;
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. protected function cleanChanges($path, $update)
  72. {
  73. if (!$this->io->isInteractive()) {
  74. return parent::cleanChanges($path, $update);
  75. }
  76. if (!$changes = $this->getLocalChanges($path)) {
  77. return;
  78. }
  79. $changes = array_map(function ($elem) {
  80. return ' '.$elem;
  81. }, preg_split('{\s*\r?\n\s*}', $changes));
  82. $this->io->write(' <error>The package has modified files:</error>');
  83. $this->io->write(array_slice($changes, 0, 10));
  84. if (count($changes) > 10) {
  85. $this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  86. }
  87. while (true) {
  88. switch ($this->io->ask(' <info>Discard changes [y,n,v,'.($update ? 's,' : '').'?]?</info> ', '?')) {
  89. case 'y':
  90. if (0 !== $this->process->execute('git reset --hard', $output, $path)) {
  91. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  92. }
  93. break 2;
  94. case 's':
  95. if (!$update) {
  96. goto help;
  97. }
  98. if (0 !== $this->process->execute('git stash', $output, $path)) {
  99. throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
  100. }
  101. $this->hasStashedChanges = true;
  102. break 2;
  103. case 'n':
  104. throw new \RuntimeException('Update aborted');
  105. case 'v':
  106. $this->io->write($changes);
  107. break;
  108. case '?':
  109. default:
  110. help:
  111. $this->io->write(array(
  112. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  113. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  114. ' v - view modified files',
  115. ));
  116. if ($update) {
  117. $this->io->write(' s - stash changes and try to reapply them after the update');
  118. }
  119. $this->io->write(' ? - print help');
  120. break;
  121. }
  122. }
  123. }
  124. /**
  125. * {@inheritDoc}
  126. */
  127. protected function reapplyChanges($path)
  128. {
  129. if ($this->hasStashedChanges) {
  130. $this->io->write(' <info>Re-applying stashed changes');
  131. if (0 !== $this->process->execute('git stash pop', $output, $path)) {
  132. throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput());
  133. }
  134. }
  135. }
  136. protected function updateToCommit($path, $reference, $branch, $date)
  137. {
  138. $template = 'git checkout %s && git reset --hard %1$s';
  139. $branch = preg_replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch);
  140. $branches = null;
  141. if (0 === $this->process->execute('git branch -r', $output, $path)) {
  142. $branches = $output;
  143. }
  144. // check whether non-commitish are branches or tags, and fetch branches with the remote name
  145. $gitRef = $reference;
  146. if (!preg_match('{^[a-f0-9]{40}$}', $reference)
  147. && $branches
  148. && preg_match('{^\s+composer/'.preg_quote($reference).'$}m', $output)
  149. ) {
  150. $command = sprintf('git checkout -B %s %s && git reset --hard %2$s', escapeshellarg($branch), escapeshellarg('composer/'.$reference));
  151. if (0 === $this->process->execute($command, $output, $path)) {
  152. return;
  153. }
  154. }
  155. // try to checkout branch by name and then reset it so it's on the proper branch name
  156. if (preg_match('{^[a-f0-9]{40}$}', $reference)) {
  157. // add 'v' in front of the branch if it was stripped when generating the pretty name
  158. if (!preg_match('{^\s+composer/'.preg_quote($branch).'$}m', $branches) && preg_match('{^\s+composer/v'.preg_quote($branch).'$}m', $branches)) {
  159. $branch = 'v' . $branch;
  160. }
  161. $command = sprintf('git checkout %s', escapeshellarg($branch));
  162. $fallbackCommand = sprintf('git checkout -B %s %s', escapeshellarg($branch), escapeshellarg('composer/'.$branch));
  163. if (0 === $this->process->execute($command, $output, $path)
  164. || 0 === $this->process->execute($fallbackCommand, $output, $path)
  165. ) {
  166. $command = sprintf('git reset --hard %s', escapeshellarg($reference));
  167. if (0 === $this->process->execute($command, $output, $path)) {
  168. return;
  169. }
  170. }
  171. }
  172. $command = sprintf($template, escapeshellarg($gitRef));
  173. if (0 === $this->process->execute($command, $output, $path)) {
  174. return;
  175. }
  176. // reference was not found (prints "fatal: reference is not a tree: $ref")
  177. if ($date && false !== strpos($this->process->getErrorOutput(), $reference)) {
  178. $date = $date->format('U');
  179. // guess which remote branch to look at first
  180. $command = 'git branch -r';
  181. if (0 !== $this->process->execute($command, $output, $path)) {
  182. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  183. }
  184. $guessTemplate = 'git log --until=%s --date=raw -n1 --pretty=%%H %s';
  185. foreach ($this->process->splitLines($output) as $line) {
  186. if (preg_match('{^composer/'.preg_quote($branch).'(?:\.x)?$}i', trim($line))) {
  187. // find the previous commit by date in the given branch
  188. if (0 === $this->process->execute(sprintf($guessTemplate, $date, escapeshellarg(trim($line))), $output, $path)) {
  189. $newReference = trim($output);
  190. }
  191. break;
  192. }
  193. }
  194. if (empty($newReference)) {
  195. // no matching branch found, find the previous commit by date in all commits
  196. if (0 !== $this->process->execute(sprintf($guessTemplate, $date, '--all'), $output, $path)) {
  197. throw new \RuntimeException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput());
  198. }
  199. $newReference = trim($output);
  200. }
  201. // checkout the new recovered ref
  202. $command = sprintf($template, escapeshellarg($reference));
  203. if (0 === $this->process->execute($command, $output, $path)) {
  204. $this->io->write(' '.$reference.' is gone (history was rewritten?), recovered by checking out '.$newReference);
  205. return;
  206. }
  207. }
  208. throw new \RuntimeException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput());
  209. }
  210. /**
  211. * Runs a command doing attempts for each protocol supported by github.
  212. *
  213. * @param callable $commandCallable A callable building the command for the given url
  214. * @param string $url
  215. * @param string $path The directory to remove for each attempt (null if not needed)
  216. * @throws \RuntimeException
  217. */
  218. protected function runCommand($commandCallable, $url, $path = null)
  219. {
  220. $handler = array($this, 'outputHandler');
  221. // public github, autoswitch protocols
  222. if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) {
  223. $protocols = $this->config->get('github-protocols');
  224. if (!is_array($protocols)) {
  225. throw new \RuntimeException('Config value "github-protocols" must be an array, got '.gettype($protocols));
  226. }
  227. $messages = array();
  228. foreach ($protocols as $protocol) {
  229. $url = $protocol . $match[1];
  230. if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) {
  231. return;
  232. }
  233. $messages[] = '- ' . $url . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput());
  234. if (null !== $path) {
  235. $this->filesystem->removeDirectory($path);
  236. }
  237. }
  238. // failed to checkout, first check git accessibility
  239. $this->throwException('Failed to clone ' . $this->sanitizeUrl($url) .' via git, https and http protocols, aborting.' . "\n\n" . implode("\n", $messages), $url);
  240. }
  241. $command = call_user_func($commandCallable, $url);
  242. if (0 !== $this->process->execute($command, $handler)) {
  243. if (preg_match('{^git@github.com:(.+?)\.git$}i', $url, $match) && $this->io->isInteractive()) {
  244. // private github repository without git access, try https with auth
  245. $retries = 3;
  246. $retrying = false;
  247. do {
  248. if ($retrying) {
  249. $this->io->write('Invalid credentials');
  250. }
  251. if (!$this->io->hasAuthorization('github.com') || $retrying) {
  252. $username = $this->io->ask('Username: ');
  253. $password = $this->io->askAndHideAnswer('Password: ');
  254. $this->io->setAuthorization('github.com', $username, $password);
  255. }
  256. $auth = $this->io->getAuthorization('github.com');
  257. $url = 'https://'.$auth['username'] . ':' . $auth['password'] . '@github.com/'.$match[1].'.git';
  258. $command = call_user_func($commandCallable, $url);
  259. if (0 === $this->process->execute($command, $handler)) {
  260. return;
  261. }
  262. if (null !== $path) {
  263. $this->filesystem->removeDirectory($path);
  264. }
  265. $retrying = true;
  266. } while (--$retries);
  267. }
  268. if (null !== $path) {
  269. $this->filesystem->removeDirectory($path);
  270. }
  271. $this->throwException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput(), $url);
  272. }
  273. }
  274. public function outputHandler($type, $buffer)
  275. {
  276. if ($type !== 'out') {
  277. return;
  278. }
  279. if ($this->io->isVerbose()) {
  280. $this->io->write($buffer, false);
  281. }
  282. }
  283. protected function throwException($message, $url)
  284. {
  285. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  286. throw new \RuntimeException('Failed to clone '.$this->sanitizeUrl($url).', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  287. }
  288. throw new \RuntimeException($message);
  289. }
  290. protected function sanitizeUrl($message)
  291. {
  292. return preg_replace('{://(.+?):.+?@}', '://$1:***@', $message);
  293. }
  294. protected function setPushUrl(PackageInterface $package, $path)
  295. {
  296. // set push url for github projects
  297. if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) {
  298. $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git';
  299. $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl));
  300. $this->process->execute($cmd, $ignoredOutput, $path);
  301. }
  302. }
  303. /**
  304. * {@inheritDoc}
  305. */
  306. protected function getCommitLogs($fromReference, $toReference, $path)
  307. {
  308. $command = sprintf('cd %s && git log %s..%s --pretty=format:"%%h - %%an: %%s"', escapeshellarg($path), $fromReference, $toReference);
  309. if (0 !== $this->process->execute($command, $output)) {
  310. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  311. }
  312. return $output;
  313. }
  314. }