HgDownloader.php 3.4 KB

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