Skip to content

[Fix] Add --pr-aware flag to prune and fix dirty worktree detection#61

Merged
samzong merged 2 commits intomainfrom
fix/prune-pr-aware
Mar 30, 2026
Merged

[Fix] Add --pr-aware flag to prune and fix dirty worktree detection#61
samzong merged 2 commits intomainfrom
fix/prune-pr-aware

Conversation

@samzong
Copy link
Copy Markdown
Owner

@samzong samzong commented Mar 30, 2026

What's changed?

  • Add --pr-aware flag to gmc wt prune that checks GitHub PR state via gh CLI before deciding whether to remove a worktree
  • Single batch gh pr list call (not N+1 per branch) with local map lookup
  • Decision matrix: MERGED→remove, CLOSED→skip, OPEN→skip, no PR→skip
  • Structured PruneEntry output with table display (NAME, BRANCH, PR, PR STATE, ACTION, REASON)
  • Mockable ghRunFunc seam for testability without real gh binary
  • Shared collectPruneCandidates eliminates duplication between classic and PR-aware paths
  • Fix pre-existing bug: dirty worktree check status == "modified" never matched (GetWorktreeStatus returns detailed strings like 1 file changed). Changed to status != "clean".
  • 9 new tests covering batch parsing, case normalization, decision matrix, gh failure, dirty skip, and force removal

Why

  • Internalizes the logic from skill/scripts/wt-cleanup.sh as native Go, eliminating the python3 dependency
  • The dirty check bugfix is split into its own commit for clear reviewability

Closes #53

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a --pr-aware flag to the worktree prune command, enabling the tool to verify GitHub PR statuses via the gh CLI before deleting worktrees. The implementation refactors the pruning logic into distinct strategies and adds a tabular report for better visibility. Review feedback recommends increasing the PR list limit for larger repositories and passing the report structure by pointer to ensure idiomatic consistency and avoid value-copying bugs.

Comment thread internal/worktree/prune.go
Comment thread internal/worktree/prune.go Outdated
samzong added 2 commits March 30, 2026 13:45
GetWorktreeStatus returns detailed strings like '1 file changed' or
'1 untracked', never the literal 'modified'. The previous equality
check status == "modified" never matched, silently skipping the
dirty worktree protection. Compare against 'clean' instead.

Signed-off-by: samzong <samzong.lu@gmail.com>
Add PR-aware pruning that checks GitHub PR state via gh CLI before
deciding whether to remove a worktree. This internalizes the logic
from skill/scripts/wt-cleanup.sh as native Go code.

Key design decisions:
- Single batch gh pr list call (not N+1 per branch)
- ghRunFunc seam for testability without real gh binary
- Report.PruneEntries carries structured data (no side-channel)
- Shared collectPruneCandidates eliminates classic/pr-aware duplication
- strings.ToUpper normalizes PR state for case-insensitive matching

Decision matrix for --pr-aware:
- MERGED + clean → remove (or would_remove with --dry-run)
- MERGED + dirty → skip (unless --force)
- CLOSED → skip (PR closed, not merged)
- OPEN → skip (PR still open)
- No PR → skip

Closes #53

Signed-off-by: samzong <samzong.lu@gmail.com>
@samzong samzong force-pushed the fix/prune-pr-aware branch from ef619d0 to fae2c40 Compare March 30, 2026 05:47
@samzong samzong merged commit aa169fc into main Mar 30, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(worktree): add --pr-aware flag to prune command

1 participant