VcsRepository.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 $isVerbose;
  30. protected $isVeryVerbose;
  31. protected $io;
  32. protected $config;
  33. protected $versionParser;
  34. protected $type;
  35. protected $loader;
  36. protected $repoConfig;
  37. protected $branchErrorOccurred = false;
  38. private $drivers;
  39. /** @var VcsDriverInterface */
  40. private $driver;
  41. /** @var VersionCacheInterface */
  42. private $versionCache;
  43. private $emptyReferences = array();
  44. public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $dispatcher = null, array $drivers = null, VersionCacheInterface $versionCache = null)
  45. {
  46. parent::__construct();
  47. $this->drivers = $drivers ?: array(
  48. 'github' => 'Composer\Repository\Vcs\GitHubDriver',
  49. 'gitlab' => 'Composer\Repository\Vcs\GitLabDriver',
  50. 'git-bitbucket' => 'Composer\Repository\Vcs\GitBitbucketDriver',
  51. 'git' => 'Composer\Repository\Vcs\GitDriver',
  52. 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver',
  53. 'hg' => 'Composer\Repository\Vcs\HgDriver',
  54. 'perforce' => 'Composer\Repository\Vcs\PerforceDriver',
  55. 'fossil' => 'Composer\Repository\Vcs\FossilDriver',
  56. // svn must be last because identifying a subversion server for sure is practically impossible
  57. 'svn' => 'Composer\Repository\Vcs\SvnDriver',
  58. );
  59. $this->url = $repoConfig['url'];
  60. $this->io = $io;
  61. $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs';
  62. $this->isVerbose = $io->isVerbose();
  63. $this->isVeryVerbose = $io->isVeryVerbose();
  64. $this->config = $config;
  65. $this->repoConfig = $repoConfig;
  66. $this->versionCache = $versionCache;
  67. }
  68. public function getRepoConfig()
  69. {
  70. return $this->repoConfig;
  71. }
  72. public function setLoader(LoaderInterface $loader)
  73. {
  74. $this->loader = $loader;
  75. }
  76. public function getDriver()
  77. {
  78. if ($this->driver) {
  79. return $this->driver;
  80. }
  81. if (isset($this->drivers[$this->type])) {
  82. $class = $this->drivers[$this->type];
  83. $this->driver = new $class($this->repoConfig, $this->io, $this->config);
  84. $this->driver->initialize();
  85. return $this->driver;
  86. }
  87. foreach ($this->drivers as $driver) {
  88. if ($driver::supports($this->io, $this->config, $this->url)) {
  89. $this->driver = new $driver($this->repoConfig, $this->io, $this->config);
  90. $this->driver->initialize();
  91. return $this->driver;
  92. }
  93. }
  94. foreach ($this->drivers as $driver) {
  95. if ($driver::supports($this->io, $this->config, $this->url, true)) {
  96. $this->driver = new $driver($this->repoConfig, $this->io, $this->config);
  97. $this->driver->initialize();
  98. return $this->driver;
  99. }
  100. }
  101. }
  102. public function hadInvalidBranches()
  103. {
  104. return $this->branchErrorOccurred;
  105. }
  106. public function getEmptyReferences()
  107. {
  108. return $this->emptyReferences;
  109. }
  110. protected function initialize()
  111. {
  112. parent::initialize();
  113. $isVerbose = $this->isVerbose;
  114. $isVeryVerbose = $this->isVeryVerbose;
  115. $driver = $this->getDriver();
  116. if (!$driver) {
  117. throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url);
  118. }
  119. $this->versionParser = new VersionParser;
  120. if (!$this->loader) {
  121. $this->loader = new ArrayLoader($this->versionParser);
  122. }
  123. try {
  124. if ($driver->hasComposerFile($driver->getRootIdentifier())) {
  125. $data = $driver->getComposerInformation($driver->getRootIdentifier());
  126. $this->packageName = !empty($data['name']) ? $data['name'] : null;
  127. }
  128. } catch (\Exception $e) {
  129. if ($isVeryVerbose) {
  130. $this->io->writeError('<error>Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().'</error>');
  131. }
  132. }
  133. foreach ($driver->getTags() as $tag => $identifier) {
  134. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $tag . '</comment>)';
  135. if ($isVeryVerbose) {
  136. $this->io->writeError($msg);
  137. } elseif ($isVerbose) {
  138. $this->io->overwriteError($msg, false);
  139. }
  140. // strip the release- prefix from tags if present
  141. $tag = str_replace('release-', '', $tag);
  142. $cachedPackage = $this->getCachedPackageVersion($tag, $identifier, $isVerbose, $isVeryVerbose);
  143. if ($cachedPackage) {
  144. $this->addPackage($cachedPackage);
  145. continue;
  146. } elseif ($cachedPackage === false) {
  147. $this->emptyReferences[] = $identifier;
  148. continue;
  149. }
  150. if (!$parsedTag = $this->validateTag($tag)) {
  151. if ($isVeryVerbose) {
  152. $this->io->writeError('<warning>Skipped tag '.$tag.', invalid tag name</warning>');
  153. }
  154. continue;
  155. }
  156. try {
  157. if (!$data = $driver->getComposerInformation($identifier)) {
  158. if ($isVeryVerbose) {
  159. $this->io->writeError('<warning>Skipped tag '.$tag.', no composer file</warning>');
  160. }
  161. $this->emptyReferences[] = $identifier;
  162. continue;
  163. }
  164. // manually versioned package
  165. if (isset($data['version'])) {
  166. $data['version_normalized'] = $this->versionParser->normalize($data['version']);
  167. } else {
  168. // auto-versioned package, read value from tag
  169. $data['version'] = $tag;
  170. $data['version_normalized'] = $parsedTag;
  171. }
  172. // make sure tag packages have no -dev flag
  173. $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']);
  174. $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']);
  175. // broken package, version doesn't match tag
  176. if ($data['version_normalized'] !== $parsedTag) {
  177. if ($isVeryVerbose) {
  178. $this->io->writeError('<warning>Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json</warning>');
  179. }
  180. continue;
  181. }
  182. $tagPackageName = isset($data['name']) ? $data['name'] : $this->packageName;
  183. if ($existingPackage = $this->findPackage($tagPackageName, $data['version_normalized'])) {
  184. if ($isVeryVerbose) {
  185. $this->io->writeError('<warning>Skipped tag '.$tag.', it conflicts with an another tag ('.$existingPackage->getPrettyVersion().') as both resolve to '.$data['version_normalized'].' internally</warning>');
  186. }
  187. continue;
  188. }
  189. if ($isVeryVerbose) {
  190. $this->io->writeError('Importing tag '.$tag.' ('.$data['version_normalized'].')');
  191. }
  192. $this->addPackage($this->loader->load($this->preProcess($driver, $data, $identifier)));
  193. } catch (\Exception $e) {
  194. if ($e instanceof TransportException && $e->getCode() === 404) {
  195. $this->emptyReferences[] = $identifier;
  196. }
  197. if ($isVeryVerbose) {
  198. $this->io->writeError('<warning>Skipped tag '.$tag.', '.($e instanceof TransportException ? 'no composer file was found' : $e->getMessage()).'</warning>');
  199. }
  200. continue;
  201. }
  202. }
  203. if (!$isVeryVerbose) {
  204. $this->io->overwriteError('', false);
  205. }
  206. $branches = $driver->getBranches();
  207. foreach ($branches as $branch => $identifier) {
  208. $msg = 'Reading composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $branch . '</comment>)';
  209. if ($isVeryVerbose) {
  210. $this->io->writeError($msg);
  211. } elseif ($isVerbose) {
  212. $this->io->overwriteError($msg, false);
  213. }
  214. if ($branch === 'trunk' && isset($branches['master'])) {
  215. if ($isVeryVerbose) {
  216. $this->io->writeError('<warning>Skipped branch '.$branch.', can not parse both master and trunk branches as they both resolve to 9999999-dev internally</warning>');
  217. }
  218. continue;
  219. }
  220. if (!$parsedBranch = $this->validateBranch($branch)) {
  221. if ($isVeryVerbose) {
  222. $this->io->writeError('<warning>Skipped branch '.$branch.', invalid name</warning>');
  223. }
  224. continue;
  225. }
  226. // make sure branch packages have a dev flag
  227. if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) {
  228. $version = 'dev-' . $branch;
  229. } else {
  230. $prefix = substr($branch, 0, 1) === 'v' ? 'v' : '';
  231. $version = $prefix . preg_replace('{(\.9{7})+}', '.x', $parsedBranch);
  232. }
  233. $cachedPackage = $this->getCachedPackageVersion($version, $identifier, $isVerbose, $isVeryVerbose);
  234. if ($cachedPackage) {
  235. $this->addPackage($cachedPackage);
  236. continue;
  237. } elseif ($cachedPackage === false) {
  238. $this->emptyReferences[] = $identifier;
  239. continue;
  240. }
  241. try {
  242. if (!$data = $driver->getComposerInformation($identifier)) {
  243. if ($isVeryVerbose) {
  244. $this->io->writeError('<warning>Skipped branch '.$branch.', no composer file</warning>');
  245. }
  246. $this->emptyReferences[] = $identifier;
  247. continue;
  248. }
  249. // branches are always auto-versioned, read value from branch name
  250. $data['version'] = $version;
  251. $data['version_normalized'] = $parsedBranch;
  252. if ($isVeryVerbose) {
  253. $this->io->writeError('Importing branch '.$branch.' ('.$data['version'].')');
  254. }
  255. $packageData = $this->preProcess($driver, $data, $identifier);
  256. $package = $this->loader->load($packageData);
  257. if ($this->loader instanceof ValidatingArrayLoader && $this->loader->getWarnings()) {
  258. throw new InvalidPackageException($this->loader->getErrors(), $this->loader->getWarnings(), $packageData);
  259. }
  260. $this->addPackage($package);
  261. } catch (TransportException $e) {
  262. if ($e->getCode() === 404) {
  263. $this->emptyReferences[] = $identifier;
  264. }
  265. if ($isVeryVerbose) {
  266. $this->io->writeError('<warning>Skipped branch '.$branch.', no composer file was found</warning>');
  267. }
  268. continue;
  269. } catch (\Exception $e) {
  270. if (!$isVeryVerbose) {
  271. $this->io->writeError('');
  272. }
  273. $this->branchErrorOccurred = true;
  274. $this->io->writeError('<error>Skipped branch '.$branch.', '.$e->getMessage().'</error>');
  275. $this->io->writeError('');
  276. continue;
  277. }
  278. }
  279. $driver->cleanup();
  280. if (!$isVeryVerbose) {
  281. $this->io->overwriteError('', false);
  282. }
  283. if (!$this->getPackages()) {
  284. throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.');
  285. }
  286. }
  287. protected function preProcess(VcsDriverInterface $driver, array $data, $identifier)
  288. {
  289. // keep the name of the main identifier for all packages
  290. $dataPackageName = isset($data['name']) ? $data['name'] : null;
  291. $data['name'] = $this->packageName ?: $dataPackageName;
  292. if (!isset($data['dist'])) {
  293. $data['dist'] = $driver->getDist($identifier);
  294. }
  295. if (!isset($data['source'])) {
  296. $data['source'] = $driver->getSource($identifier);
  297. }
  298. return $data;
  299. }
  300. private function validateBranch($branch)
  301. {
  302. try {
  303. return $this->versionParser->normalizeBranch($branch);
  304. } catch (\Exception $e) {
  305. }
  306. return false;
  307. }
  308. private function validateTag($version)
  309. {
  310. try {
  311. return $this->versionParser->normalize($version);
  312. } catch (\Exception $e) {
  313. }
  314. return false;
  315. }
  316. private function getCachedPackageVersion($version, $identifier, $isVerbose, $isVeryVerbose)
  317. {
  318. if (!$this->versionCache) {
  319. return;
  320. }
  321. $cachedPackage = $this->versionCache->getVersionPackage($version, $identifier);
  322. if ($cachedPackage === false) {
  323. if ($isVeryVerbose) {
  324. $this->io->writeError('<warning>Skipped '.$version.', no composer file (cached from ref '.$identifier.')</warning>');
  325. }
  326. return false;
  327. }
  328. if ($cachedPackage) {
  329. $msg = 'Found cached composer.json of <info>' . ($this->packageName ?: $this->url) . '</info> (<comment>' . $version . '</comment>)';
  330. if ($isVeryVerbose) {
  331. $this->io->writeError($msg);
  332. } elseif ($isVerbose) {
  333. $this->io->overwriteError($msg, false);
  334. }
  335. if ($existingPackage = $this->findPackage($cachedPackage['name'], $cachedPackage['version_normalized'])) {
  336. if ($isVeryVerbose) {
  337. $this->io->writeError('<warning>Skipped cached version '.$version.', it conflicts with an another tag ('.$existingPackage->getPrettyVersion().') as both resolve to '.$cachedPackage['version_normalized'].' internally</warning>');
  338. }
  339. $cachedPackage = null;
  340. }
  341. }
  342. if ($cachedPackage) {
  343. return $this->loader->load($cachedPackage);
  344. }
  345. return null;
  346. }
  347. }