VcsRepository.php 17 KB

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