HgDownloader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  15. * @author Per Bernhardt <plb@webfactory.de>
  16. */
  17. class HgDownloader extends VcsDownloader
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function doDownload(PackageInterface $package, $path, $url)
  23. {
  24. $url = ProcessExecutor::escape($url);
  25. $ref = ProcessExecutor::escape($package->getSourceReference());
  26. $this->io->writeError(" Cloning ".$package->getSourceReference());
  27. $command = sprintf('hg clone %s %s', $url, ProcessExecutor::escape($path));
  28. if (0 !== $this->process->execute($command, $ignoredOutput)) {
  29. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  30. }
  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. $url = ProcessExecutor::escape($url);
  42. $ref = ProcessExecutor::escape($target->getSourceReference());
  43. $this->io->writeError(" Updating to ".$target->getSourceReference());
  44. if (!is_dir($path.'/.hg')) {
  45. throw new \RuntimeException('The .hg directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  46. }
  47. $command = sprintf('hg pull %s && hg up %s', $url, $ref);
  48. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  49. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  50. }
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function getLocalChanges(PackageInterface $package, $path)
  56. {
  57. if (!is_dir($path.'/.hg')) {
  58. return;
  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', $fromReference, $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. }