From a0b6ef155820b2b6e072a41bc15c60ae6d2a13af Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 12:28:16 +0300 Subject: [PATCH 1/3] fix(repo): do not resurrect out-of-view deletions on sparse completion With a decided root recorded, completion of a stopped merge, cherry-pick or rebase no longer reads the parents for the out-of-sparse carry. A path missing from the decided root was decided deleted; the tip used to copy it back. Parents still supply still-protected paths and remain the fallback for state without a decided root. Ref #238 --- crates/repo/src/lib.rs | 2 + crates/repo/src/repo.rs | 23 +++-- crates/repo/src/sparse_completion.rs | 145 +++++++++++++++++++++++++++ docs/adr/0034-sparse-checkouts.md | 11 ++ 4 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 crates/repo/src/sparse_completion.rs diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 59e7f424..448e5dee 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -44,6 +44,8 @@ pub mod secrets; pub mod serve_tokens; pub mod signatures; pub mod sparse; +#[cfg(test)] +mod sparse_completion; pub mod stdio_transport; pub mod sync; pub mod textdiff; diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 55e1951a..7d3d8cb4 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -661,7 +661,10 @@ impl Repo { // behavior), both parents on merge completion for paths the // decided tree doesn't know (and as full fallback when the merge // state predates the decided-root record, i.e. was written by an - // older code path). Ours wins across parents. A pick's non-parent + // older code path). Ours wins across parents. With a decided root + // the parents supply only still-protected paths, never the + // out-of-sparse carry: a plain path missing from the decided root + // was decided deleted (#238). A pick's non-parent // `pick_head` is NOT a source: any picked-side content the pick // kept is in the decided tree — a protected path in the picked // commit but absent from the decided tree was decided *deleted* @@ -700,9 +703,10 @@ impl Repo { // Decided tree first (merge completion only), then the parents // restricted to paths outside the decided tree. - let mut sources: Vec< - std::collections::BTreeMap, - > = Vec::new(); + // The flag marks a parent behind a decided root: it may still + // supply a protected path, but never an out-of-sparse carry (#238). + type Entries = std::collections::BTreeMap; + let mut sources: Vec<(Entries, bool)> = Vec::new(); let mut decided_paths: std::collections::BTreeSet = Default::default(); if let Some(dr) = decided_root { let decided = if partial { @@ -711,7 +715,7 @@ impl Repo { worktree::tree_file_entries_with_perms(&mut store, dr)? }; decided_paths = decided.keys().cloned().collect(); - sources.push(decided); + sources.push((decided, false)); } for parent in tip.into_iter().chain(merge_head) { let parent_root = store.get_snapshot(&parent)?.root; @@ -728,7 +732,7 @@ impl Repo { worktree::tree_file_entries_with_perms(&mut store, parent_root)? }; entries.retain(|p, _| !decided_paths.contains(p)); - sources.push(entries); + sources.push((entries, decided_root.is_some())); } // Paths the stopped operation wrote in plaintext. One of them that @@ -740,14 +744,17 @@ impl Repo { } else { Default::default() }; - for entries in sources { + for (entries, behind_decided) in sources { for (path, (blob_id, mode, perms)) in entries { if on_disk.contains(&path) || conflict_plaintext.contains(&path) { continue; } let still_protected = perms & scl_core::PROTECTED != 0 && crate::protect::matching_prefix(&protection, &path).is_some(); - let out_of_sparse = !sparse.matches(&path); + // A path missing from the decided root was decided deleted + // (the other side's clean deletion included): a parent must + // not carry it back just because it is out of view. + let out_of_sparse = !behind_decided && !sparse.matches(&path); if !still_protected && !out_of_sparse { continue; } diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs new file mode 100644 index 00000000..9aa0a6eb --- /dev/null +++ b/crates/repo/src/sparse_completion.rs @@ -0,0 +1,145 @@ +//! A stopped merge, cherry-pick or rebase on a FULL clone with a sparse spec +//! must complete to the same tree as without sparse (#238): an out-of-view +//! path the other side deleted stays deleted, while its clean adds and +//! modifies land and ours-side-only out-of-view changes are kept. + +use std::collections::BTreeMap; + +use crate::conflicts::ResolveSide; +use crate::repo::Repo; +use crate::worktree; + +#[derive(Clone, Copy, Debug)] +enum Op { + Merge, + Pick, + Rebase, +} + +/// Run the scenario and return the completed tip's `path -> content`. +/// +/// base: `src/a.txt`, `docs/{old,mod,ours,ours_del}.txt`. `main` edits +/// `src/a.txt`, edits `docs/ours.txt` and deletes `docs/ours_del.txt`; +/// `feature` edits `src/a.txt` (a conflict), deletes `docs/old.txt`, edits +/// `docs/mod.txt` and adds `docs/new.txt`. `docs/` is outside the `src/` view. +fn run(tag: &str, op: Op, sparse: bool) -> BTreeMap> { + let root = std::env::temp_dir().join(format!( + "scl-sparse-completion-{tag}-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::create_dir_all(root.join("docs")).unwrap(); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("src/a.txt"), b"a\nb\nc\n").unwrap(); + for f in ["old", "mod", "ours", "ours_del"] { + std::fs::write(root.join(format!("docs/{f}.txt")), b"v1\n").unwrap(); + } + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + + std::fs::write(root.join("src/a.txt"), b"a\nMAIN\nc\n").unwrap(); + std::fs::write(root.join("docs/ours.txt"), b"ours v2\n").unwrap(); + std::fs::remove_file(root.join("docs/ours_del.txt")).unwrap(); + repo.commit("me", "main").unwrap(); + + repo.switch("feature").unwrap(); + std::fs::write(root.join("src/a.txt"), b"a\nFEAT\nc\n").unwrap(); + std::fs::remove_file(root.join("docs/old.txt")).unwrap(); + std::fs::write(root.join("docs/mod.txt"), b"theirs v2\n").unwrap(); + std::fs::write(root.join("docs/new.txt"), b"new\n").unwrap(); + repo.commit("me", "feature").unwrap(); + if !matches!(op, Op::Rebase) { + repo.switch("main").unwrap(); + } + + if sparse { + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!( + !root.join("docs/mod.txt").exists(), + "setup: docs/ is out of view" + ); + } + + match op { + Op::Merge => { + repo.merge("feature", "me").unwrap_err(); + } + Op::Pick => { + repo.cherry_pick("feature", "me", None, None).unwrap_err(); + } + Op::Rebase => { + let out = repo.rebase("main", "me", None).unwrap(); + assert!(matches!(out, crate::replay::RebaseResult::Stopped { .. })); + } + } + repo.resolve_path("src/a.txt", ResolveSide::Theirs, None) + .unwrap(); + match op { + Op::Rebase => { + repo.rebase_continue("me", None).unwrap(); + } + _ => { + 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 mut out = BTreeMap::new(); + for (p, (id, _, _)) in worktree::tree_file_entries_with_perms(&mut s, tree).unwrap() { + match s.get(&id).unwrap() { + scl_core::Object::Blob(b) => out.insert(p, b.to_vec()), + _ => unreachable!(), + }; + } + drop(s); + drop(repo); + std::fs::remove_dir_all(&root).ok(); + out +} + +fn check(op: Op) { + let tag = format!("{op:?}").to_lowercase(); + let full = run(&format!("{tag}-full"), op, false); + let sparse = run(&format!("{tag}-sparse"), op, true); + assert!(!full.contains_key("docs/old.txt"), "reference: deleted"); + assert!( + !sparse.contains_key("docs/old.txt"), + "{op:?}: the other side's clean out-of-view deletion must not resurrect" + ); + assert_eq!( + sparse["docs/mod.txt"], b"theirs v2\n", + "{op:?}: clean modify lands" + ); + assert_eq!(sparse["docs/new.txt"], b"new\n", "{op:?}: clean add lands"); + assert_eq!( + sparse["docs/ours.txt"], b"ours v2\n", + "{op:?}: ours' edit kept" + ); + assert!( + !sparse.contains_key("docs/ours_del.txt"), + "{op:?}: ours' delete kept" + ); + assert_eq!( + sparse, full, + "{op:?}: sparse completion equals the full one" + ); +} + +#[test] +fn a_sparse_merge_keeps_the_other_sides_out_of_view_deletion() { + check(Op::Merge); +} + +#[test] +fn a_sparse_cherry_pick_keeps_the_picked_commits_out_of_view_deletion() { + check(Op::Pick); +} + +#[test] +fn a_sparse_rebase_continue_keeps_the_replayed_commits_out_of_view_deletion() { + check(Op::Rebase); +} diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index ce0a3218..29dd829c 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -168,3 +168,14 @@ every claim below is checked against the shipped code, not the plan. `MergeInProgress`/`PickInProgress`/`RebaseInProgress` errors the P21 policy-op guards already use elsewhere, added in review before Task 4 landed rather than discovered after. +6. **Completion carries out-of-view paths from the decided root, not the + parents (#238).** A stopped merge, cherry-pick or rebase on a full clone + records a decided root, and `snapshot_files` carries an absent out-of-view + path from it. Earlier builds also read the tip and merge head, so an + out-of-view file the other side had cleanly deleted came back from the + tip, silently losing the deletion. With a decided root the parents now + supply only still-protected paths; they remain the full fallback for + state written without a decided root. Pinned by + `sparse_completion.rs` (merge, pick, rebase: the deletion stays, a clean + add and modify land, ours-side-only out-of-view changes are kept, and the + tree equals the result without sparse). From 07440b24d0d0d47ef465cbbf47e2250898ef1ea4 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 12:36:28 +0300 Subject: [PATCH 2/3] test(repo): assert the sparse completion temp repo is removed (ref #238) --- crates/repo/src/sparse_completion.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index 9aa0a6eb..f1f15409 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -97,7 +97,8 @@ fn run(tag: &str, op: Op, sparse: bool) -> BTreeMap> { } drop(s); drop(repo); - std::fs::remove_dir_all(&root).ok(); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "temp repo must be removed"); out } From 3a5f6c3d8aedc0e6c90ac335d39fb46d68e38c42 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 13:11:23 +0300 Subject: [PATCH 3/3] fix(repo): complete from the decided root alone, pin protected, dir and mode cases With a decided root, parents no longer supply still-protected paths either: a protected file the other side deleted cleanly came back after a conflicted completion. Tests cover that, a file replaced by a directory out of view, and an out-of-view exec bit. ADR-0034 refinement 6 now scopes the decided root to public branches. Ref #238 --- crates/repo/src/repo.rs | 69 +++--- crates/repo/src/sparse_completion.rs | 352 ++++++++++++++++++++------- docs/adr/0034-sparse-checkouts.md | 30 ++- 3 files changed, 307 insertions(+), 144 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 7d3d8cb4..b76b7c9e 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -658,13 +658,13 @@ impl Repo { // committed the stale ours blob.) A path PRESENT in the decided // tree is decided, period: parents are never consulted for it. // 2. The parents — `tip` only on a non-merge commit (unchanged - // behavior), both parents on merge completion for paths the - // decided tree doesn't know (and as full fallback when the merge - // state predates the decided-root record, i.e. was written by an - // older code path). Ours wins across parents. With a decided root - // the parents supply only still-protected paths, never the - // out-of-sparse carry: a plain path missing from the decided root - // was decided deleted (#238). A pick's non-parent + // behavior), both parents on a merge completion only as the full + // fallback when the state predates the decided-root record (written + // by an older code path). Ours wins across parents. With a decided + // root the parents are NOT consulted at all: it holds every + // surviving path, so a path missing from it (protected or + // out-of-sparse) was decided deleted and must not come back + // (#238). A pick's non-parent // `pick_head` is NOT a source: any picked-side content the pick // kept is in the decided tree — a protected path in the picked // commit but absent from the decided tree was decided *deleted* @@ -701,38 +701,32 @@ impl Repo { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); - // Decided tree first (merge completion only), then the parents - // restricted to paths outside the decided tree. - // The flag marks a parent behind a decided root: it may still - // supply a protected path, but never an out-of-sparse carry (#238). + // The decided tree (merge, pick or rebase completion) is the only + // source when one exists; the parents are the fallback for a plain + // commit and for state written without a decided root. type Entries = std::collections::BTreeMap; - let mut sources: Vec<(Entries, bool)> = Vec::new(); - let mut decided_paths: std::collections::BTreeSet = Default::default(); + let mut sources: Vec = Vec::new(); if let Some(dr) = decided_root { - let decided = if partial { + sources.push(if partial { worktree::tree_file_entries_with_perms_sparse(&mut store, dr, sparse)? } else { worktree::tree_file_entries_with_perms(&mut store, dr)? - }; - decided_paths = decided.keys().cloned().collect(); - sources.push((decided, false)); - } - for parent in tip.into_iter().chain(merge_head) { - let parent_root = store.get_snapshot(&parent)?.root; - let mut entries = if partial { - // A parent is only a fallback behind the decided root - // (merge completion); a subtree it lacks was never the - // only record of a path, so skip it, not fail. - worktree::tree_file_entries_with_perms_sparse_present( - &mut store, - parent_root, - sparse, - )? - } else { - worktree::tree_file_entries_with_perms(&mut store, parent_root)? - }; - entries.retain(|p, _| !decided_paths.contains(p)); - sources.push((entries, decided_root.is_some())); + }); + } else { + for parent in tip.into_iter().chain(merge_head) { + let parent_root = store.get_snapshot(&parent)?.root; + sources.push(if partial { + // A subtree a parent lacks was never the only record + // of a path, so skip it, not fail. + worktree::tree_file_entries_with_perms_sparse_present( + &mut store, + parent_root, + sparse, + )? + } else { + worktree::tree_file_entries_with_perms(&mut store, parent_root)? + }); + } } // Paths the stopped operation wrote in plaintext. One of them that @@ -744,17 +738,14 @@ impl Repo { } else { Default::default() }; - for (entries, behind_decided) in sources { + for entries in sources { for (path, (blob_id, mode, perms)) in entries { if on_disk.contains(&path) || conflict_plaintext.contains(&path) { continue; } let still_protected = perms & scl_core::PROTECTED != 0 && crate::protect::matching_prefix(&protection, &path).is_some(); - // A path missing from the decided root was decided deleted - // (the other side's clean deletion included): a parent must - // not carry it back just because it is out of view. - let out_of_sparse = !behind_decided && !sparse.matches(&path); + let out_of_sparse = !sparse.matches(&path); if !still_protected && !out_of_sparse { continue; } diff --git a/crates/repo/src/sparse_completion.rs b/crates/repo/src/sparse_completion.rs index f1f15409..28c492a5 100644 --- a/crates/repo/src/sparse_completion.rs +++ b/crates/repo/src/sparse_completion.rs @@ -1,9 +1,13 @@ -//! A stopped merge, cherry-pick or rebase on a FULL clone with a sparse spec -//! must complete to the same tree as without sparse (#238): an out-of-view -//! path the other side deleted stays deleted, while its clean adds and -//! modifies land and ours-side-only out-of-view changes are kept. +//! A stopped merge, cherry-pick or rebase on a FULL clone must complete from +//! its decided root (#238). With a sparse spec an out-of-view path the other +//! side deleted stays deleted while its clean adds and modifies land and +//! ours-side-only out-of-view changes are kept; a protected path the other +//! side deleted stays deleted too. use std::collections::BTreeMap; +use std::path::Path; + +use scl_core::{FileMode, Object}; use crate::conflicts::ResolveSide; use crate::repo::Repo; @@ -16,131 +20,291 @@ enum Op { Rebase, } -/// Run the scenario and return the completed tip's `path -> content`. -/// -/// base: `src/a.txt`, `docs/{old,mod,ours,ours_del}.txt`. `main` edits -/// `src/a.txt`, edits `docs/ours.txt` and deletes `docs/ours_del.txt`; -/// `feature` edits `src/a.txt` (a conflict), deletes `docs/old.txt`, edits -/// `docs/mod.txt` and adds `docs/new.txt`. `docs/` is outside the `src/` view. -fn run(tag: &str, op: Op, sparse: bool) -> BTreeMap> { - let root = std::env::temp_dir().join(format!( - "scl-sparse-completion-{tag}-{}", - std::process::id() - )); - let _ = std::fs::remove_dir_all(&root); - std::fs::create_dir_all(root.join("src")).unwrap(); - std::fs::create_dir_all(root.join("docs")).unwrap(); - let repo = Repo::init(&root).unwrap(); - std::fs::write(root.join("src/a.txt"), b"a\nb\nc\n").unwrap(); - for f in ["old", "mod", "ours", "ours_del"] { - std::fs::write(root.join(format!("docs/{f}.txt")), b"v1\n").unwrap(); - } - repo.commit("me", "base").unwrap(); - repo.branch("feature").unwrap(); - - std::fs::write(root.join("src/a.txt"), b"a\nMAIN\nc\n").unwrap(); - std::fs::write(root.join("docs/ours.txt"), b"ours v2\n").unwrap(); - std::fs::remove_file(root.join("docs/ours_del.txt")).unwrap(); - repo.commit("me", "main").unwrap(); - - repo.switch("feature").unwrap(); - std::fs::write(root.join("src/a.txt"), b"a\nFEAT\nc\n").unwrap(); - std::fs::remove_file(root.join("docs/old.txt")).unwrap(); - std::fs::write(root.join("docs/mod.txt"), b"theirs v2\n").unwrap(); - std::fs::write(root.join("docs/new.txt"), b"new\n").unwrap(); - repo.commit("me", "feature").unwrap(); - if !matches!(op, Op::Rebase) { - repo.switch("main").unwrap(); - } +/// A working-tree edit applied before a commit. +enum Edit<'a> { + Write(&'a str, &'a [u8]), + Exec(&'a str, &'a [u8]), + Remove(&'a str), +} - if sparse { - repo.set_sparse(&["src/".into()], None).unwrap(); - assert!( - !root.join("docs/mod.txt").exists(), - "setup: docs/ is out of view" - ); +fn apply(root: &Path, edits: &[Edit]) { + for e in edits { + match e { + Edit::Write(p, b) | Edit::Exec(p, b) => { + let full = root.join(p); + std::fs::create_dir_all(full.parent().unwrap()).unwrap(); + std::fs::write(&full, b).unwrap(); + #[cfg(unix)] + if matches!(e, Edit::Exec(..)) { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&full, std::fs::Permissions::from_mode(0o755)) + .unwrap(); + } + } + Edit::Remove(p) => std::fs::remove_file(root.join(p)).unwrap(), + } } +} +/// Stop `op` of `feature` against `main`, resolve `src/a.txt` to the other +/// side, complete, and return the tip's `path -> (content, mode)`. Cleans up. +fn finish( + root: &Path, + repo: Repo, + op: Op, + identity: Option<&scl_crypto::SecretKey>, + conflict: &str, +) -> BTreeMap, FileMode)> { match op { Op::Merge => { - repo.merge("feature", "me").unwrap_err(); + repo.merge_with_identity("feature", "me", identity) + .unwrap_err(); } Op::Pick => { - repo.cherry_pick("feature", "me", None, None).unwrap_err(); + repo.cherry_pick("feature", "me", identity, None) + .unwrap_err(); } Op::Rebase => { - let out = repo.rebase("main", "me", None).unwrap(); + let out = repo.rebase("main", "me", identity).unwrap(); assert!(matches!(out, crate::replay::RebaseResult::Stopped { .. })); } } - repo.resolve_path("src/a.txt", ResolveSide::Theirs, None) + repo.resolve_path(conflict, ResolveSide::Theirs, identity) .unwrap(); match op { Op::Rebase => { - repo.rebase_continue("me", None).unwrap(); + repo.rebase_continue("me", identity).unwrap(); } _ => { 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 mut out = BTreeMap::new(); - for (p, (id, _, _)) in worktree::tree_file_entries_with_perms(&mut s, tree).unwrap() { - match s.get(&id).unwrap() { - scl_core::Object::Blob(b) => out.insert(p, b.to_vec()), - _ => unreachable!(), - }; + { + let store_arc = repo.vfs().store(); + let mut s = store_arc.lock().unwrap(); + let tree = s.get_snapshot(&tip).unwrap().root; + for (p, (id, mode, _)) in worktree::tree_file_entries_with_perms(&mut s, tree).unwrap() { + match s.get(&id).unwrap() { + Object::Blob(b) => out.insert(p, (b.to_vec(), mode)), + _ => unreachable!(), + }; + } } - drop(s); drop(repo); - std::fs::remove_dir_all(&root).unwrap(); + std::fs::remove_dir_all(root).unwrap(); assert!(!root.exists(), "temp repo must be removed"); out } -fn check(op: Op) { - let tag = format!("{op:?}").to_lowercase(); - let full = run(&format!("{tag}-full"), op, false); - let sparse = run(&format!("{tag}-sparse"), op, true); - assert!(!full.contains_key("docs/old.txt"), "reference: deleted"); - assert!( - !sparse.contains_key("docs/old.txt"), - "{op:?}: the other side's clean out-of-view deletion must not resurrect" - ); - assert_eq!( - sparse["docs/mod.txt"], b"theirs v2\n", - "{op:?}: clean modify lands" - ); - assert_eq!(sparse["docs/new.txt"], b"new\n", "{op:?}: clean add lands"); - assert_eq!( - sparse["docs/ours.txt"], b"ours v2\n", - "{op:?}: ours' edit kept" - ); - assert!( - !sparse.contains_key("docs/ours_del.txt"), - "{op:?}: ours' delete kept" - ); - assert_eq!( - sparse, full, - "{op:?}: sparse completion equals the full one" - ); +/// base -> `main` edits and `feature` edits (both touch `src/a.txt`, so the +/// operation stops), optionally under sparse `src/`. Everything under +/// `docs/` is outside the view. +fn run( + tag: &str, + op: Op, + sparse: bool, + base: &[Edit], + main: &[Edit], + feature: &[Edit], +) -> BTreeMap, FileMode)> { + let root = std::env::temp_dir().join(format!( + "scl-sparse-completion-{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); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + apply(&root, main); + repo.commit("me", "main").unwrap(); + repo.switch("feature").unwrap(); + apply(&root, feature); + repo.commit("me", "feature").unwrap(); + // Narrow before leaving `feature`, so its out-of-view files are not left + // on disk to block the switch (a directory where `main` has a file). + if sparse { + repo.set_sparse(&["src/".into()], None).unwrap(); + // Drop what the narrowing left behind: empty directories. + for d in ["docs/x", "docs"] { + let _ = std::fs::remove_dir(root.join(d)); + } + assert!(!root.join("docs").exists(), "setup: docs/ is out of view"); + } + if !matches!(op, Op::Rebase) { + repo.switch("main").unwrap(); + } + finish(&root, repo, op, None, "src/a.txt") +} + +fn ops() -> [Op; 3] { + [Op::Merge, Op::Pick, Op::Rebase] +} + +fn tag(op: Op, what: &str) -> String { + format!("{}-{what}", format!("{op:?}").to_lowercase()) +} + +const BASE_A: Edit = Edit::Write("src/a.txt", b"a\nb\nc\n"); +const MAIN_A: Edit = Edit::Write("src/a.txt", b"a\nMAIN\nc\n"); +const FEAT_A: Edit = Edit::Write("src/a.txt", b"a\nFEAT\nc\n"); + +fn text(m: &BTreeMap, FileMode)>) -> BTreeMap<&str, &[u8]> { + m.iter().map(|(p, (b, _))| (p.as_str(), &b[..])).collect() } #[test] -fn a_sparse_merge_keeps_the_other_sides_out_of_view_deletion() { - check(Op::Merge); +fn a_sparse_completion_keeps_the_other_sides_out_of_view_deletion_and_lands_the_rest() { + for op in ops() { + let args = |sparse| { + run( + &tag(op, if sparse { "del-sparse" } else { "del-full" }), + op, + sparse, + &[ + BASE_A, + Edit::Write("docs/old.txt", b"v1\n"), + Edit::Write("docs/mod.txt", b"v1\n"), + Edit::Write("docs/ours.txt", b"v1\n"), + Edit::Write("docs/ours_del.txt", b"v1\n"), + ], + &[ + MAIN_A, + Edit::Write("docs/ours.txt", b"ours v2\n"), + Edit::Remove("docs/ours_del.txt"), + ], + &[ + FEAT_A, + Edit::Remove("docs/old.txt"), + Edit::Write("docs/mod.txt", b"theirs v2\n"), + Edit::Write("docs/new.txt", b"new\n"), + ], + ) + }; + let (full, sparse) = (args(false), args(true)); + assert!(!full.contains_key("docs/old.txt"), "reference: deleted"); + assert!( + !sparse.contains_key("docs/old.txt"), + "{op:?}: the other side's clean out-of-view deletion must not resurrect" + ); + let s = text(&sparse); + assert_eq!( + s["docs/mod.txt"], b"theirs v2\n", + "{op:?}: clean modify lands" + ); + assert_eq!(s["docs/new.txt"], b"new\n", "{op:?}: clean add lands"); + assert_eq!(s["docs/ours.txt"], b"ours v2\n", "{op:?}: ours' edit kept"); + assert!( + !s.contains_key("docs/ours_del.txt"), + "{op:?}: ours' delete kept" + ); + assert_eq!( + s, + text(&full), + "{op:?}: sparse completion equals the full one" + ); + } +} + +/// The other side turns the out-of-view file `docs/x` into a directory. The +/// parents used to carry `docs/x` back beside `docs/x/y`, a corrupt tree. +#[test] +fn a_sparse_completion_accepts_a_file_replaced_by_a_directory_out_of_view() { + for op in ops() { + let sparse = run( + &tag(op, "dir"), + op, + true, + &[BASE_A, Edit::Write("docs/x", b"file\n")], + &[MAIN_A], + &[ + FEAT_A, + Edit::Remove("docs/x"), + Edit::Write("docs/x/y", b"y\n"), + ], + ); + let s = text(&sparse); + assert!(!s.contains_key("docs/x"), "{op:?}: the file is gone"); + assert_eq!(s["docs/x/y"], b"y\n", "{op:?}: the directory's file landed"); + } } +/// The other side adds the exec bit to an out-of-view file. Compared apart +/// from the equality check: the non-sparse reference drops exec bits (#237). +#[cfg(unix)] #[test] -fn a_sparse_cherry_pick_keeps_the_picked_commits_out_of_view_deletion() { - check(Op::Pick); +fn a_sparse_completion_lands_an_out_of_view_mode_change() { + for op in ops() { + let sparse = run( + &tag(op, "mode"), + op, + true, + &[BASE_A, Edit::Write("docs/run.sh", b"#!/bin/sh\n")], + &[MAIN_A], + &[FEAT_A, Edit::Exec("docs/run.sh", b"#!/bin/sh\n")], + ); + assert_eq!(sparse["docs/run.sh"].1, FileMode::EXEC, "{op:?}"); + } } +/// The other side deletes a protected file while an unrelated conflict stops +/// the operation: the decided root dropped it, so completion must too. The +/// deletion is committed as a raw snapshot because a plain commit carries an +/// absent protected path forward. #[test] -fn a_sparse_rebase_continue_keeps_the_replayed_commits_out_of_view_deletion() { - check(Op::Rebase); +fn a_completion_keeps_the_other_sides_protected_deletion() { + for op in ops() { + let root = std::env::temp_dir().join(format!( + "scl-sparse-completion-{}-{}", + tag(op, "prot"), + std::process::id() + )); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = Repo::init(&root).unwrap(); + let (sk, pk) = scl_crypto::generate_keypair(); + repo.protect("secret/", &[pk], None).unwrap(); + apply(&root, &[BASE_A, Edit::Write("secret/a.txt", b"s\n")]); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + apply(&root, &[MAIN_A]); + repo.commit("me", "main").unwrap(); + + repo.switch_with_identity("feature", Some(&sk)).unwrap(); + let tip = repo.head_tip().unwrap().unwrap(); + let protection = repo.snapshot(&tip).unwrap().protection; + let tree = repo + .vfs() + .write_tree_with_perms(&[( + "src/a.txt".to_string(), + b"a\nFEAT\nc\n".to_vec(), + FileMode::FILE, + 0, + )]) + .unwrap(); + repo.commit_snapshot( + tree, + vec![tip], + Default::default(), + protection, + "me", + "feature deletes secret/a.txt", + ) + .unwrap(); + // Match the disk to that tree before leaving the branch. + apply(&root, &[FEAT_A, Edit::Remove("secret/a.txt")]); + if !matches!(op, Op::Rebase) { + repo.switch_with_identity("main", Some(&sk)).unwrap(); + } + let done = finish(&root, repo, op, Some(&sk), "src/a.txt"); + let s = text(&done); + assert!( + !s.contains_key("secret/a.txt"), + "{op:?}: the protected deletion must not be undone" + ); + assert_eq!(s["src/a.txt"], b"a\nFEAT\nc\n"); + } } diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 29dd829c..d44cdc53 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -168,14 +168,22 @@ every claim below is checked against the shipped code, not the plan. `MergeInProgress`/`PickInProgress`/`RebaseInProgress` errors the P21 policy-op guards already use elsewhere, added in review before Task 4 landed rather than discovered after. -6. **Completion carries out-of-view paths from the decided root, not the - parents (#238).** A stopped merge, cherry-pick or rebase on a full clone - records a decided root, and `snapshot_files` carries an absent out-of-view - path from it. Earlier builds also read the tip and merge head, so an - out-of-view file the other side had cleanly deleted came back from the - tip, silently losing the deletion. With a decided root the parents now - supply only still-protected paths; they remain the full fallback for - state written without a decided root. Pinned by - `sparse_completion.rs` (merge, pick, rebase: the deletion stays, a clean - add and modify land, ours-side-only out-of-view changes are kept, and the - tree equals the result without sparse). +6. **Completion carries from the decided root, not the parents (#238).** + A stopped merge, cherry-pick or rebase on a public branch records a + decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`); a merge into a + private branch writes none and completes through `commit_private`, which + this refinement does not cover. `snapshot_files` carries an absent + out-of-view or still-protected path from the decided root. Earlier builds + also read the tip and merge head, so a path the other side had cleanly + deleted (an out-of-view file, or a protected file) came back from the tip + and the deletion was silently lost; a file the other side turned into a + directory was carried beside it, a corrupt tree. With a decided root the + parents are no longer consulted: on a full clone it holds every surviving + entry (protected ones as ciphertext), and conflicted paths and sealing + inputs are on disk or in `CONFLICT_PLAINTEXT`. They stay the full fallback + for state written without a decided root. Pinned by `sparse_completion.rs` + (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). PR #236 + owns refinement 5; its "not always true today ... #238" sentence goes away + once both land.