FossilDownloader.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 BohwaZ <http://bohwaz.net/>
  16. */
  17. class FossilDownloader extends VcsDownloader
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function doDownload(PackageInterface $package, $path, $url)
  23. {
  24. // Ensure we are allowed to use this URL by config
  25. $this->config->prohibitUrlByConfig($url, $this->io);
  26. $url = ProcessExecutor::escape($url);
  27. $ref = ProcessExecutor::escape($package->getSourceReference());
  28. $repoFile = $path . '.fossil';
  29. $this->io->writeError("Cloning ".$package->getSourceReference());
  30. $command = sprintf('fossil clone %s %s', $url, ProcessExecutor::escape($repoFile));
  31. if (0 !== $this->process->execute($command, $ignoredOutput)) {
  32. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  33. }
  34. $command = sprintf('fossil open %s --nested', ProcessExecutor::escape($repoFile));
  35. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  36. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  37. }
  38. $command = sprintf('fossil update %s', $ref);
  39. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  40. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  41. }
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
  47. {
  48. // Ensure we are allowed to use this URL by config
  49. $this->config->prohibitUrlByConfig($url, $this->io);
  50. $url = ProcessExecutor::escape($url);
  51. $ref = ProcessExecutor::escape($target->getSourceReference());
  52. $this->io->writeError(" Updating to ".$target->getSourceReference());
  53. if (!$this->hasMetadataRepository($path)) {
  54. throw new \RuntimeException('The .fslckout file is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
  55. }
  56. $command = sprintf('fossil pull && fossil up %s', $ref);
  57. if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) {
  58. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  59. }
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getLocalChanges(PackageInterface $package, $path)
  65. {
  66. if (!$this->hasMetadataRepository($path)) {
  67. return null;
  68. }
  69. $this->process->execute('fossil changes', $output, realpath($path));
  70. return trim($output) ?: null;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. protected function getCommitLogs($fromReference, $toReference, $path)
  76. {
  77. $command = sprintf('fossil timeline -t ci -W 0 -n 0 before %s', ProcessExecutor::escape($toReference));
  78. if (0 !== $this->process->execute($command, $output, realpath($path))) {
  79. throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
  80. }
  81. $log = '';
  82. $match = '/\d\d:\d\d:\d\d\s+\[' . $toReference . '\]/';
  83. foreach ($this->process->splitLines($output) as $line) {
  84. if (preg_match($match, $line)) {
  85. break;
  86. }
  87. $log .= $line;
  88. }
  89. return $log;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. protected function hasMetadataRepository($path)
  95. {
  96. return is_file($path . '/.fslckout') || is_file($path . '/_FOSSIL_');
  97. }
  98. }