HgDownloader.php 1.9 KB

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