mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +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
@@ -5,9 +5,11 @@
|
||||
use crate::activity::{Activity, now_secs};
|
||||
use crate::discover::{discover, group_of, is_ready, subdirs};
|
||||
use crate::ipc;
|
||||
use crate::merged::{MergedCache, has_candidates};
|
||||
use crate::purge;
|
||||
use crate::visibility::{RealForge, Visibility, snapshot_gate};
|
||||
use grove_core::config::Config;
|
||||
use grove_core::prune::Pruned;
|
||||
use grove_core::receive::Received;
|
||||
use grove_core::sync::{Options, sync_repo};
|
||||
use notify::{EventKind, RecursiveMode, Watcher};
|
||||
@@ -427,8 +429,8 @@ fn run(cfg: Config, st: Settings, rx: Receiver<Msg>, ctx: Ctx) {
|
||||
let mut next_full = Instant::now();
|
||||
// When a debounced rediscovery is due (set by the first event of a burst, never pushed back).
|
||||
let mut rescan_at: Option<Instant> = None;
|
||||
// Repos whose forge first confirmed their deletion at this time (see `purge`).
|
||||
let mut gone_since: HashMap<PathBuf, (u64, u64)> = HashMap::new();
|
||||
// Deletions being confirmed and merged pull requests, per repo.
|
||||
let mut forge = ForgeMemory::default();
|
||||
|
||||
loop {
|
||||
let now = Instant::now();
|
||||
@@ -518,15 +520,7 @@ fn run(cfg: Config, st: Settings, rx: Receiver<Msg>, ctx: Ctx) {
|
||||
// While backing off after a failure, still snapshot local work (no network needed);
|
||||
// the network is retried at the first pass after the backoff expires.
|
||||
let offline = backoff_until.get(&r).is_some_and(|u| *u > now);
|
||||
let failures = pass(
|
||||
&r,
|
||||
&cfg,
|
||||
&st,
|
||||
&mut visibility,
|
||||
offline,
|
||||
&mut gone_since,
|
||||
&ctx,
|
||||
);
|
||||
let failures = pass(&r, &cfg, &st, &mut visibility, offline, &mut forge, &ctx);
|
||||
if !is_ready(&r) {
|
||||
// It vanished mid-pass: whatever the pass reported is moot.
|
||||
let (found, _) = rescan(&root, &groups, &mut watch, &ctx);
|
||||
@@ -563,6 +557,15 @@ fn forget_removed(
|
||||
backoff_until.retain(|r, _| found.contains(r));
|
||||
}
|
||||
|
||||
/// What the daemon remembers between passes about each repo's forge (in memory only).
|
||||
#[derive(Default)]
|
||||
struct ForgeMemory {
|
||||
/// Repos whose forge first confirmed their deletion at this time (see `purge`).
|
||||
gone_since: HashMap<PathBuf, (u64, u64)>,
|
||||
/// Merged pull requests per repo (see `merged`).
|
||||
merged: MergedCache,
|
||||
}
|
||||
|
||||
/// One sync pass for `repo`; returns the number of consecutive failures (0 = success).
|
||||
fn pass(
|
||||
repo: &Path,
|
||||
@@ -570,9 +573,10 @@ fn pass(
|
||||
st: &Settings,
|
||||
visibility: &mut Visibility,
|
||||
offline: bool,
|
||||
gone_since: &mut HashMap<PathBuf, (u64, u64)>,
|
||||
forge: &mut ForgeMemory,
|
||||
ctx: &Ctx,
|
||||
) -> u32 {
|
||||
let ForgeMemory { gone_since, merged } = forge;
|
||||
let name = repo.display().to_string();
|
||||
if !is_ready(repo) {
|
||||
// Being migrated/cloned (or gone): leave it alone, the rediscovery sorts it out.
|
||||
@@ -603,6 +607,22 @@ fn pass(
|
||||
}
|
||||
allowed
|
||||
};
|
||||
// Offline passes prune nothing; the last answer still keeps pruned branches away.
|
||||
let merged_heads = if !cfg.sync.prune_merged || !has_candidates(repo) {
|
||||
Vec::new()
|
||||
} else if offline {
|
||||
merged.cached(repo)
|
||||
} else {
|
||||
let h = merged.heads(cfg, repo, &st.remote, &RealForge, Instant::now());
|
||||
if let Some(e) = h.new_error {
|
||||
ctx.activity.log(
|
||||
"prune_lookup_failed",
|
||||
Some(&name),
|
||||
format!("cannot list merged pull requests: {e}"),
|
||||
);
|
||||
}
|
||||
h.heads
|
||||
};
|
||||
let opts = Options {
|
||||
machine: cfg.machine.name.clone(),
|
||||
stamp: stamp(),
|
||||
@@ -610,6 +630,8 @@ fn pass(
|
||||
offline,
|
||||
materialize,
|
||||
push_snapshots,
|
||||
merged: merged_heads,
|
||||
disposable: cfg.sync.prune_disposable.clone(),
|
||||
};
|
||||
let result = sync_repo(repo, &opts);
|
||||
if !offline && purge_if_gone(repo, cfg, st, &result, gone_since, ctx) {
|
||||
@@ -679,6 +701,30 @@ fn pass(
|
||||
for a in &rep.archived {
|
||||
ctx.activity.log("archived", Some(&name), format!("{a:?}"));
|
||||
}
|
||||
let mut kept = Vec::new();
|
||||
for p in &rep.pruned {
|
||||
match p {
|
||||
Pruned::Removed {
|
||||
branch,
|
||||
pr,
|
||||
path,
|
||||
backup_ref,
|
||||
} => ctx.activity.log(
|
||||
"pruned_merged",
|
||||
Some(&name),
|
||||
format!(
|
||||
"{branch}: merged in PR #{pr}; removed {} (tip kept in {backup_ref})",
|
||||
path.display()
|
||||
),
|
||||
),
|
||||
Pruned::Kept { branch, pr, why } => {
|
||||
kept.push(format!("{branch}: merged in PR #{pr} but kept: {why}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
for k in merged.new_notes(repo, kept) {
|
||||
ctx.activity.log("prune_kept", Some(&name), k);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !errors.is_empty() && !is_ready(repo) {
|
||||
|
||||
@@ -6,6 +6,7 @@ pub mod activity;
|
||||
pub mod daemon;
|
||||
pub mod discover;
|
||||
pub mod ipc;
|
||||
pub mod merged;
|
||||
pub mod purge;
|
||||
#[cfg(test)]
|
||||
#[path = "../../../test-support/tmp.rs"]
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
//! Which branches of a repo were merged through a pull request on its forge (`origin`), so
|
||||
//! that their worktrees can be pruned (`grove_core::prune`).
|
||||
//!
|
||||
//! The daemon asks the forge at most every [`TTL`] per repo, and only for repos that have a
|
||||
//! worktree on a branch other than the default one. A failed lookup keeps the last answer, so a
|
||||
//! forge outage never makes `materialize` bring back worktrees that were pruned.
|
||||
|
||||
use crate::visibility::{RealForge, parse_remote, provider_for};
|
||||
use grove_core::config::{Config, Provider as ProviderCfg};
|
||||
use grove_core::git::Git;
|
||||
use grove_core::inspect::worktrees;
|
||||
use grove_core::prune::MergedHead;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// How long a forge answer is reused.
|
||||
pub const TTL: Duration = Duration::from_secs(300);
|
||||
|
||||
/// Lists the merged pull requests of a repo on a forge.
|
||||
pub trait PullSource {
|
||||
fn merged(
|
||||
&self,
|
||||
id: &str,
|
||||
pc: &ProviderCfg,
|
||||
owner: &str,
|
||||
name: &str,
|
||||
) -> Result<Vec<MergedHead>, String>;
|
||||
}
|
||||
|
||||
impl PullSource for RealForge {
|
||||
fn merged(
|
||||
&self,
|
||||
id: &str,
|
||||
pc: &ProviderCfg,
|
||||
owner: &str,
|
||||
name: &str,
|
||||
) -> Result<Vec<MergedHead>, String> {
|
||||
let (token, _) = grove_providers::token::resolve(id, pc).map_err(|e| e.to_string())?;
|
||||
let pulls = grove_providers::build(pc, token)
|
||||
.merged_pulls(owner, name)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(pulls
|
||||
.into_iter()
|
||||
.map(|p| MergedHead {
|
||||
number: p.number,
|
||||
branch: p.head_ref,
|
||||
sha: p.head_sha,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// The merged pull requests of the repo at `repo` (bare layout), looked up on the forge that
|
||||
/// serves its `remote` fetch URL.
|
||||
pub fn lookup(
|
||||
cfg: &Config,
|
||||
repo: &Path,
|
||||
remote: &str,
|
||||
src: &dyn PullSource,
|
||||
) -> Result<Vec<MergedHead>, String> {
|
||||
let url = Git::new(repo.join(".bare"))
|
||||
.run(&["remote", "get-url", remote])
|
||||
.map_err(|e| e.to_string())?;
|
||||
let id = parse_remote(&url).ok_or_else(|| format!("cannot parse remote URL {url:?}"))?;
|
||||
let (pid, pc) = provider_for(cfg, &id)?;
|
||||
src.merged(pid, pc, &id.owner, &id.name)
|
||||
}
|
||||
|
||||
/// True when the repo has a worktree on a branch other than `.bare`'s HEAD: only then can
|
||||
/// anything be pruned, so only then is the forge asked.
|
||||
pub fn has_candidates(repo: &Path) -> bool {
|
||||
let bare = repo.join(".bare");
|
||||
let default = Git::new(&bare)
|
||||
.probe(&["symbolic-ref", "--short", "-q", "HEAD"])
|
||||
.ok()
|
||||
.flatten();
|
||||
worktrees(&bare).is_ok_and(|wts| {
|
||||
wts.iter()
|
||||
.filter_map(|w| w.branch.as_deref())
|
||||
.any(|b| Some(b) != default.as_deref())
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Entry {
|
||||
checked: Option<Instant>,
|
||||
heads: Vec<MergedHead>,
|
||||
error: Option<String>,
|
||||
/// "Kept" notes already logged, so a merged worktree with local work is reported once.
|
||||
noted: BTreeSet<String>,
|
||||
}
|
||||
|
||||
/// Per-repo forge answers and notes, held by the daemon loop (in memory only).
|
||||
#[derive(Default)]
|
||||
pub struct MergedCache {
|
||||
entries: HashMap<PathBuf, Entry>,
|
||||
}
|
||||
|
||||
/// Result of [`MergedCache::heads`].
|
||||
pub struct Heads {
|
||||
pub heads: Vec<MergedHead>,
|
||||
/// A lookup error that differs from the previous one (to be logged once).
|
||||
pub new_error: Option<String>,
|
||||
}
|
||||
|
||||
impl MergedCache {
|
||||
/// The merged heads of `repo`, asking `src` when the cached answer is older than [`TTL`].
|
||||
pub fn heads(
|
||||
&mut self,
|
||||
cfg: &Config,
|
||||
repo: &Path,
|
||||
remote: &str,
|
||||
src: &dyn PullSource,
|
||||
now: Instant,
|
||||
) -> Heads {
|
||||
let e = self.entries.entry(repo.to_path_buf()).or_default();
|
||||
if e.checked
|
||||
.is_some_and(|t| now.saturating_duration_since(t) < TTL)
|
||||
{
|
||||
return Heads {
|
||||
heads: e.heads.clone(),
|
||||
new_error: None,
|
||||
};
|
||||
}
|
||||
e.checked = Some(now);
|
||||
let mut new_error = None;
|
||||
match lookup(cfg, repo, remote, src) {
|
||||
Ok(h) => {
|
||||
e.heads = h;
|
||||
e.error = None;
|
||||
}
|
||||
Err(err) => {
|
||||
if e.error.as_deref() != Some(&err) {
|
||||
new_error = Some(err.clone());
|
||||
}
|
||||
e.error = Some(err);
|
||||
}
|
||||
}
|
||||
Heads {
|
||||
heads: e.heads.clone(),
|
||||
new_error,
|
||||
}
|
||||
}
|
||||
|
||||
/// Of the "kept" notes of this pass, those not logged yet for `repo`. Notes that no longer
|
||||
/// apply are forgotten, so they are logged again if they come back.
|
||||
pub fn new_notes(&mut self, repo: &Path, notes: Vec<String>) -> Vec<String> {
|
||||
let e = self.entries.entry(repo.to_path_buf()).or_default();
|
||||
let now: BTreeSet<String> = notes.into_iter().collect();
|
||||
let fresh = now.difference(&e.noted).cloned().collect();
|
||||
e.noted = now;
|
||||
fresh
|
||||
}
|
||||
|
||||
/// The last answer for `repo`, without asking the forge (offline passes).
|
||||
pub fn cached(&self, repo: &Path) -> Vec<MergedHead> {
|
||||
self.entries
|
||||
.get(repo)
|
||||
.map(|e| e.heads.clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_tmp as tmp;
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
struct Fake {
|
||||
calls: Cell<u32>,
|
||||
asked: RefCell<Vec<String>>,
|
||||
answer: RefCell<Result<Vec<MergedHead>, String>>,
|
||||
}
|
||||
impl PullSource for Fake {
|
||||
fn merged(
|
||||
&self,
|
||||
id: &str,
|
||||
_: &ProviderCfg,
|
||||
owner: &str,
|
||||
name: &str,
|
||||
) -> Result<Vec<MergedHead>, String> {
|
||||
self.calls.set(self.calls.get() + 1);
|
||||
self.asked.borrow_mut().push(format!("{id}:{owner}/{name}"));
|
||||
self.answer.borrow().clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn head(n: u64) -> MergedHead {
|
||||
MergedHead {
|
||||
number: n,
|
||||
branch: format!("b{n}"),
|
||||
sha: "0".repeat(40),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answers_are_cached_and_kept_on_failure() {
|
||||
let d = tmp::TempDir::new("merged-cache");
|
||||
let repo = d.path().join("G/r");
|
||||
std::fs::create_dir_all(&repo).unwrap();
|
||||
let bare = repo.join(".bare");
|
||||
Git::new(d.path())
|
||||
.run(&["init", "-q", "--bare", bare.to_str().unwrap()])
|
||||
.unwrap();
|
||||
Git::new(&bare)
|
||||
.run(&["remote", "add", "origin", "git@git.example.es:Org/r.git"])
|
||||
.unwrap();
|
||||
let cfg = Config::parse(
|
||||
"[machine]\nname=\"m\"\n[root]\npath=\"/x\"\n\
|
||||
[providers.gitea]\nkind=\"gitea\"\nurl=\"https://git.example.es\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
let f = Fake {
|
||||
calls: Cell::new(0),
|
||||
asked: RefCell::new(vec![]),
|
||||
answer: RefCell::new(Ok(vec![head(1)])),
|
||||
};
|
||||
let mut c = MergedCache::default();
|
||||
let t0 = Instant::now();
|
||||
assert_eq!(c.heads(&cfg, &repo, "origin", &f, t0).heads, vec![head(1)]);
|
||||
assert_eq!(f.asked.borrow().as_slice(), ["gitea:Org/r"]);
|
||||
// Within the TTL the forge is not asked again.
|
||||
c.heads(&cfg, &repo, "origin", &f, t0 + Duration::from_secs(10));
|
||||
assert_eq!(f.calls.get(), 1);
|
||||
// A failure keeps the last answer and is reported once.
|
||||
*f.answer.borrow_mut() = Err("down".into());
|
||||
let h = c.heads(&cfg, &repo, "origin", &f, t0 + TTL);
|
||||
assert_eq!((h.heads, h.new_error), (vec![head(1)], Some("down".into())));
|
||||
let h = c.heads(&cfg, &repo, "origin", &f, t0 + TTL * 2);
|
||||
assert_eq!((h.heads, h.new_error), (vec![head(1)], None));
|
||||
assert_eq!(f.calls.get(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notes_are_reported_once_while_they_hold() {
|
||||
let mut c = MergedCache::default();
|
||||
let r = Path::new("/r");
|
||||
assert_eq!(c.new_notes(r, vec!["a".into()]), vec!["a".to_string()]);
|
||||
assert!(c.new_notes(r, vec!["a".into()]).is_empty());
|
||||
assert!(c.new_notes(r, vec![]).is_empty());
|
||||
assert_eq!(c.new_notes(r, vec!["a".into()]), vec!["a".to_string()]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user