From fa87149631be7f1110114ded36f402de0bca7be9 Mon Sep 17 00:00:00 2001 From: henderkes Date: Sun, 21 Sep 2025 19:59:11 +0200 Subject: [PATCH] shallow submodules as well (grpc download goes from 2.9 gb to 900mb) --- src/SPC/command/DownloadCommand.php | 4 ++-- src/SPC/store/Downloader.php | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/SPC/command/DownloadCommand.php b/src/SPC/command/DownloadCommand.php index b9bae6a5..00bcc194 100644 --- a/src/SPC/command/DownloadCommand.php +++ b/src/SPC/command/DownloadCommand.php @@ -106,7 +106,7 @@ class DownloadCommand extends BaseCommand } // retry - $retry = intval($this->getOption('retry')); + $retry = (int) $this->getOption('retry'); f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry); // Use shallow-clone can reduce git resource download @@ -265,7 +265,7 @@ class DownloadCommand extends BaseCommand f_passthru((PHP_OS_FAMILY === 'Windows' ? 'rmdir /s /q ' : 'rm -rf ') . DOWNLOAD_PATH); } // unzip command check - if (PHP_OS_FAMILY !== 'Windows' && !$this->findCommand('unzip')) { + if (PHP_OS_FAMILY !== 'Windows' && !self::findCommand('unzip')) { $this->output->writeln('Missing unzip command, you need to install it first !'); $this->output->writeln('You can use "bin/spc doctor" command to check and install required tools'); return static::FAILURE; diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index d659253e..3e50c93d 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -65,19 +65,19 @@ class Downloader url: "https://api.github.com/repos/{$source['repo']}/{$type}", hooks: [[CurlHook::class, 'setupGithubToken']], retries: self::getRetryAttempts() - ), true); + ), true, 512, JSON_THROW_ON_ERROR); $url = null; - for ($i = 0; $i < count($data); ++$i) { - if (($data[$i]['prerelease'] ?? false) === true && ($source['prefer-stable'] ?? false)) { + foreach ($data as $rel) { + if (($rel['prerelease'] ?? false) === true && ($source['prefer-stable'] ?? false)) { continue; } if (!($source['match'] ?? null)) { - $url = $data[$i]['tarball_url'] ?? null; + $url = $rel['tarball_url'] ?? null; break; } - if (preg_match('|' . $source['match'] . '|', $data[$i]['tarball_url'])) { - $url = $data[$i]['tarball_url']; + if (preg_match('|' . $source['match'] . '|', $rel['tarball_url'])) { + $url = $rel['tarball_url']; break; } } @@ -232,7 +232,8 @@ class Downloader $quiet = !defined('DEBUG_MODE') ? '-q --quiet' : ''; $git = SPC_GIT_EXEC; $shallow = defined('GIT_SHALLOW_CLONE') ? '--depth 1 --single-branch' : ''; - $recursive = ($submodules === null) ? '--recursive' : ''; + $recursive = ($submodules === null && defined('GIT_SHALLOW_CLONE')) ? '--recursive --shallow-submodules' : null; + $recursive ??= $submodules === null ? '--recursive' : ''; try { self::registerCancelEvent(function () use ($download_path) { @@ -243,8 +244,9 @@ class Downloader }); f_passthru("{$git} clone {$quiet} --config core.autocrlf=false --branch \"{$branch}\" {$shallow} {$recursive} \"{$url}\" \"{$download_path}\""); if ($submodules !== null) { + $depth_flag = defined('GIT_SHALLOW_CLONE') ? '--depth 1' : ''; foreach ($submodules as $submodule) { - f_passthru("cd \"{$download_path}\" && {$git} submodule update --init " . escapeshellarg($submodule)); + f_passthru("cd \"{$download_path}\" && {$git} submodule update --init {$depth_flag} " . escapeshellarg($submodule)); } } } catch (SPCException $e) { @@ -399,7 +401,7 @@ class Downloader * Download source * * @param string $name source name - * @param null|array{ + * @param null|array{ * type: string, * repo: ?string, * url: ?string, @@ -414,7 +416,7 @@ class Downloader * path: ?string, * text: ?string * } - * } $source source meta info: [type, path, rev, url, filename, regex, license] + * } $source source meta info: [type, path, rev, url, filename, regex, license] * @param bool $force Whether to force download (default: false) * @param int $download_as Lock source type (default: SPC_LOCK_SOURCE) */