From aa6be02c64ab3bb639ed0e57aabb5e8f484aac96 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 8 Apr 2020 17:53:15 +0200 Subject: [PATCH] Allow COMPOSER_DISABLE_NETWORK to work with GitHubDriver by doing a cache priming pass first --- doc/03-cli.md | 3 +++ src/Composer/Repository/ComposerRepository.php | 9 +++------ src/Composer/Repository/Vcs/GitDriver.php | 3 +++ src/Composer/Repository/Vcs/GitHubDriver.php | 10 +++++++++- src/Composer/Util/Git.php | 4 +++- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 725be430b..3a964c390 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -958,4 +958,7 @@ can also set it to `*` to ignore the proxy for all HTTP requests. If set to `1`, disables network access (best effort). This can be used for debugging or to run Composer on a plane or a starship with poor connectivity. +If set to `prime`, GitHub VCS repositories will prime the cache so it can then be used +fully offline with `1`. + ← [Libraries](02-libraries.md) | [Schema](04-schema.md) → diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 6fc8a2a85..e9131839c 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -1065,8 +1065,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito if ($cacheKey && ($contents = $this->cache->read($cacheKey))) { if (!$this->degradedMode) { - $this->io->writeError(''.$e->getMessage().''); - $this->io->writeError(''.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date'); + $this->io->writeError(''.$this->url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date'); } $this->degradedMode = true; $data = JsonFile::parseJson($contents, $this->cache->getRoot().$cacheKey); @@ -1133,8 +1132,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } if (!$this->degradedMode) { - $this->io->writeError(''.$e->getMessage().''); - $this->io->writeError(''.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date'); + $this->io->writeError(''.$this->url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date'); } $this->degradedMode = true; @@ -1209,8 +1207,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } if (!$degradedMode) { - $io->writeError(''.$e->getMessage().''); - $io->writeError(''.$url.' could not be fully loaded, package information was loaded from the local cache and may be out of date'); + $io->writeError(''.$url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date'); } $degradedMode = true; diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index cc6e3edb4..1b96c4cde 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -62,6 +62,9 @@ class GitDriver extends VcsDriver $gitUtil = new GitUtil($this->io, $this->config, $this->process, $fs); if (!$gitUtil->syncMirror($this->url, $this->repoDir)) { + if (!is_dir($this->repoDir)) { + throw new \RuntimeException('Failed to clone '.$this->url.' to read package information from it'); + } $this->io->writeError('Failed to update '.$this->url.', package information from this repository may be outdated'); } diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index c79b91e6b..7b609a80c 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -507,7 +507,15 @@ class GitHubDriver extends VcsDriver $repoDataUrl = $this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository; - $this->repoData = $this->getContents($repoDataUrl, true)->decodeJson(); + try { + $this->repoData = $this->getContents($repoDataUrl, true)->decodeJson(); + } catch (TransportException $e) { + if ($e->getCode() === 499) { + $this->attemptCloneFallback(); + } else { + throw $e; + } + } if (null === $this->repoData && null !== $this->gitDriver) { return; } diff --git a/src/Composer/Util/Git.php b/src/Composer/Util/Git.php index 00e36830e..72524b4f9 100644 --- a/src/Composer/Util/Git.php +++ b/src/Composer/Util/Git.php @@ -248,7 +248,9 @@ class Git public function syncMirror($url, $dir) { - if (getenv('COMPOSER_DISABLE_NETWORK')) { + if (getenv('COMPOSER_DISABLE_NETWORK') && getenv('COMPOSER_DISABLE_NETWORK') !== 'prime') { + $this->io->writeError('Aborting git mirror sync of '.$url.' as network is disabled'); + return false; }