GitDownloader.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class GitDownloader 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. if (!extension_loaded('openssl')) {
  34. throw new \RuntimeException('You must enable the openssl extension to clone git repositories');
  35. }
  36. $url = escapeshellarg($package->getSourceUrl());
  37. $ref = escapeshellarg($package->getSourceReference());
  38. system(sprintf('git clone %s %s && cd %2$s && git reset --hard %s', $url, $path, $ref));
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function update(PackageInterface $initial, PackageInterface $target, $path)
  44. {
  45. if (!$target->getSourceReference()) {
  46. throw new \InvalidArgumentException('The given package is missing reference information');
  47. }
  48. $this->enforceCleanDirectory($path);
  49. system(sprintf('cd %s && git fetch && git reset --hard %s', $path, $target->getSourceReference()));
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function remove(PackageInterface $package, $path)
  55. {
  56. $this->enforceCleanDirectory($path);
  57. $fs = new Util\Filesystem();
  58. $fs->remove($path);
  59. }
  60. private function enforceCleanDirectory($path)
  61. {
  62. exec(sprintf('cd %s && git status --porcelain', $path), $output);
  63. if (implode('', $output)) {
  64. throw new \RuntimeException('Source directory has uncommitted changes');
  65. }
  66. }
  67. }