diff --git a/CLAUDE.md b/CLAUDE.md index aa803119..2bbc91e9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -311,6 +311,19 @@ transport-adjacent. The rest, imperatively: `.sc/sparse-widen` (local-only like the rest, removed with the spec write or the op); abort and completion delete what an interrupted widen laid outside the view. +- **`materialize` deletes or overwrites a path only if it is the stop's own + file (ADR-0034 refinement 8, #255, #256).** A file the replaced tree tracks goes + only when the disk copy is a regular file with exactly the tracked bytes (an + edit, a replacement or a symlink survives); a path outside the sparse view is + never reported, since nothing is laid there and a copy is the user's. An + abort (`merge_abort`, `cherry_pick_abort`, `rebase_abort`; they return an + `AbortReport`) finishes anyway: it removes what the stop laid, leaves the + rest and lists it in `kept` (`sc` prints `left in place (changed since the + stop)`), and overwrites a file only if it is still the stopped tree's bytes or + the stop owns it (recorded plaintext, a conflicted path). Recorded plaintext (`CONFLICT_PLAINTEXT`, which now includes the + carried protected files a stop decrypted) and sidecars are removed whatever + their bytes, since an edit of them is the discarded resolution. Merge abort + compares against the decided root, not theirs' tree. - **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. A directory holding only tracked files is not such a case: a file/directory change at one path crosses switch, undo, abort and `--continue`, and the emptied directories are cleared (#247). `materialize` checks every such directory before its first removal, so a refusal changes nothing and an abort stays retryable; a forward op also refuses a user's empty directory there (it cannot tell it from a husk), while `materialize` itself prunes empty ones. (On a case-insensitive checkout, replacing `foo/a` with a file `Foo` is refused: the check does not follow the case alias. Fail-closed.) On a sparse checkout the same holds for a path the result adds @@ -331,7 +344,7 @@ truth — do not re-list items here); entries that name an issue mirror the open GitHub issues. Standing entries: `sc ws` workspace refresh (#215), a limit-aware `sc fetch` and an object-count clone limit (#217 remainder), the non-unix `.sc/local-key` ACL (#214), and the open -sparse, private-branch and abort edge cases (#241, #246, #247, #248). +sparse guard and user-file safety limits (#241, ADR-0034 refinement 8). ## Agent skills diff --git a/ROADMAP.md b/ROADMAP.md index 00d1c370..e61abe13 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -784,13 +784,15 @@ P28 review follow-ons, the port de-flake, the clippy allow-list) are recorded in agent may have open. Abandon and re-fork covers it meanwhile. - **Sparse untracked-file guard edge cases.** #241: on a case-insensitive filesystem the untracked-file guard can alias an old-view path with a newly - in-view one. #248: an untracked file at an out-of-view path that the other side adds wins - over the merge result at completion. -- **Abort and private-merge stop bugs.** #246: `--abort` of a merge stopped on a - private branch fails with `WrongKind`, and a binary conflict's `.theirs` - sidecar is committed into the sealed tree. #247: `--abort` of a stopped - merge, pick or rebase fails with `IsADirectory` when the decided root replaced - a file with a directory. + in-view one. +- **User-file safety limits (ADR-0034 refinement 8, ADR-0044).** A file whose + bytes equal the tracked or laid ones cannot be told from the stop's own + write, so it is removed. `merge --abort` of a private-branch merge, which + keeps no decided root, restores the tip over every path it tracks (a + post-stop edit of one, or a user file at a path the stop removed, is lost) + and treats a conflicted path as the stop's whatever its bytes. An abort with + no decided root (residue) removes nothing and does not say so. Private forward operations (switch, fast-forward, clean merge) + remove in-view paths on the dirty check alone, without a byte comparison. - **Network-Git same-remote edge cases** (ADR-0018's re-synthesis limitation across different Git repos). - **Richer signer trust models** beyond trusted-key lists (delegation, expiry) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index dad2688c..edce5426 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2220,6 +2220,17 @@ fn run_status(json: bool, identity: Option) -> Result<()> { Ok(()) } +/// Say what an abort did not restore: protected paths it has no key for, and +/// files it left because they are no longer what the stop laid (#255). +fn print_abort_report(report: &scl_repo::AbortReport) { + for path in &report.skipped { + eprintln!("skipped (no key): {path}"); + } + for path in &report.kept { + eprintln!("left in place (changed since the stop): {path}"); + } +} + fn run_merge( branch: Option, abort: bool, @@ -2234,11 +2245,9 @@ fn run_merge( } else { None }; - let skipped = repo.merge_abort_with_identity(sk.as_ref())?; + let report = repo.merge_abort_with_identity(sk.as_ref())?; println!("merge aborted; working tree restored"); - for path in &skipped { - eprintln!("skipped (no key): {path}"); - } + print_abort_report(&report); return Ok(()); } let branch = branch.ok_or_else(|| anyhow::anyhow!("merge: provide a branch or --abort"))?; @@ -2279,11 +2288,9 @@ fn run_cherry_pick( ) -> Result<()> { let repo = open_repo()?; if abort { - let skipped = repo.cherry_pick_abort()?; + let report = repo.cherry_pick_abort()?; println!("cherry-pick aborted; working tree restored"); - for path in &skipped { - eprintln!("skipped (no key): {path}"); - } + print_abort_report(&report); return Ok(()); } let refname = refname.ok_or_else(|| anyhow::anyhow!("cherry-pick needs a ref or --abort"))?; @@ -2323,11 +2330,9 @@ fn run_rebase( ) -> Result<()> { let repo = open_repo()?; if abort { - let skipped = repo.rebase_abort()?; + let report = repo.rebase_abort()?; println!("rebase aborted; working tree restored"); - for path in &skipped { - eprintln!("skipped (no key): {path}"); - } + print_abort_report(&report); return Ok(()); } // Soft-resolve like `run_merge`/`run_cherry_pick`: a missing identity file diff --git a/crates/core/src/object.rs b/crates/core/src/object.rs index a2904c9c..7582fa88 100644 --- a/crates/core/src/object.rs +++ b/crates/core/src/object.rs @@ -315,6 +315,26 @@ impl Object { Object::Blob(Arc::from(bytes.into().into_boxed_slice())) } + /// The id a blob with the bytes read from `reader` would have, computed + /// in fixed-size chunks so a big file is never held in memory. Equal to + /// `Object::blob(bytes).id()` (pinned by a test). + pub fn blob_id_of_reader(mut reader: impl std::io::Read) -> std::io::Result { + let mut hasher = blake3::Hasher::new(); + hasher.update(&[TAG_BLOB]); + let mut buf = [0_u8; 64 * 1024]; + loop { + match reader.read(&mut buf) { + Ok(0) => break, + Ok(n) => { + hasher.update(&buf[..n]); + } + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + Ok(ObjectId::from_bytes(*hasher.finalize().as_bytes())) + } + /// Bytes counted against the store's blob budget (0 for small resident /// kinds). Sealed payloads are blob-sized ciphertext and get the same /// budget/eviction treatment as plaintext blobs. @@ -798,6 +818,14 @@ impl<'a> Reader<'a> { mod tests { use super::*; + #[test] + fn blob_id_of_reader_matches_the_blob_id() { + for bytes in [vec![], b"hello".to_vec(), vec![7_u8; 200_000]] { + let streamed = Object::blob_id_of_reader(&bytes[..]).unwrap(); + assert_eq!(streamed, Object::blob(bytes).id()); + } + } + #[test] fn blob_roundtrip_and_dedup() { let a = Object::blob(b"hello".to_vec()); diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index b2d78d28..e3152403 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -60,6 +60,8 @@ pub mod textdiff; pub mod tls_pins; pub mod transcripts; pub mod transport; +#[cfg(test)] +mod user_file_safety; pub mod walfmt; pub mod wire; pub mod workspace; @@ -79,7 +81,7 @@ pub use promisor::{PackFilter, Promisor, Scope}; pub use protect_ops::PrefixRecipient; pub use remote::{RemoteConfig, RemoteKind}; pub use replay::{PickResult, RebaseResult}; -pub use repo::{Repo, Status}; +pub use repo::{AbortReport, Repo, Status}; pub use rewrap::RewrapReport; pub use scanner::ScanReport; pub use secrets::SecretInfo; diff --git a/crates/repo/src/partial_walks.rs b/crates/repo/src/partial_walks.rs index d5cb6f13..5a9aeef8 100644 --- a/crates/repo/src/partial_walks.rs +++ b/crates/repo/src/partial_walks.rs @@ -673,7 +673,8 @@ fn every_materializer_call_site_fills_or_is_exempt() { continue; } let call = t.contains("worktree::materialize(") - || t.contains("worktree::materialize_with_old_view("); + || t.contains("worktree::materialize_with_old_view(") + || t.contains("worktree::materialize_restore("); if !call { continue; } @@ -686,7 +687,7 @@ fn every_materializer_call_site_fills_or_is_exempt() { ); } } - assert!(checked >= 15, "the scan found only {checked} call sites"); + assert!(checked >= 14, "the scan found only {checked} call sites"); } /// A private branch cannot be checked out on a partial clone (sealing and diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 19cdb717..d9f99410 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -394,10 +394,25 @@ impl Repo { &target.keys().cloned().collect(), )?; } + // A path outside the view is never laid out, so a copy on disk is the + // user's own (#256). A failed removal is an error: the file may be + // decrypted plaintext that must not linger. + worktree::refuse_blocked_parents( + &self.layout.root, + target.keys().filter(|p| sparse.matches(p)), + &|c| old_paths.contains(c) && !target.contains_key(c), + )?; for p in old_paths { - if !target.contains_key(p) || !sparse.matches(p) { + if !target.contains_key(p) + && sparse.matches(p) + && !worktree::under_symlink(&self.layout.root, p) + { let full = worktree::safe_join(&self.layout.root, p)?; - let _ = std::fs::remove_file(full); + match std::fs::remove_file(full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } } } let mut skipped = Vec::new(); @@ -880,7 +895,10 @@ impl Repo { /// only the checkout needs restoring. It needs the identity (the tip is a /// manifest); a caller who cannot open the branch is refused before any /// write, leaving the stop intact for someone who can. - pub(crate) fn merge_abort_private(&self, identity: Option<&SecretKey>) -> Result> { + pub(crate) fn merge_abort_private( + &self, + identity: Option<&SecretKey>, + ) -> Result { let (branch, mid, manifest) = self .head_private()? .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; @@ -889,6 +907,10 @@ impl Repo { let theirs_id = crate::merge_state::read_merge_head(&self.layout)? .expect("in_progress is true but MERGE_HEAD is absent"); let sparse = self.sparse_spec()?; + let plaintext = crate::conflict_plaintext::read(&self.layout)?; + let conflicted: BTreeSet = crate::merge_state::read_conflicts(&self.layout)? + .into_iter() + .collect(); let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -896,13 +918,45 @@ impl Repo { let ours = self.get_inner_snapshot(&mut store, &ctx, &inner_tip)?; let theirs = store.get_snapshot(&theirs_id)?; let prior = self.inner_tree_files(&mut store, &ctx, ours.root)?; - // The stop wrote files from ours and from theirs, in view only; theirs' - // adds are not in the restored tree, so the removal pass must know - // them (as the public abort does by passing theirs' root). - let mut written: BTreeSet = prior.keys().cloned().collect(); - written - .extend(worktree::tree_file_entries_with_perms(&mut store, theirs.root)?.into_keys()); - written.retain(|p| sparse.matches(p)); + // The stop wrote files from theirs, in view only; those the restored + // tip does not track must go, but only the stop's own: a conflicted + // path or recorded plaintext, or a file that is still theirs' bytes. + // A stop keeps no decided root for a private branch (it would be + // unsealed), so a conflicted path cannot be compared by content. + // Anything else at such a path is the user's and is reported (#255). + let mut written: BTreeSet = prior + .keys() + .filter(|p| sparse.matches(p)) + .cloned() + .collect(); + let mut kept = Vec::new(); + for (p, (blob_id, _, perms)) in + worktree::tree_file_entries_with_perms(&mut store, theirs.root)? + { + if prior.contains_key(&p) || !sparse.matches(&p) { + continue; + } + let full = worktree::safe_join(&self.layout.root, &p)?; + // Not knowing what stands there must stop the abort before it + // clears the stop's records (decrypted text could linger). + let Some(meta) = worktree::lstat(&full)? else { + continue; + }; + let recorded = plaintext.contains(&p); + // The stop wrote regular files: a symlink there is the user's. + let ours = meta.is_file() + && !worktree::under_symlink(&self.layout.root, &p) + && if perms & PROTECTED != 0 { + recorded + } else { + recorded || conflicted.contains(&p) || worktree::disk_is_blob(&full, &blob_id) + }; + if ours { + written.insert(p); + } else { + kept.push(p); + } + } let skipped = self.materialize_private( "merge --abort", &mut store, @@ -917,7 +971,7 @@ impl Repo { // After the restore, so a failed restore leaves the sidecars to retry. self.remove_conflict_sidecars_unless(&mut |p| prior.contains_key(p))?; crate::merge_state::clear(&self.layout)?; - Ok(skipped) + Ok(crate::repo::AbortReport { skipped, kept }) } /// Build inner trees + snapshot from a decided file map, seal everything @@ -1522,6 +1576,21 @@ impl Repo { } } } + // Drop the prior-tip paths the merge deleted, with the removals + // above and before any conflict output is written: a failure here + // then leaves no markers without a merge state, and is never + // swallowed (completion would resurrect the file from disk). + for p in prior_paths.difference(&merged_paths) { + if !sparse.matches(p) { + continue; + } + let full = worktree::safe_join(&self.layout.root, p)?; + match std::fs::remove_file(full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + } // Record what is about to land on disk in plaintext, and the // sidecars, before writing it: completion and abort need both // (see `conflict_plaintext`). Nothing here is sealed, and it names @@ -1563,13 +1632,6 @@ impl Repo { let full = worktree::safe_join(&self.layout.root, name)?; std::fs::write(&full, bytes)?; } - for p in prior_paths.difference(&merged_paths) { - if !sparse.matches(p) { - continue; - } - let full = worktree::safe_join(&self.layout.root, p)?; - let _ = std::fs::remove_file(full); - } crate::merge_state::write(&self.layout, &theirs, &fm.conflicts, None)?; return Err(Error::MergeConflicts(fm.conflicts.len())); } diff --git a/crates/repo/src/private_abort.rs b/crates/repo/src/private_abort.rs index 247c4ad6..1036244a 100644 --- a/crates/repo/src/private_abort.rs +++ b/crates/repo/src/private_abort.rs @@ -156,6 +156,24 @@ fn abort_after_a_text_conflict_restores_the_private_tip() { f.finish(); } +#[test] +fn abort_keeps_a_file_of_theirs_the_user_changed_after_the_stop() { + // `m.txt` is theirs' add, laid by the stop. The user's edit of it is not + // the stop's file any more, and abort says it left it (#255). + let f = fixture("kept", true, false, false); + assert!(matches!( + f.repo.merge_into_private("main", "alice", f.id()), + Err(Error::MergeConflicts(1)) + )); + put(&f.root, "m.txt", b"mine\n"); + let report = f.repo.merge_abort_with_identity(f.id()).unwrap(); + f.assert_no_state(); + assert_eq!(report.kept, vec!["m.txt".to_string()]); + assert_eq!(read(&f.root, "m.txt"), "mine\n"); + assert_eq!(read(&f.root, "a.txt"), "private edit\n"); + f.finish(); +} + #[test] fn abort_after_a_binary_conflict_removes_the_sidecar() { let f = fixture("bin", false, true, false); diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index d503d9c5..27a8ee4e 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -619,11 +619,12 @@ impl Repo { /// moved, so abort is its own inverse; nothing to undo. Errors /// [`Error::InvalidArgument`] if no cherry-pick is in progress. /// - /// Returns the protected paths that could not be restored (P21): no - /// identity is available at abort time, so protected files in the - /// restored tree are skipped (left absent) rather than decrypted — - /// mirrors [`crate::repo::Repo::merge_abort`]'s contract exactly. - pub fn cherry_pick_abort(&self) -> Result> { + /// Returns an [`crate::repo::AbortReport`] (P21, #255): the protected + /// paths that could not be restored (no identity is available at abort + /// time, so they are skipped rather than decrypted) and the paths left in + /// place because the user changed them since the stop — mirrors + /// [`crate::repo::Repo::merge_abort`]'s contract exactly. + pub fn cherry_pick_abort(&self) -> Result { if !crate::pick_state::in_progress(&self.layout) { return Err(Error::InvalidArgument( "no cherry-pick in progress — nothing to abort".into(), @@ -631,7 +632,7 @@ impl Repo { } let decided = crate::pick_state::read_decided_root(&self.layout)?; let ours_tip = self.head_tip()?; - let mut skipped = Vec::new(); + let mut report = crate::repo::AbortReport::default(); let mut sidecar_roots: Vec = Vec::new(); if let Some(tip) = ours_tip { let snap = self.snapshot(&tip)?; @@ -643,29 +644,15 @@ impl Repo { // before anything is removed: a view gap must leave the stopped // pick intact and retryable (ADR-0049 decision 10). self.ensure_view(snap.root, &self.sparse_spec()?)?; - let store_arc = self.vfs().store(); - let mut store = store_arc.lock().unwrap(); - // No identity at abort time: nothing is decrypted, so `materialize` - // records nothing — pass `None`, no cache to open or save - // (P33 Task 7 item 7). - skipped = worktree::materialize( - &self.layout, - &mut store, - snap.root, - decided, - &snap.protection, - None, - &self.sparse_spec()?, - None, - )?; + report = self.restore_over_stop(snap.root, &snap.protection, decided)?; } if let Some(root) = sidecar_roots.first() { - self.remove_conflict_plaintext(*root)?; + report.keep_all(self.remove_conflict_plaintext(*root)?); } self.remove_conflict_sidecars(&sidecar_roots)?; self.remove_widen_residue(decided)?; crate::pick_state::clear(&self.layout)?; - Ok(skipped) + Ok(report) } /// Replay the current branch's commits onto `target`'s tip (rebase). @@ -1267,11 +1254,12 @@ impl Repo { /// ref ever moved. Errors [`Error::InvalidArgument`] if no rebase is in /// progress. /// - /// Returns the protected paths that could not be restored (P21): no - /// identity is available at abort time, so protected files in the - /// restored tree are skipped (left absent) rather than decrypted — - /// mirrors [`crate::repo::Repo::merge_abort`]'s contract exactly. - pub fn rebase_abort(&self) -> Result> { + /// Returns an [`crate::repo::AbortReport`] (P21, #255): the protected + /// paths that could not be restored (no identity is available at abort + /// time, so they are skipped rather than decrypted) and the paths left in + /// place because the user changed them since the stop — mirrors + /// [`crate::repo::Repo::merge_abort`]'s contract exactly. + pub fn rebase_abort(&self) -> Result { let Some(st) = crate::rebase_state::read(&self.layout)? else { return Err(Error::InvalidArgument( "no rebase in progress — nothing to abort".into(), @@ -1282,8 +1270,7 @@ impl Repo { // disk as the tree the working tree currently, actually reflects — // pass it as `old_root` so the deletion pass drops files the stop // pulled in from the target side (e.g. a target-only new file) that - // `original_tip` never had (P19 review fix, mirrors `merge_abort`'s - // `theirs_root`-as-`old_root` pattern in `crates/repo/src/repo.rs`). + // `original_tip` never had (P19 review fix, mirrors `merge_abort`). // Falls back to `None` (full clean materialize) only for residue // where the decided root is unexpectedly absent. let decided = crate::rebase_state::read_decided_root(&self.layout)?; @@ -1292,28 +1279,12 @@ impl Repo { // anything is removed: a view gap must leave the stopped rebase // intact and retryable (ADR-0049 decision 10). self.ensure_view(snap.root, &self.sparse_spec()?)?; - let skipped = { - let store_arc = self.vfs().store(); - let mut store = store_arc.lock().unwrap(); - // No identity at abort time: nothing is decrypted, so `materialize` - // records nothing — pass `None`, no cache to open or save - // (P33 Task 7 item 7). - worktree::materialize( - &self.layout, - &mut store, - snap.root, - decided, - &snap.protection, - None, - &self.sparse_spec()?, - None, - )? - }; - self.remove_conflict_plaintext(snap.root)?; + let mut report = self.restore_over_stop(snap.root, &snap.protection, decided)?; + report.keep_all(self.remove_conflict_plaintext(snap.root)?); self.remove_conflict_sidecars(&[snap.root])?; self.remove_widen_residue(decided)?; crate::rebase_state::clear(&self.layout)?; - Ok(skipped) + Ok(report) } } @@ -1717,7 +1688,6 @@ mod tests { assert_eq!(repo.head_tip().unwrap(), Some(main_tip)); let before_x = std::fs::read(root.join("x.txt")).unwrap(); - let before_stable = std::fs::read(root.join("stable.txt")).unwrap(); let ops_before = repo.oplog().unwrap().len(); let err = repo.cherry_pick("work", "me", None, None).unwrap_err(); @@ -1731,7 +1701,10 @@ mod tests { std::fs::write(root.join("x.txt"), b"garbage\n").unwrap(); std::fs::write(root.join("stable.txt"), b"also garbage\n").unwrap(); - repo.cherry_pick_abort().unwrap(); + let report = repo.cherry_pick_abort().unwrap(); + // The pick never touched stable.txt, so the later edit is the user's + // (#255): kept and reported, where x.txt (conflicted) is restored. + assert_eq!(report.kept, vec!["stable.txt".to_string()]); assert!(!repo.pick_in_progress(), "state cleared"); assert_eq!(repo.pick_head().unwrap(), None); @@ -1753,8 +1726,8 @@ mod tests { ); assert_eq!( std::fs::read(root.join("stable.txt")).unwrap(), - before_stable, - "stable.txt byte-identical" + b"also garbage\n", + "an edit of an untouched file survives" ); assert!( !root.join("new.txt").exists(), @@ -3569,8 +3542,8 @@ mod tests { remove_root(&root); } - /// A record naming another stop's decided root is stale: nothing is - /// removed on its account. + /// A record naming another stop's decided root is stale: it is ignored and + /// cleared with the stop. #[test] fn a_stale_widen_record_is_ignored() { let (root, repo) = merge_stopped_with_an_ours_only_file("sparse-widen-stale"); @@ -3583,7 +3556,11 @@ mod tests { let stale = record.replacen(&decided.to_hex(), &ObjectId::of(b"other").to_hex(), 1); std::fs::write(repo.layout().sparse_widen_path(), stale).unwrap(); repo.merge_abort().unwrap(); - assert!(root.join("docs/mine.md").exists(), "not ours to remove"); + // The record is ignored, but the file is an unchanged copy of a path + // the stop's decided tree tracks, so the byte rule removes it as it + // would any tracked out-of-view copy (#255). An edited one stays. + assert!(!root.join("docs/mine.md").exists()); + assert!(!repo.layout().sparse_widen_path().exists()); drop(repo); remove_root(&root); } @@ -6321,7 +6298,7 @@ mod tests { let err = repo.cherry_pick("work", "me", None, None).unwrap_err(); assert!(matches!(err, Error::PickConflicts(_)), "got {err:?}"); - let skipped = repo.cherry_pick_abort().unwrap(); + let skipped = repo.cherry_pick_abort().unwrap().skipped; assert_eq!( skipped, vec!["secret/a.txt".to_string()], @@ -6357,7 +6334,7 @@ mod tests { let outcome = repo.rebase("main", "me", None).unwrap(); assert!(matches!(outcome, RebaseResult::Stopped { .. })); - let skipped = repo.rebase_abort().unwrap(); + let skipped = repo.rebase_abort().unwrap().skipped; assert_eq!( skipped, vec!["secret/a.txt".to_string()], @@ -6390,7 +6367,6 @@ mod tests { let original_tip = repo.head_tip().unwrap().unwrap(); let before_x = std::fs::read(root.join("x.txt")).unwrap(); - let before_stable = std::fs::read(root.join("stable.txt")).unwrap(); let ops_before = repo.oplog().unwrap().len(); let outcome = repo.rebase("main", "me", None).unwrap(); @@ -6401,7 +6377,9 @@ mod tests { std::fs::write(root.join("x.txt"), b"garbage\n").unwrap(); std::fs::write(root.join("stable.txt"), b"also garbage\n").unwrap(); - repo.rebase_abort().unwrap(); + // stable.txt is untouched by the rebase: the later edit is the user's. + let report = repo.rebase_abort().unwrap(); + assert_eq!(report.kept, vec!["stable.txt".to_string()]); assert!(!repo.rebase_in_progress(), "state cleared"); assert!(repo.rebase_progress().unwrap().is_none()); @@ -6417,8 +6395,8 @@ mod tests { ); assert_eq!( std::fs::read(root.join("stable.txt")).unwrap(), - before_stable, - "stable.txt byte-identical" + b"also garbage\n", + "an edit of an untouched file survives" ); assert_eq!( repo.oplog().unwrap().len(), diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 697c7633..5923944b 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -34,6 +34,29 @@ type ProtectedFile = crate::protect::SealInput; /// Working-tree status against HEAD. pub type Status = Diff; +impl AbortReport { + /// Add paths left in place, keeping the list sorted and free of repeats. + pub(crate) fn keep_all(&mut self, paths: Vec) { + self.kept.extend(paths); + self.kept.sort(); + self.kept.dedup(); + } +} + +/// What an abort did not restore. The abort itself completes either way and +/// its state is cleared. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct AbortReport { + /// Protected paths left absent: no identity at abort time (or this + /// identity cannot read them). + pub skipped: Vec, + /// Paths left as the user has them, because they are not the stop's file + /// any more: a file the stop laid that was changed or replaced since (not + /// removed), or a file the user put where the restored tip has one (not + /// overwritten). The user decides what to do with each (#255). + pub kept: Vec, +} + /// A handle to an open persistent repo. Holds the single-writer lock for its /// lifetime. pub struct Repo { @@ -1964,6 +1987,7 @@ impl Repo { // the store lock. self.ensure_view(conflict_root, &sparse)?; self.refuse_untracked_overwrite(op, Some(old_root), conflict_root)?; + let decrypted: Vec; { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -1973,7 +1997,7 @@ impl Repo { // skipped content, so nothing is lost by not surfacing the list // here (the `Err`/`Stopped` return can't carry it). let mut cache = self.open_protected_cache()?; - let _skipped = worktree::materialize( + let skipped = worktree::materialize( &self.layout, &mut store, conflict_root, @@ -1986,14 +2010,30 @@ impl Repo { // Conflict materialization moves no branch ref; best-effort save // so cache trouble never aborts the conflict write-out (P33 Task 7). cache.save_best_effort(); + // A carried protected file the identity could read is now on disk + // in plaintext, though no tree records that: abort may delete it + // only because it is recorded (#255), like the private stop. + let skipped: std::collections::HashSet<&String> = skipped.iter().collect(); + decrypted = worktree::tree_file_entries_with_perms_sparse( + &mut store, + conflict_root, + &self.sparse_spec()?, + )? + .into_iter() + .filter(|(p, (_, _, perms))| perms & scl_core::PROTECTED != 0 && !skipped.contains(p)) + .map(|(p, _)| p) + .collect(); } - // Record what is about to be written in plaintext, before writing it: - // completion and abort need the list (see `conflict_plaintext`). + // Record the plaintext on disk: what is about to be written + // (`to_encrypt`, before writing it) and what `materialize` just + // decrypted. Completion and abort need the list (see + // `conflict_plaintext`). crate::conflict_plaintext::write( &self.layout, &to_encrypt .iter() .map(|(p, ..)| p.clone()) + .chain(decrypted) .collect::>(), )?; crate::conflict_plaintext::write_sidecars( @@ -2022,11 +2062,14 @@ impl Repo { /// protected content (`conflict_plaintext`), except a path the restored /// tree tracks as a plain file: that one is the user's own. Call it after /// the restore. A lookup that fails keeps the file: it would be a path the - /// restored tree may track. - pub(crate) fn remove_conflict_plaintext(&self, restored_root: ObjectId) -> Result<()> { + /// restored tree may track. Returns the recorded paths it left because + /// the user's symlink (or a symlinked parent) + /// stands there; a directory is reported by the restore itself. + pub(crate) fn remove_conflict_plaintext(&self, restored_root: ObjectId) -> Result> { let paths = crate::conflict_plaintext::read(&self.layout)?; + let mut retained = Vec::new(); if paths.is_empty() { - return Ok(()); + return Ok(retained); } let partial = self.promisor()?.is_some(); let store_arc = self.vfs.store(); @@ -2037,7 +2080,20 @@ impl Repo { Ok(Some((_, _, perms))) => perms & scl_core::PROTECTED != 0, Err(_) => false, }; - if remove { + // A regular file only: the stop wrote one, so a symlink there is + // the user's, and so is anything behind a symlinked parent. + let full = worktree::safe_join(&self.layout.root, &path)?; + // Not knowing what stands there must stop the abort before its + // state is cleared, like a failed removal. + let meta = worktree::lstat(&full)?; + let behind_link = worktree::under_symlink(&self.layout.root, &path); + let is_file = !behind_link && meta.as_ref().is_some_and(|m| m.is_file()); + if remove && meta.as_ref().is_some_and(|m| m.is_symlink() || behind_link) { + // The user's link where the stop wrote plaintext: left, and + // told to the caller. + retained.push(path.clone()); + } + if remove && is_file { // Only an already-absent file is fine: any other failure must // stop the abort before its state is cleared, or the decrypted // text stays on disk with nothing left to retry. @@ -2048,7 +2104,7 @@ impl Repo { } } } - Ok(()) + Ok(retained) } /// [`Repo::refuse_untracked_overwrite_vs`] against the tree `old_root` @@ -2412,13 +2468,14 @@ impl Repo { /// Abandon an in-progress merge: restore the working tree to the current tip /// and clear merge state. Errors if no merge is in progress. /// - /// Returns the protected paths that could not be restored. No identity is - /// available at abort time, so protected files are skipped (left absent) + /// The report lists the protected paths that could not be restored. No + /// identity is available at abort time, so protected files are skipped (left absent) /// rather than decrypted — that's expected; re-supply an identity via a - /// `switch_with_identity` back to the branch to materialize them. A merge - /// into a private branch cannot be aborted this way: it needs - /// [`Repo::merge_abort_with_identity`]. - pub fn merge_abort(&self) -> Result> { + /// `switch_with_identity` back to the branch to materialize them. It also + /// lists any path left in place because the user changed it since the stop + /// ([`AbortReport::kept`]). A merge into a private branch cannot be aborted + /// this way: it needs [`Repo::merge_abort_with_identity`]. + pub fn merge_abort(&self) -> Result { self.merge_abort_with_identity(None) } @@ -2429,7 +2486,7 @@ impl Repo { pub fn merge_abort_with_identity( &self, identity: Option<&scl_crypto::SecretKey>, - ) -> Result> { + ) -> Result { if !crate::merge_state::in_progress(&self.layout) { return Err(Error::InvalidArgument("no merge in progress".into())); } @@ -2446,45 +2503,69 @@ impl Repo { let ours_root = self.snapshot(&ours_id)?.root; self.ensure_view(ours_root, &self.sparse_spec()?)?; } - // Restore working tree to ours tip. Pass theirs' root as `old_root` so - // the deletion pass drops files the conflicted merge pulled in from - // theirs; materializing against ours==old would delete nothing. - let mut skipped = Vec::new(); + // Restore the working tree to ours tip. The tree the stop laid out is + // its decided root: the deletion pass must drop what it pulled in from + // theirs, and only that (theirs' own tree also holds paths the result + // deleted, which the stop never wrote). A stop without a decided root + // falls back to theirs' tree. + let mut report = AbortReport::default(); let mut sidecar_roots: Vec = Vec::new(); let ours_tip = self.head_tip()?; if let Some(ours_id) = ours_tip { - let store_arc = self.vfs.store(); - let mut store = store_arc.lock().unwrap(); - let ours_snap = store.get_snapshot(&ours_id)?; - let ours_root = ours_snap.root; - let ours_protection = ours_snap.protection; - let theirs_root = store.get_snapshot(&theirs_id)?.root; - sidecar_roots = vec![ours_root]; - // No identity at abort time: protected files in ours are skipped - // (not decrypted). With nothing decrypted, `materialize` records - // nothing into a cache, so pass `None` — no cache to open or save - // (P33 Task 7 item 7). + let ours_snap = self.snapshot(&ours_id)?; + sidecar_roots = vec![ours_snap.root]; + let decided = match crate::merge_state::read_decided_root(&self.layout)? { + Some(d) => d, + None => self.snapshot(&theirs_id)?.root, + }; // ensure_view ran above, before the lock and before any removal. - skipped = worktree::materialize( - &self.layout, - &mut store, - ours_root, - Some(theirs_root), - &ours_protection, - None, - &self.sparse_spec()?, - None, - )?; + report = + self.restore_over_stop(ours_snap.root, &ours_snap.protection, Some(decided))?; } if let Some(root) = sidecar_roots.first() { - self.remove_conflict_plaintext(*root)?; + report.keep_all(self.remove_conflict_plaintext(*root)?); } self.remove_conflict_sidecars(&sidecar_roots)?; // What an interrupted stopped-op widen laid, which the walk above - // (over theirs' tree) cannot see. + // (over the decided tree) cannot see. self.remove_widen_residue(crate::merge_state::read_decided_root(&self.layout)?)?; crate::merge_state::clear(&self.layout)?; - Ok(skipped) + Ok(report) + } + + /// Re-lay `tip_root` over the tree a stopped merge, pick or rebase left on + /// disk (`stopped`), for its abort: no identity, so nothing is decrypted + /// and no cache is touched (P33 Task 7 item 7). Deletes only what the + /// stop laid and nobody changed, and leaves the rest in the report (#255, + /// `worktree::materialize_restore`). The caller has already filled the + /// view (`ensure_view`). + pub(crate) fn restore_over_stop( + &self, + tip_root: ObjectId, + protection: &scl_core::Protection, + stopped: Option, + ) -> Result { + // What the stop owns outright: the plaintext it recorded and the + // paths it left conflicted. A user's edit of either is the resolution + // abort discards; an edit of any other file is theirs to keep. + let mut owned = crate::conflict_plaintext::read(&self.layout)?; + owned.extend(self.active_conflicts()?); + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + // ensure-view-exempt: every caller filled the view before the first removal. + let m = worktree::materialize_restore( + &self.layout, + &mut store, + tip_root, + stopped, + protection, + &self.sparse_spec()?, + &owned, + )?; + Ok(AbortReport { + skipped: m.skipped, + kept: m.kept, + }) } /// Snapshots from the current tip back through parents (newest first). @@ -2654,10 +2735,16 @@ impl Repo { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); let target_paths = worktree::tree_file_entries_with_perms(&mut store, target_root)?; + let sparse = self.sparse_spec()?; for p in &old_paths { - if !target_paths.contains_key(p) { + // Out of the view nothing was laid: a copy is the user's (#256). + if !target_paths.contains_key(p) && sparse.matches(p) { let full = worktree::safe_join(&self.layout.root, p)?; - let _ = std::fs::remove_file(full); + match std::fs::remove_file(full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } } } } diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 97375058..655ebed4 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -626,8 +626,8 @@ impl Repo { /// On completion this is what keeps the files out of the commit's input. /// On abort it only covers what the abort's own walk cannot see (an /// ours-only file, or a subtree never fetched for theirs): that walk - /// removes any out-of-view path theirs tracks regardless of bytes, by - /// design (abort restores ours). Returns whether a valid record existed. + /// follows the decided tree and applies the same byte rule (ADR-0034 + /// refinement 8). Returns whether a valid record existed. pub(crate) fn remove_widen_residue(&self, decided: Option) -> Result { Ok(self.remove_widen_residue_keeping(decided)?.is_some()) } diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs new file mode 100644 index 00000000..eadca58f --- /dev/null +++ b/crates/repo/src/user_file_safety.rs @@ -0,0 +1,509 @@ +//! A stop's checkout and an abort delete or overwrite a path only when it is +//! the stop's own file: tracked by the tree being replaced with exactly its +//! bytes, or recorded as written by the stop. Anything else is the user's and +//! is left in place (#255, #256). Pinned for merge, cherry-pick and rebase. + +use std::path::{Path, PathBuf}; + +use crate::replay::RebaseResult; +use crate::repo::{AbortReport, Repo}; + +#[derive(Clone, Copy, Debug)] +enum Op { + Merge, + Pick, + Rebase, +} + +const OPS: [Op; 3] = [Op::Merge, Op::Pick, Op::Rebase]; + +fn root_for(tag: &str, op: Op) -> PathBuf { + let root = std::env::temp_dir().join(format!( + "scl-user-file-safety-{tag}-{op:?}-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + root +} + +fn put(root: &Path, rel: &str, bytes: &[u8]) { + let full = root.join(rel); + std::fs::create_dir_all(full.parent().unwrap()).unwrap(); + std::fs::write(full, bytes).unwrap(); +} + +fn read(root: &Path, rel: &str) -> Vec { + std::fs::read(root.join(rel)).unwrap() +} + +fn stop(repo: &Repo, op: Op) { + match op { + Op::Merge => { + repo.merge("feature", "me").unwrap_err(); + } + Op::Pick => { + repo.cherry_pick("feature", "me", None, None).unwrap_err(); + } + Op::Rebase => assert!(matches!( + repo.rebase("main", "me", None).unwrap(), + RebaseResult::Stopped { .. } + )), + } +} + +fn abort(repo: &Repo, op: Op) -> AbortReport { + match op { + Op::Merge => repo.merge_abort(), + Op::Pick => repo.cherry_pick_abort(), + Op::Rebase => repo.rebase_abort(), + } + .unwrap() +} + +fn finish(root: &Path, repo: Repo) { + drop(repo); + std::fs::remove_dir_all(root).unwrap(); + assert!(!root.exists(), "test repository must leave no residue"); +} + +/// `x.txt` conflicts as text. `main` deletes `y.txt` and adds `main_only.txt`; +/// `feature` keeps `y.txt` and adds `added.txt`. Merge and pick run on `main` +/// (theirs = feature), rebase runs on `feature` (onto main). `view`, if set, +/// narrows to `src/` (which then holds the conflict as `src/x.txt`) before the +/// stop, which it runs, with `docs/*` tracked outside it. +fn stopped(tag: &str, op: Op, view: bool) -> (PathBuf, Repo) { + let root = root_for(tag, op); + let repo = Repo::init(&root).unwrap(); + let x = if view { "src/x.txt" } else { "x.txt" }; + put(&root, x, b"a\nb\nc\n"); + put(&root, "y.txt", b"y\n"); + put(&root, "docs/d.txt", b"d\n"); + put(&root, "docs/e.txt", b"e\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + put(&root, x, b"a\nMAIN\nc\n"); + std::fs::remove_file(root.join("y.txt")).unwrap(); + put(&root, "main_only.txt", b"from main\n"); + repo.commit("me", "main").unwrap(); + repo.switch("feature").unwrap(); + put(&root, x, b"a\nFEATURE\nc\n"); + put(&root, "added.txt", b"from feature\n"); + repo.commit("me", "feature").unwrap(); + if view { + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!( + !root.join("docs/d.txt").exists(), + "setup: docs/ is out of view" + ); + } + if !matches!(op, Op::Rebase) { + repo.switch("main").unwrap(); + } + stop(&repo, op); + (root, repo) +} + +/// The file the stop lays that the restored tip does not track. +fn laid(op: Op) -> &'static str { + match op { + Op::Merge | Op::Pick => "added.txt", + Op::Rebase => "main_only.txt", + } +} + +#[test] +fn abort_removes_the_files_the_stop_laid() { + for op in OPS { + let (root, repo) = stopped("laid", op, false); + assert!(root.join(laid(op)).exists(), "{op:?}: setup"); + let report = abort(&repo, op); + assert!(!root.join(laid(op)).exists(), "{op:?}: laid file survived"); + assert!(report.kept.is_empty(), "{op:?}: {report:?}"); + finish(&root, repo); + } +} + +#[test] +fn abort_keeps_a_stop_file_the_user_changed_and_says_so() { + for op in OPS { + let (root, repo) = stopped("edited", op, false); + put(&root, laid(op), b"MINE\n"); + let report = abort(&repo, op); + assert_eq!(read(&root, laid(op)), b"MINE\n", "{op:?}"); + assert_eq!(report.kept, vec![laid(op).to_string()], "{op:?}"); + finish(&root, repo); + } +} + +#[test] +fn abort_keeps_a_user_file_at_a_path_the_stop_never_laid() { + // `y.txt` is in theirs' tree (merge, pick) or the restored tip (rebase) + // but not in the stop's result: the user's new file there is not ours. + for op in OPS { + let (root, repo) = stopped("never", op, false); + assert!(!root.join("y.txt").exists(), "{op:?}: setup"); + put(&root, "y.txt", b"MINE\n"); + let report = abort(&repo, op); + assert_eq!(read(&root, "y.txt"), b"MINE\n", "{op:?}"); + // Rebase would have restored its own y.txt there; it says it did not. + let expect: Vec = if matches!(op, Op::Rebase) { + vec!["y.txt".into()] + } else { + vec![] + }; + assert_eq!(report.kept, expect, "{op:?}"); + finish(&root, repo); + } +} + +#[test] +fn abort_keeps_a_symlink_where_the_stop_laid_a_file() { + #[cfg(unix)] + for op in OPS { + let (root, repo) = stopped("symlink", op, false); + std::fs::remove_file(root.join(laid(op))).unwrap(); + std::fs::write(root.join("elsewhere"), b"from main\n").unwrap(); + std::os::unix::fs::symlink("elsewhere", root.join(laid(op))).unwrap(); + let report = abort(&repo, op); + assert!( + root.join(laid(op)).symlink_metadata().unwrap().is_symlink(), + "{op:?}" + ); + assert_eq!(report.kept, vec![laid(op).to_string()], "{op:?}"); + finish(&root, repo); + } +} + +#[test] +fn a_stop_keeps_a_local_copy_of_an_out_of_view_tracked_file() { + for op in OPS { + let (root, repo) = { + // The user restores local copies before the stop: one edited, one + // identical to the tip. They are tracked, so the stop's guard + // skips them. + let root = root_for("oov-stop", op); + let repo = Repo::init(&root).unwrap(); + put(&root, "src/x.txt", b"a\nb\nc\n"); + put(&root, "docs/d.txt", b"d\n"); + put(&root, "docs/e.txt", b"e\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + put(&root, "src/x.txt", b"a\nMAIN\nc\n"); + repo.commit("me", "main").unwrap(); + repo.switch("feature").unwrap(); + put(&root, "src/x.txt", b"a\nFEATURE\nc\n"); + repo.commit("me", "feature").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + if !matches!(op, Op::Rebase) { + repo.switch("main").unwrap(); + } + put(&root, "docs/d.txt", b"MINE\n"); + put(&root, "docs/e.txt", b"e\n"); + (root, repo) + }; + stop(&repo, op); + assert_eq!(read(&root, "docs/d.txt"), b"MINE\n", "{op:?}: stop"); + assert!( + !root.join("docs/e.txt").exists(), + "{op:?}: an unchanged copy goes as before" + ); + let report = abort(&repo, op); + assert_eq!(read(&root, "docs/d.txt"), b"MINE\n", "{op:?}: abort"); + assert!( + report.kept.is_empty(), + "{op:?}: out of view is not reported" + ); + finish(&root, repo); + } +} + +#[test] +fn an_edited_out_of_view_file_is_committed_as_the_users_edit() { + // Completion semantics stay (ADR-0034 refinement 5): the user's copy of a + // tracked out-of-view file is read like any tracked file. + for op in [Op::Merge, Op::Pick] { + let (root, repo) = stopped("oov-commit", op, true); + put(&root, "docs/d.txt", b"MINE\n"); + put(&root, "src/x.txt", b"a\nresolved\nc\n"); + repo.commit("me", "complete").unwrap(); + let tip = repo.head_tip().unwrap().unwrap(); + let root_id = repo.snapshot(&tip).unwrap().root; + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + let files = crate::worktree::tree_file_entries_with_perms(&mut store, root_id).unwrap(); + let (blob, ..) = files["docs/d.txt"]; + assert_eq!( + store.get(&blob).unwrap(), + scl_core::Object::blob(b"MINE\n".to_vec()), + "{op:?}" + ); + drop(store); + finish(&root, repo); + } +} + +/// `zz/a` is tracked; theirs replaces it with a file `zz`, and `x.txt` +/// conflicts. The stop removes `zz/a` and lays the file `zz`. +fn stopped_with_a_file_over_a_directory(tag: &str, op: Op) -> (PathBuf, Repo) { + let root = root_for(tag, op); + let repo = Repo::init(&root).unwrap(); + put(&root, "x.txt", b"a\nb\nc\n"); + put(&root, "zz/a", b"a\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + put(&root, "x.txt", b"a\nMAIN\nc\n"); + repo.commit("me", "main").unwrap(); + repo.switch("feature").unwrap(); + put(&root, "x.txt", b"a\nFEATURE\nc\n"); + std::fs::remove_dir_all(root.join("zz")).unwrap(); + put(&root, "zz", b"file\n"); + repo.commit("me", "feature").unwrap(); + repo.switch("main").unwrap(); + stop(&repo, op); + assert_eq!(read(&root, "zz"), b"file\n", "{op:?}: setup"); + (root, repo) +} + +#[test] +fn abort_refuses_before_any_write_when_a_kept_file_blocks_a_directory() { + for op in [Op::Merge, Op::Pick] { + let (root, repo) = stopped_with_a_file_over_a_directory("blocked", op); + put(&root, "zz", b"MINE\n"); + let markers = read(&root, "x.txt"); + let err = match op { + Op::Merge => repo.merge_abort(), + _ => repo.cherry_pick_abort(), + } + .unwrap_err(); + assert!( + matches!(&err, crate::Error::InvalidArgument(m) if m.contains("move it away")), + "{op:?}: {err:?}" + ); + assert_eq!(read(&root, "x.txt"), markers, "{op:?}: nothing written"); + assert_eq!(read(&root, "zz"), b"MINE\n", "{op:?}"); + // The stop is intact and the abort works once the file is moved. + std::fs::remove_file(root.join("zz")).unwrap(); + let report = abort(&repo, op); + assert!(report.kept.is_empty(), "{op:?}: {report:?}"); + assert_eq!(read(&root, "zz/a"), b"a\n", "{op:?}"); + finish(&root, repo); + } +} + +#[cfg(unix)] +#[test] +fn abort_never_writes_through_a_symlink_where_a_directory_must_be() { + let (root, repo) = stopped_with_a_file_over_a_directory("symlink-dir", Op::Merge); + let outside = root.with_extension("outside"); + std::fs::create_dir_all(&outside).unwrap(); + std::fs::remove_file(root.join("zz")).unwrap(); + std::os::unix::fs::symlink(&outside, root.join("zz")).unwrap(); + repo.merge_abort().unwrap_err(); + assert!(!outside.join("a").exists(), "wrote through the symlink"); + std::fs::remove_dir_all(&outside).unwrap(); + finish(&root, repo); +} + +/// A merge stopped with a protected `secret/new.txt` of theirs decrypted onto disk. +fn stopped_with_a_decrypted_file(tag: &str) -> (PathBuf, Repo) { + let (sk, pk) = scl_crypto::generate_keypair(); + let root = root_for(tag, Op::Merge); + let repo = Repo::init(&root).unwrap(); + repo.protect("secret/", &[pk], None).unwrap(); + put(&root, "x.txt", b"a\nb\nc\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + put(&root, "x.txt", b"a\nMAIN\nc\n"); + repo.commit("me", "main").unwrap(); + repo.switch_with_identity("feature", Some(&sk)).unwrap(); + put(&root, "x.txt", b"a\nFEATURE\nc\n"); + put(&root, "secret/new.txt", b"top secret\n"); + repo.commit("me", "feature").unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + repo.merge_with_identity("feature", "me", Some(&sk)) + .unwrap_err(); + assert_eq!(read(&root, "secret/new.txt"), b"top secret\n", "setup"); + assert!(crate::conflict_plaintext::read(repo.layout()) + .unwrap() + .contains("secret/new.txt")); + (root, repo) +} + +#[test] +fn abort_removes_a_protected_file_the_stop_decrypted_and_recorded() { + let (root, repo) = stopped_with_a_decrypted_file("protected"); + let report = repo.merge_abort().unwrap(); + assert!(!root.join("secret/new.txt").exists(), "plaintext survived"); + assert!(report.kept.is_empty(), "{report:?}"); + finish(&root, repo); +} + +#[cfg(unix)] +#[test] +fn abort_keeps_a_symlink_at_a_recorded_protected_path() { + let (root, repo) = stopped_with_a_decrypted_file("protected-symlink"); + std::fs::remove_file(root.join("secret/new.txt")).unwrap(); + std::fs::write(root.join("elsewhere"), b"x\n").unwrap(); + std::os::unix::fs::symlink("elsewhere", root.join("secret/new.txt")).unwrap(); + let report = repo.merge_abort().unwrap(); + assert!(root + .join("secret/new.txt") + .symlink_metadata() + .unwrap() + .is_symlink()); + assert_eq!(report.kept, vec!["secret/new.txt".to_string()]); + finish(&root, repo); +} + +#[test] +fn abort_keeps_an_edit_of_a_clean_file_and_discards_a_conflict_resolution() { + for op in OPS { + let (root, repo) = stopped("clean-edit", op, false); + put(&root, "docs/e.txt", b"MINE\n"); + put(&root, "x.txt", b"resolved\n"); + let report = abort(&repo, op); + assert_eq!(read(&root, "docs/e.txt"), b"MINE\n", "{op:?}"); + assert_eq!(report.kept, vec!["docs/e.txt".to_string()], "{op:?}"); + let tip = if matches!(op, Op::Rebase) { + "FEATURE" + } else { + "MAIN" + }; + assert_eq!( + read(&root, "x.txt"), + format!("a\n{tip}\nc\n").into_bytes(), + "{op:?}: the resolution of a conflict is discarded" + ); + finish(&root, repo); + } +} + +#[test] +fn abort_keeps_a_user_file_at_a_protected_path_the_stop_removed() { + let (sk, pk) = scl_crypto::generate_keypair(); + let root = root_for("protected-user", Op::Merge); + let repo = Repo::init(&root).unwrap(); + repo.protect("secret/", &[pk], None).unwrap(); + put(&root, "x.txt", b"a\nb\nc\n"); + put(&root, "secret/old.txt", b"old\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + put(&root, "x.txt", b"a\nMAIN\nc\n"); + repo.commit("me", "main").unwrap(); + repo.switch_with_identity("feature", Some(&sk)).unwrap(); + // A plain commit would carry the absent protected path forward, so the + // deletion is committed as a raw snapshot. + let tip = repo.head_tip().unwrap().unwrap(); + let protection = repo.snapshot(&tip).unwrap().protection; + let tree = repo + .vfs() + .write_tree_with_perms(&[( + "x.txt".to_string(), + b"a\nFEATURE\nc\n".to_vec(), + scl_core::FileMode::FILE, + 0, + )]) + .unwrap(); + repo.commit_snapshot( + tree, + vec![tip], + Default::default(), + protection, + "me", + "feature drops the secret", + ) + .unwrap(); + put(&root, "x.txt", b"a\nFEATURE\nc\n"); + std::fs::remove_file(root.join("secret/old.txt")).unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + repo.merge_with_identity("feature", "me", Some(&sk)) + .unwrap_err(); + assert!( + !root.join("secret/old.txt").exists(), + "setup: the stop removed it" + ); + put(&root, "secret/old.txt", b"mine\n"); + let report = repo.merge_abort().unwrap(); + assert_eq!(read(&root, "secret/old.txt"), b"mine\n"); + assert_eq!(report.kept, vec!["secret/old.txt".to_string()]); + finish(&root, repo); +} + +#[cfg(unix)] +#[test] +fn abort_never_writes_through_a_symlink_at_a_conflicted_path() { + for op in OPS { + let (root, repo) = stopped("conflict-symlink", op, false); + let victim = root.with_extension("victim"); + std::fs::write(&victim, b"precious\n").unwrap(); + std::fs::remove_file(root.join("x.txt")).unwrap(); + std::os::unix::fs::symlink(&victim, root.join("x.txt")).unwrap(); + let report = abort(&repo, op); + assert_eq!(std::fs::read(&victim).unwrap(), b"precious\n", "{op:?}"); + assert_eq!(report.kept, vec!["x.txt".to_string()], "{op:?}"); + std::fs::remove_file(&victim).unwrap(); + finish(&root, repo); + } +} + +#[test] +fn abort_discards_the_resolution_of_a_modify_delete_conflict() { + // Ours deleted `y.txt`, theirs changed it: the stop leaves theirs' file as + // the conflict. The user's edit of it is the resolution abort discards. + for op in [Op::Merge, Op::Pick] { + let root = root_for("modify-delete", op); + let repo = Repo::init(&root).unwrap(); + put(&root, "y.txt", b"y\n"); + put(&root, "x.txt", b"keep\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + std::fs::remove_file(root.join("y.txt")).unwrap(); + repo.commit("me", "main deletes y").unwrap(); + repo.switch("feature").unwrap(); + put(&root, "y.txt", b"y changed\n"); + repo.commit("me", "feature edits y").unwrap(); + repo.switch("main").unwrap(); + stop(&repo, op); + assert!(root.join("y.txt").exists(), "{op:?}: setup"); + put(&root, "y.txt", b"my resolution\n"); + let report = abort(&repo, op); + assert!(!root.join("y.txt").exists(), "{op:?}"); + assert!(report.kept.is_empty(), "{op:?}: {report:?}"); + finish(&root, repo); + } +} + +#[cfg(unix)] +#[test] +fn abort_never_deletes_through_a_symlinked_parent_directory() { + for op in [Op::Merge, Op::Pick] { + let root = root_for("link-parent", op); + let repo = Repo::init(&root).unwrap(); + put(&root, "d/y.txt", b"y\n"); + put(&root, "x.txt", b"keep\n"); + repo.commit("me", "base").unwrap(); + repo.branch("feature").unwrap(); + std::fs::remove_file(root.join("d/y.txt")).unwrap(); + repo.commit("me", "main deletes d/y").unwrap(); + repo.switch("feature").unwrap(); + put(&root, "d/y.txt", b"y changed\n"); + repo.commit("me", "feature edits d/y").unwrap(); + repo.switch("main").unwrap(); + stop(&repo, op); + let outside = root.with_extension("outside"); + std::fs::create_dir_all(&outside).unwrap(); + std::fs::write(outside.join("y.txt"), b"precious\n").unwrap(); + std::fs::remove_dir_all(root.join("d")).unwrap(); + std::os::unix::fs::symlink(&outside, root.join("d")).unwrap(); + let report = abort(&repo, op); + assert_eq!( + std::fs::read(outside.join("y.txt")).unwrap(), + b"precious\n", + "{op:?}" + ); + assert_eq!(report.kept, vec!["d/y.txt".to_string()], "{op:?}"); + std::fs::remove_dir_all(&outside).unwrap(); + finish(&root, repo); + } +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index c6336ffd..eeb6def3 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -509,24 +509,6 @@ pub(crate) fn tree_file_ids_sparse( Ok(out) } -/// Like [`tree_file_ids_sparse`], but skips a subtree the store lacks instead -/// of failing. For `materialize`'s old-root removal walk on a partial clone: -/// the old root was laid out under a preflighted view, so an absent subtree -/// lay outside that view and has nothing on disk to remove. A fetch can leave -/// a tree absent that is inside the promisor filter but outside the view -/// (ADR-0048). -fn tree_file_ids_sparse_present( - store: &mut Store, - root: ObjectId, - sparse: &Sparse, -) -> Result> { - let mut out = BTreeMap::new(); - if store.contains(&root) { - walk_tree_sparse(store, root, String::new(), sparse, &mut out, true)?; - } - Ok(out) -} - fn walk_tree_sparse( store: &mut Store, tree_id: ObjectId, @@ -828,6 +810,38 @@ pub(crate) fn added_view_files( Ok((plain, protected)) } +/// Whether `full` is a regular file whose bytes are exactly the blob `blob_id`: +/// the one test for "this file is still what the tree says, so it is ours to +/// delete or overwrite". A symlink, a directory, an unreadable file or any +/// other bytes read as no. One chunked read of the disk file and no store +/// access, so memory stays flat for a big file and it works when the blob is +/// not held (a partial clone gap). +pub(crate) fn disk_is_blob(full: &Path, blob_id: &ObjectId) -> bool { + disk_blob_id(full).as_ref() == Some(blob_id) +} + +/// The blob id of the regular file at `full`, hashed in chunks (a big file is +/// never held in memory); `None` for a symlink, a directory, a missing or +/// unreadable file. +fn disk_blob_id(full: &Path) -> Option { + if !full.symlink_metadata().ok()?.is_file() { + return None; + } + Object::blob_id_of_reader(std::fs::File::open(full).ok()?).ok() +} + +/// What [`materialize_restore`] did besides the writes. +#[derive(Debug, Default)] +pub(crate) struct Materialized { + /// Protected paths left absent: no key for them. + pub skipped: Vec, + /// In-view paths left as the user has them: a file the tree being replaced + /// tracked but whose bytes (or type) changed since, so it was not removed; + /// or, on a restore, a file standing where the restored tree needs one + /// that the replaced tree did not track, so it was not overwritten. + pub kept: Vec, +} + /// Of `paths`, those that exist in the tree at `root` as plain files and whose /// file on disk has exactly the blob's bytes: what an interrupted widen wrote /// and nobody changed since. A file that merely has the same bytes but was @@ -848,13 +862,7 @@ pub(crate) fn recorded_paths_laid( if perms & PROTECTED != 0 { continue; } - let Ok(Object::Blob(bytes)) = store.get(&blob_id) else { - continue; - }; - let full = safe_join(&layout.root, path)?; - // A symlink is never ours, and a read error reads as "not laid". - let is_file = full.symlink_metadata().is_ok_and(|m| m.is_file()); - if is_file && std::fs::read(&full).is_ok_and(|disk| disk[..] == bytes[..]) { + if disk_is_blob(&safe_join(&layout.root, path)?, &blob_id) { laid.insert(path.clone()); } } @@ -910,6 +918,80 @@ pub(crate) fn lay_out_added_view( Ok(protected) } +/// Whether a parent directory of the tracked path `rel` under `root` is a +/// symlink: a file reached through it lies outside the working tree, so it is +/// never ours to delete. Reads only. +pub(crate) fn under_symlink(root: &Path, rel: &str) -> bool { + let mut end = 0; + while let Some(i) = rel[end..].find('/') { + end += i; + if root + .join(&rel[..end]) + .symlink_metadata() + .is_ok_and(|m| m.is_symlink()) + { + return true; + } + end += 1; + } + false +} + +/// `symlink_metadata` that reads "nothing there" (also when a parent is a +/// file) as `None`, and surfaces any other failure: not knowing what stands +/// at a path must stop the caller before it clears state. +pub(crate) fn lstat(full: &Path) -> Result> { + match full.symlink_metadata() { + Ok(m) => Ok(Some(m)), + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory + ) => + { + Ok(None) + } + Err(e) => Err(e.into()), + } +} + +/// Refuse, before anything is written, a target path (`paths`) with a file or +/// symlink standing where one of its parent directories must be, unless +/// `removable` names it (the removal pass is about to delete it). Writing +/// through it would fail halfway (a file) or land outside the working tree +/// (a symlink to a directory). Reads only; each directory is looked at once. +pub(crate) fn refuse_blocked_parents<'a>( + root: &Path, + paths: impl Iterator, + removable: &dyn Fn(&str) -> bool, +) -> Result<()> { + let mut checked: BTreeSet<&str> = BTreeSet::new(); + for path in paths { + let mut end = 0; + while let Some(i) = path[end..].find('/') { + end += i; + let parent = &path[..end]; + end += 1; + if checked.contains(parent) { + continue; + } + let blocked = root + .join(parent) + .symlink_metadata() + .is_ok_and(|m| !m.is_dir()) + && !removable(parent); + if blocked { + return Err(Error::InvalidArgument(format!( + "cannot write {path}: {parent} is a file or symlink of yours where a \ + directory must be; move it away, then retry" + ))); + } + checked.insert(parent); + } + } + Ok(()) +} + /// Make room for a file at `full` (tracked path `path`) where a directory /// stands: the removal pass has deleted the tracked files under it, so what is /// left is a husk of empty directories, which goes. A directory that still @@ -1028,8 +1110,78 @@ pub(crate) fn materialize_with_old_view( identity: Option<&scl_crypto::SecretKey>, sparse: &Sparse, old_view: Option<&Sparse>, - mut cache: Option<&mut ProtectedCache>, + cache: Option<&mut ProtectedCache>, ) -> Result> { + Ok(materialize_impl( + layout, + store, + target_root, + old_root, + protection, + identity, + sparse, + old_view, + None, + cache, + )? + .skipped) +} + +/// [`materialize`] for an abort, which restores a tip over a stopped +/// operation's result (`old_root`, the tree the working tree reflects). The +/// stop's own files go and the rest stay: `owned` is what the stop recorded +/// writing in plaintext (`CONFLICT_PLAINTEXT`) plus its conflicted paths, +/// which cannot or need not be compared with a tree, so a protected path is +/// removed only if it is there and an owned file is overwritten whatever its +/// bytes. Any other in-view file is written only if it is still the replaced +/// tree's bytes (or absent): a file the user changed since stays. What it +/// left is in [`Materialized::kept`] (#255). +#[allow(clippy::too_many_arguments)] +pub(crate) fn materialize_restore( + layout: &Layout, + store: &mut Store, + target_root: ObjectId, + old_root: Option, + protection: &Protection, + sparse: &Sparse, + owned: &BTreeSet, +) -> Result { + materialize_impl( + layout, + store, + target_root, + old_root, + protection, + None, + sparse, + None, + Some(owned), + None, + ) +} + +/// The one implementation behind [`materialize`], [`materialize_with_old_view`] +/// and [`materialize_restore`] (`restore_owned` is `Some` for the last). +/// +/// A file the old tree tracks is removed only if the disk copy is still +/// exactly the tracked bytes (so an edit, a replacement or a symlink the user +/// put there survives). A protected path's plaintext cannot be compared +/// without a key: it goes only as a regular file and, on a restore, only if the +/// stop recorded it. A path outside the view is never reported: nothing is +/// laid there, so a copy is the user's own (#256). +#[allow(clippy::too_many_arguments)] +fn materialize_impl( + layout: &Layout, + store: &mut Store, + target_root: ObjectId, + old_root: Option, + protection: &Protection, + identity: Option<&scl_crypto::SecretKey>, + sparse: &Sparse, + old_view: Option<&Sparse>, + restore_owned: Option<&BTreeSet>, + mut cache: Option<&mut ProtectedCache>, +) -> Result { // Fail before the first write if the view's closure is not all present // (partial clones only; ADR-0048). The check lives here so every caller // is covered and none can forget it. @@ -1050,6 +1202,10 @@ pub(crate) fn materialize_with_old_view( // Set when an interrupted `sparse set`'s marker widened the removal bound; // cleared once this call has finished laying the tree out under `sparse`. let mut prev_marker_used = false; + let mut kept = Vec::new(); + // Paths the replaced tree tracks (within the removal walk's bound), with + // their blob ids. + let mut old_tracked: BTreeMap = BTreeMap::new(); if let Some(old) = old_root { // The old-root removal walk: bounded by the spec the old root was // laid out under (`old_view`, or `sparse` itself when the spec is @@ -1076,11 +1232,41 @@ pub(crate) fn materialize_with_old_view( None => (Sparse::default(), false), }; let old_files = if partial { - tree_file_ids_sparse_present(store, old, &old_walk_bound)? + tree_file_entries_with_perms_sparse_present(store, old, &old_walk_bound)? } else { - tree_file_ids_sparse(store, old, &old_walk_bound)? + tree_file_entries_with_perms_sparse(store, old, &old_walk_bound)? }; let gone = |p: &str| !target.contains_key(p) || !sparse.matches(p); + // Decide every removal before the first one: which gone files are + // still the tree's own bytes, and so ours to delete. + let mut removals: BTreeSet<&str> = BTreeSet::new(); + for (p, (blob_id, _, perms)) in old_files.iter().filter(|(p, _)| gone(p)) { + let full = safe_join(&layout.root, p)?; + if lstat(&full)?.is_none() { + continue; + } + let ours = !under_symlink(&layout.root, p) + && if perms & PROTECTED != 0 { + // Plaintext cannot be compared without a key, and leftover + // plaintext of a protected file must not linger: a forward op + // removes it, a restore only if the stop recorded it. Only a + // file is ever unlinked. + full.symlink_metadata().is_ok_and(|m| m.is_file()) + && restore_owned.is_none_or(|owned| owned.contains(p)) + } else { + // Still the tree's bytes, or a path the stop owns (recorded + // plaintext, a live conflict) where an edit is the resolution + // a restore discards. Only a regular file either way. + disk_is_blob(&full, blob_id) + || (restore_owned.is_some_and(|owned| owned.contains(p)) + && full.symlink_metadata().is_ok_and(|m| m.is_file())) + }; + if ours { + removals.insert(p); + } else if sparse.matches(p) { + kept.push(p.clone()); + } + } // Before the first removal: a directory a target file must replace // may hold only directories and files this pass is about to delete. // Anything else is the user's, and refusing now (rather than at the @@ -1088,7 +1274,7 @@ pub(crate) fn materialize_with_old_view( for path in target.keys().filter(|p| sparse.matches(p)) { let full = safe_join(&layout.root, path)?; if full.symlink_metadata().is_ok_and(|m| m.is_dir()) - && !dir_clearable(&full, path, &|c| old_files.contains_key(c) && gone(c)) + && !dir_clearable(&full, path, &|c| removals.contains(c)) { return Err(Error::InvalidArgument(format!( "cannot write {path}: a directory with untracked or ignored content stands \ @@ -1096,12 +1282,22 @@ pub(crate) fn materialize_with_old_view( ))); } } - for p in old_files.keys() { - if gone(p) { - let full = safe_join(&layout.root, p)?; - let _ = std::fs::remove_file(full); + refuse_blocked_parents( + &layout.root, + target.keys().filter(|p| sparse.matches(p)), + &|c| removals.contains(c), + )?; + for p in removals { + match std::fs::remove_file(safe_join(&layout.root, p)?) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), } } + old_tracked = old_files + .into_iter() + .map(|(p, (blob, ..))| (p, blob)) + .collect(); } let mut skipped = Vec::new(); for (path, (blob_id, mode, perms)) in &target { @@ -1109,6 +1305,29 @@ pub(crate) fn materialize_with_old_view( continue; } let full = safe_join(&layout.root, path)?; + // A restore overwrites a file only if it is still the replaced tree's + // (or already the restored one), or the stop recorded it or marked it + // conflicted, where an edit is the resolution abort discards. A + // plaintext protected file never equals a blob, so it needs the + // record. A directory is for `clear` to judge. + // A symlink is never the stop's (it writes regular files) and writing + // would follow it, out of the working tree. + if restore_owned.is_some() && full.symlink_metadata().is_ok_and(|m| m.is_symlink()) { + kept.push(path.clone()); + continue; + } + if let Some(owned) = restore_owned.filter(|_| old_root.is_some()) { + if !owned.contains(path) && full.symlink_metadata().is_ok_and(|m| !m.is_dir()) { + let disk = disk_blob_id(&full); + let is_stopped = old_tracked + .get(path) + .is_some_and(|old| disk.as_ref() == Some(old)); + if disk.as_ref() != Some(blob_id) && !is_stopped { + kept.push(path.clone()); + continue; + } + } + } let bytes = match store.get(blob_id)? { Object::Blob(b) => b, _ => continue, @@ -1179,7 +1398,8 @@ pub(crate) fn materialize_with_old_view( if prev_marker_used { crate::sparse::clear_prev(layout)?; } - Ok(skipped) + kept.sort(); + Ok(Materialized { skipped, kept }) } #[cfg(test)] @@ -1197,6 +1417,28 @@ mod tests { (layout, store) } + #[cfg(unix)] + #[test] + fn lstat_reads_a_file_parent_as_absent_and_under_symlink_stays_below_the_root() { + let root = std::env::temp_dir().join(format!("scl-repo-lstat-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(root.join("real")).unwrap(); + std::fs::write(root.join("file"), b"f").unwrap(); + std::os::unix::fs::symlink(root.join("real"), root.join("link")).unwrap(); + assert!(lstat(&root.join("file/child")).unwrap().is_none()); + assert!(lstat(&root.join("missing")).unwrap().is_none()); + assert!(lstat(&root.join("file")).unwrap().is_some()); + assert!(under_symlink(&root, "link/a.txt")); + assert!(!under_symlink(&root, "real/a.txt")); + // The root itself being reached through a link does not count. + let alias = root.with_extension("alias"); + std::os::unix::fs::symlink(&root, &alias).unwrap(); + assert!(!under_symlink(&alias, "real/a.txt")); + std::fs::remove_file(&alias).unwrap(); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + #[test] fn read_worktree_respects_scignore_but_keeps_tracked() { let root = std::env::temp_dir().join(format!("scl-repo-wt-ignore-{}", std::process::id())); diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index c3fcb313..5fac0879 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -196,8 +196,7 @@ every claim below is checked against the shipped code, not the plan. bytes, before the op's state is cleared: the abort's own walk follows the other side's tree and the restored view, so it cannot see an ours-only file or a subtree never fetched for theirs. A file the user edited since is - left by this cleanup; the abort's own walk still removes any out-of-view path theirs - tracks, whatever its bytes (abort restores ours). At completion a kept edited + left by this cleanup, and by the abort's own walk (refinement 8). At completion a kept edited file outside the view is, on a full clone, read like any tracked file and committed with the user's edit (intended: it is their change, and a sparse full clone reads tracked files wherever they sit); on a partial clone the commit is refused naming @@ -265,7 +264,7 @@ every claim below is checked against the shipped code, not the plan. 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), + copy of it is read as an edit; a stop's `materialize` leaves it, refinement 8), 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 @@ -273,3 +272,69 @@ every claim below is checked against the shipped code, not the plan. 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`. +8. **`materialize` and abort remove only what is provably the stop's (#255, + #256).** Two data-loss holes shared one cause: the removal pass deleted every + path the old tree tracked and the target did not, with no look at the disk. + On a sparse checkout that included every out-of-view path the old tree + tracks (on a full clone the walk is unbounded and the target is + view-filtered), so a user's local copy of one was deleted by any stop; on + abort it removed any in-view path theirs held that the tip did not, so a + file the user created there after the stop went too. One rule now: a path is + deleted or overwritten only if (a) the tree being replaced tracks it and the + disk file is a regular file with exactly those bytes (`worktree::disk_is_blob`: + one chunked read of the disk file, hashed with `Object::blob_id_of_reader`, so + memory stays flat for a big file; no store access and no size lookup, so it + also works on a partial clone's gap), or (b) the stop recorded it + (`CONFLICT_PLAINTEXT`, `CONFLICT_SIDECARS`, the `sparse-widen` journal) and it + still matches what was laid where that is checkable. A path that fails the + test is left alone: out of the view it is the user's copy and is not + reported (completion still reads it as an edit, refinement 5); in view, + `materialize_restore` lists it in `Materialized::kept`. The decision is made + for every candidate before the first removal, and the #247 directory + pre-check uses the same set, so a kept file under a directory a target file + must replace refuses atomically instead of failing mid-write. + Choice: an abort **completes and reports** rather than refusing, because it + must still restore the tip and drop the stop's state, and a refusal on a + kept resolution edit would leave the user unable to abort; `AbortReport` + (`skipped` for protected paths without a key, `kept` for these) is returned + by `merge_abort[_with_identity]`, `cherry_pick_abort` and `rebase_abort`, and + `sc` prints each kept path. The stop itself never refuses on this account + (the guards of refinement 7 and #250 still do). A restore also writes over a + file only if it is still the stopped tree's bytes (or already the restored + ones), or the stop owns it: recorded plaintext or a path left conflicted + (`active_conflicts`), where an edit is the resolution abort discards. So a + user's edit of an untouched file, or a file or symlink put at a path the + stopped tree lacked, is kept and reported. A conflict already resolved with + `sc resolve` leaves the conflict list and is reported as kept if its bytes + differ. + A kept file or symlink standing where a parent directory of a target path + must be (a stop laid a file `zz` over a tracked `zz/a`, and the user changed + it) is refused before any removal or write with a message naming it + (`worktree::refuse_blocked_parents`, also in `materialize_private`): writing + through it would fail halfway or, for a symlink to a directory, land outside + the working tree. A restore with no replaced tree (residue without a decided + root) skips the "not tracked by the stopped tree" check, which would + otherwise keep every differing tip path. + Details: `merge_abort` now takes the stop's decided root as the replaced + tree (theirs' tree also holds paths the result deleted, which the stop never + wrote), falling back to theirs only without one. A protected old path is + plaintext and cannot be compared without a key: forward operations (already + dirty-checked) still remove it, a restore only if recorded, which is why the + public stop now records the carried protected files it decrypted, as the + private stop already did. Recorded plaintext, a path left conflicted + (a modify/delete conflict's surviving file included) and sidecars are + removed without a byte check, as regular files only (a symlink or directory + there is kept and reported, including by `remove_conflict_plaintext`): the user's edit of them is the resolution abort + discards. Recording the decrypted files also changes completion: a recorded protected + path the user deleted during the stop is committed as deleted instead of + being carried back from a parent (the rule plain files already followed). + Only a regular file is unlinked as protected plaintext. A forward op still + removes a user's plaintext copy at an out-of-view protected tracked path + (leftover plaintext of a protected file must not linger; `sparse set` and + `switch` do the same), a stated limit. A stop recorded by an older + build lacks the decrypted entries, so aborting it keeps such a file + (reported as kept); delete it by hand. The private-to-public switch's own + removal skips out-of-view paths and surfaces unlink errors too. Limits: a user file whose bytes equal the tracked or laid ones is + indistinguishable from the stop's own write and goes; a failed unlink other + than "not found" is now an error instead of being swallowed. Pinned by + `user_file_safety.rs` (merge, pick, rebase) and `private_abort.rs`. diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index bf55e776..c408a8c8 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -417,9 +417,11 @@ sidecar into the tree and left it on disk. Now: gets `PrivateNoAccess` before any write and the stop stays for someone who can finish it. It then restores the private tip through the same checkout path as `switch` (decrypting protected files the identity can read, skipping - the rest), removes every in-view path theirs' tree holds that the tip does not (as - the public abort does with theirs' root, so an untracked user file at such a - path goes too), removes the recorded sidecars unless the tip tracks that name, and clears the merge + the rest), removes the in-view paths theirs' tree holds that the tip does not and + that are the stop's own (a conflicted path, a recorded plaintext path, or a + file whose bytes still equal theirs' blob); any other file there is the + user's, is left, and is returned in `AbortReport::kept` (#255, ADR-0034 + refinement 8), removes the recorded sidecars unless the tip tracks that name, and clears the merge state. The ref never moved and nothing was stored outside the sealed manifest, so there is nothing to undo at rest. It does not re-derive the three-way (that can fail for a recipient who cannot read a protected path @@ -438,6 +440,16 @@ sidecar into the tree and left it on disk. Now: exists on disk, before any write, since abort and completion would otherwise delete a file the user owns. +A failed removal of such a file is an error, not ignored (#255). Limits: the +private stop keeps no decided root and no record of the bytes it laid, so a +conflicted path cannot be compared by content and counts as the stop's, and the +restore writes the tip over every path the tip tracks: a post-stop edit of one +(or a user file at a tip path the stop removed) is lost, unlike the public +abort. Closing that needs the stop to record what it laid without putting +content ids in unsealed state. The stop's removal of a prior-tip path the +merge deleted now runs before any conflict output and surfaces its error. Out-of-view paths are never removed by +`materialize_private` (#256). + Pinned by `private_abort.rs` and `crates/cli/tests/merge_exit.rs`. ## Deferred (to ROADMAP.md)