feat(clean): add --force flag to git gtr clean --merged#158
feat(clean): add --force flag to git gtr clean --merged#158maro114510 wants to merge 7 commits intocoderabbitai:mainfrom
Conversation
Extends _clean_should_skip() with an optional third argument `force`. When force=1, uncommitted-changes and untracked-files checks are bypassed, allowing removal of worktrees that contain build artifacts or other disposable local state. Detached HEAD and empty-branch protections are retained regardless of --force, as those states are indeterminate. Propagates force through _clean_merged() (new 6th arg) and cmd_clean() (new --force|-f flag spec entry). All new arguments default to 0, preserving full backward compatibility.
Covers four cases for _clean_should_skip with force=1: - dirty worktree (staged changes) → not skipped - worktree with untracked files → not skipped - detached HEAD → still skipped (protection retained) - empty branch name → still skipped (protection retained) Also adds a smoke test confirming cmd_clean accepts --force without error.
Updates all three shell completion files (Bash, Zsh, Fish) so that --force and -f appear as tab-completion candidates for `git gtr clean`.
Adds --force, -f to the Options section of `git gtr help clean` and the full help page. Adds two usage examples demonstrating --force alone and combined with --yes. Updates README.md with the same option entry and examples.
WalkthroughAdds a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/cmd_clean.bats (1)
105-108: Consider adding a short-flag acceptance test (-f) for parity.You validate
--force, but not-f. A tiny test here would lock in alias behavior.Suggested test addition
`@test` "cmd_clean accepts --force flag without error" { run cmd_clean --force [ "$status" -eq 0 ] } + +@test "cmd_clean accepts -f flag without error" { + run cmd_clean -f + [ "$status" -eq 0 ] +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/cmd_clean.bats` around lines 105 - 108, Add a parallel test for the short alias of the force flag by creating a new bats test similar to the existing "cmd_clean accepts --force flag without error" that runs cmd_clean -f and asserts exit status 0; locate the existing cmd_clean test block and duplicate its structure but replace "--force" with "-f" so the CLI alias behavior for the -f short flag is validated.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/commands/help.sh`:
- Line 573: Update the full-help description for the option string "--force, -f"
in the help text to reflect actual behavior: state that it forces removal even
if the worktree has uncommitted changes and also bypasses skipping of untracked
files (i.e., it will remove tracked and untracked files). Match the wording to
the shorter help entry earlier in the file (the other "--force, -f" occurrence)
so both descriptions are consistent.
---
Nitpick comments:
In `@tests/cmd_clean.bats`:
- Around line 105-108: Add a parallel test for the short alias of the force flag
by creating a new bats test similar to the existing "cmd_clean accepts --force
flag without error" that runs cmd_clean -f and asserts exit status 0; locate the
existing cmd_clean test block and duplicate its structure but replace "--force"
with "-f" so the CLI alias behavior for the -f short flag is validated.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 095e7ce2-ee76-40cb-823e-fb0129190857
📒 Files selected for processing (7)
README.mdcompletions/_git-gtrcompletions/git-gtr.fishcompletions/gtr.bashlib/commands/clean.shlib/commands/help.shtests/cmd_clean.bats
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@completions/_git-gtr`:
- Around line 86-88: Update the completion option descriptions to mention
untracked files: change the '--force[Force removal even if worktree has
uncommitted changes]' description to include "or untracked files" (e.g., "Force
removal even if worktree has uncommitted changes or untracked files") and
likewise update the '-n[Show what would be removed]' description if it should
also clarify it includes untracked files; edit the strings for the '-n' and
'--force' entries in the completions/_git-gtr file to reflect this wording.
In `@completions/git-gtr.fish`:
- Line 106: The completion for the git clean command uses the entry with
condition '__fish_git_gtr_using_command clean' and option flag '-l force' whose
description currently says "Force removal even if worktree has uncommitted
changes"; update that description to mention that --force also bypasses the
untracked files check (e.g., include "and untracked files") so it accurately
reflects the behavior of the --force flag for git clean.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2c89d570-6c5c-4669-8871-f92cfc77b650
📒 Files selected for processing (6)
README.mdcompletions/_git-gtrcompletions/git-gtr.fishcompletions/gtr.bashlib/commands/clean.shlib/commands/help.sh
🚧 Files skipped from review as they are similar to previous changes (2)
- README.md
- completions/gtr.bash
There was a problem hiding this comment.
♻️ Duplicate comments (1)
completions/git-gtr.fish (1)
106-106:⚠️ Potential issue | 🟡 MinorClean
--forcedescription is still incomplete.Line 106 mentions only uncommitted changes, but
clean --merged --forcealso bypasses untracked-files checks. Please align wording with actual behavior.📝 Proposed fix
-complete -c git -n '__fish_git_gtr_using_command clean' -l force -d 'Force removal even if worktree has uncommitted changes' +complete -c git -n '__fish_git_gtr_using_command clean' -l force -d 'Force removal even if worktree has uncommitted changes or untracked files'🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@completions/git-gtr.fish` at line 106, Update the description text for the --force completion entry (the complete invocation using '__fish_git_gtr_using_command clean' with flag '-l force') so it accurately reflects behavior: mention that --force forces removal of files even if the worktree has uncommitted changes and, when used with --merged, also bypasses untracked-files checks (i.e., will remove files that would otherwise be protected by untracked-files settings).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@completions/git-gtr.fish`:
- Line 106: Update the description text for the --force completion entry (the
complete invocation using '__fish_git_gtr_using_command clean' with flag '-l
force') so it accurately reflects behavior: mention that --force forces removal
of files even if the worktree has uncommitted changes and, when used with
--merged, also bypasses untracked-files checks (i.e., will remove files that
would otherwise be protected by untracked-files settings).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ccf8cb03-71b0-4343-ad83-af4053685790
📒 Files selected for processing (2)
completions/_git-gtrcompletions/git-gtr.fish
🚧 Files skipped from review as they are similar to previous changes (1)
- completions/_git-gtr
Pull Request
Description
Adds
--forcetogit gtr clean --mergedso that worktrees with uncommitted changes or untracked files are no longer automatically skipped when the flag is explicitly provided.Design boundary:
--forcebypasses only the uncommitted-changes and untracked-files checks. Detached HEAD and empty-branch protections are intentionally retained — those states indicate the branch identity is indeterminate, meaning removal could discard unrecoverable work regardless of--force.--yesscope is unchanged; it continues to control confirmation prompts only.Motivation
clean --mergedskips any worktree with uncommitted changes or untracked files to prevent accidental data loss — a sound default for work-in-progress. However, when those files are build artifacts, generated output, or throwaway logs, the check becomes an obstacle.The only workaround today is manually running
git stashorgit clean -fdin each affected worktree first.git gtr rmalready provides--forcefor the same class of problem in single-worktree removal. This PR bringsclean --mergedin line with that existing precedent.Fixes # #157
Type of Change
Testing
bats tests/)shellcheck lib/commands/clean.sh)Manual Testing Checklist
Tested on:
Core functionality tested:
git gtr new <branch>- Create worktreegit gtr go <branch>- Navigate to worktreegit gtr editor <branch>- Open in editor (if applicable)git gtr ai <branch>- Start AI tool (if applicable)git gtr rm <branch>- Remove worktreegit gtr list- List worktreesgit gtr config- Configuration commands (if applicable)git gtr clean --mergedTest Steps
git gtr clean --merged --yes(without --force)git gtr clean --merged --force --yesExpected behavior:
Actual behavior:
Matches expected.
Breaking Changes
Checklist
Before submitting this PR, please check:
git gtr(production) and./bin/gtr(development)Additional Context
Implementation propagates the flag through the call chain:
License Acknowledgment
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache License 2.0.
Summary by CodeRabbit
New Features
--force(-f) to the clean command to allow forced removal of worktrees even with uncommitted changes or untracked files; supports auto-confirmation when combined with--yes.Documentation
--forcebehavior and examples.Tests
--forcebehavior, including dirty/untracked worktrees and preserved protections (detached HEAD, empty branches).