| 61 | result.WorktreePath = path |
| 62 | return result, nil |
| 63 | } |
| 64 | |
| 65 | func RemoveWorktree(ctx context.Context, path string) error { |
| 66 | if path == "" { |
| 67 | return nil |
| 68 | } |
| 69 | repoRoot := FindGitRoot(path) |
| 70 | if repoRoot == "" { |
| 71 | _ = os.RemoveAll(path) |
| 72 | return nil |
| 73 | } |
| 74 | cmdCtx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 75 | defer cancel() |
| 76 | cmd := exec.CommandContext(cmdCtx, "git", "worktree", "remove", "--force", path) |
| 77 | cmd.Dir = repoRoot |
| 78 | if out, err := cmd.CombinedOutput(); err != nil { |
| 79 | _ = os.RemoveAll(path) |
| 80 | _ = out |
| 81 | return nil |
| 82 | } |
| 83 | pruneCtx, pruneCancel := context.WithTimeout(ctx, 10*time.Second) |
| 84 | defer pruneCancel() |
| 85 | prune := exec.CommandContext(pruneCtx, "git", "worktree", "prune") |
| 86 | prune.Dir = repoRoot |
| 87 | _ = prune.Run() |
| 88 | _ = os.RemoveAll(path) |
| 89 | return nil |
| 90 | } |
| 91 | |