chore: rename project to grove and add MIT license

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-11-16 12:52:25 +01:00
1 parent 126acb4b57
commit c935d779e1
29 files changed
+99 -76

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
#![allow(dead_code)]
use grove_core::git::Git;
use std::fs;
use std::path::{Path, PathBuf};
pub fn tmpdir(tag: &str) -> PathBuf {
let p = std::env::temp_dir().join(format!(
"grove-test-{tag}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&p).unwrap();
p
}
pub fn init(dir: &Path) -> Git {
fs::create_dir_all(dir).unwrap();
let g = Git::new(dir);
g.run(&["init", "-q", "-b", "main"]).unwrap();
g.run(&["config", "user.email", "t@t"]).unwrap();
g.run(&["config", "user.name", "t"]).unwrap();
g
}
pub fn commit(g: &Git, file: &str, body: &str) {
let path = g.dir().join(file);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, body).unwrap();
g.run(&["add", "."]).unwrap();
g.run(&["commit", "-qm", file]).unwrap();
}
/// A repo with history, a second branch, a stash, staged, modified, untracked and ignored files.
pub fn messy_repo(dir: &Path) -> Git {
let g = init(dir);
commit(&g, ".gitignore", "*.env\n");
commit(&g, "src/a.txt", "one\n");
commit(&g, "b.txt", "b\n");
g.run(&["branch", "other"]).unwrap();
fs::write(dir.join("b.txt"), "b changed for stash\n").unwrap();
g.run(&["stash", "push", "-q"]).unwrap();
fs::write(dir.join("src/a.txt"), "one\nmodified\n").unwrap();
fs::write(dir.join("staged.txt"), "staged\n").unwrap();
g.run(&["add", "staged.txt"]).unwrap();
fs::write(dir.join("untracked.txt"), "u\n").unwrap();
fs::write(dir.join("secret.env"), "s\n").unwrap();
g
}
+117
View File
@@ -0,0 +1,117 @@
use grove_core::git::Git;
use grove_core::inspect::{state, worktrees};
use grove_core::state::State;
use std::fs;
use std::path::{Path, PathBuf};
fn tmpdir(tag: &str) -> PathBuf {
let p = std::env::temp_dir().join(format!(
"grove-test-{tag}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&p).unwrap();
p
}
fn commit(g: &Git, dir: &Path, file: &str, body: &str) {
fs::write(dir.join(file), body).unwrap();
g.run(&["add", "."]).unwrap();
g.run(&["commit", "-qm", file]).unwrap();
}
/// A bare "remote" plus two clones (two machines) tracking its `main`.
fn setup() -> (Git, PathBuf, Git, PathBuf) {
let base = tmpdir("inspect");
let remote = base.join("remote.git");
Git::new(&base)
.run(&[
"init",
"-q",
"--bare",
"-b",
"main",
remote.to_str().unwrap(),
])
.unwrap();
let mut out = Vec::new();
for name in ["m1", "m2"] {
let dir = base.join(name);
Git::new(&base)
.run(&[
"clone",
"-q",
remote.to_str().unwrap(),
dir.to_str().unwrap(),
])
.unwrap();
let g = Git::new(&dir);
g.run(&["config", "user.email", "t@t"]).unwrap();
g.run(&["config", "user.name", "t"]).unwrap();
out.push((g, dir));
}
let (g2, d2) = out.pop().unwrap();
let (g1, d1) = out.pop().unwrap();
commit(&g1, &d1, "a.txt", "1\n");
g1.run(&["push", "-q", "-u", "origin", "main"]).unwrap();
g2.run(&["pull", "-q", "origin", "main"]).unwrap();
g2.run(&["branch", "-q", "--set-upstream-to=origin/main", "main"])
.unwrap();
(g1, d1, g2, d2)
}
#[test]
fn classifies_real_repos() {
let (g1, d1, g2, d2) = setup();
assert_eq!(state(&d1), State::Synced);
fs::write(d1.join("wip.txt"), "x").unwrap();
assert_eq!(state(&d1), State::Dirty);
fs::remove_file(d1.join("wip.txt")).unwrap();
commit(&g1, &d1, "b.txt", "2\n");
assert_eq!(state(&d1), State::Ahead);
g1.run(&["push", "-q"]).unwrap();
g2.run(&["fetch", "-q"]).unwrap();
assert_eq!(state(&d2), State::Behind);
fs::write(d2.join("wip2.txt"), "x").unwrap();
assert_eq!(state(&d2), State::BehindDirty);
fs::remove_file(d2.join("wip2.txt")).unwrap();
commit(&g2, &d2, "c.txt", "3\n");
assert_eq!(state(&d2), State::Diverged);
}
#[test]
fn lists_worktrees_skipping_bare() {
let base = tmpdir("wtlist");
let src = base.join("src");
fs::create_dir_all(&src).unwrap();
let g = Git::new(&src);
g.run(&["init", "-q", "-b", "main"]).unwrap();
g.run(&["config", "user.email", "t@t"]).unwrap();
g.run(&["config", "user.name", "t"]).unwrap();
commit(&g, &src, "a.txt", "1\n");
let bare = base.join("r/.bare");
fs::create_dir_all(bare.parent().unwrap()).unwrap();
Git::new(&base)
.run(&[
"clone",
"-q",
"--bare",
src.to_str().unwrap(),
bare.to_str().unwrap(),
])
.unwrap();
let bg = Git::new(&bare);
let wt = base.join("r/main");
bg.run(&["worktree", "add", "-q", wt.to_str().unwrap(), "main"])
.unwrap();
let list = worktrees(&bare).unwrap();
assert_eq!(list.len(), 1);
assert_eq!(list[0].branch.as_deref(), Some("main"));
}
+211
View File
@@ -0,0 +1,211 @@
mod common;
use common::*;
use grove_core::git::Git;
use grove_core::migrate::{MigrateError, Step, migrate, migrate_failing_after};
use std::fs;
fn names(dir: &std::path::Path) -> Vec<String> {
let mut v: Vec<String> = fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
.collect();
v.sort();
v
}
#[test]
fn migrates_preserving_all_local_state() {
let base = tmpdir("mig");
let repo = base.join("proj");
let g = messy_repo(&repo);
let status = g.run(&["status", "--porcelain", "--ignored"]).unwrap();
let stash = g.run(&["stash", "list", "--format=%H"]).unwrap();
let branches = g
.run(&["branch", "--format=%(refname) %(objectname)"])
.unwrap();
let r = migrate(&repo, "desktop", &base.join("backups"), "20261116").unwrap();
assert_eq!(names(&repo), vec![".bare", "main"]);
assert_eq!(r.worktree, repo.canonicalize().unwrap().join("main"));
let wt = Git::new(&r.worktree);
assert_eq!(
wt.run(&["status", "--porcelain", "--ignored"]).unwrap(),
status
);
assert_eq!(wt.run(&["stash", "list", "--format=%H"]).unwrap(), stash);
assert_eq!(
wt.run(&["branch", "--format=%(refname) %(objectname)"])
.unwrap(),
branches
);
assert!(
status.contains("A staged.txt"),
"staged state kept: {status}"
);
assert!(r.worktree.join("secret.env").exists(), "ignored file kept");
assert!(r.worktree.join("untracked.txt").exists());
assert_eq!(
fs::read_to_string(r.worktree.join("src/a.txt")).unwrap(),
"one\nmodified\n"
);
// Backup exists and the backup ref points at the old HEAD.
assert!(r.backup_tar.metadata().unwrap().len() > 0);
assert_eq!(
wt.run(&["rev-parse", &r.backup_ref]).unwrap(),
wt.run(&["rev-parse", "HEAD"]).unwrap()
);
// Repo is bare now; the worktree is still fully usable.
assert_eq!(
Git::new(&r.bare_dir)
.run(&["rev-parse", "--is-bare-repository"])
.unwrap(),
"true"
);
commit(&wt, "after.txt", "x\n");
}
#[test]
fn slash_branch_gets_flattened_directory() {
let base = tmpdir("slash");
let repo = base.join("p");
let g = init(&repo);
commit(&g, "a.txt", "1\n");
g.run(&["checkout", "-q", "-b", "feat/login"]).unwrap();
let r = migrate(&repo, "m", &base.join("bk"), "1").unwrap();
assert!(r.worktree.ends_with("feat-login"));
assert_eq!(names(&repo), vec![".bare", "feat-login"]);
}
#[test]
fn every_failure_point_rolls_back_to_the_original_layout() {
for step in [
Step::Staged,
Step::GitMoved,
Step::WorktreeAdded,
Step::Filled,
Step::IndexMoved,
Step::Verified,
] {
let base = tmpdir("rb");
let repo = base.join("proj");
let g = messy_repo(&repo);
let status = g.run(&["status", "--porcelain", "--ignored"]).unwrap();
let stash = g.run(&["stash", "list", "--format=%H"]).unwrap();
let before_names = names(&repo);
let err = migrate_failing_after(&repo, "m", &base.join("bk"), "1", step).unwrap_err();
let MigrateError::Failed {
rolled_back,
backup,
..
} = err
else {
panic!("{step:?}: expected Failed")
};
assert!(rolled_back, "{step:?}: rollback incomplete");
assert!(backup.exists());
assert_eq!(names(&repo), before_names, "{step:?}: layout restored");
let g = Git::new(&repo);
assert_eq!(
g.run(&["rev-parse", "--is-bare-repository"]).unwrap(),
"false",
"{step:?}"
);
assert_eq!(
g.run(&["status", "--porcelain", "--ignored"]).unwrap(),
status,
"{step:?}"
);
assert_eq!(
g.run(&["stash", "list", "--format=%H"]).unwrap(),
stash,
"{step:?}"
);
assert_eq!(
g.run(&["worktree", "list", "--porcelain"])
.unwrap()
.matches("worktree ")
.count(),
1
);
// And a retry succeeds afterwards.
migrate(&repo, "m", &base.join("bk"), "2").unwrap();
}
}
fn refused(r: Result<grove_core::migrate::Report, MigrateError>) -> String {
match r {
Err(MigrateError::Refused(m)) => m,
other => panic!("expected Refused, got {other:?}"),
}
}
#[test]
fn refuses_unsafe_situations_without_touching_anything() {
let base = tmpdir("ref");
let bk = base.join("bk");
let detached = base.join("detached");
let g = init(&detached);
commit(&g, "a", "1");
g.run(&["checkout", "-q", "--detach"]).unwrap();
assert!(refused(migrate(&detached, "m", &bk, "1")).contains("detached"));
let unborn = base.join("unborn");
init(&unborn);
assert!(refused(migrate(&unborn, "m", &bk, "1")).contains("no commits"));
let merging = base.join("merging");
let g = init(&merging);
commit(&g, "a", "1");
fs::write(merging.join(".git/MERGE_HEAD"), "x").unwrap();
assert!(refused(migrate(&merging, "m", &bk, "1")).contains("in progress"));
let subm = base.join("subm");
let g = init(&subm);
commit(&g, "a", "1");
fs::write(subm.join(".gitmodules"), "").unwrap();
assert!(refused(migrate(&subm, "m", &bk, "1")).contains("submodule"));
let multi = base.join("multi");
let g = init(&multi);
commit(&g, "a", "1");
g.run(&[
"worktree",
"add",
"-q",
"-b",
"x",
base.join("extra").to_str().unwrap(),
])
.unwrap();
assert!(refused(migrate(&multi, "m", &bk, "1")).contains("linked worktrees"));
let done = base.join("done");
let g = init(&done);
commit(&g, "a", "1");
migrate(&done, "m", &bk, "1").unwrap();
assert!(refused(migrate(&done, "m", &bk, "2")).contains("already migrated"));
let inside = base.join("inside");
let g = init(&inside);
commit(&g, "a", "1");
assert!(refused(migrate(&inside, "m", &inside.join("bk"), "1")).contains("outside"));
assert!(inside.join(".git").is_dir());
assert!(
!inside.join("bk").exists(),
"refusal must not create the backup dir"
);
let sparse = base.join("sparse");
let g = init(&sparse);
commit(&g, "a", "1");
g.run(&["config", "core.sparseCheckout", "true"]).unwrap();
assert!(refused(migrate(&sparse, "m", &bk, "1")).contains("sparse"));
// Subdirectory is not a repo root.
fs::create_dir(inside.join("sub")).unwrap();
assert!(refused(migrate(&inside.join("sub"), "m", &bk, "1")).contains("plain clone"));
}
+407
View File
@@ -0,0 +1,407 @@
mod common;
use common::*;
use grove_core::git::Git;
use grove_core::migrate::migrate;
use grove_core::receive::*;
use grove_core::snapshot::take;
use grove_core::state::State;
use std::fs;
use std::path::PathBuf;
struct World {
base: PathBuf,
remote: PathBuf,
g1: Git,
d1: PathBuf,
g2: Git,
d2: PathBuf,
}
/// Bare remote plus two plain clones (machines "m1" and "m2") sharing `main` with one commit.
fn world() -> World {
let base = tmpdir("recv");
let remote = base.join("remote.git");
Git::new(&base)
.run(&[
"init",
"-q",
"--bare",
"-b",
"main",
remote.to_str().unwrap(),
])
.unwrap();
let d1 = base.join("m1");
let g1 = init(&d1);
g1.run(&["remote", "add", "origin", remote.to_str().unwrap()])
.unwrap();
commit(&g1, "a.txt", "1\n");
commit(&g1, ".gitignore", "*.env\n");
g1.run(&["push", "-q", "-u", "origin", "main"]).unwrap();
let d2 = base.join("m2");
Git::new(&base)
.run(&[
"clone",
"-q",
remote.to_str().unwrap(),
d2.to_str().unwrap(),
])
.unwrap();
let g2 = Git::new(&d2);
g2.run(&["config", "user.email", "t@t"]).unwrap();
g2.run(&["config", "user.name", "t"]).unwrap();
World {
base,
remote,
g1,
d1,
g2,
d2,
}
}
#[test]
fn fetch_works_when_remote_has_no_snapshots() {
let w = world();
fetch(&w.d2, REMOTE).unwrap();
}
#[test]
fn clean_behind_worktree_fast_forwards_with_backup() {
let w = world();
commit(&w.g1, "b.txt", "2\n");
w.g1.run(&["push", "-q"]).unwrap();
fetch(&w.d2, REMOTE).unwrap();
let old = w.g2.run(&["rev-parse", "HEAD"]).unwrap();
let r = receive(&w.d2, "m2", "t1").unwrap();
assert!(matches!(r, Received::FastForwarded { .. }));
assert_eq!(
w.g2.run(&["rev-parse", "HEAD"]).unwrap(),
w.g1.run(&["rev-parse", "HEAD"]).unwrap()
);
assert!(w.d2.join("b.txt").exists());
assert_eq!(
w.g2.run(&["rev-parse", "refs/sync-backup/m2/t1/receive/main"])
.unwrap(),
old
);
}
#[test]
fn dirty_worktree_is_never_touched() {
let w = world();
commit(&w.g1, "b.txt", "2\n");
w.g1.run(&["push", "-q"]).unwrap();
fs::write(w.d2.join("a.txt"), "1\nlocal edit\n").unwrap();
fetch(&w.d2, REMOTE).unwrap();
let head = w.g2.run(&["rev-parse", "HEAD"]).unwrap();
assert_eq!(
receive(&w.d2, "m2", "t").unwrap(),
Received::Skipped(State::BehindDirty)
);
assert_eq!(w.g2.run(&["rev-parse", "HEAD"]).unwrap(), head);
assert_eq!(
fs::read_to_string(w.d2.join("a.txt")).unwrap(),
"1\nlocal edit\n"
);
assert!(!w.d2.join("b.txt").exists());
}
#[test]
fn divergence_keeps_both_sides_intact() {
let w = world();
commit(&w.g1, "b.txt", "remote\n");
w.g1.run(&["push", "-q"]).unwrap();
commit(&w.g2, "c.txt", "local\n");
fetch(&w.d2, REMOTE).unwrap();
let head = w.g2.run(&["rev-parse", "HEAD"]).unwrap();
assert_eq!(
receive(&w.d2, "m2", "t").unwrap(),
Received::Skipped(State::Diverged)
);
assert_eq!(w.g2.run(&["rev-parse", "HEAD"]).unwrap(), head);
assert!(w.d2.join("c.txt").exists() && !w.d2.join("b.txt").exists());
// The remote side is still reachable for resolution.
assert!(w.g2.run(&["rev-parse", "origin/main"]).is_ok());
}
#[test]
fn snapshot_roundtrip_between_machines() {
let w = world();
// m1 commits (pushed) and then has work in progress on top.
commit(&w.g1, "b.txt", "2\n");
w.g1.run(&["push", "-q"]).unwrap();
fs::write(w.d1.join("a.txt"), "1\nwip edit\n").unwrap();
fs::write(w.d1.join("new.txt"), "untracked\n").unwrap();
fs::write(w.d1.join("s.env"), "secret\n").unwrap();
fs::remove_file(w.d1.join("b.txt")).unwrap();
take(&w.d1, "m1", "t1").unwrap();
push_snapshot(&w.d1, "m1", REMOTE).unwrap();
// m2 is behind by one commit and clean.
fetch(&w.d2, REMOTE).unwrap();
let snaps = foreign_snapshots(&w.d2, "m2").unwrap();
assert_eq!(snaps.len(), 1);
assert_eq!(snaps[0].machine, "m1");
assert!(
foreign_snapshots(&w.d2, "m1").unwrap().is_empty(),
"own snapshots excluded"
);
assert_eq!(
apply_snapshot(&w.d2, &snaps[0], "m2", "t2").unwrap(),
Applied::Applied
);
assert_eq!(
fs::read_to_string(w.d2.join("a.txt")).unwrap(),
"1\nwip edit\n"
);
assert!(w.d2.join("new.txt").exists());
assert!(!w.d2.join("b.txt").exists(), "deletion applied");
assert!(!w.d2.join("s.env").exists(), "ignored file never travels");
// Arrives as plain working changes, nothing staged.
assert_eq!(
w.g2.run(&["diff", "--cached", "--name-only"]).unwrap(),
"",
"nothing staged"
);
// HEAD fast-forwarded to the snapshot's base.
assert_eq!(w.g2.run(&["rev-parse", "HEAD"]).unwrap(), snaps[0].based_on);
}
#[test]
fn snapshot_is_declined_when_target_has_local_work() {
let w = world();
fs::write(w.d1.join("x.txt"), "x\n").unwrap();
take(&w.d1, "m1", "t1").unwrap();
push_snapshot(&w.d1, "m1", REMOTE).unwrap();
fetch(&w.d2, REMOTE).unwrap();
let snap = foreign_snapshots(&w.d2, "m2").unwrap().remove(0);
fs::write(w.d2.join("mine.txt"), "m\n").unwrap();
assert!(matches!(
apply_snapshot(&w.d2, &snap, "m2", "t").unwrap(),
Applied::Declined(_)
));
assert!(!w.d2.join("x.txt").exists());
fs::remove_file(w.d2.join("mine.txt")).unwrap();
commit(&w.g2, "local.txt", "l\n");
assert!(matches!(
apply_snapshot(&w.d2, &snap, "m2", "t").unwrap(),
Applied::Declined(_)
));
}
#[test]
fn snapshot_does_not_clobber_untracked_files() {
let w = world();
fs::write(w.d1.join("clash.txt"), "from m1\n").unwrap();
take(&w.d1, "m1", "t1").unwrap();
push_snapshot(&w.d1, "m1", REMOTE).unwrap();
fetch(&w.d2, REMOTE).unwrap();
let snap = foreign_snapshots(&w.d2, "m2").unwrap().remove(0);
// Dirty (untracked) worktree is declined before anything is written.
fs::write(w.d2.join("clash.txt"), "from m2\n").unwrap();
assert!(matches!(
apply_snapshot(&w.d2, &snap, "m2", "t").unwrap(),
Applied::Declined(_)
));
assert_eq!(
fs::read_to_string(w.d2.join("clash.txt")).unwrap(),
"from m2\n"
);
}
#[test]
fn materializes_new_remote_branches_and_archives_gone_ones() {
let w = world();
// m2 becomes a bare+worktree repo.
let r = migrate(&w.d2, "m2", &w.base.join("bk"), "1").unwrap();
let root = w.d2.canonicalize().unwrap();
assert_eq!(materialize(&root, REMOTE).unwrap().len(), 0);
w.g1.run(&["checkout", "-q", "-b", "feat/x"]).unwrap();
commit(&w.g1, "x.txt", "x\n");
w.g1.run(&["push", "-q", "-u", "origin", "feat/x"]).unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
let created = materialize(&root, REMOTE).unwrap();
assert_eq!(created, vec![root.join("feat-x")]);
assert!(root.join("feat-x/x.txt").exists());
assert_eq!(state_of(&root.join("feat-x")), State::Synced);
assert!(materialize(&root, REMOTE).unwrap().is_empty(), "idempotent");
// Remote branch deleted: clean worktree is archived with a backup ref.
w.g1.run(&["push", "-q", "origin", "--delete", "feat/x"])
.unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
let out = archive_gone(&root, "m2", "t9").unwrap();
assert_eq!(out.len(), 1);
assert!(matches!(&out[0], Archived::Removed { branch, .. } if branch == "feat/x"));
assert!(!root.join("feat-x").exists());
let bg = Git::new(&r.bare_dir);
assert!(
bg.run(&["rev-parse", "refs/sync-backup/m2/t9/archive/feat/x"])
.is_ok()
);
assert!(
bg.probe(&["rev-parse", "-q", "--verify", "refs/heads/feat/x"])
.unwrap()
.is_none()
);
}
#[test]
fn gone_branch_with_local_changes_is_kept_and_flagged() {
let w = world();
let r = migrate(&w.d2, "m2", &w.base.join("bk"), "1").unwrap();
let root = w.d2.canonicalize().unwrap();
w.g1.run(&["checkout", "-q", "-b", "feat/y"]).unwrap();
commit(&w.g1, "y.txt", "y\n");
w.g1.run(&["push", "-q", "-u", "origin", "feat/y"]).unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
materialize(&root, REMOTE).unwrap();
fs::write(root.join("feat-y/wip.txt"), "w\n").unwrap();
w.g1.run(&["push", "-q", "origin", "--delete", "feat/y"])
.unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
assert_eq!(
archive_gone(&root, "m2", "t").unwrap(),
vec![Archived::KeptDirty {
branch: "feat/y".into()
}]
);
assert!(root.join("feat-y/wip.txt").exists());
let _ = &w.remote;
}
fn state_of(p: &std::path::Path) -> State {
grove_core::inspect::state(p)
}
// ---- regression tests for the review findings ----
#[test]
fn archive_keeps_clean_worktree_that_holds_ignored_files() {
let w = world();
let r = migrate(&w.d2, "m2", &w.base.join("bk"), "1").unwrap();
let root = w.d2.canonicalize().unwrap();
w.g1.run(&["checkout", "-q", "-b", "feat/e"]).unwrap();
commit(&w.g1, "e.txt", "e\n");
w.g1.run(&["push", "-q", "-u", "origin", "feat/e"]).unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
materialize(&root, REMOTE).unwrap();
fs::write(root.join("feat-e/local.env"), "SECRET=1\n").unwrap(); // ignored by *.env
assert_eq!(
state_of(&root.join("feat-e")),
State::Synced,
"ignored files are invisible to status"
);
w.g1.run(&["push", "-q", "origin", "--delete", "feat/e"])
.unwrap();
fetch(&r.bare_dir, REMOTE).unwrap();
assert_eq!(
archive_gone(&root, "m2", "t").unwrap(),
vec![Archived::KeptDirty {
branch: "feat/e".into()
}]
);
assert_eq!(
fs::read_to_string(root.join("feat-e/local.env")).unwrap(),
"SECRET=1\n"
);
}
#[test]
fn archive_does_nothing_when_remote_shows_no_branches() {
let w = world();
let r = migrate(&w.d2, "m2", &w.base.join("bk"), "1").unwrap();
let root = w.d2.canonicalize().unwrap();
let bg = Git::new(&r.bare_dir);
for line in bg
.run(&["for-each-ref", "--format=%(refname)", "refs/remotes"])
.unwrap()
.lines()
{
bg.run(&["update-ref", "-d", line]).unwrap();
}
assert!(archive_gone(&root, "m2", "t").unwrap().is_empty());
assert!(root.join("main").exists());
}
#[test]
fn fast_forward_declines_instead_of_overwriting_local_ignored_file() {
let w = world();
commit(&w.g1, "conf.local", "theirs\n");
w.g1.run(&["push", "-q"]).unwrap();
fs::write(w.d2.join(".git/info/exclude"), "conf.local\n").unwrap();
fs::write(w.d2.join("conf.local"), "mine\n").unwrap();
fetch(&w.d2, REMOTE).unwrap();
let head = w.g2.run(&["rev-parse", "HEAD"]).unwrap();
assert!(matches!(
receive(&w.d2, "m2", "t").unwrap(),
Received::Declined(_)
));
assert_eq!(w.g2.run(&["rev-parse", "HEAD"]).unwrap(), head);
assert_eq!(
fs::read_to_string(w.d2.join("conf.local")).unwrap(),
"mine\n"
);
}
#[test]
fn snapshot_apply_declines_instead_of_overwriting_local_ignored_file() {
let w = world();
fs::write(w.d1.join("conf.local"), "theirs\n").unwrap();
take(&w.d1, "m1", "t1").unwrap();
push_snapshot(&w.d1, "m1", REMOTE).unwrap();
fs::write(w.d2.join(".git/info/exclude"), "conf.local\n").unwrap();
fs::write(w.d2.join("conf.local"), "mine\n").unwrap();
fetch(&w.d2, REMOTE).unwrap();
let snap = foreign_snapshots(&w.d2, "m2").unwrap().remove(0);
assert!(matches!(
apply_snapshot(&w.d2, &snap, "m2", "t").unwrap(),
Applied::Declined(_)
));
assert_eq!(
fs::read_to_string(w.d2.join("conf.local")).unwrap(),
"mine\n"
);
}
#[test]
fn archive_and_receive_in_one_pass_do_not_collide_on_backup_refs() {
use grove_core::sync::{Options, sync_repo};
let w = world();
migrate(&w.d2, "m2", &w.base.join("bk"), "1").unwrap();
let root = w.d2.canonicalize().unwrap();
let o = |stamp: &str| Options {
machine: "m2".into(),
stamp: stamp.into(),
remote: "origin".into(),
offline: false,
materialize: true,
};
w.g1.run(&["checkout", "-q", "-b", "feat/z"]).unwrap();
commit(&w.g1, "z.txt", "z\n");
w.g1.run(&["push", "-q", "-u", "origin", "feat/z"]).unwrap();
sync_repo(&root, &o("s1")).unwrap();
assert!(root.join("feat-z").exists());
// Same pass: feat/z disappears upstream AND main moves.
w.g1.run(&["push", "-q", "origin", "--delete", "feat/z"])
.unwrap();
w.g1.run(&["checkout", "-q", "main"]).unwrap();
commit(&w.g1, "m.txt", "m\n");
w.g1.run(&["push", "-q", "origin", "main"]).unwrap();
let rep = sync_repo(&root, &o("s2")).unwrap();
assert_eq!(rep.archived.len(), 1);
for wt in &rep.worktrees {
assert!(wt.errors.is_empty(), "{:?}", wt.errors);
}
assert!(
root.join("main/m.txt").exists(),
"main fast-forwarded in the same pass"
);
}
+137
View File
@@ -0,0 +1,137 @@
use grove_core::git::Git;
use grove_core::snapshot::{Outcome, take};
use std::fs;
use std::path::{Path, PathBuf};
fn tmpdir(tag: &str) -> PathBuf {
let p = std::env::temp_dir().join(format!(
"grove-test-{tag}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&p).unwrap();
p
}
fn repo(dir: &Path) -> Git {
let g = Git::new(dir);
g.run(&["init", "-q", "-b", "main"]).unwrap();
g.run(&["config", "user.email", "t@t"]).unwrap();
g.run(&["config", "user.name", "t"]).unwrap();
fs::write(dir.join("a.txt"), "hi\n").unwrap();
fs::write(dir.join(".gitignore"), "*.env\n").unwrap();
g.run(&["add", "."]).unwrap();
g.run(&["commit", "-qm", "init"]).unwrap();
g
}
fn files(g: &Git, rev: &str) -> Vec<String> {
g.run(&["ls-tree", "-r", "--name-only", rev])
.unwrap()
.lines()
.map(String::from)
.collect()
}
#[test]
fn snapshot_captures_wip_without_touching_user_state() {
let dir = tmpdir("snap");
let g = repo(&dir);
fs::write(dir.join("a.txt"), "hi\nedit\n").unwrap();
fs::write(dir.join("new.txt"), "untracked\n").unwrap();
fs::write(dir.join("secret.env"), "x\n").unwrap();
g.run(&["add", "a.txt"]).unwrap(); // staged edit
let head_before = g.run(&["rev-parse", "HEAD"]).unwrap();
let status_before = g.run(&["status", "--porcelain"]).unwrap();
let out = take(&dir, "desktop", "t1").unwrap();
let Outcome::Created(s) = out else {
panic!("expected Created")
};
assert_eq!(s.refname, "refs/sync/desktop/main");
let f = files(&g, &s.commit);
assert!(f.contains(&"new.txt".to_string()), "untracked captured");
assert!(!f.contains(&"secret.env".to_string()), "ignored excluded");
assert_eq!(
g.run(&["show", &format!("{}:a.txt", s.commit)]).unwrap(),
"hi\nedit"
);
// User's branch, index and worktree are untouched.
assert_eq!(g.run(&["rev-parse", "HEAD"]).unwrap(), head_before);
assert_eq!(g.run(&["status", "--porcelain"]).unwrap(), status_before);
assert_eq!(
g.run(&["rev-parse", "refs/heads/main"]).unwrap(),
head_before
);
// First parent is HEAD.
assert_eq!(
g.run(&["rev-parse", &format!("{}^1", s.commit)]).unwrap(),
head_before
);
}
#[test]
fn unchanged_tree_is_a_noop_and_changes_chain_snapshots() {
let dir = tmpdir("chain");
let g = repo(&dir);
fs::write(dir.join("w.txt"), "1\n").unwrap();
let Outcome::Created(first) = take(&dir, "m", "t1").unwrap() else {
panic!()
};
assert_eq!(
take(&dir, "m", "t2").unwrap(),
Outcome::Unchanged(first.commit.clone())
);
fs::write(dir.join("w.txt"), "2\n").unwrap();
let Outcome::Created(second) = take(&dir, "m", "t3").unwrap() else {
panic!()
};
assert_eq!(
g.run(&["rev-parse", &format!("{}^2", second.commit)])
.unwrap(),
first.commit
);
}
#[test]
fn works_in_a_linked_worktree_of_a_bare_repo() {
let base = tmpdir("bare");
let src = base.join("src");
fs::create_dir_all(&src).unwrap();
repo(&src);
let bare = base.join("repo/.bare");
fs::create_dir_all(bare.parent().unwrap()).unwrap();
Git::new(&base)
.run(&[
"clone",
"-q",
"--bare",
src.to_str().unwrap(),
bare.to_str().unwrap(),
])
.unwrap();
let bg = Git::new(&bare);
let wt = base.join("repo/main");
bg.run(&["worktree", "add", "-q", wt.to_str().unwrap(), "main"])
.unwrap();
fs::write(wt.join("wip.txt"), "wip\n").unwrap();
let Outcome::Created(s) = take(&wt, "laptop", "t").unwrap() else {
panic!()
};
assert!(files(&bg, &s.commit).contains(&"wip.txt".to_string()));
}
#[test]
fn detached_head_is_an_error() {
let dir = tmpdir("detached");
let g = repo(&dir);
g.run(&["checkout", "-q", "--detach"]).unwrap();
assert!(take(&dir, "m", "t").is_err());
}
+116
View File
@@ -0,0 +1,116 @@
mod common;
use common::*;
use grove_core::git::Git;
use grove_core::receive::Applied;
use grove_core::state::State;
use grove_core::sync::{Options, sync_repo};
use std::fs;
use std::path::Path;
fn opts(machine: &str, stamp: &str) -> Options {
Options {
machine: machine.into(),
stamp: stamp.into(),
remote: "origin".into(),
offline: false,
materialize: true,
}
}
fn clone(base: &Path, remote: &Path, name: &str) -> (Git, std::path::PathBuf) {
let d = base.join(name);
Git::new(base)
.run(&["clone", "-q", remote.to_str().unwrap(), d.to_str().unwrap()])
.unwrap();
let g = Git::new(&d);
g.run(&["config", "user.email", "t@t"]).unwrap();
g.run(&["config", "user.name", "t"]).unwrap();
(g, d)
}
#[test]
fn two_machines_converge_without_manual_git() {
let base = tmpdir("sync");
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 (ga, da) = clone(&base, &remote, "A");
let (_gb, db) = clone(&base, &remote, "B");
// A works (WIP only, nothing committed) and syncs.
fs::write(da.join("a.txt"), "1\nwork\n").unwrap();
fs::write(da.join("scratch.txt"), "s\n").unwrap();
let ra = sync_repo(&da, &opts("A", "t1")).unwrap();
assert_eq!(ra.worktrees[0].snapshot, Some("created"));
assert!(
ra.worktrees[0].errors.is_empty(),
"{:?}",
ra.worktrees[0].errors
);
// B syncs and ends up with A's work in progress.
let rb = sync_repo(&db, &opts("B", "t2")).unwrap();
assert_eq!(
rb.worktrees[0].applied.as_ref().unwrap().1,
Applied::Applied
);
assert_eq!(fs::read_to_string(db.join("a.txt")).unwrap(), "1\nwork\n");
assert!(db.join("scratch.txt").exists());
// A commits and pushes by hand; B (now dirty from the applied WIP) must not be touched.
ga.run(&["add", "-A"]).unwrap();
ga.run(&["commit", "-qm", "work"]).unwrap();
ga.run(&["push", "-q"]).unwrap();
let rb2 = sync_repo(&db, &opts("B", "t3")).unwrap();
assert_eq!(rb2.worktrees[0].state, State::BehindDirty);
assert_eq!(fs::read_to_string(db.join("a.txt")).unwrap(), "1\nwork\n");
// Discarding B's applied copy makes it clean; the next sync fast-forwards it.
Git::new(&db).run(&["checkout", "-q", "--", "."]).unwrap();
fs::remove_file(db.join("scratch.txt")).unwrap();
let rb3 = sync_repo(&db, &opts("B", "t4")).unwrap();
assert_eq!(rb3.worktrees[0].state, State::Synced);
assert!(db.join("scratch.txt").exists());
}
#[test]
fn offline_pass_skips_network_and_never_fails() {
let base = tmpdir("offline");
let g = init(&base.join("p"));
commit(&g, "a.txt", "1\n");
fs::write(base.join("p/w.txt"), "w\n").unwrap();
let mut o = opts("A", "t");
o.offline = true;
let r = sync_repo(&base.join("p"), &o).unwrap();
assert_eq!(r.worktrees[0].snapshot, Some("created"));
assert!(r.worktrees[0].errors.is_empty());
assert!(r.fetch_error.is_none());
}
#[test]
fn unreachable_remote_is_reported_but_local_snapshot_still_taken() {
let base = tmpdir("noremote");
let g = init(&base.join("p"));
commit(&g, "a.txt", "1\n");
g.run(&["remote", "add", "origin", "/nonexistent/remote.git"])
.unwrap();
fs::write(base.join("p/w.txt"), "w\n").unwrap();
let r = sync_repo(&base.join("p"), &opts("A", "t")).unwrap();
assert!(r.fetch_error.is_some());
assert_eq!(r.worktrees[0].snapshot, Some("created"));
assert!(!r.worktrees[0].errors.is_empty(), "push failure surfaced");
assert_eq!(r.worktrees[0].state, State::Dirty);
}