VcsRepository.php 12 KB

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