From 8e5a09c4f86eadee3893f2af367dc5f5aaca93a4 Mon Sep 17 00:00:00 2001 From: Willie Date: Wed, 30 Sep 2026 01:51:53 +0800 Subject: [PATCH] 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: " 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. --- cmd/skillshare/uninstall.go | 43 ++++++++++++++--- cmd/skillshare/uninstall_project.go | 15 +++++- tests/integration/uninstall_project_test.go | 10 ++-- tests/integration/uninstall_test.go | 52 +++++++++++++++++++++ 4 files changed, 106 insertions(+), 14 deletions(-) diff --git a/cmd/skillshare/uninstall.go b/cmd/skillshare/uninstall.go index 7feae9db9..9a4da3f5d 100644 --- a/cmd/skillshare/uninstall.go +++ b/cmd/skillshare/uninstall.go @@ -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 diff --git a/cmd/skillshare/uninstall_project.go b/cmd/skillshare/uninstall_project.go index a14ba31b4..71bb5e825 100644 --- a/cmd/skillshare/uninstall_project.go +++ b/cmd/skillshare/uninstall_project.go @@ -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())) diff --git a/tests/integration/uninstall_project_test.go b/tests/integration/uninstall_project_test.go index f6240cdb0..28b392d09 100644 --- a/tests/integration/uninstall_project_test.go +++ b/tests/integration/uninstall_project_test.go @@ -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") } } diff --git a/tests/integration/uninstall_test.go b/tests/integration/uninstall_test.go index c451f6219..89d25fb69 100644 --- a/tests/integration/uninstall_test.go +++ b/tests/integration/uninstall_test.go @@ -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") + } +}