-
Notifications
You must be signed in to change notification settings - Fork 2
Add gw clean command to batch remove merged worktrees #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Implement a new 'gw clean' command that safely removes multiple worktrees that have been merged to origin/main. The command performs the same safety checks as 'gw end' (uncommitted changes, unpushed commits, merge status) and only removes worktrees that pass all checks. Includes --force and --dry-run flags for better control. π€ Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Detect when a worktree has missing or corrupt git metadata (exit status 128) and display a user-friendly error message instead of showing the raw git error. The new message clearly indicates that the worktree is not a valid git repository and suggests the metadata may be missing or corrupt. π€ Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
- Display only directory names instead of full paths using filepath.Base()
- Simplify section headers ("Removable (2)" vs "Removable worktrees (2)")
- Combine multiple reasons into single line with comma separation
- Shorten warning messages:
- "Has uncommitted changes" β "uncommitted changes"
- "Has unpushed commits" β "unpushed commits"
- "Not merged to origin/main" β "not merged"
- Long broken worktree message β "invalid git repository"
- Remove redundant "merged to origin/main" for removable worktrees
- Add visual separator (β) for non-removable reasons
- Update all tests to match new output format
π€ Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #17 +/- ##
==========================================
+ Coverage 73.18% 73.38% +0.20%
==========================================
Files 23 24 +1
Lines 1529 1676 +147
==========================================
+ Hits 1119 1230 +111
- Misses 331 357 +26
- Partials 79 89 +10
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
π New features to boost your workflow:
|
Code Review SummaryThis is a well-implemented PR that adds a useful β Strengths1. Excellent Code Quality
2. User Experience
3. Safety First
4. Documentation
π Issues & SuggestionsHigh Priority1. Missing constant for default base branch (cmd/command.go:621)if wt.Branch == "" || wt.Branch == defaultBaseBranch || wt.Branch == "master" {Issue: Fix: Either import/define the constant or use the string literal "main" directly (check how it's handled in other commands). Medium Priority2. Code duplication with EndCommand (cmd/command.go:676-733)The
Suggestion: Extract common safety check logic into a shared function to reduce duplication and ensure consistency: func performWorktreeSafetyChecks(deps *Dependencies) (hasChanges, hasUnpushed, isMerged bool, errors []error)3. Inconsistent error message format (cmd/command.go:685, 702, 712, 722)Some errors append full error details: status.Warnings = append(status.Warnings, fmt.Sprintf("Could not access directory: %v", err))While others use concise messages: status.Warnings = append(status.Warnings, "uncommitted changes")Suggestion: Consider making error messages consistently concise for the user-facing output, perhaps logging full errors to stderr instead. 4. Directory change side effects (cmd/command.go:684, 730)The function changes the current working directory multiple times without proper error handling on restoration: _ = os.Chdir(originalDir)Suggestion: While the underscore shows intentional ignoring, consider at least logging if restoration fails, as this could affect subsequent operations. Low Priority5. Hard-coded "main" branch (cmd/command.go:720)isMerged, err := c.deps.Git.IsMergedToOrigin("main")Observation: This is consistent with 6. Test naming could be more specific (cmd/command_test.go:24)const testBranch123 = "123/impl"Suggestion: Consider 7. Magic number in loop display (cmd/command.go:760)if i > 0 {
fmt.Fprintf(c.deps.Stdout, "\n")
}Suggestion: Add a comment explaining why the blank line is added (to separate non-removable worktree entries for readability). π‘οΈ Security Considerationsβ No security issues found. The command:
π§ͺ TestingStrengths:
Suggestion:Consider adding a test for the case where ποΈ Performanceβ No performance concerns. The implementation:
π Minor Nits
π― Final Recommendationβ APPROVE with minor fixes required This is high-quality code that adds valuable functionality. The only blocking issue is the missing Required before merge:
Recommended for follow-up:
Code Coverage: 81.9% is excellent for this type of utility. The test coverage comprehensively validates the new functionality. Great work on this feature! π π€ Review generated with Claude Code |
All features are part of the new gw clean command, so they should be in the Added section rather than split across Added/Changed/Fixed. π€ Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Code Review: Add gw clean commandOverall AssessmentThis is a well-implemented feature with excellent test coverage and thoughtful UX improvements. The code follows existing patterns, has comprehensive safety checks, and is thoroughly tested. However, there are a few security concerns and minor improvements to consider. β Strengths1. Excellent Test Coverage
2. Strong Safety Model
3. Good UX Design
4. Code Quality
π΄ Security ConcernsCritical: Command Injection VulnerabilityLocation: The code passes user-controlled input directly to // Line 63 - CreateWorktree
cmd = exec.Command("git", "worktree", "add", worktreeDir, "-b", branchName, baseBranch)
// Line 114 - RemoveWorktreeByPath
cmd := exec.Command("git", "worktree", "remove", worktreePath)
// Line 236 - RunCommand (MOST DANGEROUS)
func RunCommand(command string) error {
cmd := exec.Command("sh", "-c", command) // Direct shell execution\!
...
}Risk: While
Specific concerns:
Recommendations:
Example validation: func validateBranchName(branch string) error {
// Allow alphanumeric, hyphens, slashes, underscores
matched, _ := regexp.MatchString("^[a-zA-Z0-9/_-]+$", branch)
if \!matched {
return fmt.Errorf("invalid branch name: %s", branch)
}
return nil
}
|
Extract emoji/symbol constants (β, β, β , β) to package-level constants for consistency across commands and easier maintenance. This addresses the review comment on line 750 to improve code consistency and make it easier to update symbols in the future. π€ Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Code Review for PR #17: Add gw clean commandOverall, this is a well-implemented and thoroughly tested feature that adds significant value to the tool. The code quality is high, follows existing patterns, and includes comprehensive test coverage. β Strengths1. Excellent Code Organization
2. Comprehensive Test Coverage
3. User Experience
4. Safety First
5. Documentation
π Issues & ConcernsCritical: Hardcoded Emoji Symbols π¨Lines: cmd/command.go:758, 767, 797, 802, 810, 820, 823 The code uses hardcoded emoji symbols instead of the symbol constants defined at lines 14-22. This creates inconsistency with the refactored init.go which properly uses the constants. Impact:
Recommendation: Replace hardcoded emojis with symbol constants (symbolSuccess, symbolError, symbolWarning) Security: Directory Traversal Risk
|
Summary
This PR introduces a new
gw cleancommand that allows users to batch remove multiple git worktrees that have been safely merged to origin/main, along with improvements to error messages and output formatting.Features Added
New
gw cleancommand for batch worktree removalgw end:--forceflag to skip confirmation prompt--dry-runflag to preview what would be removedauto_remove_branchconfiguration for automatic branch cleanupImproved error messages
Enhanced output formatting
Implementation Details
cmd/command.goEndCommandTest plan
make fmtandmake check- all tests pass--dry-runflag--forceflagauto_remove_branchconfigurationπ€ Generated with Claude Code