From 3f1c54286b73f5a6293e76a02166a3ef2f6707ba Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 18:04:00 +0300 Subject: [PATCH 1/4] fix(repo): refuse a conflict stop that would overwrite an untracked file The stop writes plaintext and sidecars with plain writes outside materialize. A file already on disk at one of those paths that the working tree's tree does not track is the user's own, typically an ignored local copy, and abort then deleted it. Refuse before any write. Resolves ADR-0049 q21. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/replay.rs | 44 ++++++++++++++++++++++++ crates/repo/src/repo.rs | 28 +++++++++++++++ docs/adr/0049-merge-on-partial-clones.md | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index b92f4c8a..219874e1 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -2779,6 +2779,50 @@ mod tests { std::fs::remove_dir_all(&root).ok(); } + /// ADR-0049 q21: the conflict stop refuses, before any write, when the + /// plaintext it would write lands on an untracked file the user kept (the + /// "stop tracking, keep it ignored" move). Nothing is lost, and the pick + /// can be retried once the file is moved. + #[test] + fn a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy() { + let root = tmp_root("cp-prot-untracked"); + let repo = Repo::init(&root).unwrap(); + let (alice_sk, alice_pk) = scl_crypto::generate_keypair(); + std::fs::create_dir_all(root.join("secret")).unwrap(); + std::fs::write(root.join("secret/a.txt"), b"l1\nl2\nl3\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::remove_file(root.join("secret/a.txt")).unwrap(); + std::fs::write(root.join(".scignore"), b"secret/\n").unwrap(); + repo.commit("me", "main deletes and ignores").unwrap(); + repo.switch("work").unwrap(); + repo.protect("secret/", &[alice_pk], None).unwrap(); + std::fs::write(root.join("secret/a.txt"), b"EDITED\nl2\nl3\n").unwrap(); + repo.commit("me", "work edits").unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + // The user's own ignored copy of the file main stopped tracking. + std::fs::create_dir_all(root.join("secret")).unwrap(); + std::fs::write(root.join("secret/a.txt"), b"MY LOCAL COPY\n").unwrap(); + let before = repo.head_tip().unwrap(); + + let err = repo + .cherry_pick("work", "me", Some(&alice_sk), None) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("overwrite an untracked")), + "got {err:?}" + ); + assert!(!repo.pick_in_progress()); + assert_eq!(repo.head_tip().unwrap(), before); + assert_eq!( + std::fs::read(root.join("secret/a.txt")).unwrap(), + b"MY LOCAL COPY\n", + "the local copy is untouched" + ); + drop(repo); + std::fs::remove_dir_all(&root).ok(); + } + #[test] fn cherry_pick_protected_conflict_writes_plaintext_markers_worktree_only() { // Same-line edits of secret/a.txt on both sides: the pick conflicts. diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index b59e846e..68c4ee81 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1799,6 +1799,34 @@ impl Repo { } } } + // The stop writes plaintext and sidecars with plain writes, outside + // `materialize`. A file already on disk at one of those paths that + // `old_root` does not track is the user's own (an ignored or untracked + // copy): the write would replace it, and abort would then delete it. + // Refuse instead (ADR-0049 q21). A lookup that fails counts as tracked. + { + let partial = self.promisor()?.is_some(); + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let targets = to_encrypt + .iter() + .map(|(p, ..)| p.as_str()) + .chain(sidecars.iter().map(|(r, _)| r.as_str())); + for rel in targets { + let full = worktree::safe_join(&self.layout.root, rel)?; + if full.symlink_metadata().is_err() { + continue; + } + let tracked = worktree::tree_entry_at(&mut store, old_root, rel, partial) + .map_or(true, |e| e.is_some()); + if !tracked { + return Err(Error::InvalidArgument(format!( + "merge has conflicts, and writing {rel} would overwrite an untracked or \ + ignored file; move that file away, then retry" + ))); + } + } + } // A carried subtree can sit inside the view: fill its closure before // the first working-tree write (lazy partial clones only), outside // the store lock. diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index eee13650..b8d815b5 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -184,7 +184,7 @@ Replay is a three-way merge with a different base, and `replay_commit` is the on 19. **Completion can drop a newly tracked ignored file.** *Resolved:* completion reads the working tree with the decided root's paths counted as tracked, so an ignore rule cannot hide a file the operation itself added. Test `completing_a_conflicted_pick_keeps_an_added_file_the_parent_ignores`. 20. **A stopped pick or rebase without its decided root.** `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` are written before the state's in-progress signal, so the state cannot normally be missing. If it is (a lost file, a power loss before the data reached disk), a partial clone's completion refuses, because grafting from the tip would keep the old out-of-view content; `--abort` still works. -21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** The stop writes the decrypted text with a plain write, so a local copy the user kept at that path (the usual "stop tracking, keep it ignored" move) is replaced, and abort now removes the path. The loss happens at the stop and predates the record; refusing the stop when an untracked file sits at a plaintext path would close it. +21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** *Resolved:* `materialize_conflict_state` refuses, before any write, when a plaintext or sidecar path already exists on disk and the tree the working tree reflects does not track it. A clean merge, pick or rebase still overwrites an untracked file that a materialized added file lands on; that is `materialize`'s behaviour and is not changed here. Test `a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy`. ## Alternatives considered From 5664d1909d052f486dd771ce782177372a35242b Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 18:11:17 +0300 Subject: [PATCH 2/4] fix(repo): treat out-of-view paths as untracked on disk in the stop guard A full clone with a sparse view can track a path the view never materialized. A file there is the user's own, so the conflict-stop guard no longer counts it as tracked. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/replay.rs | 46 +++++++++++++++++++++++++++++++++++++++ crates/repo/src/repo.rs | 8 +++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 219874e1..af08661b 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -2823,6 +2823,52 @@ mod tests { std::fs::remove_dir_all(&root).ok(); } + /// A full clone with a sparse view: the tree tracks a protected path the + /// view never materialized, and the user keeps an ignored local copy there. + /// An in-view conflict together with a clean protected merge of that path + /// would write plaintext over it, so the stop refuses. + #[test] + fn a_conflict_stop_does_not_treat_an_out_of_view_path_as_materialized() { + let root = tmp_root("mg-sparse-untracked"); + let repo = Repo::init(&root).unwrap(); + let (alice_sk, alice_pk) = scl_crypto::generate_keypair(); + repo.protect("vault/", &[alice_pk], None).unwrap(); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("vault")).unwrap(); + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"a\nb\nc\nd\ne\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"A\nb\nc\nd\ne\n").unwrap(); + repo.commit("me", "main edits").unwrap(); + repo.switch_with_identity("work", Some(&alice_sk)).unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("vault/s.txt"), b"a\nb\nc\nd\nE\n").unwrap(); + repo.commit("me", "work edits").unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.set_sparse(&["src/".to_string()], Some(&alice_sk)) + .unwrap(); + assert!(!root.join("vault/s.txt").exists(), "out of the view"); + std::fs::create_dir_all(root.join("vault")).unwrap(); + std::fs::write(root.join("vault/s.txt"), b"MY LOCAL COPY\n").unwrap(); + + let err = repo + .merge_with_identity("work", "me", Some(&alice_sk)) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("overwrite an untracked")), + "got {err:?}" + ); + assert!(!repo.merge_in_progress()); + assert_eq!( + std::fs::read(root.join("vault/s.txt")).unwrap(), + b"MY LOCAL COPY\n" + ); + drop(repo); + std::fs::remove_dir_all(&root).ok(); + } + #[test] fn cherry_pick_protected_conflict_writes_plaintext_markers_worktree_only() { // Same-line edits of secret/a.txt on both sides: the pick conflicts. diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 68c4ee81..92f51fa5 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1817,8 +1817,12 @@ impl Repo { if full.symlink_metadata().is_err() { continue; } - let tracked = worktree::tree_entry_at(&mut store, old_root, rel, partial) - .map_or(true, |e| e.is_some()); + // A path the sparse view never materialized holds no tracked + // file on disk, even if the tree tracks it: whatever is there + // is the user's own. + let tracked = sparse.matches(rel) + && worktree::tree_entry_at(&mut store, old_root, rel, partial) + .map_or(true, |e| e.is_some()); if !tracked { return Err(Error::InvalidArgument(format!( "merge has conflicts, and writing {rel} would overwrite an untracked or \ From 860adda4b09c154237ccd1e684b93dcb65f009f5 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 18:13:41 +0300 Subject: [PATCH 3/4] fix(repo): harden the conflict-stop overwrite guard A failed lookup now counts as untracked, a file standing where a parent directory must be is refused before any write, a directory at the path is named, and the message hints at a leftover sidecar. ADR-0049 q21 scopes the guard to merge, pick and rebase; the private-branch merge stop has none. Adds sidecar and blocked-parent tests. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/replay.rs | 79 ++++++++++++++++++++++++ crates/repo/src/repo.rs | 26 ++++++-- docs/adr/0049-merge-on-partial-clones.md | 2 +- 3 files changed, 100 insertions(+), 7 deletions(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index af08661b..53438b0d 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -2869,6 +2869,85 @@ mod tests { std::fs::remove_dir_all(&root).ok(); } + /// A binary conflict writes `.theirs`. An untracked file already + /// there (a leftover from an earlier stop, or the user's own) is refused, + /// not overwritten. + #[test] + fn a_binary_conflict_stop_refuses_over_an_untracked_sidecar() { + let root = tmp_root("mg-sidecar-untracked"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("bin.dat"), b"\xff\xfebase\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("bin.dat"), b"\xff\xfeOURS\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("bin.dat"), b"\xff\xfeTHEIRS\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + std::fs::write(root.join("bin.dat.theirs"), b"leftover\n").unwrap(); + + let err = repo.merge("work", "me").unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("overwrite an untracked")), + "got {err:?}" + ); + assert!(!repo.merge_in_progress()); + assert_eq!( + std::fs::read(root.join("bin.dat.theirs")).unwrap(), + b"leftover\n" + ); + assert_eq!( + std::fs::read(root.join("bin.dat")).unwrap(), + b"\xff\xfeOURS\n" + ); + drop(repo); + std::fs::remove_dir_all(&root).ok(); + } + + /// A file where a parent directory of a plaintext path must be fails the + /// write halfway; the stop refuses before writing anything instead. + #[test] + fn a_conflict_stop_refuses_when_a_file_blocks_a_parent_directory() { + let root = tmp_root("cp-prot-blocked-parent"); + let repo = Repo::init(&root).unwrap(); + let (alice_sk, alice_pk) = scl_crypto::generate_keypair(); + std::fs::create_dir_all(root.join("secret")).unwrap(); + std::fs::write(root.join("secret/a.txt"), b"l1\nl2\nl3\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::remove_file(root.join("secret/a.txt")).unwrap(); + std::fs::write(root.join(".scignore"), b"secret\n").unwrap(); + repo.commit("me", "main deletes and ignores").unwrap(); + repo.switch("work").unwrap(); + repo.protect("secret/", &[alice_pk], None).unwrap(); + std::fs::write(root.join("secret/a.txt"), b"EDITED\nl2\nl3\n").unwrap(); + repo.commit("me", "work edits").unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + // The user keeps an ignored FILE named `secret`. + let _ = std::fs::remove_dir_all(root.join("secret")); + std::fs::write(root.join("secret"), b"not a directory\n").unwrap(); + + let err = repo + .cherry_pick("work", "me", Some(&alice_sk), None) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("stands where a directory")), + "got {err:?}" + ); + assert!(!repo.pick_in_progress()); + assert!( + !root.join(".sc/CONFLICT_PLAINTEXT").exists(), + "nothing written" + ); + assert_eq!( + std::fs::read(root.join("secret")).unwrap(), + b"not a directory\n" + ); + drop(repo); + std::fs::remove_dir_all(&root).ok(); + } + #[test] fn cherry_pick_protected_conflict_writes_plaintext_markers_worktree_only() { // Same-line edits of secret/a.txt on both sides: the pick conflicts. diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 92f51fa5..e9e9888f 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1800,10 +1800,12 @@ impl Repo { } } // The stop writes plaintext and sidecars with plain writes, outside - // `materialize`. A file already on disk at one of those paths that - // `old_root` does not track is the user's own (an ignored or untracked - // copy): the write would replace it, and abort would then delete it. - // Refuse instead (ADR-0049 q21). A lookup that fails counts as tracked. + // `materialize`. A file or directory already on disk at one of those + // paths that `old_root` does not track is the user's own (an ignored or + // untracked copy, or a leftover sidecar): the write would replace it, + // and abort would then delete it. A file standing where a parent + // directory must be would fail the write halfway. Refuse both before + // any write (ADR-0049 q21). A lookup that fails counts as untracked. { let partial = self.promisor()?.is_some(); let store_arc = self.vfs.store(); @@ -1814,6 +1816,17 @@ impl Repo { .chain(sidecars.iter().map(|(r, _)| r.as_str())); for rel in targets { let full = worktree::safe_join(&self.layout.root, rel)?; + let blocked_parent = full.ancestors().skip(1).any(|a| { + a.starts_with(&self.layout.root) + && a != self.layout.root + && a.symlink_metadata().is_ok_and(|m| !m.is_dir()) + }); + if blocked_parent { + return Err(Error::InvalidArgument(format!( + "merge has conflicts, and a file stands where a directory for {rel} must be; \ + move that file away, then retry" + ))); + } if full.symlink_metadata().is_err() { continue; } @@ -1822,11 +1835,12 @@ impl Repo { // is the user's own. let tracked = sparse.matches(rel) && worktree::tree_entry_at(&mut store, old_root, rel, partial) - .map_or(true, |e| e.is_some()); + .is_ok_and(|e| e.is_some()); if !tracked { return Err(Error::InvalidArgument(format!( "merge has conflicts, and writing {rel} would overwrite an untracked or \ - ignored file; move that file away, then retry" + ignored file or directory (possibly a sidecar left by an earlier stop); \ + move it away, then retry" ))); } } diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index b8d815b5..c07b08a5 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -184,7 +184,7 @@ Replay is a three-way merge with a different base, and `replay_commit` is the on 19. **Completion can drop a newly tracked ignored file.** *Resolved:* completion reads the working tree with the decided root's paths counted as tracked, so an ignore rule cannot hide a file the operation itself added. Test `completing_a_conflicted_pick_keeps_an_added_file_the_parent_ignores`. 20. **A stopped pick or rebase without its decided root.** `PICK_DECIDED_ROOT` and `REBASE_DECIDED_ROOT` are written before the state's in-progress signal, so the state cannot normally be missing. If it is (a lost file, a power loss before the data reached disk), a partial clone's completion refuses, because grafting from the tip would keep the old out-of-view content; `--abort` still works. -21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** *Resolved:* `materialize_conflict_state` refuses, before any write, when a plaintext or sidecar path already exists on disk and the tree the working tree reflects does not track it. A clean merge, pick or rebase still overwrites an untracked file that a materialized added file lands on; that is `materialize`'s behaviour and is not changed here. Test `a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy`. +21. **A conflict stop overwrites an ignored or untracked file at a plaintext path.** *Resolved for merge, cherry-pick and rebase:* `materialize_conflict_state` refuses, before any write, when a plaintext or sidecar path already exists on disk (as a file or directory, including a leftover sidecar) and the tree the working tree reflects does not track it or the sparse view never materialized it, or when a file stands where a parent directory must be. A lookup that fails counts as untracked. A clean merge, pick or rebase still overwrites an untracked file that a materialized added file lands on; that is `materialize`'s behaviour and is not changed here. The private-branch merge stop (`merge_into_private`) writes its files directly and has no such guard. Tests `a_conflict_stop_refuses_to_overwrite_an_untracked_local_copy`, `a_conflict_stop_does_not_treat_an_out_of_view_path_as_materialized`, `a_binary_conflict_stop_refuses_over_an_untracked_sidecar` and `a_conflict_stop_refuses_when_a_file_blocks_a_parent_directory`. ## Alternatives considered From e3da68bc9140635cf6887bd07b873fd2682da0a3 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Sun, 18 Oct 2026 18:21:33 +0300 Subject: [PATCH 4/4] fix(repo): let a tracked file become a directory and refuse a directory target The conflict-stop guard rejected a tracked file standing where the result puts a directory, though materialize removes it first. It also accepted a directory at a path the old tree tracks as a file. The decision is now a small function with unit tests for both transitions. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/repo.rs | 148 +++++++++++++++++++++++++++++++++------- 1 file changed, 123 insertions(+), 25 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index e9e9888f..f9169047 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1815,33 +1815,29 @@ impl Repo { .map(|(p, ..)| p.as_str()) .chain(sidecars.iter().map(|(r, _)| r.as_str())); for rel in targets { - let full = worktree::safe_join(&self.layout.root, rel)?; - let blocked_parent = full.ancestors().skip(1).any(|a| { - a.starts_with(&self.layout.root) - && a != self.layout.root - && a.symlink_metadata().is_ok_and(|m| !m.is_dir()) - }); - if blocked_parent { - return Err(Error::InvalidArgument(format!( - "merge has conflicts, and a file stands where a directory for {rel} must be; \ - move that file away, then retry" - ))); - } - if full.symlink_metadata().is_err() { - continue; - } // A path the sparse view never materialized holds no tracked // file on disk, even if the tree tracks it: whatever is there - // is the user's own. - let tracked = sparse.matches(rel) - && worktree::tree_entry_at(&mut store, old_root, rel, partial) - .is_ok_and(|e| e.is_some()); - if !tracked { - return Err(Error::InvalidArgument(format!( - "merge has conflicts, and writing {rel} would overwrite an untracked or \ - ignored file or directory (possibly a sidecar left by an earlier stop); \ - move it away, then retry" - ))); + // is the user's own. A failed lookup counts as untracked. + let mut tracked = |p: &str| { + sparse.matches(p) + && worktree::tree_entry_at(&mut store, old_root, p, partial) + .is_ok_and(|e| e.is_some()) + }; + match stop_write_blocker(&self.layout.root, rel, &mut tracked)? { + None => {} + Some(StopBlocker::ParentFile) => { + return Err(Error::InvalidArgument(format!( + "merge has conflicts, and a file stands where a directory for {rel} \ + must be; move that file away, then retry" + ))) + } + Some(StopBlocker::Occupied) => { + return Err(Error::InvalidArgument(format!( + "merge has conflicts, and writing {rel} would overwrite an untracked \ + or ignored file or directory (possibly a sidecar left by an earlier \ + stop); move it away, then retry" + ))) + } } } } @@ -2308,6 +2304,45 @@ impl Repo { } } +/// What stands in the way of a conflict stop's direct write to `rel`. +#[derive(Debug, PartialEq, Eq)] +enum StopBlocker { + /// A file the old tree does not track sits where a parent directory of + /// `rel` must be. + ParentFile, + /// `rel` itself exists on disk and is not a tracked file: an untracked or + /// ignored copy, a leftover sidecar, or a directory. + Occupied, +} + +/// Decide whether a conflict stop may write `rel` under `root` (ADR-0049 +/// q21). `tracked(path)` says whether the tree the working tree reflects +/// tracks `path` as a file that is on disk. A tracked file standing where a +/// parent directory must be is fine: the stop's materialize removes it before +/// the write, as a merge that replaces a file with a directory does. A +/// directory at `rel` is never fine, tracked or not: the write would fail. +fn stop_write_blocker( + root: &std::path::Path, + rel: &str, + tracked: &mut impl FnMut(&str) -> bool, +) -> Result> { + let comps: Vec<&str> = rel.split('/').collect(); + for i in 1..comps.len() { + let prefix = comps[..i].join("/"); + let full = worktree::safe_join(root, &prefix)?; + if full.symlink_metadata().is_ok_and(|m| !m.is_dir()) && !tracked(&prefix) { + return Ok(Some(StopBlocker::ParentFile)); + } + } + let full = worktree::safe_join(root, rel)?; + match full.symlink_metadata() { + Err(_) => Ok(None), + Ok(m) if m.is_dir() => Ok(Some(StopBlocker::Occupied)), + Ok(_) if tracked(rel) => Ok(None), + Ok(_) => Ok(Some(StopBlocker::Occupied)), + } +} + #[cfg(test)] mod tests { use super::*; @@ -6419,3 +6454,66 @@ mod tests { std::fs::remove_dir_all(&dst_root).unwrap(); } } + +#[cfg(test)] +mod stop_blocker_tests { + use super::*; + + fn tmp(tag: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("scl-stop-blocker-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + #[test] + fn a_tracked_file_replaced_by_a_directory_does_not_block() { + let root = tmp("file-to-dir"); + std::fs::write(root.join("a"), b"tracked\n").unwrap(); + let tracked = |p: &str| p == "a"; + assert_eq!( + stop_write_blocker(&root, "a/b", &mut { tracked }).unwrap(), + None, + "the old tree tracks `a`; materialize removes it before the write" + ); + let untracked = |_: &str| false; + assert_eq!( + stop_write_blocker(&root, "a/b", &mut { untracked }).unwrap(), + Some(StopBlocker::ParentFile) + ); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_directory_at_the_target_blocks_even_when_tracked() { + let root = tmp("dir-at-target"); + std::fs::create_dir_all(root.join("x/y")).unwrap(); + let tracked = |_: &str| true; + assert_eq!( + stop_write_blocker(&root, "x/y", &mut { tracked }).unwrap(), + Some(StopBlocker::Occupied) + ); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_tracked_file_target_is_fine_and_an_untracked_one_is_not() { + let root = tmp("file-target"); + std::fs::write(root.join("f"), b"x").unwrap(); + let tracked = |p: &str| p == "f"; + assert_eq!( + stop_write_blocker(&root, "f", &mut { tracked }).unwrap(), + None + ); + let untracked = |_: &str| false; + assert_eq!( + stop_write_blocker(&root, "f", &mut { untracked }).unwrap(), + Some(StopBlocker::Occupied) + ); + assert_eq!( + stop_write_blocker(&root, "missing", &mut { untracked }).unwrap(), + None + ); + std::fs::remove_dir_all(&root).unwrap(); + } +}