VcsRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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;
  12. use Composer\Downloader\TransportException;
  13. use Composer\Repository\Vcs\VcsDriverInterface;
  14. use Composer\Package\Version\VersionParser;
  15. use Composer\Package\Loader\ArrayLoader;
  16. use Composer\Package\Loader\ValidatingArrayLoader;
  17. use Composer\Package\Loader\InvalidPackageException;
  18. use Composer\Package\Loader\LoaderInterface;
  19. use Composer\EventDispatcher\EventDispatcher;
  20. use Composer\IO\IOInterface;
  21. use Composer\Config;
  22. /**
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInterface
  26. {
  27. protected $url;
  28. protected $packageName;
  29. protected $verbose;
  30. protected $io;
  31. protected $config;
  32. protected $versionParser;
  33. protected $type;
  34. protected $loader;
  35. protected $repoConfig;
  36. protected $branchErrorOccurred = false;
  37. private $drivers;
  38. public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $dispatcher = null, array $drivers = null)
  39. {
  40. parent::__construct();
  41. $this->drivers = $drivers ?: array(
  42. 'github' => 'Composer\Repository\Vcs\GitHubDriver',
  43. 'gitlab' => 'Composer\Repository\Vcs\GitLabDriver',
  44. 'git-bitbucket' => 'Composer\Repository\Vcs\GitBitbucketDriver',
  45. 'git' => 'Composer\Repository\Vcs\GitDriver',
  46. 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver',
  47. 'hg' => 'Composer\Repository\Vcs\HgDriver',
  48. 'perforce' => 'Composer\Repository\Vcs\PerforceDriver',
  49. // svn must be last because identifying a subversion server for sure is practically impossible
  50. 'svn' => 'Composer\Repository\Vcs\SvnDriver',
  51. );
  52. $this->url = $repoConfig['url'];
  53. $this->io = $io;
  54. $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs';
  55. $this->verbose = $io->isVeryVerbose();
  56. $this->config = $config;
  57. $this->repoConfig = $repoConfig;
  58. }
  59. public function getRepoConfig()
  60. {
  61. return $this->repoConfig;
  62. }
  63. public function setLoader(LoaderInterface $loader)
  64. {
  65. $this->loader = $loader;
  66. }
  67. public function getDriver()
  68. {
  69. if (isset($this->drivers[$this->type])) {
  70. $class = $this->drivers[$this->type];
  71. $driver = new $class($this->repoConfig, $this->io, $this->config);
  72. /** @var VcsDriverInterface $driver */
  73. $driver->initialize();
  74. return $driver;
  75. }
  76. foreach ($this->drivers as $driver) {
  77. if ($driver::supports($this->io, $this->config, $this->url)) {
  78. $driver = new $driver($this->repoConfig, $this->io, $this->config);
  79. /** @var VcsDriverInterface $driver */
  80. $driver->initialize();
  81. return $driver;
  82. }
  83. }
  84. foreach ($this->drivers as $driver) {
  85. if ($driver::supports($this->io, $this->config, $this->url, true)) {
  86. $driver = new $driver($this->repoConfig, $this->io, $this->config);
  87. /** @var VcsDriverInterface $driver */
  88. $driver->initialize();
  89. return $driver;
  90. }
  91. }
  92. }
  93. public function hadInvalidBranches()
  94. {
  95. return $this->branchErrorOccurred;
  96. }
  97. protected function initialize()
  98. {
  99. parent::initialize();
  100. $verbose = $this->verbose;
  101. $driver = $this->getDriver();
  102. if (!$driver) {
  103. throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
  104. }
  105. $this->versionParser = new VersionParser;
  106. if (!$this->loader) {
  107. $this->loader = new ArrayLoader($this->versionParser);
  108. }
  109. try {
  110. if ($driver->hasComposerFile($driver->getRootIdentifier())) {
  111. $data = $driver->getComposerInformation($driver->getRootIdentifier());
  112. $this->packageName = !empty($data['name']) ? $data['name'] : null;
  113. }
  114. } catch (\Exception $e) {
  115. if ($verbose) {
  116. $this->io->writeError('<error>Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().'</error>');
  117. }
  118. }
  119. foreach ($driver->getTags() as $tag => $identifier) {
  120. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $tag . '</comment>)';
  121. if ($verbose) {
  122. $this->io->writeError($msg);
  123. } else {
  124. $this->io->overwriteError($msg, false);
  125. }
  126. // strip the release- prefix from tags if present
  127. $tag = str_replace('release-', '', $tag);
  128. if (!$parsedTag = $this->validateTag($tag)) {
  129. if ($verbose) {
  130. $this->io->writeError('<warning>Skipped tag '.$tag.', invalid tag name</warning>');
  131. }
  132. continue;
  133. }
  134. try {
  135. if (!$data = $driver->getComposerInformation($identifier)) {
  136. if ($verbose) {
  137. $this->io->writeError('<warning>Skipped tag '.$tag.', no composer file</warning>');
  138. }
  139. continue;
  140. }
  141. // manually versioned package
  142. if (isset($data['version'])) {
  143. $data['version_normalized'] = $this->versionParser->normalize($data['version']);
  144. } else {
  145. // auto-versioned package, read value from tag
  146. $data['version'] = $tag;
  147. $data['version_normalized'] = $parsedTag;
  148. }
  149. // make sure tag packages have no -dev flag
  150. $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']);
  151. $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']);
  152. // broken package, version doesn't match tag
  153. if ($data['version_normalized'] !== $parsedTag) {
  154. if ($verbose) {
  155. $this->io->writeError('<warning>Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json</warning>');
  156. }
  157. continue;
  158. }
  159. if ($verbose) {
  160. $this->io->writeError('Importing tag '.$tag.' ('.$data['version_normalized'].')');
  161. }
  162. $this->addPackage($this->loader->load($this->preProcess($driver, $data, $identifier)));
  163. } catch (\Exception $e) {
  164. if ($verbose) {
  165. $this->io->writeError('<warning>Skipped tag '.$tag.', '.($e instanceof TransportException ? 'no composer file was found' : $e->getMessage()).'</warning>');
  166. }
  167. continue;
  168. }
  169. }
  170. if (!$verbose) {
  171. $this->io->overwriteError('', false);
  172. }
  173. foreach ($driver->getBranches() as $branch => $identifier) {
  174. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $branch . '</comment>)';
  175. if ($verbose) {
  176. $this->io->writeError($msg);
  177. } else {
  178. $this->io->overwriteError($msg, false);
  179. }
  180. if (!$parsedBranch = $this->validateBranch($branch)) {
  181. if ($verbose) {
  182. $this->io->writeError('<warning>Skipped branch '.$branch.', invalid name</warning>');
  183. }
  184. continue;
  185. }
  186. try {
  187. if (!$data = $driver->getComposerInformation($identifier)) {
  188. if ($verbose) {
  189. $this->io->writeError('<warning>Skipped branch '.$branch.', no composer file</warning>');
  190. }
  191. continue;
  192. }
  193. // branches are always auto-versioned, read value from branch name
  194. $data['version'] = $branch;
  195. $data['version_normalized'] = $parsedBranch;
  196. // make sure branch packages have a dev flag
  197. if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
  198. $data['version'] = 'dev-' . $data['version'];
  199. } else {
  200. $prefix = substr($branch, 0, 1) === 'v' ? 'v' : '';
  201. $data['version'] = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
  202. }
  203. if ($verbose) {
  204. $this->io->writeError('Importing branch '.$branch.' ('.$data['version'].')');
  205. }
  206. $packageData = $this->preProcess($driver, $data, $identifier);
  207. $package = $this->loader->load($packageData);
  208. if ($this->loader instanceof ValidatingArrayLoader && $this->loader->getWarnings()) {
  209. throw new InvalidPackageException($this->loader->getErrors(), $this->loader->getWarnings(), $packageData);
  210. }
  211. $this->addPackage($package);
  212. } catch (TransportException $e) {
  213. if ($verbose) {
  214. $this->io->writeError('<warning>Skipped branch '.$branch.', no composer file was found</warning>');
  215. }
  216. continue;
  217. } catch (\Exception $e) {
  218. if (!$verbose) {
  219. $this->io->writeError('');
  220. }
  221. $this->branchErrorOccurred = true;
  222. $this->io->writeError('<error>Skipped branch '.$branch.', '.$e->getMessage().'</error>');
  223. $this->io->writeError('');
  224. continue;
  225. }
  226. }
  227. $driver->cleanup();
  228. if (!$verbose) {
  229. $this->io->overwriteError('', false);
  230. }
  231. if (!$this->getPackages()) {
  232. throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.');
  233. }
  234. }
  235. protected function preProcess(VcsDriverInterface $driver, array $data, $identifier)
  236. {
  237. // keep the name of the main identifier for all packages
  238. $data['name'] = $this->packageName ?: $data['name'];
  239. if (!isset($data['dist'])) {
  240. $data['dist'] = $driver->getDist($identifier);
  241. }
  242. if (!isset($data['source'])) {
  243. $data['source'] = $driver->getSource($identifier);
  244. }
  245. return $data;
  246. }
  247. private function validateBranch($branch)
  248. {
  249. try {
  250. return $this->versionParser->normalizeBranch($branch);
  251. } catch (\Exception $e) {
  252. }
  253. return false;
  254. }
  255. private function validateTag($version)
  256. {
  257. try {
  258. return $this->versionParser->normalize($version);
  259. } catch (\Exception $e) {
  260. }
  261. return false;
  262. }
  263. }