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 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-11-17 17:26:20 +01:00
1 parent 933ad65cf0
commit 7ed5675a00
2 files changed
+63

No files matched your search

+17
View File
@@ -45,6 +45,7 @@ pub(crate) fn ignored_conflicts(g: &Git, target: &str) -> Result<Vec<String>, Gi
"-z", "-z",
])?; ])?;
let ignored: Vec<&str> = ignored.split('\0').filter(|p| !p.is_empty()).collect(); 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 Ok(added
.into_iter() .into_iter()
.filter(|a| { .filter(|a| {
@@ -52,10 +53,26 @@ pub(crate) fn ignored_conflicts(g: &Git, target: &str) -> Result<Vec<String>, Gi
.iter() .iter()
.any(|i| a == i || (i.ends_with('/') && a.starts_with(i))) .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) .map(String::from)
.collect()) .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<String, GitError> { fn current_branch(g: &Git) -> Result<String, GitError> {
g.probe(&["symbolic-ref", "--short", "-q", "HEAD"])? g.probe(&["symbolic-ref", "--short", "-q", "HEAD"])?
.ok_or_else(|| GitError::Other("detached HEAD".into())) .ok_or_else(|| GitError::Other("detached HEAD".into()))
+46
View File
@@ -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] #[test]
fn snapshot_apply_declines_instead_of_overwriting_local_ignored_file() { fn snapshot_apply_declines_instead_of_overwriting_local_ignored_file() {
let w = world(); let w = world();