FossilDriver.php 6.6 KB

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