GitDownloader.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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\Git as GitUtil;
  14. use Composer\Util\Platform;
  15. use Composer\Util\ProcessExecutor;
  16. use Composer\IO\IOInterface;
  17. use Composer\Util\Filesystem;
  18. use Composer\Config;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface, VcsCapableDownloaderInterface
  23. {
  24. private $hasStashedChanges = false;
  25. private $hasDiscardedChanges = false;
  26. private $gitUtil;
  27. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
  28. {
  29. parent::__construct($io, $config, $process, $fs);
  30. $this->gitUtil = new GitUtil($this->io, $this->config, $this->process, $this->filesystem);
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function doDownload(PackageInterface $package, $path, $url)
  36. {
  37. GitUtil::cleanEnv();
  38. $path = $this->normalizePath($path);
  39. $ref = $package->getSourceReference();
  40. $flag = Platform::isWindows() ? '/D ' : '';
  41. $command = 'git clone --no-checkout %s %s && cd '.$flag.'%2$s && git remote add composer %1$s && git fetch composer';
  42. $this->io->writeError(" Cloning ".$ref);
  43. $commandCallable = function ($url) use ($ref, $path, $command) {
  44. return sprintf($command, ProcessExecutor::escape($url), ProcessExecutor::escape($path), ProcessExecutor::escape($ref));
  45. };
  46. $this->gitUtil->runCommand($commandCallable, $url, $path, true);
  47. if ($url !== $package->getSourceUrl()) {
  48. $this->updateOriginUrl($path, $package->getSourceUrl());
  49. } else {
  50. $this->setPushUrl($path, $url);
  51. }
  52. if ($newRef = $this->updateToCommit($path, $ref, $package->getPrettyVersion(), $package->getReleaseDate())) {
  53. if ($package->getDistReference() === $package->getSourceReference()) {
  54. $package->setDistReference($newRef);
  55. }
  56. $package->setSourceReference($newRef);
  57. }
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  63. {
  64. GitUtil::cleanEnv();
  65. if (!$this->hasMetadataRepository($path)) {
  66. throw new \RuntimeException('The .git directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  67. }
  68. $updateOriginUrl = false;
  69. if (
  70. 0 === $this->process->execute('git remote -v', $output, $path)
  71. && preg_match('{^origin\s+(?P<url>\S+)}m', $output, $originMatch)
  72. && preg_match('{^composer\s+(?P<url>\S+)}m', $output, $composerMatch)
  73. ) {
  74. if ($originMatch['url'] === $composerMatch['url'] && $composerMatch['url'] !== $target->getSourceUrl()) {
  75. $updateOriginUrl = true;
  76. }
  77. }
  78. $ref = $target->getSourceReference();
  79. $this->io->writeError(" Checking out ".$ref);
  80. $command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer';
  81. $commandCallable = function ($url) use ($command) {
  82. return sprintf($command, ProcessExecutor::escape($url));
  83. };
  84. $this->gitUtil->runCommand($commandCallable, $url, $path);
  85. if ($newRef = $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate())) {
  86. if ($target->getDistReference() === $target->getSourceReference()) {
  87. $target->setDistReference($newRef);
  88. }
  89. $target->setSourceReference($newRef);
  90. }
  91. if ($updateOriginUrl) {
  92. $this->updateOriginUrl($path, $target->getSourceUrl());
  93. }
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getLocalChanges(PackageInterface $package, $path)
  99. {
  100. GitUtil::cleanEnv();
  101. if (!$this->hasMetadataRepository($path)) {
  102. return;
  103. }
  104. $command = 'git status --porcelain --untracked-files=no';
  105. if (0 !== $this->process->execute($command, $output, $path)) {
  106. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  107. }
  108. return trim($output) ?: null;
  109. }
  110. public function getUnpushedChanges(PackageInterface $package, $path)
  111. {
  112. GitUtil::cleanEnv();
  113. $path = $this->normalizePath($path);
  114. if (!$this->hasMetadataRepository($path)) {
  115. return;
  116. }
  117. $command = 'git show-ref --head -d';
  118. if (0 !== $this->process->execute($command, $output, $path)) {
  119. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  120. }
  121. $refs = trim($output);
  122. if (!preg_match('{^([a-f0-9]+) HEAD$}mi', $refs, $match)) {
  123. // could not match the HEAD for some reason
  124. return;
  125. }
  126. $headRef = $match[1];
  127. if (!preg_match_all('{^'.$headRef.' refs/heads/(.+)$}mi', $refs, $matches)) {
  128. // not on a branch, we are either on a not-modified tag or some sort of detached head, so skip this
  129. return;
  130. }
  131. // use the first match as branch name for now
  132. $branch = $matches[1][0];
  133. $unpushedChanges = null;
  134. // do two passes, as if we find anything we want to fetch and then re-try
  135. for ($i = 0; $i <= 1; $i++) {
  136. // try to find the a matching branch name in the composer remote
  137. foreach ($matches[1] as $candidate) {
  138. if (preg_match('{^[a-f0-9]+ refs/remotes/((?:composer|origin)/'.preg_quote($candidate).')$}mi', $refs, $match)) {
  139. $branch = $candidate;
  140. $remoteBranch = $match[1];
  141. break;
  142. }
  143. }
  144. // if it doesn't exist, then we assume it is an unpushed branch
  145. // this is bad as we have no reference point to do a diff so we just bail listing
  146. // the branch as being unpushed
  147. if (!isset($remoteBranch)) {
  148. $unpushedChanges = 'Branch ' . $branch . ' could not be found on the origin remote and appears to be unpushed';
  149. } else {
  150. $command = sprintf('git diff --name-status %s...%s --', $remoteBranch, $branch);
  151. if (0 !== $this->process->execute($command, $output, $path)) {
  152. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  153. }
  154. $unpushedChanges = trim($output) ?: null;
  155. }
  156. // first pass and we found unpushed changes, fetch from both remotes to make sure we have up to date
  157. // remotes and then try again as outdated remotes can sometimes cause false-positives
  158. if ($unpushedChanges && $i === 0) {
  159. $this->process->execute('git fetch composer && git fetch origin', $output, $path);
  160. }
  161. // abort after first pass if we didn't find anything
  162. if (!$unpushedChanges) {
  163. break;
  164. }
  165. }
  166. return $unpushedChanges;
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function getVcsReference(PackageInterface $package, $path)
  172. {
  173. if (!$this->hasMetadataRepository($path)) {
  174. return;
  175. }
  176. GitUtil::cleanEnv();
  177. $command = 'git log --pretty="%H" -n1 HEAD';
  178. if (0 !== $this->process->execute($command, $output, $path)) {
  179. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  180. }
  181. return trim($output) ?: null;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. protected function cleanChanges(PackageInterface $package, $path, $update)
  187. {
  188. GitUtil::cleanEnv();
  189. $path = $this->normalizePath($path);
  190. $unpushed = $this->getUnpushedChanges($package, $path);
  191. if ($unpushed && ($this->io->isInteractive() || $this->config->get('discard-changes') !== true)) {
  192. throw new \RuntimeException('Source directory ' . $path . ' has unpushed changes on the current branch: '."\n".$unpushed);
  193. }
  194. if (!$changes = $this->getLocalChanges($package, $path)) {
  195. return;
  196. }
  197. if (!$this->io->isInteractive()) {
  198. $discardChanges = $this->config->get('discard-changes');
  199. if (true === $discardChanges) {
  200. return $this->discardChanges($path);
  201. }
  202. if ('stash' === $discardChanges) {
  203. if (!$update) {
  204. return parent::cleanChanges($package, $path, $update);
  205. }
  206. return $this->stashChanges($path);
  207. }
  208. return parent::cleanChanges($package, $path, $update);
  209. }
  210. $changes = array_map(function ($elem) {
  211. return ' '.$elem;
  212. }, preg_split('{\s*\r?\n\s*}', $changes));
  213. $this->io->writeError(' <error>The package has modified files:</error>');
  214. $this->io->writeError(array_slice($changes, 0, 10));
  215. if (count($changes) > 10) {
  216. $this->io->writeError(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
  217. }
  218. while (true) {
  219. switch ($this->io->ask(' <info>Discard changes [y,n,v,d,'.($update ? 's,' : '').'?]?</info> ', '?')) {
  220. case 'y':
  221. $this->discardChanges($path);
  222. break 2;
  223. case 's':
  224. if (!$update) {
  225. goto help;
  226. }
  227. $this->stashChanges($path);
  228. break 2;
  229. case 'n':
  230. throw new \RuntimeException('Update aborted');
  231. case 'v':
  232. $this->io->writeError($changes);
  233. break;
  234. case 'd':
  235. $this->viewDiff($path);
  236. break;
  237. case '?':
  238. default:
  239. help:
  240. $this->io->writeError(array(
  241. ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
  242. ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
  243. ' v - view modified files',
  244. ' d - view local modifications (diff)',
  245. ));
  246. if ($update) {
  247. $this->io->writeError(' s - stash changes and try to reapply them after the update');
  248. }
  249. $this->io->writeError(' ? - print help');
  250. break;
  251. }
  252. }
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. protected function reapplyChanges($path)
  258. {
  259. $path = $this->normalizePath($path);
  260. if ($this->hasStashedChanges) {
  261. $this->hasStashedChanges = false;
  262. $this->io->writeError(' <info>Re-applying stashed changes</info>');
  263. if (0 !== $this->process->execute('git stash pop', $output, $path)) {
  264. throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput());
  265. }
  266. }
  267. $this->hasDiscardedChanges = false;
  268. }
  269. /**
  270. * Updates the given path to the given commit ref
  271. *
  272. * @param string $path
  273. * @param string $reference
  274. * @param string $branch
  275. * @param \DateTime $date
  276. * @throws \RuntimeException
  277. * @return null|string if a string is returned, it is the commit reference that was checked out if the original could not be found
  278. */
  279. protected function updateToCommit($path, $reference, $branch, $date)
  280. {
  281. $force = $this->hasDiscardedChanges || $this->hasStashedChanges ? '-f ' : '';
  282. // This uses the "--" sequence to separate branch from file parameters.
  283. //
  284. // Otherwise git tries the branch name as well as file name.
  285. // If the non-existent branch is actually the name of a file, the file
  286. // is checked out.
  287. $template = 'git checkout '.$force.'%s -- && git reset --hard %1$s --';
  288. $branch = preg_replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch);
  289. $branches = null;
  290. if (0 === $this->process->execute('git branch -r', $output, $path)) {
  291. $branches = $output;
  292. }
  293. // check whether non-commitish are branches or tags, and fetch branches with the remote name
  294. $gitRef = $reference;
  295. if (!preg_match('{^[a-f0-9]{40}$}', $reference)
  296. && $branches
  297. && preg_match('{^\s+composer/'.preg_quote($reference).'$}m', $branches)
  298. ) {
  299. $command = sprintf('git checkout '.$force.'-B %s %s -- && git reset --hard %2$s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$reference));
  300. if (0 === $this->process->execute($command, $output, $path)) {
  301. return;
  302. }
  303. }
  304. // try to checkout branch by name and then reset it so it's on the proper branch name
  305. if (preg_match('{^[a-f0-9]{40}$}', $reference)) {
  306. // add 'v' in front of the branch if it was stripped when generating the pretty name
  307. if (!preg_match('{^\s+composer/'.preg_quote($branch).'$}m', $branches) && preg_match('{^\s+composer/v'.preg_quote($branch).'$}m', $branches)) {
  308. $branch = 'v' . $branch;
  309. }
  310. $command = sprintf('git checkout %s --', ProcessExecutor::escape($branch));
  311. $fallbackCommand = sprintf('git checkout '.$force.'-B %s %s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$branch));
  312. if (0 === $this->process->execute($command, $output, $path)
  313. || 0 === $this->process->execute($fallbackCommand, $output, $path)
  314. ) {
  315. $command = sprintf('git reset --hard %s --', ProcessExecutor::escape($reference));
  316. if (0 === $this->process->execute($command, $output, $path)) {
  317. return;
  318. }
  319. }
  320. }
  321. $command = sprintf($template, ProcessExecutor::escape($gitRef));
  322. if (0 === $this->process->execute($command, $output, $path)) {
  323. return;
  324. }
  325. // reference was not found (prints "fatal: reference is not a tree: $ref")
  326. if (false !== strpos($this->process->getErrorOutput(), $reference)) {
  327. $this->io->writeError(' <warning>'.$reference.' is gone (history was rewritten?)</warning>');
  328. }
  329. throw new \RuntimeException(GitUtil::sanitizeUrl('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()));
  330. }
  331. protected function updateOriginUrl($path, $url)
  332. {
  333. $this->process->execute(sprintf('git remote set-url origin %s', ProcessExecutor::escape($url)), $output, $path);
  334. $this->setPushUrl($path, $url);
  335. }
  336. protected function setPushUrl($path, $url)
  337. {
  338. // set push url for github projects
  339. if (preg_match('{^(?:https?|git)://'.GitUtil::getGitHubDomainsRegex($this->config).'/([^/]+)/([^/]+?)(?:\.git)?$}', $url, $match)) {
  340. $protocols = $this->config->get('github-protocols');
  341. $pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git';
  342. if (!in_array('ssh', $protocols, true)) {
  343. $pushUrl = 'https://' . $match[1] . '/'.$match[2].'/'.$match[3].'.git';
  344. }
  345. $cmd = sprintf('git remote set-url --push origin %s', ProcessExecutor::escape($pushUrl));
  346. $this->process->execute($cmd, $ignoredOutput, $path);
  347. }
  348. }
  349. /**
  350. * {@inheritDoc}
  351. */
  352. protected function getCommitLogs($fromReference, $toReference, $path)
  353. {
  354. $path = $this->normalizePath($path);
  355. $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $toReference);
  356. if (0 !== $this->process->execute($command, $output, $path)) {
  357. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  358. }
  359. return $output;
  360. }
  361. /**
  362. * @param $path
  363. * @throws \RuntimeException
  364. */
  365. protected function discardChanges($path)
  366. {
  367. $path = $this->normalizePath($path);
  368. if (0 !== $this->process->execute('git reset --hard', $output, $path)) {
  369. throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
  370. }
  371. $this->hasDiscardedChanges = true;
  372. }
  373. /**
  374. * @param $path
  375. * @throws \RuntimeException
  376. */
  377. protected function stashChanges($path)
  378. {
  379. $path = $this->normalizePath($path);
  380. if (0 !== $this->process->execute('git stash --include-untracked', $output, $path)) {
  381. throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
  382. }
  383. $this->hasStashedChanges = true;
  384. }
  385. /**
  386. * @param $path
  387. * @throws \RuntimeException
  388. */
  389. protected function viewDiff($path)
  390. {
  391. $path = $this->normalizePath($path);
  392. if (0 !== $this->process->execute('git diff HEAD', $output, $path)) {
  393. throw new \RuntimeException("Could not view diff\n\n:".$this->process->getErrorOutput());
  394. }
  395. $this->io->writeError($output);
  396. }
  397. protected function normalizePath($path)
  398. {
  399. if (Platform::isWindows() && strlen($path) > 0) {
  400. $basePath = $path;
  401. $removed = array();
  402. while (!is_dir($basePath) && $basePath !== '\\') {
  403. array_unshift($removed, basename($basePath));
  404. $basePath = dirname($basePath);
  405. }
  406. if ($basePath === '\\') {
  407. return $path;
  408. }
  409. $path = rtrim(realpath($basePath) . '/' . implode('/', $removed), '/');
  410. }
  411. return $path;
  412. }
  413. /**
  414. * {@inheritDoc}
  415. */
  416. protected function hasMetadataRepository($path)
  417. {
  418. $path = $this->normalizePath($path);
  419. return is_dir($path.'/.git');
  420. }
  421. }