From 63c6bde8ba8f448253bfd5874091e19f7105ff01 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 20:21:36 +0300 Subject: [PATCH 1/9] fix(repo): delete or overwrite only the stop's own files on materialize and abort (ref #255 #256) materialize's 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 removed a user's local copy of any out-of-view tracked file; on abort it removed a file the user created after the stop at a path theirs held. A path is now removed only when the disk copy is a regular file with exactly the tracked bytes, or the stop recorded it. Out-of-view copies are left and not reported; in-view ones are listed in the new AbortReport.kept, which sc prints. A restore no longer overwrites a user file at a tip path the stopped tree did not track. merge --abort compares against the decided root, the public stop records the protected files it decrypted, and a failed unlink on the private paths is an error. --- CLAUDE.md | 15 +- ROADMAP.md | 15 +- crates/cli/src/main.rs | 29 ++- crates/repo/src/lib.rs | 4 +- crates/repo/src/partial_walks.rs | 5 +- crates/repo/src/private.rs | 69 +++++- crates/repo/src/private_abort.rs | 18 ++ crates/repo/src/replay.rs | 79 +++---- crates/repo/src/repo.rs | 117 +++++++--- crates/repo/src/sparse.rs | 4 +- crates/repo/src/user_file_safety.rs | 244 +++++++++++++++++++++ crates/repo/src/worktree.rs | 173 ++++++++++++--- docs/adr/0034-sparse-checkouts.md | 46 +++- docs/adr/0044-per-branch-access-control.md | 14 +- 14 files changed, 669 insertions(+), 163 deletions(-) create mode 100644 crates/repo/src/user_file_safety.rs diff --git a/CLAUDE.md b/CLAUDE.md index bb2bde70..eb9efbd4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -287,6 +287,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 does not overwrite a user file at a tip path the stop's tree + lacked. 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 @@ -307,7 +320,7 @@ truth — do not re-list items here); entries that name an issue mirror the open GitHub issues. Standing entries: named/concurrent `sc ws` sessions (#215), blob-size clone filters (#217), bucket compaction and gc (#216), the non-unix `.sc/local-key` ACL (#214), transcript-drop delivery gaps (#230), 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 c21a54c4..70b4a0eb 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -759,13 +759,14 @@ P28 review follow-ons, the port de-flake, the clippy allow-list) are recorded in later; both are documented limits, not regressions. - **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, still overwrites a user file at a path the stop + removed from the tip, and treats a conflicted path as the stop's whatever + its bytes. 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 26269204..6227627b 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2173,6 +2173,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, @@ -2187,11 +2198,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"))?; @@ -2232,11 +2241,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"))?; @@ -2276,11 +2283,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/repo/src/lib.rs b/crates/repo/src/lib.rs index 18c4202d..ead6ec58 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -58,6 +58,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; @@ -75,7 +77,7 @@ pub use promisor::Promisor; 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 01e77601..dd88a9dd 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 aba30038..a6282412 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -394,10 +394,17 @@ 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. for p in old_paths { - if !target.contains_key(p) || !sparse.matches(p) { + if !target.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()), + } } } let mut skipped = Vec::new(); @@ -880,7 +887,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 +899,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 +910,40 @@ 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)?; + if full.symlink_metadata().is_err() { + continue; + } + let recorded = plaintext.contains(&p); + let ours = 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 +958,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 @@ -1568,7 +1609,11 @@ impl Repo { continue; } 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()), + } } 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..3cd64e80 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,21 +644,7 @@ 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)?; @@ -665,7 +652,7 @@ impl Repo { 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, - )? - }; + let report = self.restore_over_stop(snap.root, &snap.protection, decided)?; 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) } } @@ -3569,8 +3540,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 +3554,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 +6296,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 +6332,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()], diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index e5b35b67..72716c80 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -34,6 +34,20 @@ type ProtectedFile = crate::protect::SealInput; /// Working-tree status against HEAD. pub type Status = Diff; +/// 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 { @@ -1961,6 +1975,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(); @@ -1970,7 +1985,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, @@ -1983,6 +1998,18 @@ 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. + 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`). @@ -1991,6 +2018,7 @@ impl Repo { &to_encrypt .iter() .map(|(p, ..)| p.clone()) + .chain(decrypted) .collect::>(), )?; crate::conflict_plaintext::write_sidecars( @@ -2409,13 +2437,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) } @@ -2426,7 +2455,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())); } @@ -2443,35 +2472,24 @@ 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)?; @@ -2481,7 +2499,38 @@ impl Repo { // (over theirs' 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 { + let owned = crate::conflict_plaintext::read(&self.layout)?; + 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). diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index f6bee3a2..f030399b 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -623,8 +623,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..ac03dc66 --- /dev/null +++ b/crates/repo/src/user_file_safety.rs @@ -0,0 +1,244 @@ +//! 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); + } +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index d1c31169..843b0789 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, @@ -816,6 +798,29 @@ 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 read of the disk file and no store access, so +/// it is cheap for a big file and works when the blob is not held (a partial +/// clone gap). +pub(crate) fn disk_is_blob(full: &Path, blob_id: &ObjectId) -> bool { + full.symlink_metadata().is_ok_and(|m| m.is_file()) + && std::fs::read(full).is_ok_and(|disk| Object::blob(disk).id() == *blob_id) +} + +/// 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 @@ -836,13 +841,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()); } } @@ -1016,8 +1015,74 @@ 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`), which cannot be compared with +/// a tree, so a protected path is removed only if it is there. An in-view +/// path the restored tree needs but the replaced tree did not track is +/// written only if nothing of the user's stands there. 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 rule for removing a file the old tree tracks: it goes only if the +/// disk copy is still exactly the tracked bytes (so an edit, a replacement or +/// a symlink the user put there survives), or, for a protected path (whose +/// plaintext cannot be compared without a key), if the caller is not +/// restoring or 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. @@ -1038,6 +1103,9 @@ 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). + let mut old_tracked: BTreeSet = BTreeSet::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 @@ -1064,11 +1132,30 @@ 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 full.symlink_metadata().is_err() { + continue; + } + let ours = if perms & PROTECTED != 0 { + restore_owned.is_none_or(|owned| owned.contains(p)) + } else { + disk_is_blob(&full, blob_id) + }; + 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 @@ -1076,7 +1163,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 \ @@ -1084,12 +1171,14 @@ 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); + 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_keys().collect(); } let mut skipped = Vec::new(); for (path, (blob_id, mode, perms)) in &target { @@ -1097,6 +1186,21 @@ pub(crate) fn materialize_with_old_view( continue; } let full = safe_join(&layout.root, path)?; + // A restore overwrites what the replaced tree tracked (the stop's + // markers, or a file the user edited, are discarded by design) and + // what the stop recorded, never a file or symlink of the user's at a + // path neither covers. A directory is for `clear` to judge. + if let Some(owned) = restore_owned { + if perms & PROTECTED == 0 + && !old_tracked.contains(path) + && !owned.contains(path) + && full.symlink_metadata().is_ok_and(|m| !m.is_dir()) + && !disk_is_blob(&full, blob_id) + { + kept.push(path.clone()); + continue; + } + } let bytes = match store.get(blob_id)? { Object::Blob(b) => b, _ => continue, @@ -1167,7 +1271,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)] diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index c3fcb313..66a29627 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,44 @@ 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 read of the disk file, hashed, 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 does not write + over a file or symlink of the user's at a path the restored tree needs but + the stopped tree did not track and the stop did not record. + 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 and sidecars are removed + without a byte check: the user's edit of them is the resolution abort + discards. 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..090ad5ad 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,12 @@ 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, so a conflicted path cannot be compared by +content and counts as the stop's, and a user file at a tip path the stop +removed is overwritten by the restore. 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) From 2e026b4a704380e8a6fa254a51c5017252ee2c67 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 20:52:14 +0300 Subject: [PATCH 2/9] fix(repo): refuse an abort whose kept file blocks a directory, cover protected cases (ref #255 #256) Review found a kept file or symlink standing where a parent directory of a restored path must be made the abort fail halfway or write through the symlink. Refuse before any write instead, in materialize and the private restore. A restore with no replaced tree no longer keeps every differing path. Protected plaintext is unlinked only as a regular file, and the private-to-public switch skips out-of-view paths and surfaces unlink errors. Document the completion effect of recording decrypted files. --- crates/repo/src/private.rs | 5 ++ crates/repo/src/repo.rs | 18 ++++-- crates/repo/src/user_file_safety.rs | 90 +++++++++++++++++++++++++++++ crates/repo/src/worktree.rs | 69 ++++++++++++++++++---- docs/adr/0034-sparse-checkouts.md | 19 +++++- 5 files changed, 185 insertions(+), 16 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index a6282412..b27a1acc 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -397,6 +397,11 @@ impl Repo { // 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) { let full = worktree::safe_join(&self.layout.root, p)?; diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 72716c80..c231a752 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2011,8 +2011,10 @@ impl Repo { .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 @@ -2496,7 +2498,7 @@ impl Repo { } 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(report) @@ -2700,10 +2702,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/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index ac03dc66..70e36fb6 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -242,3 +242,93 @@ fn an_edited_out_of_view_file_is_committed_as_the_users_edit() { 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); +} + +#[test] +fn abort_removes_a_protected_file_the_stop_decrypted_and_recorded() { + let (sk, pk) = scl_crypto::generate_keypair(); + let root = root_for("protected", 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")); + let report = repo.merge_abort().unwrap(); + assert!(!root.join("secret/new.txt").exists(), "plaintext survived"); + assert!(report.kept.is_empty(), "{report:?}"); + finish(&root, repo); +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 843b0789..deffe4c6 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -897,6 +897,43 @@ pub(crate) fn lay_out_added_view( Ok(protected) } +/// 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 @@ -1064,12 +1101,15 @@ pub(crate) fn materialize_restore( ) } -// The rule for removing a file the old tree tracks: it goes only if the -/// disk copy is still exactly the tracked bytes (so an edit, a replacement or -/// a symlink the user put there survives), or, for a protected path (whose -/// plaintext cannot be compared without a key), if the caller is not -/// restoring or 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). +/// 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, @@ -1146,7 +1186,12 @@ fn materialize_impl( continue; } let ours = if perms & PROTECTED != 0 { - restore_owned.is_none_or(|owned| owned.contains(p)) + // 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 { disk_is_blob(&full, blob_id) }; @@ -1171,6 +1216,11 @@ fn materialize_impl( ))); } } + 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(()) => {} @@ -1190,9 +1240,8 @@ fn materialize_impl( // markers, or a file the user edited, are discarded by design) and // what the stop recorded, never a file or symlink of the user's at a // path neither covers. A directory is for `clear` to judge. - if let Some(owned) = restore_owned { - if perms & PROTECTED == 0 - && !old_tracked.contains(path) + if let Some(owned) = restore_owned.filter(|_| old_root.is_some()) { + if !old_tracked.contains(path) && !owned.contains(path) && full.symlink_metadata().is_ok_and(|m| !m.is_dir()) && !disk_is_blob(&full, blob_id) diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 66a29627..7a2c5fcd 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -301,6 +301,14 @@ every claim below is checked against the shipped code, not the plan. (the guards of refinement 7 and #250 still do). A restore also does not write over a file or symlink of the user's at a path the restored tree needs but the stopped tree did not track and the stop did not record. + 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 @@ -309,7 +317,16 @@ every claim below is checked against the shipped code, not the plan. public stop now records the carried protected files it decrypted, as the private stop already did. Recorded plaintext and sidecars are removed without a byte check: the user's edit of them is the resolution abort - discards. Limits: a user file whose bytes equal the tracked or laid ones is + 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`. From 80298303f3854b497d1a1169857b736c354a5122 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 21:11:11 +0300 Subject: [PATCH 3/9] fix(repo): abort overwrites only the stopped tree's own bytes; stream the blob hash (ref #255 #256) Review (Codex, code-reviewer): a restore overwrote a user's edit of a file the stop never touched, and a protected user file at a path the stop removed. It now writes over a file only if it is still the stopped tree's bytes or the stop owns it (recorded plaintext, a conflicted path). disk_is_blob hashes in chunks (Object::blob_id_of_reader) instead of loading the file. The private stop's best-effort unlink is restored: an error there would leave conflict files with no merge state. --- CLAUDE.md | 4 +- crates/core/src/object.rs | 28 +++++++++++ crates/repo/src/private.rs | 8 ++-- crates/repo/src/replay.rs | 19 ++++---- crates/repo/src/repo.rs | 6 ++- crates/repo/src/user_file_safety.rs | 74 +++++++++++++++++++++++++++++ crates/repo/src/worktree.rs | 65 +++++++++++++++---------- docs/adr/0034-sparse-checkouts.md | 14 ++++-- 8 files changed, 174 insertions(+), 44 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index eb9efbd4..62afba77 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -295,8 +295,8 @@ transport-adjacent. The rest, imperatively: 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 does not overwrite a user file at a tip path the stop's tree - lacked. Recorded plaintext (`CONFLICT_PLAINTEXT`, which now includes 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. 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/private.rs b/crates/repo/src/private.rs index b27a1acc..4a10fe51 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -1614,11 +1614,9 @@ impl Repo { 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()), - } + // Best effort, as before: an error here would leave the + // conflict files written above with no merge state to abort. + 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/replay.rs b/crates/repo/src/replay.rs index 3cd64e80..09ab5b76 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -1688,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(); @@ -1702,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); @@ -1724,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(), @@ -6365,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(); @@ -6376,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()); @@ -6392,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 c231a752..3e0689be 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2516,7 +2516,11 @@ impl Repo { protection: &scl_core::Protection, stopped: Option, ) -> Result { - let owned = crate::conflict_plaintext::read(&self.layout)?; + // 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. diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index 70e36fb6..195ec767 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -332,3 +332,77 @@ fn abort_removes_a_protected_file_the_stop_decrypted_and_recorded() { assert!(report.kept.is_empty(), "{report:?}"); 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); +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index deffe4c6..b895c19f 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -801,12 +801,21 @@ pub(crate) fn added_view_files( /// 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 read of the disk file and no store access, so -/// it is cheap for a big file and works when the blob is not held (a partial -/// clone gap). +/// 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 { - full.symlink_metadata().is_ok_and(|m| m.is_file()) - && std::fs::read(full).is_ok_and(|disk| Object::blob(disk).id() == *blob_id) + 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. @@ -1072,11 +1081,12 @@ pub(crate) fn materialize_with_old_view( /// [`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`), which cannot be compared with -/// a tree, so a protected path is removed only if it is there. An in-view -/// path the restored tree needs but the replaced tree did not track is -/// written only if nothing of the user's stands there. What it left is in -/// [`Materialized::kept`] (#255). +/// 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, @@ -1144,8 +1154,9 @@ fn materialize_impl( // 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). - let mut old_tracked: BTreeSet = BTreeSet::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 @@ -1228,7 +1239,10 @@ fn materialize_impl( Err(e) => return Err(e.into()), } } - old_tracked = old_files.into_keys().collect(); + 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 { @@ -1236,18 +1250,21 @@ fn materialize_impl( continue; } let full = safe_join(&layout.root, path)?; - // A restore overwrites what the replaced tree tracked (the stop's - // markers, or a file the user edited, are discarded by design) and - // what the stop recorded, never a file or symlink of the user's at a - // path neither covers. A directory is for `clear` to judge. + // 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. if let Some(owned) = restore_owned.filter(|_| old_root.is_some()) { - if !old_tracked.contains(path) - && !owned.contains(path) - && full.symlink_metadata().is_ok_and(|m| !m.is_dir()) - && !disk_is_blob(&full, blob_id) - { - kept.push(path.clone()); - continue; + 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)? { diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 7a2c5fcd..d52c632a 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -282,7 +282,8 @@ every claim below is checked against the shipped code, not the plan. 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 read of the disk file, hashed, no store access and no size lookup, so it + 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 @@ -298,9 +299,14 @@ every claim below is checked against the shipped code, not the plan. (`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 does not write - over a file or symlink of the user's at a path the restored tree needs but - the stopped tree did not track and the stop did not record. + (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 From bdef9cc60eb2f97c93305a11e5ea738ba5660ef2 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 21:40:58 +0300 Subject: [PATCH 4/9] fix(repo): keep a symlink at a recorded protected path; index skipped set (ref #255 #256) --- crates/repo/src/repo.rs | 8 +++++++- crates/repo/src/user_file_safety.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 3e0689be..aa0c2451 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2001,6 +2001,7 @@ impl Repo { // 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, @@ -2064,7 +2065,12 @@ 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 (a restore reports it as kept). + let is_file = worktree::safe_join(&self.layout.root, &path)? + .symlink_metadata() + .is_ok_and(|m| m.is_file()); + 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. diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index 195ec767..5d55a83a 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -305,10 +305,10 @@ fn abort_never_writes_through_a_symlink_where_a_directory_must_be() { finish(&root, repo); } -#[test] -fn abort_removes_a_protected_file_the_stop_decrypted_and_recorded() { +/// 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("protected", Op::Merge); + 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"); @@ -327,12 +327,35 @@ fn abort_removes_a_protected_file_the_stop_decrypted_and_recorded() { 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 { From a5bc39bc0ea2200fff096f7f2fa6cd358baaf8ef Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 21:57:26 +0300 Subject: [PATCH 5/9] fix(repo): private abort keeps symlinks, stop deletes before conflict output (ref #255 #256) --- ROADMAP.md | 7 +++-- crates/repo/src/private.rs | 36 +++++++++++++--------- crates/repo/src/repo.rs | 10 ++++-- docs/adr/0044-per-branch-access-control.md | 10 ++++-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 70b4a0eb..bc9d7c37 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -763,9 +763,10 @@ P28 review follow-ons, the port de-flake, the clippy allow-list) are recorded in - **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, still overwrites a user file at a path the stop - removed from the tip, and treats a conflicted path as the stop's whatever - its bytes. Private forward operations (switch, fast-forward, clean merge) + 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). diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index 4a10fe51..a59297bd 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -938,11 +938,13 @@ impl Repo { continue; } let recorded = plaintext.contains(&p); - let ours = if perms & PROTECTED != 0 { - recorded - } else { - recorded || conflicted.contains(&p) || worktree::disk_is_blob(&full, &blob_id) - }; + // The stop wrote regular files: a symlink there is the user's. + let ours = full.symlink_metadata().is_ok_and(|m| m.is_file()) + && if perms & PROTECTED != 0 { + recorded + } else { + recorded || conflicted.contains(&p) || worktree::disk_is_blob(&full, &blob_id) + }; if ours { written.insert(p); } else { @@ -1568,6 +1570,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 @@ -1609,15 +1626,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)?; - // Best effort, as before: an error here would leave the - // conflict files written above with no merge state to abort. - 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/repo.rs b/crates/repo/src/repo.rs index aa0c2451..fc41f97e 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2067,9 +2067,13 @@ impl Repo { }; // A regular file only: the stop wrote one, so a symlink there is // the user's (a restore reports it as kept). - let is_file = worktree::safe_join(&self.layout.root, &path)? - .symlink_metadata() - .is_ok_and(|m| m.is_file()); + let is_file = match worktree::safe_join(&self.layout.root, &path)?.symlink_metadata() { + Ok(m) => m.is_file(), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, + // Not knowing what stands there must stop the abort before + // its state is cleared, like a failed removal. + Err(e) => return Err(e.into()), + }; 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 diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 090ad5ad..c408a8c8 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -441,9 +441,13 @@ sidecar into the tree and left it on disk. Now: 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, so a conflicted path cannot be compared by -content and counts as the stop's, and a user file at a tip path the stop -removed is overwritten by the restore. Out-of-view paths are never removed by +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`. From 7623528fb1aa77f1df9f12c13f1d7ce82ac537a3 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 22:23:50 +0300 Subject: [PATCH 6/9] fix(repo): a restore never writes through a symlink (ref #255 #256) --- crates/repo/src/user_file_safety.rs | 17 +++++++++++++++++ crates/repo/src/worktree.rs | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index 5d55a83a..1f67ffdb 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -429,3 +429,20 @@ fn abort_keeps_a_user_file_at_a_protected_path_the_stop_removed() { 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); + } +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index b895c19f..d2f5f233 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -1255,6 +1255,12 @@ fn materialize_impl( // 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); From b25488f46e5399c1af0ce1a756dd4ba509603c1d Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 22:39:29 +0300 Subject: [PATCH 7/9] fix(repo): abort discards an edited conflict, reports retained plaintext paths (ref #255 #256) --- crates/repo/src/private.rs | 12 +++++++---- crates/repo/src/replay.rs | 6 +++--- crates/repo/src/repo.rs | 32 ++++++++++++++++++++++++----- crates/repo/src/user_file_safety.rs | 27 ++++++++++++++++++++++++ crates/repo/src/worktree.rs | 5 +++++ docs/adr/0034-sparse-checkouts.md | 6 ++++-- 6 files changed, 74 insertions(+), 14 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index a59297bd..f4e3d4e4 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -934,12 +934,16 @@ impl Repo { continue; } let full = worktree::safe_join(&self.layout.root, &p)?; - if full.symlink_metadata().is_err() { - continue; - } + let meta = match full.symlink_metadata() { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, + // Not knowing what stands there must stop the abort before it + // clears the stop's records (decrypted text could linger). + Err(e) => return Err(e.into()), + }; let recorded = plaintext.contains(&p); // The stop wrote regular files: a symlink there is the user's. - let ours = full.symlink_metadata().is_ok_and(|m| m.is_file()) + let ours = meta.is_file() && if perms & PROTECTED != 0 { recorded } else { diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 09ab5b76..27a8ee4e 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -647,7 +647,7 @@ impl Repo { 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)?; @@ -1279,8 +1279,8 @@ 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 report = self.restore_over_stop(snap.root, &snap.protection, decided)?; - 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)?; diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index eaf388e8..21c75242 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -34,6 +34,15 @@ 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)] @@ -2053,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 + /// something other than a regular file (the user's symlink or directory) + /// stands there. + 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(); @@ -2077,6 +2089,16 @@ impl Repo { // its state is cleared, like a failed removal. Err(e) => return Err(e.into()), }; + if remove && !is_file { + // Not the stop's regular file (a symlink or directory the user + // put there): left, and told to the caller. + if worktree::safe_join(&self.layout.root, &path)? + .symlink_metadata() + .is_ok() + { + 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 @@ -2088,7 +2110,7 @@ impl Repo { } } } - Ok(()) + Ok(retained) } /// [`Repo::refuse_untracked_overwrite_vs`] against the tree `old_root` @@ -2507,7 +2529,7 @@ impl Repo { 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 diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index 1f67ffdb..1ba85057 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -446,3 +446,30 @@ fn abort_never_writes_through_a_symlink_at_a_conflicted_path() { 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); + } +} diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index d2f5f233..8c1d80e6 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -1204,7 +1204,12 @@ fn materialize_impl( 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); diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index d52c632a..5fac0879 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -321,8 +321,10 @@ every claim below is checked against the shipped code, not the plan. 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 and sidecars are removed - without a byte check: the user's edit of them is the resolution abort + 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). From 96f61b8179686b744a47e8b4dc79956256339e54 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 22:55:25 +0300 Subject: [PATCH 8/9] fix(repo): never delete through a symlinked parent; treat ENOTDIR as absent (ref #255 #256) --- crates/repo/src/private.rs | 16 ++++--- crates/repo/src/repo.rs | 28 +++++------- crates/repo/src/user_file_safety.rs | 34 ++++++++++++++ crates/repo/src/worktree.rs | 70 ++++++++++++++++++++++------- 4 files changed, 108 insertions(+), 40 deletions(-) diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index f4e3d4e4..dcb3fb7a 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -403,7 +403,10 @@ impl Repo { &|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)?; match std::fs::remove_file(full) { Ok(()) => {} @@ -934,16 +937,15 @@ impl Repo { continue; } let full = worktree::safe_join(&self.layout.root, &p)?; - let meta = match full.symlink_metadata() { - Ok(m) => m, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, - // Not knowing what stands there must stop the abort before it - // clears the stop's records (decrypted text could linger). - Err(e) => return Err(e.into()), + // 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 { diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 21c75242..5a8cac78 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2081,23 +2081,17 @@ impl Repo { Err(_) => false, }; // A regular file only: the stop wrote one, so a symlink there is - // the user's (a restore reports it as kept). - let is_file = match worktree::safe_join(&self.layout.root, &path)?.symlink_metadata() { - Ok(m) => m.is_file(), - Err(e) if e.kind() == std::io::ErrorKind::NotFound => false, - // Not knowing what stands there must stop the abort before - // its state is cleared, like a failed removal. - Err(e) => return Err(e.into()), - }; - if remove && !is_file { - // Not the stop's regular file (a symlink or directory the user - // put there): left, and told to the caller. - if worktree::safe_join(&self.layout.root, &path)? - .symlink_metadata() - .is_ok() - { - retained.push(path.clone()); - } + // 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 diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index 1ba85057..eadca58f 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -473,3 +473,37 @@ fn abort_discards_the_resolution_of_a_modify_delete_conflict() { 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 8c1d80e6..b06ba171 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -906,6 +906,43 @@ 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 @@ -1193,24 +1230,25 @@ fn materialize_impl( 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 full.symlink_metadata().is_err() { + if lstat(&full)?.is_none() { continue; } - let ours = 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())) - }; + 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) { From 016e77826c534bc6a37b2609d8c21f0943dc1f31 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 23:10:56 +0300 Subject: [PATCH 9/9] test(repo): pin lstat and under_symlink semantics (ref #255 #256) --- crates/repo/src/repo.rs | 4 ++-- crates/repo/src/worktree.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 5a8cac78..5923944b 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2063,8 +2063,8 @@ impl Repo { /// 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. Returns the recorded paths it left because - /// something other than a regular file (the user's symlink or directory) - /// stands there. + /// 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(); diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index b06ba171..208c5c2d 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -1405,6 +1405,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()));