HgDownloader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. use Composer\Util\Hg as HgUtils;
  15. /**
  16. * @author Per Bernhardt <plb@webfactory.de>
  17. */
  18. class HgDownloader extends VcsDownloader
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function doInstall(PackageInterface $package, $path, $url)
  24. {
  25. $hgUtils = new HgUtils($this->io, $this->config, $this->process);
  26. $cloneCommand = function ($url) use ($path) {
  27. return sprintf('hg clone %s %s', ProcessExecutor::escape($url), ProcessExecutor::escape($path));
  28. };
  29. $hgUtils->runCommand($cloneCommand, $url, $path);
  30. $ref = ProcessExecutor::escape($package->getSourceReference());
  31. $command = sprintf('hg up %s', $ref);
  32. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  33. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  34. }
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  40. {
  41. $hgUtils = new HgUtils($this->io, $this->config, $this->process);
  42. $ref = $target->getSourceReference();
  43. $this->io->writeError(" Updating to ".$target->getSourceReference());
  44. if (!$this->hasMetadataRepository($path)) {
  45. throw new \RuntimeException('The .hg directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  46. }
  47. $command = function ($url) use ($ref) {
  48. return sprintf('hg pull %s && hg up %s', ProcessExecutor::escape($url), ProcessExecutor::escape($ref));
  49. };
  50. $hgUtils->runCommand($command, $url, $path);
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function getLocalChanges(PackageInterface $package, $path)
  56. {
  57. if (!is_dir($path.'/.hg')) {
  58. return null;
  59. }
  60. $this->process->execute('hg st', $output, realpath($path));
  61. return trim($output) ?: null;
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. protected function getCommitLogs($fromReference, $toReference, $path)
  67. {
  68. $command = sprintf('hg log -r %s:%s --style compact', ProcessExecutor::escape($fromReference), ProcessExecutor::escape($toReference));
  69. if (0 !== $this->process->execute($command, $output, realpath($path))) {
  70. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  71. }
  72. return $output;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. protected function hasMetadataRepository($path)
  78. {
  79. return is_dir($path . '/.hg');
  80. }
  81. }