mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 07:16:11 +00:00
fix(sync): never archive or rediscover worktrees outside the layout
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d1c0ddb5dc
commit
022c5325c9
3 files changed
+115
-1
No files matched your search
@@ -347,11 +347,17 @@ pub fn archive_gone(root: &Path, machine: &str, stamp: &str) -> Result<Vec<Archi
|
||||
.map(|(b, _)| b)
|
||||
.collect();
|
||||
let wts = crate::inspect::worktrees(&bare)?;
|
||||
let canonical_root = root.canonicalize().ok();
|
||||
let mut out = Vec::new();
|
||||
for branch in gone {
|
||||
let Some(wt) = wts.iter().find(|w| w.branch.as_deref() == Some(branch)) else {
|
||||
continue;
|
||||
};
|
||||
// Only worktrees of the layout (`<root>/<dir>`) 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"])?
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
@@ -3,6 +3,16 @@
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// A linked worktree (`.git` file pointing into `<repo>/.git/worktrees/<id>`) 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<PathBuf> {
|
||||
pub fn discover(root: &Path) -> Vec<PathBuf> {
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user