mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
feat(core): snapshots policy in config and push_snapshots sync option
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fc309f9b18
commit
990f155f65
5 files changed
+121
-1
No files matched your search
@@ -62,6 +62,9 @@ pub struct Group {
|
|||||||
pub layout: Layout,
|
pub layout: Layout,
|
||||||
#[serde(default = "yes")]
|
#[serde(default = "yes")]
|
||||||
pub auto_materialize: bool,
|
pub auto_materialize: bool,
|
||||||
|
/// Overrides `sync.snapshots` for this group.
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub snapshots: Option<SnapshotPolicy>,
|
||||||
/// Account or organisation that owns the group's repos, per provider id. A provider not
|
/// Account or organisation that owns the group's repos, per provider id. A provider not
|
||||||
/// listed here uses the authenticated user.
|
/// listed here uses the authenticated user.
|
||||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
@@ -72,6 +75,20 @@ pub struct Group {
|
|||||||
pub public: Option<Route>,
|
pub public: Option<Route>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether the daemon pushes WIP snapshots (`refs/sync/<machine>/<branch>`) 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.
|
/// A primary forge plus the forges it is mirrored to.
|
||||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||||
pub struct Route {
|
pub struct Route {
|
||||||
@@ -109,6 +126,9 @@ pub struct Sync {
|
|||||||
/// Changed files larger than this many MiB are not snapshotted (0 = no limit).
|
/// Changed files larger than this many MiB are not snapshotted (0 = no limit).
|
||||||
#[serde(default = "default_max_file_mb")]
|
#[serde(default = "default_max_file_mb")]
|
||||||
pub max_file_mb: u64,
|
pub max_file_mb: u64,
|
||||||
|
/// Default snapshot push policy; a group can override it.
|
||||||
|
#[serde(default)]
|
||||||
|
pub snapshots: SnapshotPolicy,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Sync {
|
impl Default for Sync {
|
||||||
@@ -118,6 +138,7 @@ impl Default for Sync {
|
|||||||
fetch_interval_s: default_fetch(),
|
fetch_interval_s: default_fetch(),
|
||||||
ignore_extra: Vec::new(),
|
ignore_extra: Vec::new(),
|
||||||
max_file_mb: default_max_file_mb(),
|
max_file_mb: default_max_file_mb(),
|
||||||
|
snapshots: SnapshotPolicy::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,6 +222,15 @@ impl Config {
|
|||||||
Some(base.join("grove"))
|
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.
|
/// `root.path` with a leading `~` expanded.
|
||||||
pub fn root_path(&self) -> PathBuf {
|
pub fn root_path(&self) -> PathBuf {
|
||||||
expand_home(&self.root.path)
|
expand_home(&self.root.path)
|
||||||
@@ -220,6 +250,13 @@ impl Config {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
for g in &self.groups {
|
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))];
|
let mut routes = vec![("", g.route(true))];
|
||||||
if let Some(p) = &g.public {
|
if let Some(p) = &g.public {
|
||||||
routes.push(("public.", p.clone()));
|
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#"
|
const ROUTED: &str = r#"
|
||||||
[machine]
|
[machine]
|
||||||
name = "a"
|
name = "a"
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ pub struct Options {
|
|||||||
pub offline: bool,
|
pub offline: bool,
|
||||||
/// Create worktrees for new remote branches and archive gone ones (bare layout only).
|
/// Create worktrees for new remote branches and archive gone ones (bare layout only).
|
||||||
pub materialize: bool,
|
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/<machine>/*` ref is created or pushed;
|
||||||
|
/// fetching, fast-forwarding and applying other machines' snapshots are unaffected.
|
||||||
|
pub push_snapshots: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[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.
|
// 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) {
|
match take(&path, &o.machine, &o.stamp) {
|
||||||
Ok(Outcome::Created(s)) => {
|
Ok(Outcome::Created(s)) => {
|
||||||
r.snapshot = Some("created");
|
r.snapshot = Some("created");
|
||||||
|
|||||||
@@ -382,6 +382,7 @@ fn archive_and_receive_in_one_pass_do_not_collide_on_backup_refs() {
|
|||||||
remote: "origin".into(),
|
remote: "origin".into(),
|
||||||
offline: false,
|
offline: false,
|
||||||
materialize: true,
|
materialize: true,
|
||||||
|
push_snapshots: true,
|
||||||
};
|
};
|
||||||
w.g1.run(&["checkout", "-q", "-b", "feat/z"]).unwrap();
|
w.g1.run(&["checkout", "-q", "-b", "feat/z"]).unwrap();
|
||||||
commit(&w.g1, "z.txt", "z\n");
|
commit(&w.g1, "z.txt", "z\n");
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ fn opts(machine: &str, stamp: &str) -> Options {
|
|||||||
remote: "origin".into(),
|
remote: "origin".into(),
|
||||||
offline: false,
|
offline: false,
|
||||||
materialize: true,
|
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!(r.worktrees[0].state, State::BehindDirty);
|
||||||
assert_eq!(gb.run(&["show", ":a.txt"]).unwrap(), "1\nstaged");
|
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());
|
||||||
|
}
|
||||||
@@ -132,6 +132,7 @@ pub fn init(path: &Path, a: InitArgs, force: bool) -> Result<Config> {
|
|||||||
mirror_to: vec![],
|
mirror_to: vec![],
|
||||||
layout: Layout::BareWorktrees,
|
layout: Layout::BareWorktrees,
|
||||||
auto_materialize: true,
|
auto_materialize: true,
|
||||||
|
snapshots: None,
|
||||||
owners: Default::default(),
|
owners: Default::default(),
|
||||||
public: None,
|
public: None,
|
||||||
}],
|
}],
|
||||||
|
|||||||
Reference in new issue
Block a user