修复古籍索引安装脚本兼容旧版 Git

This commit is contained in:
Your Name
2026-10-09 09:16:03 +08:00
parent 637c2f1ff2
commit 88b94c2fc6
+17 -7
View File
@@ -18,8 +18,13 @@ $root = dirname(__DIR__) . '/knowledge/tcm-ancient-books';
$projectRoot = dirname(__DIR__, 2);
$indexPath = TcmAncientBooksReference::indexPath();
function runGit(array $arguments): string
function runGit(array $arguments, ?string $directory = null): string
{
$originalDirectory = getcwd();
if ($directory !== null && ($originalDirectory === false || !chdir($directory))) {
throw new RuntimeException('Could not enter Git directory: ' . $directory);
}
try {
$command = 'git ' . implode(' ', array_map('escapeshellarg', $arguments)) . ' 2>&1';
$lines = [];
$code = 0;
@@ -28,27 +33,32 @@ function runGit(array $arguments): string
throw new RuntimeException('Git command failed: ' . implode("\n", $lines));
}
return trim(implode("\n", $lines));
} finally {
if ($directory !== null) {
chdir($originalDirectory);
}
}
}
try {
if (!is_dir($root . '/.git') && !is_file($root . '/.git')) {
if (is_file($projectRoot . '/.gitmodules')
&& (is_dir($projectRoot . '/.git') || is_file($projectRoot . '/.git'))) {
runGit(['-C', $projectRoot, 'submodule', 'update', '--init', '--', 'server/knowledge/tcm-ancient-books']);
runGit(['submodule', 'update', '--init', '--', 'server/knowledge/tcm-ancient-books'], $projectRoot);
} else {
runGit(['clone', '--depth', '1', TcmAncientBooksReference::REPOSITORY_URL . '.git', $root]);
}
}
$checkoutRoot = runGit(['-C', $root, 'rev-parse', '--show-toplevel']);
$checkoutRoot = runGit(['rev-parse', '--show-toplevel'], $root);
if (strtolower(str_replace('\\', '/', (string) realpath($checkoutRoot)))
!== strtolower(str_replace('\\', '/', (string) realpath($root)))) {
throw new RuntimeException('Corpus checkout is missing or points to the parent repository');
}
$head = runGit(['-C', $root, 'rev-parse', 'HEAD']);
$head = runGit(['rev-parse', 'HEAD'], $root);
if ($head !== TcmAncientBooksReference::COMMIT) {
runGit(['-C', $root, 'fetch', '--depth', '1', 'origin', TcmAncientBooksReference::COMMIT]);
runGit(['-C', $root, 'checkout', '--detach', TcmAncientBooksReference::COMMIT]);
$head = runGit(['-C', $root, 'rev-parse', 'HEAD']);
runGit(['fetch', '--depth', '1', 'origin', TcmAncientBooksReference::COMMIT], $root);
runGit(['checkout', '--detach', TcmAncientBooksReference::COMMIT], $root);
$head = runGit(['rev-parse', 'HEAD'], $root);
}
if ($head !== TcmAncientBooksReference::COMMIT) {
throw new RuntimeException('Corpus commit mismatch');