FossilDriver.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // Make sure fossil is installed and reachable.
  33. $this->checkFossil();
  34. // Ensure we are allowed to use this URL by config.
  35. $this->config->prohibitUrlByConfig($this->url, $this->io);
  36. // Only if url points to a locally accessible directory, assume it's the checkout directory.
  37. // Otherwise, it should be something fossil can clone from.
  38. if (Filesystem::isLocalPath($this->url) && is_dir($this->url)) {
  39. $this->checkoutDir = $this->url;
  40. } else {
  41. $localName = preg_replace('{[^a-z0-9]}i', '-', $this->url);
  42. $this->repoFile = $this->config->get('cache-repo-dir') . '/' . $localName . '.fossil';
  43. $this->checkoutDir = $this->config->get('cache-vcs-dir') . '/' . $localName . '/';
  44. $this->updateLocalRepo();
  45. }
  46. $this->getTags();
  47. $this->getBranches();
  48. }
  49. /**
  50. * Check that fossil can be invoked via command line.
  51. */
  52. protected function checkFossil()
  53. {
  54. if (0 !== $this->process->execute('fossil version', $ignoredOutput)) {
  55. throw new \RuntimeException("fossil was not found, check that it is installed and in your PATH env.\n\n" . $this->process->getErrorOutput());
  56. }
  57. }
  58. /**
  59. * Clone or update existing local fossil repository.
  60. */
  61. protected function updateLocalRepo()
  62. {
  63. $fs = new Filesystem();
  64. $fs->ensureDirectoryExists($this->checkoutDir);
  65. if (!is_writable(dirname($this->checkoutDir))) {
  66. throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.$this->checkoutDir.'" directory is not writable by the current user.');
  67. }
  68. // update the repo if it is a valid fossil repository
  69. if (is_file($this->repoFile) && is_dir($this->checkoutDir) && 0 === $this->process->execute('fossil info', $output, $this->checkoutDir)) {
  70. if (0 !== $this->process->execute('fossil pull', $output, $this->checkoutDir)) {
  71. $this->io->writeError('<error>Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')</error>');
  72. }
  73. } else {
  74. // clean up directory and do a fresh clone into it
  75. $fs->removeDirectory($this->checkoutDir);
  76. $fs->remove($this->repoFile);
  77. $fs->ensureDirectoryExists($this->checkoutDir);
  78. if (0 !== $this->process->execute(sprintf('fossil clone %s %s', ProcessExecutor::escape($this->url), ProcessExecutor::escape($this->repoFile)), $output)) {
  79. $output = $this->process->getErrorOutput();
  80. throw new \RuntimeException('Failed to clone '.$this->url.' to repository ' . $this->repoFile . "\n\n" .$output);
  81. }
  82. if (0 !== $this->process->execute(sprintf('fossil open %s --nested', ProcessExecutor::escape($this->repoFile)), $output, $this->checkoutDir)) {
  83. $output = $this->process->getErrorOutput();
  84. throw new \RuntimeException('Failed to open repository '.$this->repoFile.' in ' . $this->checkoutDir . "\n\n" .$output);
  85. }
  86. }
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getRootIdentifier()
  92. {
  93. if (null === $this->rootIdentifier) {
  94. $this->rootIdentifier = 'trunk';
  95. }
  96. return $this->rootIdentifier;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function getUrl()
  102. {
  103. return $this->url;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function getSource($identifier)
  109. {
  110. return array('type' => 'fossil', 'url' => $this->getUrl(), 'reference' => $identifier);
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function getDist($identifier)
  116. {
  117. return null;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getFileContent($file, $identifier)
  123. {
  124. $command = sprintf('fossil cat -r %s %s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
  125. $this->process->execute($command, $content, $this->checkoutDir);
  126. if (!trim($content)) {
  127. return null;
  128. }
  129. return $content;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getChangeDate($identifier)
  135. {
  136. $this->process->execute('fossil finfo -b -n 1 composer.json', $output, $this->checkoutDir);
  137. list($ckout, $date, $message) = explode(' ', trim($output), 3);
  138. return new \DateTime($date, new \DateTimeZone('UTC'));
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. public function getTags()
  144. {
  145. if (null === $this->tags) {
  146. $tags = array();
  147. $this->process->execute('fossil tag list', $output, $this->checkoutDir);
  148. foreach ($this->process->splitLines($output) as $tag) {
  149. $tags[$tag] = $tag;
  150. }
  151. $this->tags = $tags;
  152. }
  153. return $this->tags;
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public function getBranches()
  159. {
  160. if (null === $this->branches) {
  161. $branches = array();
  162. $bookmarks = array();
  163. $this->process->execute('fossil branch list', $output, $this->checkoutDir);
  164. foreach ($this->process->splitLines($output) as $branch) {
  165. $branch = trim(preg_replace('/^\*/', '', trim($branch)));
  166. $branches[$branch] = $branch;
  167. }
  168. $this->branches = $branches;
  169. }
  170. return $this->branches;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public static function supports(IOInterface $io, Config $config, $url, $deep = false)
  176. {
  177. if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?(?:chiselapp\.com|fossil\.))#i', $url)) {
  178. return true;
  179. }
  180. if (preg_match('!/fossil/|\.fossil!', $url)) {
  181. return true;
  182. }
  183. // local filesystem
  184. if (Filesystem::isLocalPath($url)) {
  185. $url = Filesystem::getPlatformPath($url);
  186. if (!is_dir($url)) {
  187. return false;
  188. }
  189. $process = new ProcessExecutor();
  190. // check whether there is a fossil repo in that path
  191. if ($process->execute('fossil info', $output, $url) === 0) {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. }