Browse Source

Allow COMPOSER_DISABLE_NETWORK to work with GitHubDriver by doing a cache priming pass first

Jordi Boggiano 5 năm trước cách đây
mục cha
commit
aa6be02c64

+ 3 - 0
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) →

+ 3 - 6
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('<warning>'.$e->getMessage().'</warning>');
-                        $this->io->writeError('<warning>'.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date</warning>');
+                        $this->io->writeError('<warning>'.$this->url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date</warning>');
                     }
                     $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('<warning>'.$e->getMessage().'</warning>');
-                    $this->io->writeError('<warning>'.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date</warning>');
+                    $this->io->writeError('<warning>'.$this->url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date</warning>');
                 }
                 $this->degradedMode = true;
 
@@ -1209,8 +1207,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
             }
 
             if (!$degradedMode) {
-                $io->writeError('<warning>'.$e->getMessage().'</warning>');
-                $io->writeError('<warning>'.$url.' could not be fully loaded, package information was loaded from the local cache and may be out of date</warning>');
+                $io->writeError('<warning>'.$url.' could not be fully loaded ('.$e->getMessage().'), package information was loaded from the local cache and may be out of date</warning>');
             }
             $degradedMode = true;
 

+ 3 - 0
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('<error>Failed to update '.$this->url.', package information from this repository may be outdated</error>');
             }
 

+ 9 - 1
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;
         }

+ 3 - 1
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('<warning>Aborting git mirror sync of '.$url.' as network is disabled</warning>');
+
             return false;
         }