fix: PS 5.1 git stderr handling - use ErrorActionPreference Continue

This commit is contained in:
leesu 2026-04-01 16:19:33 +08:00
parent e5aeeae7f3
commit f0cb8c7529

View File

@ -172,53 +172,57 @@ if (-not $StartOnly) {
if ($isGit) { if ($isGit) {
Write-Host " 已有仓库,执行 git pull..." Write-Host " 已有仓库,执行 git pull..."
Push-Location $ClaudeTarget Push-Location $ClaudeTarget
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try { try {
# stash 本地修改,防止 rebase 冲突 git stash 2>&1 | Out-Null
$stashOutput = git stash 2>&1
$hasStash = $stashOutput -notmatch "No local changes"
git pull --rebase 2>&1 | ForEach-Object { Write-Host " $_" } git pull --rebase 2>&1 | ForEach-Object { Write-Host " $_" }
if ($hasStash) { git stash pop 2>&1 | Out-Null
git stash pop 2>&1 | ForEach-Object { Write-Host " $_" }
}
} }
catch { catch {
Write-Host " [WARN] git pull 失败: $_" -ForegroundColor Yellow Write-Host " [WARN] git pull 失败: $_" -ForegroundColor Yellow
Write-Host " 使用本地现有版本继续" -ForegroundColor Yellow Write-Host " 使用本地现有版本继续" -ForegroundColor Yellow
} }
finally { Pop-Location } finally {
$ErrorActionPreference = $prevEAP
Pop-Location
}
} }
else { else {
# 安全克隆: 先克隆到临时目录,成功后再替换 # 安全克隆: 先克隆到临时目录,成功后再替换
Write-Host " 备份现有 .claude/ 并克隆..." Write-Host " 备份现有 .claude/ 并克隆..."
$tempClone = "$ClaudeTarget.bw-clone-temp" $tempClone = "$ClaudeTarget.bw-clone-temp"
try { if (Test-Path $tempClone) { Remove-Item $tempClone -Recurse -Force }
if (Test-Path $tempClone) { Remove-Item $tempClone -Recurse -Force } $prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
git clone --depth 1 $GitUrl $tempClone 2>&1 | ForEach-Object { Write-Host " $_" } git clone --depth 1 $GitUrl $tempClone 2>&1 | ForEach-Object { Write-Host " $_" }
$cloneExit = $LASTEXITCODE
$ErrorActionPreference = $prevEAP
if ($cloneExit -ne 0 -or -not (Test-Path (Join-Path $tempClone "CLAUDE.md"))) {
Write-Host " [ERROR] 克隆失败 (exit=$cloneExit)" -ForegroundColor Red
if (Test-Path $tempClone) { Remove-Item $tempClone -Recurse -Force -ErrorAction SilentlyContinue }
Write-Host " 原始 .claude/ 未被修改" -ForegroundColor Yellow
exit 1
}
# 克隆成功,执行替换 # 克隆成功,执行替换
if (Test-Path $BackupPath) { Remove-Item $BackupPath -Recurse -Force } if (Test-Path $BackupPath) { Remove-Item $BackupPath -Recurse -Force }
Rename-Item $ClaudeTarget $BackupPath Rename-Item $ClaudeTarget $BackupPath
Rename-Item $tempClone $ClaudeTarget Rename-Item $tempClone $ClaudeTarget
Write-Host " [OK] 克隆完成,原始配置已备份到 .claude.bw-backup/" -ForegroundColor Green Write-Host " [OK] 克隆完成,原始配置已备份到 .claude.bw-backup/" -ForegroundColor Green
}
catch {
Write-Host " [ERROR] 克隆失败: $_" -ForegroundColor Red
if (Test-Path $tempClone) { Remove-Item $tempClone -Recurse -Force -ErrorAction SilentlyContinue }
Write-Host " 原始 .claude/ 未被修改" -ForegroundColor Yellow
exit 1
}
} }
} }
else { else {
Write-Host " 首次安装,克隆仓库..." Write-Host " 首次安装,克隆仓库..."
try { $prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
git clone --depth 1 $GitUrl $ClaudeTarget 2>&1 | ForEach-Object { Write-Host " $_" } git clone --depth 1 $GitUrl $ClaudeTarget 2>&1 | ForEach-Object { Write-Host " $_" }
if (-not (Test-Path (Join-Path $ClaudeTarget "CLAUDE.md"))) { $cloneExit = $LASTEXITCODE
throw "克隆完成但缺少 CLAUDE.md" $ErrorActionPreference = $prevEAP
}
} if ($cloneExit -ne 0 -or -not (Test-Path (Join-Path $ClaudeTarget "CLAUDE.md"))) {
catch { Write-Host " [ERROR] 克隆失败 (exit=$cloneExit)" -ForegroundColor Red
Write-Host " [ERROR] 克隆失败: $_" -ForegroundColor Red
Write-Host "" Write-Host ""
Write-Host " 可能原因:" -ForegroundColor Yellow Write-Host " 可能原因:" -ForegroundColor Yellow
Write-Host " - Gitea 服务不可达 (检查 https://code.letcareme.com)" -ForegroundColor Yellow Write-Host " - Gitea 服务不可达 (检查 https://code.letcareme.com)" -ForegroundColor Yellow
@ -230,7 +234,7 @@ if (-not $StartOnly) {
exit 1 exit 1
} }
} }
} }
else { else {
Write-Host "`n[2/6] StartOnly 模式,跳过同步" -ForegroundColor Gray Write-Host "`n[2/6] StartOnly 模式,跳过同步" -ForegroundColor Gray
} }