GitDownloader.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class GitDownloader extends VcsDownloader
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function doDownload(PackageInterface $package, $path)
  23. {
  24. $url = escapeshellarg($package->getSourceUrl());
  25. $ref = escapeshellarg($package->getSourceReference());
  26. $path = escapeshellarg($path);
  27. $this->io->write(" Cloning ".$package->getSourceReference());
  28. $command = sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref);
  29. if (0 !== $this->process->execute($command, $ignoredOutput)) {
  30. throw new \RuntimeException('Failed to execute ' . $command);
  31. }
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
  37. {
  38. $ref = escapeshellarg($target->getSourceReference());
  39. $path = escapeshellarg($path);
  40. $this->io->write(" Checking out ".$target->getSourceReference());
  41. $command = sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $ref);
  42. if (0 !== $this->process->execute($command, $ignoredOutput)) {
  43. throw new \RuntimeException('Failed to execute ' . $command);
  44. }
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function enforceCleanDirectory($path)
  50. {
  51. $command = sprintf('cd %s && git status --porcelain', escapeshellarg($path));
  52. if (0 !== $this->process->execute($command, $output)) {
  53. throw new \RuntimeException('Failed to execute ' . $command);
  54. }
  55. if (trim($output)) {
  56. throw new \RuntimeException('Source directory has uncommitted changes');
  57. }
  58. }
  59. }