FossilDriver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\Repository\Vcs;
  12. use Composer\Config;
  13. use Composer\Util\ProcessExecutor;
  14. use Composer\Util\Filesystem;
  15. use Composer\IO\IOInterface;
  16. /**
  17. * @author BohwaZ <http://bohwaz.net/>
  18. */
  19. class FossilDriver extends VcsDriver
  20. {
  21. protected $tags;
  22. protected $branches;
  23. protected $rootIdentifier;
  24. protected $repoFile;
  25. protected $checkoutDir;
  26. protected $infoCache = array();
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function initialize()
  31. {
  32. if (Filesystem::isLocalPath($this->url)) {
  33. $this->checkoutDir = $this->url;
  34. } else {
  35. $this->repoFile = $this->config->get('cache-repo-dir') . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '.fossil';
  36. $this->checkoutDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/';
  37. $fs = new Filesystem();
  38. $fs->ensureDirectoryExists($this->checkoutDir);
  39. if (!is_writable(dirname($this->checkoutDir))) {
  40. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.$this->checkoutDir.'" directory is not writable by the current user.');
  41. }
  42. // Ensure we are allowed to use this URL by config
  43. $this->config->prohibitUrlByConfig($this->url, $this->io);
  44. // update the repo if it is a valid fossil repository
  45. if (is_file($this->repoFile) && is_dir($this->checkoutDir) && 0 === $this->process->execute('fossil info', $output, $this->checkoutDir)) {
  46. if (0 !== $this->process->execute('fossil pull', $output, $this->checkoutDir)) {
  47. $this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  48. }
  49. } else {
  50. // clean up directory and do a fresh clone into it
  51. $fs->removeDirectory($this->checkoutDir);
  52. $fs->remove($this->repoFile);
  53. $fs->ensureDirectoryExists($this->checkoutDir);
  54. if (0 !== $this->process->execute(sprintf('fossil clone %s %s', ProcessExecutor::escape($this->url), ProcessExecutor::escape($this->repoFile)), $output)) {
  55. $output = $this->process->getErrorOutput();
  56. if (0 !== $this->process->execute('fossil version', $ignoredOutput)) {
  57. throw new \RuntimeException('Failed to clone '.$this->url.', fossil was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
  58. }
  59. throw new \RuntimeException('Failed to clone '.$this->url.' to repository ' . $this->repoFile . "\n\n" .$output);
  60. }
  61. if (0 !== $this->process->execute(sprintf('fossil open %s', ProcessExecutor::escape($this->repoFile)), $output, $this->checkoutDir)) {
  62. $output = $this->process->getErrorOutput();
  63. throw new \RuntimeException('Failed to open repository '.$this->repoFile.' in ' . $this->checkoutDir . "\n\n" .$output);
  64. }
  65. }
  66. }
  67. $this->getTags();
  68. $this->getBranches();
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function getRootIdentifier()
  74. {
  75. if (null === $this->rootIdentifier) {
  76. $this->rootIdentifier = 'trunk';
  77. }
  78. return $this->rootIdentifier;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function getUrl()
  84. {
  85. return $this->url;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getSource($identifier)
  91. {
  92. return array('type' => 'fossil', 'url' => $this->getUrl(), 'reference' => $identifier);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getDist($identifier)
  98. {
  99. return null;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getFileContent($file, $identifier)
  105. {
  106. $command = sprintf('fossil cat -r %s %s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
  107. $this->process->execute($command, $content, $this->checkoutDir);
  108. if (!trim($content)) {
  109. return null;
  110. }
  111. return $content;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getChangeDate($identifier)
  117. {
  118. $this->process->execute(sprintf('fossil finfo composer.json | head -n 2 | tail -n 1 | awk \'{print $1}\''), $output, $this->checkoutDir);
  119. return new \DateTime(trim($output), new \DateTimeZone('UTC'));
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function getTags()
  125. {
  126. if (null === $this->tags) {
  127. $tags = array();
  128. $this->process->execute('fossil tag list', $output, $this->checkoutDir);
  129. foreach ($this->process->splitLines($output) as $tag) {
  130. $tags[$tag] = $tag;
  131. }
  132. $this->tags = $tags;
  133. }
  134. return $this->tags;
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public function getBranches()
  140. {
  141. if (null === $this->branches) {
  142. $branches = array();
  143. $bookmarks = array();
  144. $this->process->execute('fossil branch list', $output, $this->checkoutDir);
  145. foreach ($this->process->splitLines($output) as $branch) {
  146. $branch = trim(preg_replace('/^\*/', '', trim($branch)));
  147. $branches[$branch] = $branch;
  148. }
  149. $this->branches = $branches;
  150. }
  151. return $this->branches;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  157. {
  158. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?(?:chiselapp\.com|fossil\.))#i', $url)) {
  159. return true;
  160. }
  161. if (preg_match('!/fossil/|\.fossil!', $url)) {
  162. return true;
  163. }
  164. // local filesystem
  165. if (Filesystem::isLocalPath($url)) {
  166. $url = Filesystem::getPlatformPath($url);
  167. if (!is_dir($url)) {
  168. return false;
  169. }
  170. $process = new ProcessExecutor();
  171. // check whether there is a fossil repo in that path
  172. if ($process->execute('fossil info', $output, $url) === 0) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. }