fix(uninstall): refuse to trash a tracked repo whose git status cannot be read

When git status failed, the CLI printed a warning and uninstalled the
repo anyway. A repo that cannot be read may still hold uncommitted
work, so moving it to trash risked losing it without the dirty-check
prompt. Without --force, that repo now fails with "failed to check git
status: <err>" and is counted as failed; clean items in the same batch
are still removed. --force keeps proceeding with a warning. This
matches the Web API since 20ae89a8.

The project uninstall test that asserted the old warn-and-continue
behavior now asserts the block.
This commit is contained in:
Willie
2026-09-30 01:51:53 +08:00
parent c8faab039f
commit 8e5a09c4f8
4 changed files with 106 additions and 14 deletions
+36 -7
View File
@@ -439,7 +439,14 @@ func displayUninstallInfo(target *uninstallTarget, store *install.MetadataStore)
fmt.Println()
}
// checkTrackedRepoStatus checks for uncommitted changes in tracked repos
// gitStatusError marks a tracked repo whose git status could not be read.
type gitStatusError struct{ err error }
func (e *gitStatusError) Error() string { return fmt.Sprintf("failed to check git status: %v", e.err) }
func (e *gitStatusError) Unwrap() error { return e.err }
// checkTrackedRepoStatus checks for uncommitted changes in tracked repos.
// An unreadable git status blocks the uninstall unless force is set.
func checkTrackedRepoStatus(target *uninstallTarget, force bool) error {
if !target.isTrackedRepo {
return nil
@@ -447,7 +454,10 @@ func checkTrackedRepoStatus(target *uninstallTarget, force bool) error {
isDirty, err := git.IsDirty(target.path)
if err != nil {
ui.Warning("Could not check git status: %v", err)
if !force {
return &gitStatusError{err: err}
}
ui.Warning("Could not check git status (proceeding with --force): %v", err)
return nil
}
@@ -786,6 +796,7 @@ func cmdUninstall(args []string) error {
// --- Phase 4: PRE-FLIGHT ---
var preflightSkipped int
var preflightFailed []string
if !opts.dryRun {
// Parallel git dirty checks for tracked repos
type dirtyResult struct {
@@ -833,10 +844,25 @@ func cmdUninstall(args []string) error {
}
dr := dirtyResults[i]
if dr.err != nil {
if !opts.jsonOutput {
ui.Warning("Could not check git status for %s: %v", t.name, dr.err)
if opts.force {
if !opts.jsonOutput {
ui.Warning("Could not check git status for %s (proceeding with --force): %v", t.name, dr.err)
}
preflight = append(preflight, t)
continue
}
preflight = append(preflight, t)
statusErr := &gitStatusError{err: dr.err}
if single {
if opts.jsonOutput {
return writeJSONError(statusErr)
}
ui.Error("%v", statusErr)
return statusErr
}
if !opts.jsonOutput {
ui.StepFail(t.name, statusErr.Error())
}
preflightFailed = append(preflightFailed, fmt.Sprintf("%s: %v", t.name, statusErr))
continue
}
if !dr.dirty {
@@ -864,7 +890,7 @@ func cmdUninstall(args []string) error {
}
preflight = append(preflight, t)
}
preflightSkipped = len(targets) - len(preflight)
preflightSkipped = len(targets) - len(preflight) - len(preflightFailed)
targets = preflight
summary = summarizeUninstallTargets(targets)
@@ -878,6 +904,9 @@ func cmdUninstall(args []string) error {
if preflightSkipped > 0 {
preflightErr = fmt.Errorf("%d tracked repo%s skipped due to uncommitted changes; use --force to override", preflightSkipped, pluralS(preflightSkipped))
}
if len(preflightFailed) > 0 {
preflightErr = fmt.Errorf("%s", strings.Join(preflightFailed, "; "))
}
if opts.jsonOutput {
return writeJSONError(preflightErr)
}
@@ -942,7 +971,7 @@ func cmdUninstall(args []string) error {
}
var succeeded []*uninstallTarget
var failed []string
failed := preflightFailed
if opts.jsonOutput {
// JSON mode: quiet execution, no UI output
+13 -2
View File
@@ -2,6 +2,7 @@ package main
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
@@ -219,6 +220,7 @@ func cmdUninstallProject(args []string, root string) error {
}
// --- Phase 4: PRE-FLIGHT ---
var preflightFailed []string
if !opts.dryRun {
var preflight []*uninstallTarget
for _, t := range targets {
@@ -226,12 +228,18 @@ func cmdUninstallProject(args []string, root string) error {
if single {
return err
}
var statusErr *gitStatusError
if errors.As(err, &statusErr) {
ui.StepFail(t.name, err.Error())
preflightFailed = append(preflightFailed, fmt.Sprintf("%s: %v", t.name, err))
continue
}
ui.Warning("Skipping %s: %v", t.name, err)
continue
}
preflight = append(preflight, t)
}
skippedCount := len(targets) - len(preflight)
skippedCount := len(targets) - len(preflight) - len(preflightFailed)
targets = preflight
summary = summarizeUninstallTargets(targets)
@@ -241,6 +249,9 @@ func cmdUninstallProject(args []string, root string) error {
}
if len(targets) == 0 {
if len(preflightFailed) > 0 {
return fmt.Errorf("%s", strings.Join(preflightFailed, "; "))
}
return fmt.Errorf("no skills to uninstall after pre-flight checks")
}
}
@@ -292,7 +303,7 @@ func cmdUninstallProject(args []string, root string) error {
}
var succeeded []*uninstallTarget
var failed []string
failed := preflightFailed
if batch {
sp := ui.StartSpinner(fmt.Sprintf("Uninstalling %d %s", len(targets), summary.noun()))
+5 -5
View File
@@ -163,7 +163,7 @@ func TestUninstallProject_Group(t *testing.T) {
}
}
func TestUninstallProject_TrackedRepo_GitStatusErrorWarnsAndContinues(t *testing.T) {
func TestUninstallProject_TrackedRepo_GitStatusErrorBlocksUninstall(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
@@ -178,11 +178,11 @@ func TestUninstallProject_TrackedRepo_GitStatusErrorWarnsAndContinues(t *testing
}
result := sb.RunCLIInDirWithInput(projectRoot, "y\n", "uninstall", "broken-repo", "-p")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "Could not check git status")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "failed to check git status")
if sb.FileExists(repoDir) {
t.Error("tracked repo should still be uninstalled when git status check fails")
if !sb.FileExists(repoDir) {
t.Error("tracked repo must stay in place when git status cannot be read")
}
}
+52
View File
@@ -680,3 +680,55 @@ targets: {}
result.AssertAnyOutputContains(t, "skipped")
result.AssertAnyOutputContains(t, "remaining")
}
// makeUnreadableTrackedRepo creates a tracked repo whose `git status` fails
// because its .git directory is empty.
func makeUnreadableTrackedRepo(t *testing.T, sb *testutil.Sandbox, name string) string {
t.Helper()
repoPath := filepath.Join(sb.SourcePath, name)
if err := os.MkdirAll(filepath.Join(repoPath, ".git"), 0755); err != nil {
t.Fatalf("failed to create fake .git dir: %v", err)
}
if err := os.WriteFile(filepath.Join(repoPath, "SKILL.md"), []byte("# Broken"), 0644); err != nil {
t.Fatal(err)
}
return repoPath
}
func TestUninstall_TrackedRepo_GitStatusErrorBlocksUninstall(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
repoPath := makeUnreadableTrackedRepo(t, sb, "_broken-repo")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLIWithInput("y\n", "uninstall", "_broken-repo")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "failed to check git status")
if !sb.FileExists(repoPath) {
t.Error("tracked repo must stay in place when git status cannot be read")
}
}
func TestUninstall_BatchGitStatusErrorFailsRepoAndKeepsCleanSkills(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
repoPath := makeUnreadableTrackedRepo(t, sb, "_broken-repo")
sb.CreateSkill("clean-a", map[string]string{"SKILL.md": "# A"})
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("uninstall", "--all", "--json")
result.AssertSuccess(t)
result.AssertOutputContains(t, "failed to check git status")
if !sb.FileExists(repoPath) {
t.Error("tracked repo must stay in place when git status cannot be read")
}
if sb.FileExists(filepath.Join(sb.SourcePath, "clean-a")) {
t.Error("clean-a should still be removed")
}
}