From 022c5325c9ac295580262ec6504093c33cf5ee26 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 17:08:52 +0100 Subject: [PATCH] fix(sync): never archive or rediscover worktrees outside the layout Co-Authored-By: Claude Sonnet 5.5 --- crates/grove-core/src/receive.rs | 6 +++ crates/grove-core/tests/sync.rs | 84 +++++++++++++++++++++++++++++ crates/grove-daemon/src/discover.rs | 26 ++++++++- 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/crates/grove-core/src/receive.rs b/crates/grove-core/src/receive.rs index 1182f46..fada306 100644 --- a/crates/grove-core/src/receive.rs +++ b/crates/grove-core/src/receive.rs @@ -347,11 +347,17 @@ pub fn archive_gone(root: &Path, machine: &str, stamp: &str) -> Result/`) are ours to remove; a linked worktree + // elsewhere (e.g. a sibling directory) is the user's. + if wt.path.parent() != canonical_root.as_deref() { + continue; + } // Ignored files (.env, build output...) count as local work too. if !Git::new(&wt.path) .run(&["status", "--porcelain", "--ignored"])? diff --git a/crates/grove-core/tests/sync.rs b/crates/grove-core/tests/sync.rs index 132d73b..e949dbb 100644 --- a/crates/grove-core/tests/sync.rs +++ b/crates/grove-core/tests/sync.rs @@ -168,3 +168,87 @@ 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 migrated_repo_with_linked_worktrees_syncs_only_what_it_can() { + use grove_core::migrate::migrate; + let base = tmpdir("synclinked"); + 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(); + seed.run(&["push", "-q", "origin", "main:feat/out"]) + .unwrap(); + let (ga, da) = clone(&base, &remote, "A"); + let nested = da.join(".claude/worktrees/wt1"); + ga.run(&[ + "worktree", + "add", + "-q", + "--detach", + nested.to_str().unwrap(), + ]) + .unwrap(); + let outside = base.join("A-feature"); + ga.run(&[ + "worktree", + "add", + "-q", + "--track", + "-b", + "feat/out", + outside.to_str().unwrap(), + "origin/feat/out", + ]) + .unwrap(); + migrate(&da, "A", &base.join("bk"), "1").unwrap(); + let nested = da.join("main/.claude/worktrees/wt1"); + fs::write(nested.join("a.txt"), "detached edit\n").unwrap(); + fs::write(outside.join("a.txt"), "outside edit\n").unwrap(); + + let rep = sync_repo(&da, &opts("A", "t1")).unwrap(); + let find = |p: &Path| { + rep.worktrees + .iter() + .find(|w| w.path == p.canonicalize().unwrap()) + .unwrap_or_else(|| panic!("{} not reported: {:?}", p.display(), rep.worktrees)) + }; + // Detached: reported, never snapshotted. + let d = find(&nested); + assert_eq!((d.branch.clone(), d.snapshot), (None, None)); + assert!(d.errors.is_empty(), "{:?}", d.errors); + assert_eq!(d.state, State::Dirty); + // Branch worktree elsewhere: synced under its own branch ref. + let o = find(&outside); + assert_eq!(o.snapshot, Some("created")); + assert!(o.errors.is_empty(), "{:?}", o.errors); + let bare = Git::new(da.join(".bare")); + assert!(bare.run(&["rev-parse", "refs/sync/A/feat/out"]).is_ok()); + assert!( + bare.probe(&["rev-parse", "-q", "--verify", "refs/sync/A/HEAD"]) + .unwrap() + .is_none() + ); + + // Gone upstream + clean: a worktree outside the layout is never archived or removed. + Git::new(&outside) + .run(&["checkout", "-q", "--", "."]) + .unwrap(); + seed.run(&["push", "-q", "origin", "--delete", "feat/out"]) + .unwrap(); + let rep = sync_repo(&da, &opts("A", "t2")).unwrap(); + assert!(rep.archived.is_empty(), "{:?}", rep.archived); + assert!(outside.join("a.txt").exists()); +} diff --git a/crates/grove-daemon/src/discover.rs b/crates/grove-daemon/src/discover.rs index 33a4cfc..737474b 100644 --- a/crates/grove-daemon/src/discover.rs +++ b/crates/grove-daemon/src/discover.rs @@ -3,6 +3,16 @@ use std::path::{Path, PathBuf}; +/// A linked worktree (`.git` file pointing into `/.git/worktrees/`) belongs to the repo +/// it was added from; treating it as a repo of its own would sync the same worktrees twice. +fn is_linked_worktree(p: &Path) -> bool { + std::fs::read_to_string(p.join(".git")).is_ok_and(|s| { + s.trim() + .strip_prefix("gitdir:") + .is_some_and(|g| g.trim().contains("/worktrees/")) + }) +} + fn is_repo(p: &Path) -> bool { p.join(".bare").is_dir() || p.join(".git").exists() } @@ -23,10 +33,17 @@ fn subdirs(p: &Path) -> Vec { pub fn discover(root: &Path) -> Vec { let mut out = Vec::new(); for child in subdirs(root) { + if is_linked_worktree(&child) { + continue; + } if is_repo(&child) { out.push(child); } else { - out.extend(subdirs(&child).into_iter().filter(|g| is_repo(g))); + out.extend( + subdirs(&child) + .into_iter() + .filter(|g| is_repo(g) && !is_linked_worktree(g)), + ); } } out @@ -46,9 +63,16 @@ mod tests { "grp/plain", ".hidden/.git", "c/inner/.git", + "grp/b/main", ] { std::fs::create_dir_all(base.join(d)).unwrap(); } + // Linked worktrees (siblings of their repo) are not repos of their own. + let link = "gitdir: /x/grp/b/.bare/worktrees/b-feature\n"; + std::fs::create_dir_all(base.join("grp/b-feature")).unwrap(); + std::fs::write(base.join("grp/b-feature/.git"), link).unwrap(); + std::fs::create_dir_all(base.join("top-wt/sub/.git")).unwrap(); + std::fs::write(base.join("top-wt/.git"), link).unwrap(); let found = discover(&base); assert_eq!( found,