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

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