[fix] Protect fresh worktrees from clean --merged (#335)#336
Open
MohammadYusif wants to merge 1 commit into
Open
[fix] Protect fresh worktrees from clean --merged (#335)#336MohammadYusif wants to merge 1 commit into
MohammadYusif wants to merge 1 commit into
Conversation
`git branch --merged <ref>` lists every branch reachable from the ref, including a brand-new worktree branch whose tip is the base commit (an ancestor, trivially "merged"). FindMergedCandidates added any such branch to the removal set with no check for whether it had commits of its own, so a freshly-created worktree could be silently removed and its branch force-deleted by the post-merge hook (which passes --force). Add an own-commits guard, HasOwnCommits, mirroring the empty-branch check IsSquashMerged already performs (merge-base == tip). The --merged ancestor path now skips branches that contributed nothing, while genuinely merged branches (squash and merge-commit) still have own commits and are removed. Add a regression test covering the fresh-worktree case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rimba clean --mergedwas force-deleting freshly-created worktrees that had no commits of their own.FindMergedCandidates(internal/operations/clean.go) added any branch returned bygit branch --merged <ref>to the removal set with no own-commits guard.git branch --mergedlists every branch reachable from the ref, including a brand-new worktree branch whose tip is the base commit (a trivial ancestor). With the installedpost-mergehook (which passes--force), such fresh worktrees were silently removed and their branches force-deleted.IsSquashMerged) already guarded this exact case viamergeBase == tip; the plain--mergedancestor path did not.git.HasOwnCommits(true only when the branch tip differs from its merge-base with the ref) and wired it intoFindMergedCandidates, so a--mergedhit becomes a removal candidate only when the branch actually contributed commits. Fresh and fast-forward-merged branches are protected; squash- and merge-commit-merged branches still get removed. The guard lives in candidate finding, not removal, so--forcecannot bypass it.Test Plan
make test—TestFindMergedCandidates*pass; newTestFindMergedCandidatesFreshWorktreeNotRemovedfails without the guard, passes with itmake lintmake test-e2eCloses #335