mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 15:26:09 +00:00
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
This commit is contained in:
19 files changed
+1070
-19
No files matched your search
@@ -7,3 +7,5 @@
|
||||
5. Final name: Grove (decided; binary `grove`, crates `grove-core` and `grove`).
|
||||
6. Open: `groved` under systemd gets `PATH=/usr/local/bin:/usr/bin:/bin`, so a `tea`/`gh` in `~/.local/bin` is not found and the token lookup fails. Options: ship `Environment=PATH=%h/.local/bin:...` in `contrib/groved.service`, or resolve the helpers by absolute path (`~/.local/bin`, then `PATH`). Workaround in use: a user drop-in (see `two-pc-run.md`).
|
||||
7. Open: `grove add` refuses `extensions.worktreeConfig = true` even when no `config.worktree` exists. Claude Code worktrees leave that flag behind. Options: accept it when no per-worktree config file exists (and sparse checkout is off), or keep refusing and print the exact `git config --unset` hint.
|
||||
8. Merged worktrees are pruned (`prune-merged.md`), but the PR's head branch is left on the forge and this machine's `refs/sync/<machine>/<branch>` snapshot ref stays there too. Open: should grove delete either once the PR is merged (the forge's "delete branch after merge" covers the first)?
|
||||
9. Open: a branch recreated under the name of a fast-forward-merged PR before the default branch moves has the PR head as tip and is pruned at once if clean (nothing is lost; a backup ref is kept). Options: accept it, or require the worktree to be older than the merge.
|
||||
@@ -0,0 +1,92 @@
|
||||
# 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
|
||||
|
||||
```toml
|
||||
[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.
|
||||
Reference in new issue
Block a user