From 828b44e3b28e90922716dc13072054920ed3e015 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:04:36 +0300 Subject: [PATCH 1/6] fix(repo): refuse to lay a merged, switched or replayed file onto an untracked one merge (fast-forward, clean and conflict stop), switch, cherry-pick and rebase silently overwrote an untracked or ignored file where an added file landed. They now refuse before any write, as Git does. Merging into an unborn branch, undo and sparse set are not guarded. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 3 + crates/repo/src/replay.rs | 158 +++++++++++++++++++++++ crates/repo/src/repo.rs | 82 ++++++++++++ docs/adr/0049-merge-on-partial-clones.md | 2 +- 4 files changed, 244 insertions(+), 1 deletion(-) diff --git a/CLAUDE.md b/CLAUDE.md index 25f07108..8ab323ab 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -245,6 +245,9 @@ transport-adjacent. The rest, imperatively: sidecars it wrote are listed apart in `.sc/CONFLICT_SIDECARS`; abort, `rebase --continue` and `sc resolve` remove only those, never a `.theirs` file the stop did not write; the completing commit leaves them out of the tree and removes them. +- **A merge, switch, cherry-pick or rebase refuses, before any write, to lay a file or + directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it + away and retry. Merging into an unborn branch, `undo` and `sparse set` are not guarded. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish or abort the in-progress op first. diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 6827ce3e..fc5539ca 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -507,6 +507,7 @@ impl Repo { // the view: fill its closure before any ref, spec or file // write (lazy partial clones only), outside the store lock. self.ensure_view(root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite("cherry-pick", Some(ours_root), root)?; let mut cache = self.open_protected_cache()?; { let store_arc = self.vfs().store(); @@ -768,6 +769,7 @@ impl Repo { // the fill takes the store lock, so release ours around it. drop(store); self.ensure_view(target_root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite("rebase", Some(ours_root), target_root)?; let mut store = store_arc.lock().unwrap(); let mut cache = self.open_protected_cache()?; worktree::materialize( @@ -1060,6 +1062,7 @@ impl Repo { // view: fill its closure before any ref, spec or file write (lazy // partial clones only), outside the store lock. self.ensure_view(acc_root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite("rebase", Some(disk_root), acc_root)?; // Cache hoisted above the block so its save follows the ref move // below, matching commit/amend's discipline (P33 Task 7). let mut cache = self.open_protected_cache()?; @@ -3292,6 +3295,161 @@ mod tests { remove_root(&root); } + /// `main` adds `new.txt`; `work` does not. The user has an untracked + /// `new.txt`. Returns the repo on `work`, with `main` the other branch. + fn branches_where_main_adds_a_file_the_user_already_has( + name: &str, + ) -> (std::path::PathBuf, Repo) { + let root = tmp_root(name); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), b"a\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("new.txt"), b"from other\n").unwrap(); + repo.commit("me", "other adds new.txt").unwrap(); + repo.switch("main").unwrap(); + std::fs::write(root.join("a.txt"), b"a2\n").unwrap(); + repo.commit("me", "main edits a").unwrap(); + std::fs::write(root.join("new.txt"), b"MINE\n").unwrap(); + (root, repo) + } + + fn assert_mine(root: &std::path::Path) { + assert_eq!( + std::fs::read(root.join("new.txt")).unwrap(), + b"MINE\n", + "the user's untracked file must survive" + ); + } + + #[test] + fn a_clean_merge_refuses_to_overwrite_an_untracked_file() { + let (root, repo) = branches_where_main_adds_a_file_the_user_already_has("guard-merge"); + let before = repo.head_tip().unwrap(); + let err = repo.merge("other", "me").unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("would overwrite")), + "got {err:?}" + ); + assert_eq!(repo.head_tip().unwrap(), before, "no merge commit"); + assert_mine(&root); + drop(repo); + remove_root(&root); + } + + #[test] + fn a_fast_forward_merge_and_a_switch_refuse_to_overwrite_an_untracked_file() { + let root = tmp_root("guard-ff"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), b"a\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("new.txt"), b"from other\n").unwrap(); + repo.commit("me", "other adds new.txt").unwrap(); + repo.switch("main").unwrap(); + std::fs::write(root.join("new.txt"), b"MINE\n").unwrap(); + + assert!(matches!( + repo.merge("other", "me").unwrap_err(), + Error::InvalidArgument(m) if m.contains("would overwrite") + )); + assert!(matches!( + repo.switch("other").unwrap_err(), + Error::InvalidArgument(m) if m.contains("would overwrite") + )); + assert_mine(&root); + assert!(!repo.merge_in_progress()); + drop(repo); + remove_root(&root); + } + + #[test] + fn a_cherry_pick_and_a_rebase_refuse_to_overwrite_an_untracked_file() { + let (root, repo) = branches_where_main_adds_a_file_the_user_already_has("guard-replay"); + let before = repo.head_tip().unwrap(); + assert!(matches!( + repo.cherry_pick("other", "me", None, None).unwrap_err(), + Error::InvalidArgument(m) if m.contains("would overwrite") + )); + assert_eq!(repo.head_tip().unwrap(), before); + assert!(!repo.pick_in_progress()); + assert_mine(&root); + drop(repo); + remove_root(&root); + + // Rebase `main` onto `other`, which tracks the file the user has + // untracked on `main`: the fold's final write would replace it. + let (root, repo) = branches_where_main_adds_a_file_the_user_already_has("guard-rebase"); + let before = repo.head_tip().unwrap(); + assert!(matches!( + repo.rebase("other", "me", None).unwrap_err(), + Error::InvalidArgument(m) if m.contains("would overwrite") + )); + assert_eq!(repo.head_tip().unwrap(), before); + assert!(!repo.rebase_in_progress()); + assert_mine(&root); + drop(repo); + remove_root(&root); + } + + /// The guard must not mistake what a conflict stop itself wrote for the + /// user's files: a stopped rebase whose target adds a file lays that file + /// down, and `--continue` must still finish. + #[test] + fn rebase_continue_is_not_blocked_by_files_the_stop_wrote() { + let root = tmp_root("guard-rebase-continue"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("x.txt"), b"a\nb\nc\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); + std::fs::write(root.join("added.txt"), b"from main\n").unwrap(); + repo.commit("me", "main edits x, adds a file").unwrap(); + repo.switch("feature").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nFEATURE\nc\n").unwrap(); + repo.commit("me", "feature edits x").unwrap(); + assert!(matches!( + repo.rebase("main", "me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + assert!(root.join("added.txt").exists(), "setup: the stop wrote it"); + std::fs::write(root.join("x.txt"), b"a\nresolved\nc\n").unwrap(); + repo.rebase_continue("me", None).unwrap(); + assert_eq!( + std::fs::read(root.join("added.txt")).unwrap(), + b"from main\n" + ); + drop(repo); + remove_root(&root); + } + + /// Same for a conflicted merge and pick: completing must not trip over the + /// clean-added files the stop wrote. + #[test] + fn a_stopped_merge_still_completes_with_a_file_the_stop_wrote() { + let root = tmp_root("guard-merge-continue"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("x.txt"), b"a\nb\nc\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); + repo.commit("me", "main edits x").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); + std::fs::write(root.join("added.txt"), b"from other\n").unwrap(); + repo.commit("me", "other edits x, adds a file").unwrap(); + repo.switch("main").unwrap(); + repo.merge("other", "me").unwrap_err(); + assert!(root.join("added.txt").exists()); + std::fs::write(root.join("x.txt"), b"a\nresolved\nc\n").unwrap(); + repo.commit("me", "merge").unwrap(); + assert!(repo.tracked_paths().unwrap().contains("added.txt")); + drop(repo); + remove_root(&root); + } + #[test] fn merge_abort_keeps_a_user_theirs_file_beside_a_text_conflict() { let (root, repo) = text_conflict_with_user_theirs_file("sweep-merge-abort"); diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 37b452cc..49fa08bc 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1408,6 +1408,7 @@ impl Repo { // the fill takes the store lock, so release ours around it. drop(store); self.ensure_view(theirs_root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite("merge", Some(ours_root), theirs_root)?; let mut store = store_arc.lock().unwrap(); let mut cache = self.open_protected_cache()?; let skipped = worktree::materialize( @@ -1662,6 +1663,7 @@ impl Repo { // view: fill its closure now (lazy partial clones only), before any ref, // spec or working-tree write and outside the store lock. self.ensure_view(merged_root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite("merge", Some(ours_root), merged_root)?; // Materialize the merged tree into the working dir. Protection-aware: // a merged PROTECTED entry decrypts for `identity` when possible, else @@ -1864,6 +1866,7 @@ impl Repo { // the first working-tree write (lazy partial clones only), outside // the store lock. self.ensure_view(conflict_root, &sparse)?; + self.refuse_untracked_overwrite("merge", Some(old_root), conflict_root)?; { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -1950,6 +1953,84 @@ impl Repo { Ok(()) } + /// [`Repo::refuse_untracked_overwrite_vs`] against the tree `old_root` + /// reflects on disk. + pub(crate) fn refuse_untracked_overwrite( + &self, + op: &str, + old_root: Option, + target_root: ObjectId, + ) -> Result<()> { + let sparse = self.sparse_spec()?; + let partial = self.promisor()?.is_some(); + let old: std::collections::BTreeSet = match old_root { + None => std::collections::BTreeSet::new(), + Some(old) => { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + if partial { + worktree::tree_file_entries_with_perms_sparse_present(&mut store, old, &sparse)? + } else { + worktree::tree_file_entries_with_perms_sparse(&mut store, old, &sparse)? + } + .into_keys() + .collect() + } + }; + self.refuse_untracked_overwrite_vs(op, &old, target_root) + } + + /// Refuse, before any write, a forward operation (`op`) whose result would + /// lay a file or directory on top of one the user owns: a path the target + /// tree has and `tracked` (the paths the working tree reflects) does not + /// contain, yet something already sits there (an untracked or ignored + /// copy), or an untracked file where a parent directory must be. The check + /// reads the working tree only; a path outside the sparse view is skipped + /// because nothing is written there, and on a partial clone a gap in the + /// target is left for `materialize`'s own view check to report. Git + /// refuses the same case. + pub(crate) fn refuse_untracked_overwrite_vs( + &self, + op: &str, + tracked: &std::collections::BTreeSet, + target_root: ObjectId, + ) -> Result<()> { + let sparse = self.sparse_spec()?; + let partial = self.promisor()?.is_some(); + let target = { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + if partial { + worktree::tree_file_entries_with_perms_sparse_present( + &mut store, + target_root, + &sparse, + )? + } else { + worktree::tree_file_entries_with_perms_sparse(&mut store, target_root, &sparse)? + } + }; + let mut is_tracked = |p: &str| tracked.contains(p); + for rel in target.keys().filter(|p| sparse.matches(p)) { + match stop_write_blocker(&self.layout.root, rel, &mut is_tracked)? { + None => {} + Some(StopBlocker::ParentFile) => { + return Err(Error::InvalidArgument(format!( + "{op} would fail: a file stands where a directory for {rel} must be; \ + move that file away, then retry" + ))) + } + Some(StopBlocker::Occupied) => { + return Err(Error::InvalidArgument(format!( + "{op} would overwrite an untracked or ignored file or directory at \ + {rel}; move it away, then retry" + ))) + } + } + } + Ok(()) + } + /// The sidecars the in-progress stop recorded writing. A record with no /// merge, pick or rebase in progress is residue of a crash mid-stop, and /// names nothing that is ours: it reads as empty. @@ -2245,6 +2326,7 @@ impl Repo { // A lazy, online partial clone fills the target view's closure now, // before the first stale file is removed or written (ADR-0048). self.ensure_view(target_root, &self.sparse_spec()?)?; + self.refuse_untracked_overwrite_vs("switch", &old_paths, target_root)?; if head_priv.is_some() { // Remove private-tip paths the public target doesn't track, // BEFORE materialize lays the target down. diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index c07b08a5..563f1099 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 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`. +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. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. Not guarded: merging into an unborn branch (there is no tree to call a file untracked against), `undo`, and `sparse set`. 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 906496a1bcfc919ecb398962284be5a65fd78abf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:08:43 +0300 Subject: [PATCH 2/6] fix(repo): treat out-of-view tracked paths as untracked and refuse a symlink target From an independent Sol review: a tracked path outside the sparse view was never laid out, so it must not excuse a user file in the way; and a symlink at a path the result writes is never the tracked file, since the write would follow it out of the tree. Private-branch switch/merge and case-insensitive filesystems are documented as not covered. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 2 +- crates/repo/src/replay.rs | 37 ++++++++++++++++++++++++ crates/repo/src/repo.rs | 26 ++++++++++++++++- docs/adr/0049-merge-on-partial-clones.md | 2 +- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 8ab323ab..f45ac994 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -247,7 +247,7 @@ transport-adjacent. The rest, imperatively: did not write; the completing commit leaves them out of the tree and removes them. - **A merge, switch, cherry-pick or rebase refuses, before any write, to lay a file or directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it - away and retry. Merging into an unborn branch, `undo` and `sparse set` are not guarded. + away and retry. Merging into an unborn branch, `undo`, `sparse set` and private-branch switch/merge are not guarded. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish or abort the in-progress op first. diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index fc5539ca..368d632c 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -3450,6 +3450,43 @@ mod tests { remove_root(&root); } + /// A tracked path outside the sparse view was never laid out, so a user + /// file standing where a directory for an in-view path must be is not the + /// tracked file: the guard must not count the old tree's `a` as it. + #[test] + fn a_tracked_path_outside_the_view_does_not_excuse_a_user_file_in_the_way() { + let root = tmp_root("guard-sparse"); + let repo = Repo::init(&root).unwrap(); + std::fs::create_dir_all(root.join("a")).unwrap(); + std::fs::write(root.join("a/b"), b"b\n").unwrap(); + std::fs::write(root.join("keep.txt"), b"k\n").unwrap(); + let target = repo.commit("me", "target tree has a/b").unwrap(); + let target_root = repo.snapshot(&target).unwrap().root; + // The user's own file where the directory `a` must be. + std::fs::remove_dir_all(root.join("a")).unwrap(); + std::fs::write(root.join("a"), b"MINE\n").unwrap(); + // The tree being left tracks a regular file `a`. + let old: std::collections::BTreeSet = + ["a".to_string(), "keep.txt".to_string()].into(); + + // Full view: the old tree's `a` is on disk, so it is the tracked file. + repo.refuse_untracked_overwrite_vs("switch", &old, target_root) + .unwrap(); + // Narrowed view: `a` was never laid out, so the file in the way is the + // user's and the guard refuses. + std::fs::write(repo.layout().dot_sc.join("sparse"), "a/b\n").unwrap(); + let err = repo + .refuse_untracked_overwrite_vs("switch", &old, target_root) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("would fail")), + "got {err:?}" + ); + assert_eq!(std::fs::read(root.join("a")).unwrap(), b"MINE\n"); + drop(repo); + remove_root(&root); + } + #[test] fn merge_abort_keeps_a_user_theirs_file_beside_a_text_conflict() { let (root, repo) = text_conflict_with_user_theirs_file("sweep-merge-abort"); diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 49fa08bc..d5910619 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2010,7 +2010,9 @@ impl Repo { worktree::tree_file_entries_with_perms_sparse(&mut store, target_root, &sparse)? } }; - let mut is_tracked = |p: &str| tracked.contains(p); + // A path outside the view was never laid out: whatever is on disk + // there is the user's, even if the tree tracks it. + let mut is_tracked = |p: &str| sparse.matches(p) && tracked.contains(p); for rel in target.keys().filter(|p| sparse.matches(p)) { match stop_write_blocker(&self.layout.root, rel, &mut is_tracked)? { None => {} @@ -2512,6 +2514,9 @@ fn stop_write_blocker( match full.symlink_metadata() { Err(_) => Ok(None), Ok(m) if m.is_dir() => Ok(Some(StopBlocker::Occupied)), + // A symlink is never the tracked file: a write would follow it to + // whatever it points at, outside the repository. + Ok(m) if m.file_type().is_symlink() => Ok(Some(StopBlocker::Occupied)), Ok(_) if tracked(rel) => Ok(None), Ok(_) => Ok(Some(StopBlocker::Occupied)), } @@ -6670,6 +6675,25 @@ mod stop_blocker_tests { std::fs::remove_dir_all(&root).unwrap(); } + /// A symlink is never the tracked file: writing would follow it out of the + /// repository (a protected path skipped for want of an identity leaves + /// room for one). + #[cfg(unix)] + #[test] + fn a_symlink_target_is_occupied_even_when_tracked() { + let root = tmp("stop-symlink"); + let outside = tmp("stop-symlink-outside").join("personal.txt"); + std::fs::write(&outside, b"personal\n").unwrap(); + std::os::unix::fs::symlink(&outside, root.join("p.txt")).unwrap(); + let tracked = |_: &str| true; + assert_eq!( + stop_write_blocker(&root, "p.txt", &mut { tracked }).unwrap(), + Some(StopBlocker::Occupied) + ); + std::fs::remove_dir_all(&root).ok(); + std::fs::remove_dir_all(outside.parent().unwrap()).ok(); + } + #[test] fn a_tracked_file_target_is_fine_and_an_untracked_one_is_not() { let root = tmp("file-target"); diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 563f1099..d1a3fc99 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 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. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. Not guarded: merging into an unborn branch (there is no tree to call a file untracked against), `undo`, and `sparse set`. 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`. +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. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. A symlink at a written path always counts as occupied (a write would follow it out of the tree), and a tracked path outside the sparse view does not excuse a file in the way. Not guarded: merging into an unborn branch (there is no tree to call a file untracked against), `undo`, `sparse set`, and the private-branch switch and merge, which materialize through their own path. Known limit: tracking is compared by exact path, so a case-only rename can be refused on a case-insensitive filesystem. 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 8cca2833957b5cc8add95ce23ecd2f29a9c8876e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:14:41 +0300 Subject: [PATCH 3/6] fix(repo): guard private-branch materialization, recognise case aliases, fix the switch doc Codex review: switching to or merging into a private branch now refuses to overwrite an untracked file (guard inside materialize_private, against the decrypted target paths); a case-only rename on a case-insensitive filesystem counts as tracked when the on-disk alias is provably the same file; the Repo::switch doc says what blocks a switch now. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 2 +- crates/repo/src/private.rs | 37 ++++++++++ crates/repo/src/repo.rs | 87 ++++++++++++++++++++++-- docs/adr/0049-merge-on-partial-clones.md | 2 +- 4 files changed, 121 insertions(+), 7 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index f45ac994..8ab323ab 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -247,7 +247,7 @@ transport-adjacent. The rest, imperatively: did not write; the completing commit leaves them out of the tree and removes them. - **A merge, switch, cherry-pick or rebase refuses, before any write, to lay a file or directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it - away and retry. Merging into an unborn branch, `undo`, `sparse set` and private-branch switch/merge are not guarded. + away and retry. Merging into an unborn branch, `undo` and `sparse set` are not guarded. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish or abort the in-progress op first. diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 215f1c4e..6483b52f 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -370,6 +370,7 @@ impl Repo { /// doesn't track (or that falls outside `sparse`) is removed. fn materialize_private( &self, + op: &str, store: &mut Store, ctx: &PrivateCtx, target_root: ObjectId, @@ -379,6 +380,9 @@ impl Repo { ) -> Result> { let sparse = self.sparse_spec()?; let target = self.inner_tree_files(store, ctx, target_root)?; + // Before the first removal or write: nothing of the user's may be + // replaced (see `refuse_untracked_paths`). + self.refuse_untracked_paths(op, old_paths, target.keys())?; for p in old_paths { if !target.contains_key(p) || !sparse.matches(p) { let full = worktree::safe_join(&self.layout.root, p)?; @@ -448,6 +452,7 @@ impl Repo { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); self.materialize_private( + "switch", &mut store, &ctx, tip_snap.root, @@ -1235,6 +1240,7 @@ impl Repo { index, }; let skipped = self.materialize_private( + "merge", &mut store, &ff_ctx, theirs_snap.root, @@ -1408,6 +1414,7 @@ impl Repo { .get_inner_snapshot(&mut store, &merged_ctx, &new_tip)? .protection; let skipped = self.materialize_private( + "merge", &mut store, &merged_ctx, merged_root, @@ -1746,6 +1753,36 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + /// Switching to a private branch that tracks `fix.txt` must not replace an + /// untracked `fix.txt` on the public checkout. + #[test] + fn switching_to_a_private_branch_refuses_to_overwrite_an_untracked_file() { + let (root, repo, (alice_sk, _alice_pk), _bob) = setup("guard-private"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "PRIVATE\n"); + repo.commit_private("alice", "adds fix.txt", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + assert!(!root.join("fix.txt").exists()); + write(&root, "fix.txt", "MINE\n"); + + let err = repo + .switch_with_identity("fix", Some(&alice_sk)) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("would overwrite")), + "got {err:?}" + ); + assert_eq!( + std::fs::read_to_string(root.join("fix.txt")).unwrap(), + "MINE\n" + ); + assert!(repo.head_private().unwrap().is_none(), "HEAD did not move"); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn non_recipient_is_refused_everywhere() { let (root, repo, (alice_sk, _), (bob_sk, _bob_pk)) = setup("noaccess"); diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index d5910619..cd0adaab 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2010,10 +2010,29 @@ impl Repo { worktree::tree_file_entries_with_perms_sparse(&mut store, target_root, &sparse)? } }; + self.refuse_untracked_paths(op, tracked, target.keys()) + } + + /// The path-list core of [`Repo::refuse_untracked_overwrite_vs`], for a + /// caller that already holds the target's paths (a private branch's + /// decrypted tree). + pub(crate) fn refuse_untracked_paths<'a>( + &self, + op: &str, + tracked: &std::collections::BTreeSet, + target: impl Iterator, + ) -> Result<()> { + let sparse = self.sparse_spec()?; // A path outside the view was never laid out: whatever is on disk - // there is the user's, even if the tree tracks it. - let mut is_tracked = |p: &str| sparse.matches(p) && tracked.contains(p); - for rel in target.keys().filter(|p| sparse.matches(p)) { + // there is the user's, even if the tree tracks it. On a + // case-insensitive filesystem a tracked file may answer to another + // spelling (a case-only rename): it counts as tracked only when it is + // provably the same file. + let root = &self.layout.root; + let mut is_tracked = |p: &str| { + sparse.matches(p) && (tracked.contains(p) || is_tracked_case_alias(root, tracked, p)) + }; + for rel in target.filter(|p| sparse.matches(p)) { match stop_write_blocker(&self.layout.root, rel, &mut is_tracked)? { None => {} Some(StopBlocker::ParentFile) => { @@ -2249,8 +2268,10 @@ impl Repo { /// Switch HEAD to `name` and materialize its tip into the working tree. /// /// Refuses to switch when the working tree has uncommitted modifications or - /// deletions, because `materialize` would silently overwrite them. (New, - /// untracked files are left in place and so don't block the switch.) The + /// deletions, because `materialize` would silently overwrite them. New, + /// untracked files are left in place and don't block the switch, unless the + /// target branch tracks a file at the same path (or needs a directory where + /// one stands): that fails with `InvalidArgument` before any write. The /// dirty check is protection-aware (see `status`/`diff_worktree`): a /// decrypted protected file that matches HEAD is clean and a skipped/absent /// protected file is clean, but a genuine edit to a protected file blocks @@ -2480,6 +2501,39 @@ impl Repo { } } +/// Whether `rel` is, on this filesystem, the same file as a tracked path that +/// differs from it only in case (a case-insensitive checkout where `foo` was +/// renamed to `Foo`). Same device and inode, or it is not an alias: a separate +/// user file that happens to share the spelling must stay protected. +#[cfg(unix)] +fn is_tracked_case_alias( + root: &std::path::Path, + tracked: &std::collections::BTreeSet, + rel: &str, +) -> bool { + use std::os::unix::fs::MetadataExt; + let Ok(here) = root.join(rel).symlink_metadata() else { + return false; + }; + tracked + .iter() + .filter(|t| t.as_str() != rel && t.eq_ignore_ascii_case(rel)) + .any(|t| { + root.join(t) + .symlink_metadata() + .is_ok_and(|m| m.dev() == here.dev() && m.ino() == here.ino()) + }) +} + +#[cfg(not(unix))] +fn is_tracked_case_alias( + _root: &std::path::Path, + _tracked: &std::collections::BTreeSet, + _rel: &str, +) -> bool { + false +} + /// What stands in the way of a conflict stop's direct write to `rel`. #[derive(Debug, PartialEq, Eq)] enum StopBlocker { @@ -6694,6 +6748,29 @@ mod stop_blocker_tests { std::fs::remove_dir_all(outside.parent().unwrap()).ok(); } + /// On a case-insensitive checkout `foo` renamed to `Foo` is one file under + /// two spellings; only the same device and inode makes it an alias. A + /// separate user file called `Foo` stays protected. + #[cfg(unix)] + #[test] + fn a_case_alias_of_a_tracked_file_is_tracked_only_when_it_is_the_same_file() { + let root = tmp("case-alias"); + std::fs::write(root.join("foo"), b"tracked\n").unwrap(); + let tracked: std::collections::BTreeSet = ["foo".to_string()].into(); + + // A separate file `Foo`: different inode, not an alias. + std::fs::write(root.join("Foo"), b"user's own\n").unwrap(); + assert!(!is_tracked_case_alias(&root, &tracked, "Foo")); + + // The same file under a second spelling (what a case-insensitive + // filesystem presents): an alias. + std::fs::remove_file(root.join("Foo")).unwrap(); + std::fs::hard_link(root.join("foo"), root.join("Foo")).unwrap(); + assert!(is_tracked_case_alias(&root, &tracked, "Foo")); + assert!(!is_tracked_case_alias(&root, &tracked, "Other")); + std::fs::remove_dir_all(&root).ok(); + } + #[test] fn a_tracked_file_target_is_fine_and_an_untracked_one_is_not() { let root = tmp("file-target"); diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index d1a3fc99..a8fc93a3 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 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. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. A symlink at a written path always counts as occupied (a write would follow it out of the tree), and a tracked path outside the sparse view does not excuse a file in the way. Not guarded: merging into an unborn branch (there is no tree to call a file untracked against), `undo`, `sparse set`, and the private-branch switch and merge, which materialize through their own path. Known limit: tracking is compared by exact path, so a case-only rename can be refused on a case-insensitive filesystem. 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`. +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. *Also resolved for the clean paths:* `refuse_untracked_overwrite` runs before the first write of a merge (fast-forward, clean and conflict stop), `switch`, cherry-pick and rebase (fast-forward and the fold's final write) and refuses when a file the result adds, or a directory it needs, lands on something the tree the working tree reflects does not track. It reads the working tree only, skips paths outside the sparse view, and leaves a partial-clone gap to `materialize`'s own view check. A symlink at a written path always counts as occupied (a write would follow it out of the tree), and a tracked path outside the sparse view does not excuse a file in the way. Not guarded: merging into an unborn branch (there is no tree to call a file untracked against), `undo` and `sparse set`. The private-branch switch and merge are guarded inside `materialize_private` against the decrypted target paths. Tracking is compared by exact path, plus a case alias of a tracked path that is provably the same file (same device and inode, unix only), so a case-only rename on a case-insensitive filesystem is not refused; a non-unix build still compares exact paths. 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 a34c8d9799ec14f39c091939596262c6c059a7a2 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:18:51 +0300 Subject: [PATCH 4/6] test(repo): make the case-alias test filesystem-aware On a case-insensitive filesystem (default macOS) writing Foo after foo names the same file, which is the alias the code is meant to recognise. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/repo.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index cd0adaab..05273eee 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -6749,24 +6749,29 @@ mod stop_blocker_tests { } /// On a case-insensitive checkout `foo` renamed to `Foo` is one file under - /// two spellings; only the same device and inode makes it an alias. A - /// separate user file called `Foo` stays protected. + /// two spellings; only the same device and inode makes it an alias. On a + /// case-sensitive one a separate user file called `Foo` stays protected. #[cfg(unix)] #[test] fn a_case_alias_of_a_tracked_file_is_tracked_only_when_it_is_the_same_file() { let root = tmp("case-alias"); std::fs::write(root.join("foo"), b"tracked\n").unwrap(); let tracked: std::collections::BTreeSet = ["foo".to_string()].into(); + // The filesystem under test decides whether `Foo` is a second file. + let case_insensitive = root.join("Foo").exists(); - // A separate file `Foo`: different inode, not an alias. - std::fs::write(root.join("Foo"), b"user's own\n").unwrap(); - assert!(!is_tracked_case_alias(&root, &tracked, "Foo")); - - // The same file under a second spelling (what a case-insensitive - // filesystem presents): an alias. - std::fs::remove_file(root.join("Foo")).unwrap(); - std::fs::hard_link(root.join("foo"), root.join("Foo")).unwrap(); - assert!(is_tracked_case_alias(&root, &tracked, "Foo")); + if case_insensitive { + // `Foo` already names the tracked file: an alias. + assert!(is_tracked_case_alias(&root, &tracked, "Foo")); + } else { + // A separate file `Foo`: different inode, not an alias. + std::fs::write(root.join("Foo"), b"user's own\n").unwrap(); + assert!(!is_tracked_case_alias(&root, &tracked, "Foo")); + // The same file under a second spelling: an alias. + std::fs::remove_file(root.join("Foo")).unwrap(); + std::fs::hard_link(root.join("foo"), root.join("Foo")).unwrap(); + assert!(is_tracked_case_alias(&root, &tracked, "Foo")); + } assert!(!is_tracked_case_alias(&root, &tracked, "Other")); std::fs::remove_dir_all(&root).ok(); } From 67d9c687b41c61382ed58414cac79af9569d0cbc Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:27:34 +0300 Subject: [PATCH 5/6] fix(repo): gate case aliases on a case-insensitive filesystem; a symlink parent always blocks Codex review: a hard link to a tracked file on a case-sensitive filesystem is not a case alias (probe .SC to know the filesystem answers to both spellings), and a symlink standing where a parent directory must be blocks even if the old tree tracks that name. CLAUDE.md names the private conflict stop as unguarded. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 2 +- crates/repo/src/repo.rs | 53 ++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 8ab323ab..31044300 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -247,7 +247,7 @@ transport-adjacent. The rest, imperatively: did not write; the completing commit leaves them out of the tree and removes them. - **A merge, switch, cherry-pick or rebase refuses, before any write, to lay a file or directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it - away and retry. Merging into an unborn branch, `undo` and `sparse set` are not guarded. + away and retry. Merging into an unborn branch, `undo`, `sparse set` and the conflict stop of a merge into a private branch are not guarded. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish or abort the in-progress op first. diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 05273eee..231b0393 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2503,8 +2503,9 @@ impl Repo { /// Whether `rel` is, on this filesystem, the same file as a tracked path that /// differs from it only in case (a case-insensitive checkout where `foo` was -/// renamed to `Foo`). Same device and inode, or it is not an alias: a separate -/// user file that happens to share the spelling must stay protected. +/// renamed to `Foo`). The filesystem must be case-insensitive and the two +/// spellings the same device and inode, or it is not an alias: a separate user +/// file, or a hard link to the tracked one, must stay protected. #[cfg(unix)] fn is_tracked_case_alias( root: &std::path::Path, @@ -2512,6 +2513,13 @@ fn is_tracked_case_alias( rel: &str, ) -> bool { use std::os::unix::fs::MetadataExt; + // Inode identity alone proves nothing: on a case-sensitive filesystem an + // untracked `Foo` can be a hard link to the tracked `foo`. Only a + // filesystem that answers to both spellings of a name that certainly + // exists (`.sc`) aliases case. + if root.join(".SC").symlink_metadata().is_err() { + return false; + } let Ok(here) = root.join(rel).symlink_metadata() else { return false; }; @@ -2560,8 +2568,13 @@ fn stop_write_blocker( 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)); + // A symlink is never the tracked file (a write would follow it, or the + // materialize removal would delete the user's link), so it blocks + // whether or not the old tree tracks that name. + if let Ok(m) = full.symlink_metadata() { + if !m.is_dir() && (m.file_type().is_symlink() || !tracked(&prefix)) { + return Ok(Some(StopBlocker::ParentFile)); + } } } let full = worktree::safe_join(root, rel)?; @@ -6749,33 +6762,51 @@ mod stop_blocker_tests { } /// On a case-insensitive checkout `foo` renamed to `Foo` is one file under - /// two spellings; only the same device and inode makes it an alias. On a - /// case-sensitive one a separate user file called `Foo` stays protected. + /// two spellings. On a case-sensitive one nothing aliases: a separate user + /// file called `Foo`, or even a hard link to the tracked `foo`, stays + /// protected. #[cfg(unix)] #[test] - fn a_case_alias_of_a_tracked_file_is_tracked_only_when_it_is_the_same_file() { + fn a_case_alias_of_a_tracked_file_needs_a_case_insensitive_filesystem() { let root = tmp("case-alias"); + std::fs::create_dir_all(root.join(".sc")).unwrap(); std::fs::write(root.join("foo"), b"tracked\n").unwrap(); let tracked: std::collections::BTreeSet = ["foo".to_string()].into(); - // The filesystem under test decides whether `Foo` is a second file. let case_insensitive = root.join("Foo").exists(); if case_insensitive { // `Foo` already names the tracked file: an alias. assert!(is_tracked_case_alias(&root, &tracked, "Foo")); } else { - // A separate file `Foo`: different inode, not an alias. std::fs::write(root.join("Foo"), b"user's own\n").unwrap(); assert!(!is_tracked_case_alias(&root, &tracked, "Foo")); - // The same file under a second spelling: an alias. + // A hard link shares the inode but is still the user's file here. std::fs::remove_file(root.join("Foo")).unwrap(); std::fs::hard_link(root.join("foo"), root.join("Foo")).unwrap(); - assert!(is_tracked_case_alias(&root, &tracked, "Foo")); + assert!(!is_tracked_case_alias(&root, &tracked, "Foo")); } assert!(!is_tracked_case_alias(&root, &tracked, "Other")); std::fs::remove_dir_all(&root).ok(); } + /// A symlink standing where a parent directory must be blocks even when + /// the old tree tracks that name (a protected file skipped for want of an + /// identity leaves room for one). + #[cfg(unix)] + #[test] + fn a_symlink_parent_blocks_even_when_the_name_is_tracked() { + let root = tmp("symlink-parent"); + let outside = tmp("symlink-parent-outside"); + std::os::unix::fs::symlink(&outside, root.join("a")).unwrap(); + let tracked = |_: &str| true; + assert_eq!( + stop_write_blocker(&root, "a/b", &mut { tracked }).unwrap(), + Some(StopBlocker::ParentFile) + ); + std::fs::remove_dir_all(&root).ok(); + std::fs::remove_dir_all(&outside).ok(); + } + #[test] fn a_tracked_file_target_is_fine_and_an_untracked_one_is_not() { let root = tmp("file-target"); From da94f391d1b30f15bfb71aece3edd1b2811da4ca Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 10:29:37 +0300 Subject: [PATCH 6/6] fix(repo): name the running operation in conflict-stop refusals materialize_conflict_state serves merge, cherry-pick and rebase but its refusals all said 'merge'. It now takes the operation name. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/replay.rs | 36 ++++++++++++++++++++++++++++++++++++ crates/repo/src/repo.rs | 14 ++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 368d632c..fadfaf71 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -580,6 +580,7 @@ impl Repo { wrapped, }; let conflict_root = self.materialize_conflict_state( + "cherry-pick", &carried, &carried_subtrees, &to_encrypt, @@ -1013,6 +1014,7 @@ impl Repo { wrapped, }; let conflict_root = self.materialize_conflict_state( + "rebase", &carried, &carried_subtrees, &to_encrypt, @@ -3397,6 +3399,40 @@ mod tests { /// The guard must not mistake what a conflict stop itself wrote for the /// user's files: a stopped rebase whose target adds a file lays that file /// down, and `--continue` must still finish. + /// A conflict stop names the operation that is running, and refuses before + /// any write when a clean-added file would land on an untracked one. + #[test] + fn a_conflict_stop_names_its_operation_when_it_refuses() { + let root = tmp_root("guard-stop-names-op"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("x.txt"), b"a\nb\nc\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); + repo.commit("me", "main edits x").unwrap(); + repo.switch("other").unwrap(); + std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); + std::fs::write(root.join("new.txt"), b"from other\n").unwrap(); + repo.commit("me", "other edits x, adds new.txt").unwrap(); + repo.switch("main").unwrap(); + std::fs::write(root.join("new.txt"), b"MINE\n").unwrap(); + + let err = repo.cherry_pick("other", "me", None, None).unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.starts_with("cherry-pick")), + "got {err:?}" + ); + let err = repo.merge("other", "me").unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.starts_with("merge")), + "got {err:?}" + ); + assert_mine(&root); + assert!(!repo.pick_in_progress() && !repo.merge_in_progress()); + drop(repo); + remove_root(&root); + } + #[test] fn rebase_continue_is_not_blocked_by_files_the_stop_wrote() { let root = tmp_root("guard-rebase-continue"); diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 231b0393..a4a3ed9d 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1580,6 +1580,7 @@ impl Repo { None => &[], }; let conflict_root = self.materialize_conflict_state( + "merge", &carried, carried_subtrees, &to_encrypt, @@ -1746,6 +1747,7 @@ impl Repo { /// materialize skip-write path (P24 Task 3), no markers involved. pub(crate) fn materialize_conflict_state( &self, + op: &str, carried: &[(String, Vec, scl_core::FileMode, u8)], carried_subtrees: &[(String, scl_core::TreeEntry)], to_encrypt: &[(String, Vec, scl_core::FileMode, Vec<[u8; 32]>)], @@ -1777,7 +1779,7 @@ impl Repo { if self.promisor()?.is_some() { if let Some((path, ..)) = to_encrypt.iter().find(|(p, ..)| !sparse.matches(p)) { return Err(Error::InvalidArgument(format!( - "merge has conflicts, and {path} (outside your sparse checkout) needs sealing at completion; \ + "{op} has conflicts, and {path} (outside your sparse checkout) needs sealing at completion; \ run `sc sparse set` to include it, or `sc backfill --all`, then retry" ))); } @@ -1788,7 +1790,7 @@ impl Repo { if self.promisor()?.is_some() { if let Some((rel, _)) = sidecars.iter().find(|(r, _)| !sparse.matches(r)) { return Err(Error::InvalidArgument(format!( - "merge has conflicts, and its sidecar {rel} would fall outside your sparse checkout; \ + "{op} has conflicts, and its sidecar {rel} would fall outside your sparse checkout; \ run `sc sparse set` to include it, or `sc backfill --all`, then retry" ))); } @@ -1814,7 +1816,7 @@ impl Repo { }); if tracked { return Err(Error::InvalidArgument(format!( - "merge has conflicts, and its sidecar {rel} would overwrite a tracked file; \ + "{op} has conflicts, and its sidecar {rel} would overwrite a tracked file; \ rename or remove that file, then retry" ))); } @@ -1848,13 +1850,13 @@ impl Repo { None => {} Some(StopBlocker::ParentFile) => { return Err(Error::InvalidArgument(format!( - "merge has conflicts, and a file stands where a directory for {rel} \ + "{op} 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 \ + "{op} 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" ))) @@ -1866,7 +1868,7 @@ impl Repo { // the first working-tree write (lazy partial clones only), outside // the store lock. self.ensure_view(conflict_root, &sparse)?; - self.refuse_untracked_overwrite("merge", Some(old_root), conflict_root)?; + self.refuse_untracked_overwrite(op, Some(old_root), conflict_root)?; { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap();