Skip to content

fix(worktree): accurate merged detection and list without sizes - #524

Merged
jahvon merged 2 commits into
mainfrom
fix/worktree-merged-detection
Nov 19, 2026
Merged

jahvon merged 2 commits into
mainfrom
fix/worktree-merged-detection

Conversation

@jahvon

@jahvon jahvon commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up to #520. flow wt prune --merged could delete a worktree created moments earlier, and wt list was too slow for a caller that refreshes it (Mochi's desktop worktree panel).

Notable Changes

  • Merged detection: BranchMerged used git branch --merged, which counts a branch with no commits of its own as merged, so a fresh worktree was flagged and pruned. A branch now needs commits of its own (from its reflog; without a reflog it falls back to the old check), and counts as merged when it is an ancestor of the default branch or its combined change is patch-equivalent to a commit there, which catches squash merges. It compares against origin/<default> when that exists, since a local default that is never pulled misses everything merged upstream.
  • flow wt list --no-size: skips the per-worktree disk walk, which covers node_modules and build output and dominated the run time. MCP list_worktrees keeps sizes, since agents use them to pick cleanup targets.
flowchart TD
  A[branch] --> B{own commits in reflog?}
  B -- no --> N[not merged]
  B -- yes --> C{ancestor of origin/default?}
  C -- yes --> M[merged]
  C -- no --> D{squashed tree patch-equivalent<br/>to a commit on default?}
  D -- yes --> M
  D -- no --> N
Loading

Not changed: wt add still appends the worktree dir to .gitignore without saying so, and wt remove still falls back to a folder-name match. Both are worth a look but are outside this fix.

…erges

BranchMerged used `git branch --merged`, which lists a branch with no
commits of its own, so `flow wt prune --merged` removed worktrees that
had just been created. It also never matched squash merges, and compared
against a local default branch that is often never pulled.

A branch now needs commits of its own (read from its reflog), and counts
as merged when it is an ancestor of origin's default branch (local when
there is no remote) or its combined change is patch-equivalent to a
commit there.
Measuring disk usage walks every file in every worktree, including
node_modules and build output, which makes list slow for callers that
refresh it, such as a desktop panel.
@jahvon

jahvon commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Verification: flow validate passed locally (generate, lint go, unit + e2e, generated, schemas). New tests: TestBranchMergedFreshBranch, TestBranchMergedSquash, TestBranchMergedOnRemoteOnly.

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.06977% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/services/git/worktree.go 74.28% 5 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@jahvon jahvon changed the title fix(worktree): accurate merged detection, and list without sizes fix(worktree): accurate merged detection and list without sizes Nov 19, 2026
@jahvon
jahvon marked this pull request as ready for review November 19, 2026 21:46
@jahvon
jahvon merged commit 3d58519 into main Nov 19, 2026
17 checks passed
@jahvon
jahvon deleted the fix/worktree-merged-detection branch November 19, 2026 21:47
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.

1 participant