diff --git a/CLAUDE.md b/CLAUDE.md index 7c832e4d..d1c11493 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -289,7 +289,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 (except that a private stop refuses a `.theirs` sidecar that would land on an existing file). + 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). - **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 16f07509..9ebffdab 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -377,12 +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())?; + // 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)?; @@ -459,6 +470,7 @@ impl Repo { old_paths, &tip_snap.protection, sk, + false, )? }; refs::write_head(&self.layout, name)?; @@ -896,6 +908,7 @@ impl Repo { &written, &ours.protection, sk, + true, )?; drop(store); // After the restore, so a failed restore leaves the sidecars to retry. @@ -1350,6 +1363,7 @@ impl Repo { &old_paths, &theirs_snap.protection, sk, + false, )?; drop(store); refs::write_branch_tip(&self.layout, &head, &new_mid)?; @@ -1412,6 +1426,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 @@ -1619,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 94812ae6..84946d4a 100644 --- a/crates/repo/src/private_sparse.rs +++ b/crates/repo/src/private_sparse.rs @@ -160,3 +160,78 @@ 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"); +} + +#[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"); +} + +#[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 bea184a6..6c8cf585 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,88 @@ 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 not an addition, and is left to the + /// ordinary tracked-file rules. + 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<()> { + // `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. + // 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 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)) + { + 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. @@ -2520,6 +2606,26 @@ 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)?; + 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, // BEFORE materialize lays the target down. diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index 28c492a5..c77e9cb6 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -308,3 +308,203 @@ 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(); + assert!(!root.exists(), "temp repo must be removed"); + } +} + +/// 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(); + assert!(!root.exists(), "temp repo must be removed"); + } +} + +/// 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(); + assert!(!root.exists(), "temp repo must be removed"); + } +} + +/// 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(); + 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 04d7bd81..c3fcb313 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -249,3 +249,27 @@ 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 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). 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). 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`.