HgDownloader.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Process;
  14. /**
  15. * @author Per Bernhardt <plb@webfactory.de>
  16. */
  17. class HgDownloader implements DownloaderInterface
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function getInstallationSource()
  23. {
  24. return 'source';
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function download(PackageInterface $package, $path)
  30. {
  31. if (!$package->getSourceReference()) {
  32. throw new \InvalidArgumentException('The given package is missing reference information');
  33. }
  34. $url = escapeshellarg($package->getSourceUrl());
  35. $ref = escapeshellarg($package->getSourceReference());
  36. Process::execute(sprintf('(hg clone %s %s 2> /dev/null) && cd %2$s && hg up %s', $url, $path, $ref));
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function update(PackageInterface $initial, PackageInterface $target, $path)
  42. {
  43. if (!$target->getSourceReference()) {
  44. throw new \InvalidArgumentException('The given package is missing reference information');
  45. }
  46. $this->enforceCleanDirectory($path);
  47. Process::execute(sprintf('cd %s && hg pull && hg up %s', $path, escapeshellarg($target->getSourceReference())));
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function remove(PackageInterface $package, $path)
  53. {
  54. $this->enforceCleanDirectory($path);
  55. $fs = new Util\Filesystem();
  56. $fs->removeDirectory($path);
  57. }
  58. private function enforceCleanDirectory($path)
  59. {
  60. Process::execute(sprintf('cd %s && hg st', $path), $output);
  61. if (implode('', $output)) {
  62. throw new \RuntimeException('Source directory has uncommitted changes');
  63. }
  64. }
  65. }