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