From 7ed5675a00692094326c3efb619c9ac431014a41 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Tue, 17 Nov 2026 17:26:20 +0100 Subject: [PATCH] fix(core): only existing files count as ignored conflicts A whole ignored directory (e.g. .claude/) made every incoming file under it a 'would overwrite ignored local files' conflict, even when the file did not exist locally, so such branches never fast-forwarded. Co-Authored-By: Claude Opus 5.5 --- crates/grove-core/src/receive.rs | 17 +++++++++++ crates/grove-core/tests/receive.rs | 46 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/crates/grove-core/src/receive.rs b/crates/grove-core/src/receive.rs index 9155046..2546321 100644 --- a/crates/grove-core/src/receive.rs +++ b/crates/grove-core/src/receive.rs @@ -45,6 +45,7 @@ pub(crate) fn ignored_conflicts(g: &Git, target: &str) -> Result, Gi "-z", ])?; let ignored: Vec<&str> = ignored.split('\0').filter(|p| !p.is_empty()).collect(); + let top = std::path::PathBuf::from(g.run(&["rev-parse", "--show-toplevel"])?); Ok(added .into_iter() .filter(|a| { @@ -52,10 +53,26 @@ pub(crate) fn ignored_conflicts(g: &Git, target: &str) -> Result, Gi .iter() .any(|i| a == i || (i.ends_with('/') && a.starts_with(i))) }) + // An ignored directory (`.claude/`) only covers the path: there is something to + // overwrite only if the file, or a file where one of its parent directories goes, exists. + .filter(|a| occupied(&top, a)) .map(String::from) .collect()) } +/// True when writing `rel` under `top` would replace something on disk: the path itself +/// exists, or one of its parents exists as something other than a directory. +fn occupied(top: &Path, rel: &str) -> bool { + let path = top.join(rel); + if path.symlink_metadata().is_ok() { + return true; + } + path.ancestors() + .skip(1) + .take_while(|p| p.starts_with(top) && *p != top) + .any(|p| p.symlink_metadata().is_ok_and(|m| !m.is_dir())) +} + fn current_branch(g: &Git) -> Result { g.probe(&["symbolic-ref", "--short", "-q", "HEAD"])? .ok_or_else(|| GitError::Other("detached HEAD".into())) diff --git a/crates/grove-core/tests/receive.rs b/crates/grove-core/tests/receive.rs index e208b5c..2ed70ea 100644 --- a/crates/grove-core/tests/receive.rs +++ b/crates/grove-core/tests/receive.rs @@ -353,6 +353,52 @@ fn fast_forward_declines_instead_of_overwriting_local_ignored_file() { ); } +#[test] +fn files_new_in_an_ignored_directory_do_not_block_a_fast_forward() { + let w = world(); + commit(&w.g1, "tool/settings.json", "{}\n"); + w.g1.run(&["push", "-q"]).unwrap(); + // The whole directory is ignored and exists locally, but not the incoming file. + fs::write(w.d2.join(".git/info/exclude"), "tool/\n").unwrap(); + fs::create_dir_all(w.d2.join("tool/cache")).unwrap(); + fs::write(w.d2.join("tool/cache/x"), "local\n").unwrap(); + fetch(&w.d2, REMOTE).unwrap(); + assert!( + matches!( + receive(&w.d2, "m2", "t").unwrap(), + Received::FastForwarded { .. } + ), + "nothing local would be overwritten" + ); + assert_eq!( + fs::read_to_string(w.d2.join("tool/settings.json")).unwrap(), + "{}\n" + ); + assert_eq!( + fs::read_to_string(w.d2.join("tool/cache/x")).unwrap(), + "local\n" + ); +} + +#[test] +fn an_existing_file_in_an_ignored_directory_still_blocks_a_fast_forward() { + let w = world(); + commit(&w.g1, "tool/settings.json", "theirs\n"); + w.g1.run(&["push", "-q"]).unwrap(); + fs::write(w.d2.join(".git/info/exclude"), "tool/\n").unwrap(); + fs::create_dir_all(w.d2.join("tool")).unwrap(); + fs::write(w.d2.join("tool/settings.json"), "mine\n").unwrap(); + fetch(&w.d2, REMOTE).unwrap(); + assert!(matches!( + receive(&w.d2, "m2", "t").unwrap(), + Received::Declined(_) + )); + assert_eq!( + fs::read_to_string(w.d2.join("tool/settings.json")).unwrap(), + "mine\n" + ); +} + #[test] fn snapshot_apply_declines_instead_of_overwriting_local_ignored_file() { let w = world();