From 990f155f65cfedfa9f6258d050644a958b360e6f Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 17:14:29 +0100 Subject: [PATCH] feat(core): snapshots policy in config and push_snapshots sync option Co-Authored-By: Claude Sonnet 5.5 --- crates/grove-core/src/config.rs | 72 ++++++++++++++++++++++++++++++ crates/grove-core/src/sync.rs | 6 ++- crates/grove-core/tests/receive.rs | 1 + crates/grove-core/tests/sync.rs | 42 +++++++++++++++++ crates/grove/src/workflow.rs | 1 + 5 files changed, 121 insertions(+), 1 deletion(-) diff --git a/crates/grove-core/src/config.rs b/crates/grove-core/src/config.rs index 05a0664..2f827b6 100644 --- a/crates/grove-core/src/config.rs +++ b/crates/grove-core/src/config.rs @@ -62,6 +62,9 @@ pub struct Group { pub layout: Layout, #[serde(default = "yes")] pub auto_materialize: bool, + /// Overrides `sync.snapshots` for this group. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub snapshots: Option, /// Account or organisation that owns the group's repos, per provider id. A provider not /// listed here uses the authenticated user. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] @@ -72,6 +75,20 @@ pub struct Group { pub public: Option, } +/// Whether the daemon pushes WIP snapshots (`refs/sync//`) of a repo. +/// Snapshots contain uncommitted work, so they must not land in public repos. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum SnapshotPolicy { + /// Push only when the forge confirms the repo is private (unknown counts as public). + #[default] + PrivateOnly, + /// Push for every repo, public ones included. + All, + /// Never push snapshots. + None, +} + /// A primary forge plus the forges it is mirrored to. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)] pub struct Route { @@ -109,6 +126,9 @@ pub struct Sync { /// Changed files larger than this many MiB are not snapshotted (0 = no limit). #[serde(default = "default_max_file_mb")] pub max_file_mb: u64, + /// Default snapshot push policy; a group can override it. + #[serde(default)] + pub snapshots: SnapshotPolicy, } impl Default for Sync { @@ -118,6 +138,7 @@ impl Default for Sync { fetch_interval_s: default_fetch(), ignore_extra: Vec::new(), max_file_mb: default_max_file_mb(), + snapshots: SnapshotPolicy::default(), } } } @@ -201,6 +222,15 @@ impl Config { Some(base.join("grove")) } + /// The snapshot policy of the group named `group`, falling back to `sync.snapshots`. + pub fn snapshot_policy(&self, group: &str) -> SnapshotPolicy { + self.groups + .iter() + .find(|g| g.name == group) + .and_then(|g| g.snapshots) + .unwrap_or(self.sync.snapshots) + } + /// `root.path` with a leading `~` expanded. pub fn root_path(&self) -> PathBuf { expand_home(&self.root.path) @@ -220,6 +250,13 @@ impl Config { )); } for g in &self.groups { + // Group names are directory names under the root. + if g.name.is_empty() || g.name.starts_with('.') || g.name.contains(['/', '\\']) { + return bad(format!( + "group name {:?} must be a plain directory name", + g.name + )); + } let mut routes = vec![("", g.route(true))]; if let Some(p) = &g.public { routes.push(("public.", p.clone())); @@ -346,6 +383,41 @@ name = "grove-config" )); } + #[test] + fn snapshot_policy_defaults_to_private_only_and_groups_override() { + let c = Config::parse(FULL).unwrap(); + assert_eq!(c.sync.snapshots, SnapshotPolicy::PrivateOnly); + assert_eq!(c.snapshot_policy("work"), SnapshotPolicy::PrivateOnly); + assert_eq!(c.snapshot_policy("unknown"), SnapshotPolicy::PrivateOnly); + let t = FULL + .replace( + "fetch_interval_s = 30", + "fetch_interval_s = 30\nsnapshots = \"none\"", + ) + .replace( + "mirror_to = [\"github\"]", + "mirror_to = [\"github\"]\nsnapshots = \"all\"", + ); + let c = Config::parse(&t).unwrap(); + assert_eq!(c.snapshot_policy("work"), SnapshotPolicy::All); + assert_eq!(c.snapshot_policy("other"), SnapshotPolicy::None); + let again = toml::to_string_pretty(&c).unwrap(); + assert_eq!(Config::parse(&again).unwrap(), c); + } + + #[test] + fn rejects_unknown_snapshot_policy_and_bad_group_names() { + let bad = FULL.replace("fetch_interval_s = 30", "snapshots = \"public\""); + assert!(matches!(Config::parse(&bad), Err(ConfigError::Parse(_)))); + for n in ["a/b", ".hidden", ""] { + let t = FULL.replace("name = \"work\"", &format!("name = \"{n}\"")); + assert!( + matches!(Config::parse(&t), Err(ConfigError::Invalid(_))), + "{n:?}" + ); + } + } + const ROUTED: &str = r#" [machine] name = "a" diff --git a/crates/grove-core/src/sync.rs b/crates/grove-core/src/sync.rs index 2e7b1c0..461e179 100644 --- a/crates/grove-core/src/sync.rs +++ b/crates/grove-core/src/sync.rs @@ -20,6 +20,10 @@ pub struct Options { pub offline: bool, /// Create worktrees for new remote branches and archive gone ones (bare layout only). pub materialize: bool, + /// Take and push WIP snapshots. When false (private-only policy on a public or unknown + /// repo, or `snapshots = "none"`) no `refs/sync//*` ref is created or pushed; + /// fetching, fast-forwarding and applying other machines' snapshots are unaffected. + pub push_snapshots: bool, } #[derive(Debug, Clone, PartialEq)] @@ -110,7 +114,7 @@ fn sync_worktree(r: &mut WorktreeReport, branch: &str, o: &Options, fetched: boo } }; // A clean worktree still refreshes an existing snapshot so a stale WIP never lingers. - if dirty || has_ref { + if o.push_snapshots && (dirty || has_ref) { match take(&path, &o.machine, &o.stamp) { Ok(Outcome::Created(s)) => { r.snapshot = Some("created"); diff --git a/crates/grove-core/tests/receive.rs b/crates/grove-core/tests/receive.rs index d3f252d..567614b 100644 --- a/crates/grove-core/tests/receive.rs +++ b/crates/grove-core/tests/receive.rs @@ -382,6 +382,7 @@ fn archive_and_receive_in_one_pass_do_not_collide_on_backup_refs() { remote: "origin".into(), offline: false, materialize: true, + push_snapshots: true, }; w.g1.run(&["checkout", "-q", "-b", "feat/z"]).unwrap(); commit(&w.g1, "z.txt", "z\n"); diff --git a/crates/grove-core/tests/sync.rs b/crates/grove-core/tests/sync.rs index 132d73b..52b6637 100644 --- a/crates/grove-core/tests/sync.rs +++ b/crates/grove-core/tests/sync.rs @@ -15,6 +15,7 @@ fn opts(machine: &str, stamp: &str) -> Options { remote: "origin".into(), offline: false, materialize: true, + push_snapshots: true, } } @@ -168,3 +169,44 @@ fn staged_changes_block_adoption_so_the_index_is_never_reset() { assert_eq!(r.worktrees[0].state, State::BehindDirty); assert_eq!(gb.run(&["show", ":a.txt"]).unwrap(), "1\nstaged"); } + +#[test] +fn withheld_snapshots_are_neither_taken_nor_pushed_but_fast_forward_still_works() { + let base = tmpdir("nosnap"); + let remote = base.join("r.git"); + Git::new(&base) + .run(&[ + "init", + "-q", + "--bare", + "-b", + "main", + remote.to_str().unwrap(), + ]) + .unwrap(); + let seed = init(&base.join("seed")); + seed.run(&["remote", "add", "origin", remote.to_str().unwrap()]) + .unwrap(); + commit(&seed, "a.txt", "1\n"); + seed.run(&["push", "-q", "-u", "origin", "main"]).unwrap(); + let (_g, d) = clone(&base, &remote, "A"); + fs::write(d.join("a.txt"), "1\nsecret\n").unwrap(); + let o = Options { + push_snapshots: false, + ..opts("A", "t1") + }; + let r = sync_repo(&d, &o).unwrap(); + assert_eq!(r.worktrees[0].snapshot, None); + let g = Git::new(&remote); + assert!(g.run(&["for-each-ref", "refs/sync/"]).unwrap().is_empty()); + // Fast-forwarding keeps working. + commit(&seed, "b.txt", "2\n"); + seed.run(&["push", "-q"]).unwrap(); + fs::write(d.join("a.txt"), "1\n").unwrap(); + let r = sync_repo(&d, &o).unwrap(); + assert!(matches!( + r.worktrees[0].received, + Some(Received::FastForwarded { .. }) + )); + assert!(d.join("b.txt").exists()); +} diff --git a/crates/grove/src/workflow.rs b/crates/grove/src/workflow.rs index 177e35e..aee26b3 100644 --- a/crates/grove/src/workflow.rs +++ b/crates/grove/src/workflow.rs @@ -132,6 +132,7 @@ pub fn init(path: &Path, a: InitArgs, force: bool) -> Result { mirror_to: vec![], layout: Layout::BareWorktrees, auto_materialize: true, + snapshots: None, owners: Default::default(), public: None, }],