Skip to content

[fix] Protect fresh worktrees from clean --merged (#335)#336

Open
MohammadYusif wants to merge 1 commit into
lugassawan:mainfrom
MohammadYusif:fix/issue-335
Open

[fix] Protect fresh worktrees from clean --merged (#335)#336
MohammadYusif wants to merge 1 commit into
lugassawan:mainfrom
MohammadYusif:fix/issue-335

Conversation

@MohammadYusif

Copy link
Copy Markdown

Summary

  • rimba clean --merged was force-deleting freshly-created worktrees that had no commits of their own.
  • Root cause: FindMergedCandidates (internal/operations/clean.go) added any branch returned by git branch --merged <ref> to the removal set with no own-commits guard. git branch --merged lists every branch reachable from the ref, including a brand-new worktree branch whose tip is the base commit (a trivial ancestor). With the installed post-merge hook (which passes --force), such fresh worktrees were silently removed and their branches force-deleted.
  • The squash-merge fallback (IsSquashMerged) already guarded this exact case via mergeBase == tip; the plain --merged ancestor path did not.
  • Fix: added git.HasOwnCommits (true only when the branch tip differs from its merge-base with the ref) and wired it into FindMergedCandidates, so a --merged hit 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 --force cannot bypass it.

Test Plan

  • make testTestFindMergedCandidates* pass; new TestFindMergedCandidatesFreshWorktreeNotRemoved fails without the guard, passes with it
  • make lint
  • make test-e2e

Closes #335

`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.
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.

[bug] rimba clean --merged deletes freshly-created clean worktrees (no own commits)

1 participant