Files
Grove/docs/prune-merged.md
T
Claude e9ac627863 feat(daemon): prune worktrees merged through a pull request
The daemon asks the forge (GitHub, Gitea/Forgejo) which pull requests were
merged and removes the local worktree and branch of each merged head that
holds no local work. Squash/rebase merges and head branches kept on the
forge are covered; regenerable ignored output (target/, node_modules...)
does not keep a worktree, any other ignored file does. The tip is kept in a
refs/sync-backup/<machine>/<stamp>/merged/<branch> ref, and materialize no
longer brings a pruned branch back unless it gets new commits.

New config: sync.prune_merged (default true) and sync.prune_disposable.
See docs/prune-merged.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011GZUAKXXztS4Hy4F4sSRkc
2026-11-16 21:41:56 +00:00

4.9 KiB

Pruning worktrees merged through a pull request

Worktrees of branches that were pushed, reviewed in a pull request and merged used to stay forever: archive_gone only removes a worktree when its remote branch is deleted, the forge often keeps the head branch, squash and rebase merges leave no ancestry for git to see, and a Rust/Node worktree always holds ignored build output that kept it "dirty". The daemon now asks the forge which pull requests were merged and removes those worktrees itself.

Config

[sync]
prune_merged = true                       # default
prune_disposable = ["target", "node_modules", ".venv", "venv", "__pycache__", ".pytest_cache",
  ".mypy_cache", ".ruff_cache", ".tox", ".gradle", ".next", ".nuxt", ".svelte-kit", ".turbo",
  ".parcel-cache", "dist", "build", "out", "coverage", ".cache"]   # default
  • prune_merged = false turns the feature off.
  • prune_disposable: names (not paths, not globs) of ignored files/directories that are regenerable and may go with a pruned worktree. Entries must be plain names.

What counts as merged

Provider::merged_pulls(owner, repo) lists one page of the most recently updated closed pull requests (GitHub: 100, merged_at set; Gitea/Forgejo: 50, merged: true), keeping only those whose head repo is the base repo: fork PRs reuse branch names freely. The repo is found from origin's fetch URL, matched to a configured provider like the visibility check (phase6.md), with the token from token::resolve.

When a worktree is removed

All of these must hold (grove_core::prune::prune_merged):

  1. The repo is bare layout, the fetch of this pass worked, and the pass is not offline.
  2. The worktree is on a branch that is not .bare's HEAD (the default branch), and it lives inside the repo directory (root/<group>/<repo>/..., so nested ones such as main/.claude/worktrees/x count). Linked worktrees elsewhere are the user's and are never touched.
  3. A merged PR has that branch as head and its head commit equals the local tip, or contains it (the PR got more commits from another machine before the merge).
  4. No local work: no staged, modified or untracked file, no git operation in progress (index.lock, rebase, merge, cherry-pick, revert, bisect), and no ignored entry whose name is not in prune_disposable (.env keeps it, target/ does not).

Then the tip is saved under refs/sync-backup/<machine>/<stamp>/merged/<branch>, the worktree is removed (git worktree remove --force, needed for the disposable ignored files only) and the local branch is deleted. Nothing on the forge changes: the remote branch, if any, stays.

A merged worktree with local work is kept and reported, once per change, as prune_kept (feat/x: merged in PR #12 but kept: ignored files: .env).

Not brought back by auto_materialize

materialize creates a worktree for every remote branch without one, so a pruned branch whose head was not deleted on the forge would come back on the next pass. It now skips a remote branch whose tip is the head of a merged PR or the tip this machine pruned (its merged/ backup ref), so this holds also offline or when the forge cannot be asked. New commits pushed to the branch after the merge are new work: the worktree is materialized again.

Daemon

  • The forge is asked at most every 5 minutes per repo, and only when the repo has a worktree on a non-default branch. The answer is in memory only.
  • A failed lookup keeps the last answer and is logged once as prune_lookup_failed (until the error changes).
  • Offline passes (backoff) prune nothing, but still use the last answer to keep pruned branches from being materialized.
  • Activity log: pruned_merged (removed, with the PR, path and backup ref) and prune_kept.

grove sync <path> does the same in one shot (no cache) and prints pruned ... / kept ... lines; a failed lookup is a warning and prunes nothing.

Validation

  • grove-core/tests/prune.rs: squash merge with the head branch kept on the remote (pruned with target/ present, backup ref, not re-materialized with or without a forge answer, comes back after a new commit), local work (untracked file, non-disposable ignored file, local commit) keeps the worktree, an older tip contained in the PR head is pruned, the default branch and a linked worktree outside the repo are never touched.
  • grove-providers/tests/api.rs: request shape on both forges, only merged same-repo PRs kept (fork and deleted-fork heads left out), API errors surface.
  • grove-daemon merged unit tests: TTL cache, last answer kept on failure, error and kept notes logged once.
  • Config: defaults and validation of prune_disposable.

Not verified

  • Against a real GitHub or Gitea (mock-tested only).
  • Only the first page of closed PRs is read: a branch merged long ago in a busy repo (more than 100 / 50 PRs closed since) is not found. grove sync on it does not help either; remove it by hand.