From 788fa941bedaf336f2bc4ca61c76361111291b4c Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 17:25:01 +0300 Subject: [PATCH 1/6] fix(repo): refuse an untracked file where a result adds an out-of-view path (#248) On a sparse checkout, a user's untracked or ignored file at a path the other side adds outside the view won over the addition at completion (a file on disk beats the decided root), silently, on the public and private merge-in stops. refuse_untracked_overwrite and the private materialize now refuse it before any write, naming the path. Ref #248 --- CLAUDE.md | 4 +- crates/repo/src/private.rs | 11 +- crates/repo/src/private_sparse.rs | 30 ++++ crates/repo/src/repo.rs | 78 ++++++++++- crates/repo/src/sparse_completion.rs | 196 +++++++++++++++++++++++++++ docs/adr/0034-sparse-checkouts.md | 20 +++ 6 files changed, 336 insertions(+), 3 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 52f21dd2..fe63f17b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -283,7 +283,9 @@ transport-adjacent. The rest, imperatively: the view. - **A merge, switch, cherry-pick, rebase, undo or sparse-spec change 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 and the conflict stop of a merge into a private branch are not guarded. + away and retry. On a sparse checkout the same holds for a path the result adds + outside the view (a later commit would take the user's file instead, #248). + Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (the latter only for in-view paths). - **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. A push into a repo with a stopped op is diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 974f3c25..ca901856 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -383,6 +383,12 @@ impl Repo { // 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())?; + self.refuse_untracked_out_of_view_paths( + op, + &sparse, + old_paths, + &target.keys().cloned().collect(), + )?; for p in old_paths { if !target.contains_key(p) || !sparse.matches(p) { let full = worktree::safe_join(&self.layout.root, p)?; @@ -1333,6 +1339,10 @@ impl Repo { // value (`commit_private`, #240). It reads MERGE_HEAD. let prior_entries = self.inner_tree_files(&mut store, &ctx, ours_snap.root)?; let prior_paths: BTreeSet = prior_entries.keys().cloned().collect(); + // Completion reads the working tree, where a file wins over the + // merge's addition: refuse one in the way outside the view (#248). + let merged_paths: BTreeSet = fm.files.iter().map(|f| f.path.clone()).collect(); + self.refuse_untracked_out_of_view_paths("merge", &sparse, &prior_paths, &merged_paths)?; // Carried protected ciphertext: decrypt for the resolver to see // (completion re-seals it as needed). One this recipient cannot // read has its old plaintext dropped, so completion carries the @@ -1420,7 +1430,6 @@ impl Repo { let full = worktree::safe_join(&self.layout.root, name)?; std::fs::write(&full, bytes)?; } - let merged_paths: BTreeSet = fm.files.iter().map(|f| f.path.clone()).collect(); for p in prior_paths.difference(&merged_paths) { if !sparse.matches(p) { continue; diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 94812ae6..7a70c01b 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -160,3 +160,33 @@ fn replay_onto_a_private_branch_stays_refused_under_sparse() { std::fs::remove_dir_all(&f.root).unwrap(); assert!(!f.root.exists(), "test dir left behind"); } + +#[test] +fn an_untracked_file_where_the_public_side_adds_one_out_of_view_is_refused() { + let f = fixture("collide", true, false); + let mine = f.root.join("docs/new.txt"); + std::fs::create_dir_all(mine.parent().unwrap()).unwrap(); + std::fs::write(&mine, "MINE\n").unwrap(); + let (_, before, _) = f.repo.head_private().unwrap().unwrap(); + let err = f + .repo + .merge_into_private("main", "alice", Some(&f.sk)) + .unwrap_err() + .to_string(); + assert!(err.contains("docs/new.txt"), "{err}"); + assert!(err.contains("move it away"), "{err}"); + assert!(!crate::merge_state::in_progress(f.repo.layout())); + assert_eq!(f.repo.head_private().unwrap().unwrap().1, before); + assert_eq!(std::fs::read_to_string(&mine).unwrap(), "MINE\n"); + assert_eq!( + std::fs::read_to_string(f.root.join("src/lib.rs")).unwrap(), + "private\n", + "refusal wrote nothing" + ); + // Moved away, the same run lands the public addition. + std::fs::remove_file(&mine).unwrap(); + let got = run(&f, true); + assert_eq!(got["new.txt"], "new-public\n"); + std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); +} diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 87db0585..a70aaa70 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2056,7 +2056,11 @@ impl Repo { .collect() } }; - self.refuse_untracked_overwrite_vs(op, &old, target_root) + self.refuse_untracked_overwrite_vs(op, &old, target_root)?; + match old_root { + Some(old) => self.refuse_untracked_out_of_view(op, old, target_root), + None => Ok(()), + } } /// Refuse, before any write, a forward operation (`op`) whose result would @@ -2189,6 +2193,75 @@ impl Repo { Ok(()) } + /// Refuse, before any write, an operation (`op`) whose result adds a file + /// outside the sparse view at a path where the user already has an + /// untracked or ignored file or directory. Nothing is written there, but + /// a later commit (a stop's completion, or the next ordinary one) reads + /// the working tree, where a file wins over the tree, and would commit the + /// user's bytes instead of the addition, silently (#248). `old_root` is + /// the tree the working tree reflects, `target_root` the result. A path + /// `old_root` already tracks is the user's tracked file, not a collision. + pub(crate) fn refuse_untracked_out_of_view( + &self, + op: &str, + old_root: ObjectId, + target_root: ObjectId, + ) -> Result<()> { + let sparse = self.sparse_spec()?; + if sparse.is_full() { + return Ok(()); + } + let partial = self.promisor()?.is_some(); + let (old, target) = { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let mut paths = |root| -> Result> { + // A partial clone lacks some subtrees: read what it holds. + Ok(if partial { + worktree::tree_file_entries_with_perms_sparse_present( + &mut store, + root, + &crate::Sparse::default(), + )? + } else { + worktree::tree_file_entries_with_perms(&mut store, root)? + } + .into_keys() + .collect()) + }; + (paths(old_root)?, paths(target_root)?) + }; + self.refuse_untracked_out_of_view_paths(op, &sparse, &old, &target) + } + + /// The path-list core of [`Repo::refuse_untracked_out_of_view`], for a + /// caller that already holds both trees' file paths (a private branch's + /// decrypted trees). + pub(crate) fn refuse_untracked_out_of_view_paths( + &self, + op: &str, + sparse: &crate::Sparse, + old: &std::collections::BTreeSet, + target: &std::collections::BTreeSet, + ) -> Result<()> { + // A prefix that is a file in `old` yet a directory in `target` is not + // the tracked file either: nothing removes it out of view. + let mut is_tracked = |p: &str| old.contains(p) && target.contains(p); + for rel in target + .iter() + .filter(|p| !sparse.matches(p) && !old.contains(*p)) + { + if stop_write_blocker(&self.layout.root, rel, &mut is_tracked)?.is_some() { + return Err(Error::InvalidArgument(format!( + "{op} would add {rel} (outside your sparse checkout), where an untracked or \ + ignored file or directory already stands; a later commit would take that \ + instead of the addition; 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. @@ -2490,6 +2563,9 @@ impl Repo { // 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 let Some(old) = old_root { + self.refuse_untracked_out_of_view("switch", old, 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/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index 28c492a5..b139ed58 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -308,3 +308,199 @@ fn a_completion_keeps_the_other_sides_protected_deletion() { assert_eq!(s["src/a.txt"], b"a\nFEAT\nc\n"); } } + +/// Set up `op` of `feature` against `main` under sparse `src/` (both touch +/// `src/a.txt`, so it stops) where the other side adds `docs/new.txt`, outside +/// the view: `feature` for a merge or pick, `main` for a rebase (which replays +/// `feature` onto it). The caller gets the repo before the operation starts. +fn collision_setup(tag: &str, op: Op) -> (std::path::PathBuf, Repo) { + let root = + std::env::temp_dir().join(format!("scl-sparse-collision-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = Repo::init(&root).unwrap(); + apply(&root, &[BASE_A, Edit::Write("docs/keep.txt", b"keep\n")]); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + let added = || Edit::Write("docs/new.txt", b"theirs\n"); + let rebase = matches!(op, Op::Rebase); + apply(&root, &[MAIN_A]); + if rebase { + apply(&root, &[added()]); + } + repo.commit("me", "main").unwrap(); + repo.switch("feature").unwrap(); + apply(&root, &[FEAT_A]); + if !rebase { + apply(&root, &[added()]); + } + repo.commit("me", "feature").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + if !matches!(op, Op::Rebase) { + repo.switch("main").unwrap(); + } + (root, repo) +} + +/// Start `op`: it stops on `src/a.txt` (an `Err`), or is refused. +fn start(repo: &Repo, op: Op) -> crate::error::Result<()> { + match op { + Op::Merge => repo.merge_with_identity("feature", "me", None).map(drop), + Op::Pick => repo.cherry_pick("feature", "me", None, None).map(drop), + Op::Rebase => match repo.rebase("main", "me", None)? { + crate::replay::RebaseResult::Stopped { .. } => { + Err(crate::error::Error::MergeConflicts(1)) + } + _ => Ok(()), + }, + } +} + +fn in_progress(repo: &Repo) -> bool { + crate::merge_state::in_progress(repo.layout()) + || crate::pick_state::in_progress(repo.layout()) + || crate::rebase_state::in_progress(repo.layout()) +} + +#[test] +fn a_stop_refuses_an_untracked_file_where_the_other_side_adds_one_out_of_view() { + for op in ops() { + let (root, repo) = collision_setup(&tag(op, "collide"), op); + let mine = root.join("docs/new.txt"); + std::fs::create_dir_all(mine.parent().unwrap()).unwrap(); + std::fs::write(&mine, b"MINE\n").unwrap(); + let before = std::fs::read(root.join("src/a.txt")).unwrap(); + + let err = start(&repo, op).unwrap_err().to_string(); + assert!(err.contains("docs/new.txt"), "{op:?}: {err}"); + assert!(err.contains("move it away"), "{op:?}: {err}"); + assert!(!in_progress(&repo), "{op:?}: refusal left state behind"); + assert_eq!(std::fs::read(&mine).unwrap(), b"MINE\n", "{op:?}"); + assert_eq!( + std::fs::read(root.join("src/a.txt")).unwrap(), + before, + "{op:?}: nothing written" + ); + + // Move it away and retry: the other side's file lands, as without sparse. + std::fs::remove_file(&mine).unwrap(); + start(&repo, op).unwrap_err(); + assert!(in_progress(&repo), "{op:?}: the retry must stop"); + repo.resolve_path("src/a.txt", ResolveSide::Theirs, None) + .unwrap(); + match op { + Op::Rebase => drop(repo.rebase_continue("me", None).unwrap()), + _ => drop(repo.commit("me", "complete").unwrap()), + } + let tip = repo.head_tip().unwrap().unwrap(); + let store_arc = repo.vfs().store(); + let mut s = store_arc.lock().unwrap(); + let tree = s.get_snapshot(&tip).unwrap().root; + let files = worktree::tree_file_entries_with_perms(&mut s, tree).unwrap(); + let id = files["docs/new.txt"].0; + assert!( + matches!(s.get(&id).unwrap(), Object::Blob(b) if &b[..] == b"theirs\n"), + "{op:?}: the addition lands" + ); + drop(s); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } +} + +/// A file standing where the other side needs a parent directory out of view. +#[test] +fn a_stop_refuses_an_untracked_file_where_an_out_of_view_directory_must_be() { + for op in ops() { + let (root, repo) = collision_setup(&tag(op, "parent"), op); + // The user's `docs` is a file where `docs/new.txt` needs a directory. + std::fs::remove_dir_all(root.join("docs")).ok(); + std::fs::write(root.join("docs"), b"MINE\n").unwrap(); + let err = start(&repo, op).unwrap_err().to_string(); + assert!(err.contains("docs/new.txt"), "{op:?}: {err}"); + assert!(!in_progress(&repo), "{op:?}"); + assert_eq!(std::fs::read(root.join("docs")).unwrap(), b"MINE\n"); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } +} + +/// An unrelated untracked file out of view is not in the way, and an abort +/// leaves it alone. +#[test] +fn a_stop_and_abort_leave_an_unrelated_out_of_view_file() { + for op in ops() { + let (root, repo) = collision_setup(&tag(op, "abort"), op); + std::fs::create_dir_all(root.join("docs")).unwrap(); + std::fs::write(root.join("docs/mine.txt"), b"MINE\n").unwrap(); + start(&repo, op).unwrap_err(); + assert!(in_progress(&repo), "{op:?}: it stops"); + match op { + Op::Merge => drop(repo.merge_abort().unwrap()), + Op::Pick => drop(repo.cherry_pick_abort().unwrap()), + Op::Rebase => drop(repo.rebase_abort().unwrap()), + } + assert_eq!( + std::fs::read(root.join("docs/mine.txt")).unwrap(), + b"MINE\n", + "{op:?}" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } +} + +/// The same collision on operations that never stop: the result is exact, but +/// the next commit would read the user's file in its place. +#[test] +fn a_clean_operation_refuses_an_untracked_file_where_it_adds_one_out_of_view() { + // (name, `main` also moves, then the operation) + type Run = fn(&Repo) -> crate::error::Result<()>; + let ops: [(&str, bool, Run); 3] = [ + ("merge", true, |r| { + r.merge_with_identity("feature", "me", None).map(drop) + }), + ("ff", false, |r| { + r.merge_with_identity("feature", "me", None).map(drop) + }), + ("switch", false, |r| r.switch("feature").map(drop)), + ]; + for (name, main_moves, run) in ops { + let root = + std::env::temp_dir().join(format!("scl-sparse-clean-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = Repo::init(&root).unwrap(); + apply(&root, &[BASE_A, Edit::Write("docs/keep.txt", b"keep\n")]); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + if main_moves { + apply(&root, &[MAIN_A]); + repo.commit("me", "main").unwrap(); + } + repo.switch("feature").unwrap(); + apply(&root, &[Edit::Write("docs/new.txt", b"theirs\n")]); + repo.commit("me", "feature").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + repo.switch("main").unwrap(); + apply(&root, &[Edit::Write("docs/new.txt", b"MINE\n")]); + let before = repo.head_tip().unwrap(); + + let err = run(&repo).unwrap_err().to_string(); + assert!( + err.contains("docs/new.txt") && err.contains("move it away"), + "{name}: {err}" + ); + assert_eq!(repo.head_tip().unwrap(), before, "{name}: no ref moved"); + assert_eq!(std::fs::read(root.join("docs/new.txt")).unwrap(), b"MINE\n"); + + std::fs::remove_file(root.join("docs/new.txt")).unwrap(); + run(&repo).unwrap(); + assert!( + repo.status().unwrap().added.is_empty(), + "{name}: clean after" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 50c1633b..858cc6b7 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -249,3 +249,23 @@ every claim below is checked against the shipped code, not the plan. (merge, pick, rebase: deletion kept, clean add and modify land, ours-side out-of-view changes kept, equal to the run without sparse; file to directory; an out-of-view exec bit; a protected deletion). +7. **An untracked file where the result adds an out-of-view path is refused + (#248).** Nothing is written outside the view, but a later commit reads the + working tree, and a file on disk wins over the decided root (refinement 6), + so a user's untracked or ignored file at a path the other side adds there + was committed in place of the addition, silently, on the public stop and on + the private merge-in stop. `refuse_untracked_overwrite` now also runs + `refuse_untracked_out_of_view`: for every file the result holds outside the + view that the tree the working tree reflects does not, it reuses + `stop_write_blocker` (a file, directory or symlink on the path, or a file + where a parent directory must be) and refuses before any write, naming the + path and telling the user to move it away and retry. It covers the + conflict stop of merge, cherry-pick and rebase, and the operations that + never stop (clean merge, fast-forward, clean pick and rebase, undo, switch, + and the private `materialize_private`), because their result is exact but + the next ordinary commit would take the user's file instead. A path the old + tree already tracks is the user's tracked file and is not refused, nor is + anything in the view (the existing guard). Completion still reads a file + the user creates after a stop at such a path; that is their resolution, as + for an edited tracked file (refinement 5). Pinned by `sparse_completion.rs` + and `private_sparse.rs`. From d568a3f88ea0b5d56ff6fc83d708c870580d9f8b Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 17:49:34 +0300 Subject: [PATCH 2/6] fix(repo): guard leaving a private branch too; review fixes (#248) --- crates/repo/src/private_sparse.rs | 26 ++++++++++++++++++++++++ crates/repo/src/repo.rs | 30 ++++++++++++++++++++++------ crates/repo/src/sparse_completion.rs | 4 ++++ docs/adr/0034-sparse-checkouts.md | 5 +++-- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 7a70c01b..3357f0c2 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -190,3 +190,29 @@ fn an_untracked_file_where_the_public_side_adds_one_out_of_view_is_refused() { std::fs::remove_dir_all(&f.root).unwrap(); assert!(!f.root.exists(), "test dir left behind"); } + +#[test] +fn leaving_a_private_branch_refuses_an_untracked_file_where_main_has_one_out_of_view() { + let f = fixture("leave", true, false); + let mine = f.root.join("docs/new.txt"); + std::fs::create_dir_all(mine.parent().unwrap()).unwrap(); + std::fs::write(&mine, "MINE\n").unwrap(); + let err = f + .repo + .switch_with_identity("main", Some(&f.sk)) + .unwrap_err() + .to_string(); + assert!( + err.contains("docs/new.txt") && err.contains("move it away"), + "{err}" + ); + assert!( + f.repo.head_private().unwrap().is_some(), + "still on the private branch" + ); + assert_eq!(std::fs::read_to_string(&mine).unwrap(), "MINE\n"); + std::fs::remove_file(&mine).unwrap(); + f.repo.switch_with_identity("main", Some(&f.sk)).unwrap(); + std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); +} diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index a70aaa70..5076e563 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2200,7 +2200,8 @@ impl Repo { /// the working tree, where a file wins over the tree, and would commit the /// user's bytes instead of the addition, silently (#248). `old_root` is /// the tree the working tree reflects, `target_root` the result. A path - /// `old_root` already tracks is the user's tracked file, not a collision. + /// `old_root` already tracks is not an addition, and is left to the + /// ordinary tracked-file rules. pub(crate) fn refuse_untracked_out_of_view( &self, op: &str, @@ -2244,9 +2245,9 @@ impl Repo { old: &std::collections::BTreeSet, target: &std::collections::BTreeSet, ) -> Result<()> { - // A prefix that is a file in `old` yet a directory in `target` is not - // the tracked file either: nothing removes it out of view. - let mut is_tracked = |p: &str| old.contains(p) && target.contains(p); + // `rel` is not in `old`, and a tree holds no file at an ancestor of + // another of its paths, so nothing on the way is a tracked file. + let mut is_tracked = |_: &str| false; for rel in target .iter() .filter(|p| !sparse.matches(p) && !old.contains(*p)) @@ -2563,8 +2564,25 @@ impl Repo { // 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 let Some(old) = old_root { - self.refuse_untracked_out_of_view("switch", old, target_root)?; + match old_root { + Some(old) => self.refuse_untracked_out_of_view("switch", old, target_root)?, + // Leaving a private branch: `old_paths` is its full inner tree + // (partial clones refuse private branches). + None => { + let target: std::collections::BTreeSet = { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + worktree::tree_file_entries_with_perms(&mut store, target_root)? + .into_keys() + .collect() + }; + self.refuse_untracked_out_of_view_paths( + "switch", + &self.sparse_spec()?, + &old_paths, + &target, + )?; + } } if head_priv.is_some() { // Remove private-tip paths the public target doesn't track, diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index b139ed58..c77e9cb6 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -405,6 +405,7 @@ fn a_stop_refuses_an_untracked_file_where_the_other_side_adds_one_out_of_view() drop(s); drop(repo); std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); } } @@ -422,6 +423,7 @@ fn a_stop_refuses_an_untracked_file_where_an_out_of_view_directory_must_be() { assert_eq!(std::fs::read(root.join("docs")).unwrap(), b"MINE\n"); drop(repo); std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); } } @@ -447,6 +449,7 @@ fn a_stop_and_abort_leave_an_unrelated_out_of_view_file() { ); drop(repo); std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); } } @@ -502,5 +505,6 @@ fn a_clean_operation_refuses_an_untracked_file_where_it_adds_one_out_of_view() { ); drop(repo); std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); } } diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 858cc6b7..14b487a0 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -264,8 +264,9 @@ every claim below is checked against the shipped code, not the plan. never stop (clean merge, fast-forward, clean pick and rebase, undo, switch, and the private `materialize_private`), because their result is exact but the next ordinary commit would take the user's file instead. A path the old - tree already tracks is the user's tracked file and is not refused, nor is - anything in the view (the existing guard). Completion still reads a file + tree already tracks is not an addition and is not checked here (a user's + copy of it is read as an edit; a stop's `materialize` can still delete it), + nor is anything in the view (the existing guard). Completion still reads a file the user creates after a stop at such a path; that is their resolution, as for an edited tracked file (refinement 5). Pinned by `sparse_completion.rs` and `private_sparse.rs`. From aab9ba87746d81cd27b1d0aaccc3c58dc9b07aaf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 18:03:34 +0300 Subject: [PATCH 3/6] docs(adr): note the partial-clone limit of the out-of-view guard (#248) --- docs/adr/0034-sparse-checkouts.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 14b487a0..21409f56 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -268,5 +268,8 @@ every claim below is checked against the shipped code, not the plan. copy of it is read as an edit; a stop's `materialize` can still delete it), nor is anything in the view (the existing guard). Completion still reads a file the user creates after a stop at such a path; that is their resolution, as - for an edited tracked file (refinement 5). Pinned by `sparse_completion.rs` + for an edited tracked file (refinement 5). On a partial clone the guard sees + only the subtrees the store holds, so an addition inside a carried, absent + subtree is not checked; it is not silent, because the commit then fails with + `GappedPathContent` naming the path (move the file away and retry). Pinned by `sparse_completion.rs` and `private_sparse.rs`. From 084999d770cf73c023a01f058c86b9936a1aebcf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 18:30:54 +0300 Subject: [PATCH 4/6] fix(repo): note why the partial-clone limit stays; comment the blocker reliance (#248) --- CLAUDE.md | 2 +- crates/repo/src/repo.rs | 3 +++ docs/adr/0034-sparse-checkouts.md | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 001fc8e2..d1c11493 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -291,7 +291,7 @@ transport-adjacent. The rest, imperatively: directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it away and retry. On a sparse checkout the same holds for a path the result adds outside the view (a later commit would take the user's file instead, #248). - Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (except that a private stop refuses a `.theirs` sidecar that would land on an existing file, and an out-of-view addition under a file on disk, #248). + Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (except that a private stop refuses a `.theirs` sidecar that would land on an existing file). - **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. A push into a repo with a stopped op is diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 5ddc2849..17d18513 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2247,6 +2247,9 @@ impl Repo { ) -> Result<()> { // `rel` is not in `old`, and a tree holds no file at an ancestor of // another of its paths, so nothing on the way is a tracked file. + // Relies on `stop_write_blocker` treating whatever stands at `rel` (any + // directory included) as the user's: nothing outside the view is laid + // out, so no tracked file can be on disk there. let mut is_tracked = |_: &str| false; for rel in target .iter() diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 217f2840..c3fcb313 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -271,5 +271,5 @@ every claim below is checked against the shipped code, not the plan. for an edited tracked file (refinement 5). On a partial clone the guard sees only the subtrees the store holds, so an addition inside a carried, absent subtree is not checked; it is not silent, because the commit then fails with - `GappedPathContent` naming the path (move the file away and retry). Pinned by `sparse_completion.rs` + `GappedPathContent` naming the path (move the file away and retry). Refusing up front would mean fetching or guessing the absent subtree's paths (a disk probe under a changed absent directory would refuse unrelated files), so the limit stays. Pinned by `sparse_completion.rs` and `private_sparse.rs`. From 883a85abdf883858c44685e31cfcde3d1074db51 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 18:48:58 +0300 Subject: [PATCH 5/6] fix(repo): let a private abort and case-only renames pass the out-of-view guard (#248) --- crates/repo/src/private.rs | 21 +++++++++++++++------ crates/repo/src/private_sparse.rs | 19 +++++++++++++++++++ crates/repo/src/repo.rs | 5 ++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 68a9f87a..9ebffdab 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -377,18 +377,23 @@ impl Repo { old_paths: &BTreeSet, protection: &Protection, identity: &SecretKey, + restores: bool, ) -> 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())?; - self.refuse_untracked_out_of_view_paths( - op, - &sparse, - old_paths, - &target.keys().cloned().collect(), - )?; + // A restore (abort) re-lays the tree the stop started from: nothing is + // added, and `old_paths` is only what the stop wrote, in view. + if !restores { + self.refuse_untracked_out_of_view_paths( + op, + &sparse, + old_paths, + &target.keys().cloned().collect(), + )?; + } for p in old_paths { if !target.contains_key(p) || !sparse.matches(p) { let full = worktree::safe_join(&self.layout.root, p)?; @@ -465,6 +470,7 @@ impl Repo { old_paths, &tip_snap.protection, sk, + false, )? }; refs::write_head(&self.layout, name)?; @@ -902,6 +908,7 @@ impl Repo { &written, &ours.protection, sk, + true, )?; drop(store); // After the restore, so a failed restore leaves the sidecars to retry. @@ -1356,6 +1363,7 @@ impl Repo { &old_paths, &theirs_snap.protection, sk, + false, )?; drop(store); refs::write_branch_tip(&self.layout, &head, &new_mid)?; @@ -1629,6 +1637,7 @@ impl Repo { &old_paths, &merged_prot, sk, + false, )?; drop(store); refs::write_branch_tip(&self.layout, &head, &new_mid)?; diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs index 3357f0c2..84946d4a 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -216,3 +216,22 @@ fn leaving_a_private_branch_refuses_an_untracked_file_where_main_has_one_out_of_ std::fs::remove_dir_all(&f.root).unwrap(); assert!(!f.root.exists(), "test dir left behind"); } + +#[test] +fn aborting_a_private_stop_ignores_a_file_at_an_out_of_view_tracked_path() { + let f = fixture("abort", true, false); + let sk = Some(&f.sk); + assert!(matches!( + f.repo.merge_into_private("main", "alice", sk), + Err(Error::MergeConflicts(1)) + )); + // `docs/ours.txt` is tracked by the private tip, outside the view. + put(&f.root, "docs/ours.txt", "MINE\n"); + f.repo.merge_abort_with_identity(sk).unwrap(); + assert_eq!( + std::fs::read_to_string(f.root.join("docs/ours.txt")).unwrap(), + "MINE\n" + ); + std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); +} diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 17d18513..c3f7cd39 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2250,7 +2250,10 @@ impl Repo { // Relies on `stop_write_blocker` treating whatever stands at `rel` (any // directory included) as the user's: nothing outside the view is laid // out, so no tracked file can be on disk there. - let mut is_tracked = |_: &str| false; + // Only a case alias of a path `old` laid out (a case-only rename on a + // case-insensitive filesystem) is the tracked file. + let root = &self.layout.root; + let mut is_tracked = |p: &str| is_tracked_case_alias(root, old, p); for rel in target .iter() .filter(|p| !sparse.matches(p) && !old.contains(*p)) From 184bd9fb2cd9b88483f32ba4ba5698564b2541a1 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 19:06:13 +0300 Subject: [PATCH 6/6] fix(repo): limit the out-of-view case-alias exception to laid-out paths (#248) --- crates/repo/src/repo.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index c3f7cd39..6c8cf585 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2250,10 +2250,16 @@ impl Repo { // Relies on `stop_write_blocker` treating whatever stands at `rel` (any // directory included) as the user's: nothing outside the view is laid // out, so no tracked file can be on disk there. - // Only a case alias of a path `old` laid out (a case-only rename on a - // case-insensitive filesystem) is the tracked file. + // Only a case alias of a path `old` laid out, so inside the view (a + // case-only rename on a case-insensitive filesystem), is the tracked + // file; an out-of-view old path was never on disk. let root = &self.layout.root; - let mut is_tracked = |p: &str| is_tracked_case_alias(root, old, p); + let laid_out = std::cell::OnceCell::new(); + let mut is_tracked = |p: &str| { + let laid_out: &std::collections::BTreeSet = laid_out + .get_or_init(|| old.iter().filter(|o| sparse.matches(o)).cloned().collect()); + is_tracked_case_alias(root, laid_out, p) + }; for rel in target .iter() .filter(|p| !sparse.matches(p) && !old.contains(*p))