HgDownloader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // Ensure we are allowed to use this URL by config
  25. $this->config->prohibitUrlByConfig($url, $this->io);
  26. $url = ProcessExecutor::escape($url);
  27. $ref = ProcessExecutor::escape($package->getSourceReference());
  28. $this->io->writeError(" Cloning ".$package->getSourceReference());
  29. $command = sprintf('hg clone %s %s', $url, ProcessExecutor::escape($path));
  30. if (0 !== $this->process->execute($command, $ignoredOutput)) {
  31. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  32. }
  33. $command = sprintf('hg up %s', $ref);
  34. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  35. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  36. }
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  42. {
  43. // Ensure we are allowed to use this URL by config
  44. $this->config->prohibitUrlByConfig($url, $this->io);
  45. $url = ProcessExecutor::escape($url);
  46. $ref = ProcessExecutor::escape($target->getSourceReference());
  47. $this->io->writeError(" Updating to ".$target->getSourceReference());
  48. if (!$this->hasMetadataRepository($path)) {
  49. throw new \RuntimeException('The .hg directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  50. }
  51. $command = sprintf('hg pull %s && hg up %s', $url, $ref);
  52. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  53. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  54. }
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function getLocalChanges(PackageInterface $package, $path)
  60. {
  61. if (!is_dir($path.'/.hg')) {
  62. return null;
  63. }
  64. $this->process->execute('hg st', $output, realpath($path));
  65. return trim($output) ?: null;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. protected function getCommitLogs($fromReference, $toReference, $path)
  71. {
  72. $command = sprintf('hg log -r %s:%s --style compact', $fromReference, $toReference);
  73. if (0 !== $this->process->execute($command, $output, realpath($path))) {
  74. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  75. }
  76. return $output;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. protected function hasMetadataRepository($path)
  82. {
  83. return is_dir($path . '/.hg');
  84. }
  85. }