perf(update): clone 一次仓库并本地复制替代第二次网络 clone; 排除 public/storage 和 public/build

This commit is contained in:
augushong
2026-05-25 21:28:21 +08:00
parent 279968c9ef
commit 76fe865274

View File

@@ -55,8 +55,8 @@ class AdminUpdateServiceBase
$current_version = $this->useVersion;
$current_version_dir = App::getRuntimePath() . '/update/' . $current_version;
$last_version_dir = App::getRuntimePath() . '/update/last';
$current_version_dir = App::getRuntimePath() . '/update/current';
$last_version_dir = App::getRuntimePath() . '/update/repo';
$now_dir = App::getRootPath();
$output->writeln('获取最新代码');
@@ -118,11 +118,12 @@ class AdminUpdateServiceBase
$last_version_repo->checkout($last_version);
}
$current_version_git = new Git();
$output->writeln('获取当前版本代码');
$current_version_repo = $current_version_git->cloneRepository($this->useRepo, $current_version_dir);
$output->writeln('切换版本' . $current_version);
$current_version_repo->checkout($current_version);
$output->writeln('复制当前版本代码');
$output->writeln('切换到当前版本' . $current_version);
$last_version_repo->checkout($current_version);
$this->copyDirectory($last_version_dir, $current_version_dir);
$output->writeln('切换回最新版本' . $last_version);
$last_version_repo->checkout($last_version);
$output->writeln('开始比较版本差异');
@@ -142,6 +143,8 @@ class AdminUpdateServiceBase
'runtime',
'vendor',
'.git',
'public/storage',
'public/build',
];
// 当前版本的应该被处理所有文件
@@ -426,4 +429,36 @@ class AdminUpdateServiceBase
$this->output->writeln('清理目录 ' . $dir);
PathTools::removeDir($dir);
}
/**
* 递归复制目录
* Windows 兼容,使用 PHP 原生 opendir/readdir 实现
*/
protected function copyDirectory(string $src, string $dst): void
{
if (!is_dir($src)) {
return;
}
if (!is_dir($dst)) {
mkdir($dst, 0755, true);
}
$handle = opendir($src);
while (false !== ($file = readdir($handle))) {
if ($file === '.' || $file === '..') {
continue;
}
$srcPath = $src . '/' . $file;
$dstPath = $dst . '/' . $file;
if (is_dir($srcPath)) {
$this->copyDirectory($srcPath, $dstPath);
} else {
copy($srcPath, $dstPath);
}
}
closedir($handle);
}
}