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
@@ -1,6 +1,7 @@
|
||||
use crate::http::Http;
|
||||
use crate::{
|
||||
Capabilities, Error, MirrorSpec, NewRepo, Provider, PullMirrorSpec, RemoteRepo, Result,
|
||||
Capabilities, Error, MergedPull, MirrorSpec, NewRepo, Provider, PullMirrorSpec, RemoteRepo,
|
||||
Result, same_repo,
|
||||
};
|
||||
use grove_core::config::ProviderKind;
|
||||
use serde_json::{Value, json};
|
||||
@@ -156,4 +157,24 @@ impl Provider for Gitea {
|
||||
Some(&body),
|
||||
)?))
|
||||
}
|
||||
|
||||
fn merged_pulls(&self, owner: &str, name: &str) -> Result<Vec<MergedPull>> {
|
||||
let v = self.http.ok(
|
||||
"GET",
|
||||
&format!("/repos/{owner}/{name}/pulls?state=closed&sort=recentupdate&limit=50"),
|
||||
None,
|
||||
)?;
|
||||
Ok(v.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|p| p["merged"].as_bool() == Some(true) && same_repo(p))
|
||||
.filter_map(|p| {
|
||||
Some(MergedPull {
|
||||
number: p["number"].as_u64()?,
|
||||
head_ref: p["head"]["ref"].as_str()?.to_string(),
|
||||
head_sha: p["head"]["sha"].as_str()?.to_string(),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::http::Http;
|
||||
use crate::{
|
||||
Capabilities, Error, MirrorSpec, NewRepo, Provider, PullMirrorSpec, RemoteRepo, Result,
|
||||
Capabilities, Error, MergedPull, MirrorSpec, NewRepo, Provider, PullMirrorSpec, RemoteRepo,
|
||||
Result, same_repo,
|
||||
};
|
||||
use grove_core::config::ProviderKind;
|
||||
use serde_json::{Value, json};
|
||||
@@ -138,4 +139,28 @@ impl Provider for GitHub {
|
||||
"GitHub cannot pull-mirror another forge",
|
||||
))
|
||||
}
|
||||
|
||||
fn merged_pulls(&self, owner: &str, name: &str) -> Result<Vec<MergedPull>> {
|
||||
let v = self.http.ok(
|
||||
"GET",
|
||||
&format!(
|
||||
"/repos/{owner}/{name}/pulls?state=closed&sort=updated&direction=desc&per_page=100"
|
||||
),
|
||||
None,
|
||||
)?;
|
||||
Ok(v.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|p| p["merged_at"].is_string() && same_repo(p))
|
||||
.filter_map(pull)
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
fn pull(p: &Value) -> Option<MergedPull> {
|
||||
Some(MergedPull {
|
||||
number: p["number"].as_u64()?,
|
||||
head_ref: p["head"]["ref"].as_str()?.to_string(),
|
||||
head_sha: p["head"]["sha"].as_str()?.to_string(),
|
||||
})
|
||||
}
|
||||
@@ -119,6 +119,25 @@ pub struct PullMirrorSpec {
|
||||
pub interval: String,
|
||||
}
|
||||
|
||||
/// A pull request merged on the forge whose head branch is in the same repo.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct MergedPull {
|
||||
pub number: u64,
|
||||
pub head_ref: String,
|
||||
pub head_sha: String,
|
||||
}
|
||||
|
||||
/// Head repo of a pull request equals its base repo (fork PRs reuse branch names freely).
|
||||
/// A head repo that was deleted reports `null`: not the same repo.
|
||||
pub(crate) fn same_repo(pr: &serde_json::Value) -> bool {
|
||||
let name = |side: &str| {
|
||||
pr[side]["repo"]["full_name"]
|
||||
.as_str()
|
||||
.map(str::to_ascii_lowercase)
|
||||
};
|
||||
matches!((name("head"), name("base")), (Some(h), Some(b)) if h == b)
|
||||
}
|
||||
|
||||
pub trait Provider: Send + Sync {
|
||||
fn kind(&self) -> ProviderKind;
|
||||
fn capabilities(&self) -> Capabilities;
|
||||
@@ -134,6 +153,9 @@ pub trait Provider: Send + Sync {
|
||||
fn configure_mirror(&self, owner: &str, repo: &str, spec: &MirrorSpec) -> Result<()>;
|
||||
/// Creates a pull mirror (see [`Capabilities::pull_mirror`]).
|
||||
fn create_pull_mirror(&self, spec: &PullMirrorSpec) -> Result<RemoteRepo>;
|
||||
/// The most recently updated closed pull requests (one page) that were merged, newest
|
||||
/// first, leaving out those whose head is in a fork.
|
||||
fn merged_pulls(&self, owner: &str, name: &str) -> Result<Vec<MergedPull>>;
|
||||
}
|
||||
|
||||
/// Builds the provider described by `cfg`, using `token`.
|
||||
|
||||
Reference in new issue
Block a user