From d0dec6d6288c8356319c70273fb4a5e4e4bcc396 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 11:52:49 +0300 Subject: [PATCH 01/10] fix(repo): sparse sidecar check by base path, widen while an op is stopped (#224) On a partial clone a `.theirs` sidecar now counts as in view when its conflicted path is, so a file-exact spec such as `src/a.bin` no longer refuses a valid in-view binary conflict. `sc sparse set` may now widen (never narrow, sideways or disable) while a merge, cherry-pick or rebase is stopped. It lays out only the files that come into view, from the stop's decided root, after filling and preflighting the new view for the decided root, the tip and a rebase's accumulator and running the untracked-file guard on each. Protected files that come into view stay absent until completion. The rebase --continue refusal hint now names the widen instead of an abort. Refs #224 --- .claude/skills/sc-cli-reference/SKILL.md | 6 +- CLAUDE.md | 12 +- ROADMAP.md | 7 + crates/repo/src/partial_merge.rs | 105 +++++-- crates/repo/src/partial_replay.rs | 68 +++++ crates/repo/src/replay.rs | 358 ++++++++++++++++++++++- crates/repo/src/repo.rs | 21 +- crates/repo/src/sparse.rs | 136 ++++++++- crates/repo/src/worktree.rs | 33 +++ docs/adr/0034-sparse-checkouts.md | 53 +++- docs/adr/0049-merge-on-partial-clones.md | 4 +- 11 files changed, 729 insertions(+), 74 deletions(-) diff --git a/.claude/skills/sc-cli-reference/SKILL.md b/.claude/skills/sc-cli-reference/SKILL.md index 29094d6c..b16d6366 100644 --- a/.claude/skills/sc-cli-reference/SKILL.md +++ b/.claude/skills/sc-cli-reference/SKILL.md @@ -256,7 +256,11 @@ bash demo/run_merge_ergonomics_demo.sh # sc conflicts/sc resolve proof (P cargo run --bin sc -- sparse set [--identity ] # narrow the working tree to # these path prefixes; re-lays disk now (P24). Warns per # prefix that matches no tracked path; at least one - # prefix is required (`sparse disable` is the full view) + # prefix is required (`sparse disable` is the full view). + # While a merge/pick/rebase is stopped only a widen is + # accepted (new prefixes must cover the old ones): it + # lays out the newly in-view files, leaves protected + # ones absent, and `--continue` then proceeds (#224) cargo run --bin sc -- sparse show [--json] # list the active sparse prefixes ("disabled" if # the spec is empty/absent) (P24) cargo run --bin sc -- sparse disable [--identity ] # clear the spec and rematerialize diff --git a/CLAUDE.md b/CLAUDE.md index 497aee0c..de77fad2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -240,10 +240,14 @@ transport-adjacent. The rest, imperatively: - **Sparse is checkout-only**, and a stopped merge/pick/rebase writes no conflict marker or sealing-input plaintext outside the view: an out-of-sparse conflicted path or sealing input refuses with a `sc sparse set` hint before - any write (ADR-0034, #210; from `rebase --continue` the hint says to abort - first, since `sparse set` is refused mid-rebase). A binary conflict's - `.theirs` sidecar sits beside its in-view file and may not match a - file-exact spec; completion and abort remove it. + any write (ADR-0034, #210). While a merge/pick/rebase is stopped, `sparse + set` accepts only a strict widen (new spec covers the old; narrowing, + sideways and `disable` still refuse): it lays out just the newly in-view + files from the stop's decided root and leaves protected ones absent, so a + `rebase --continue` refusal is fixed by widening and rerunning it (#224). A + binary conflict's `.theirs` sidecar sits beside its in-view file and + may not match a file-exact spec (partial clones check it by its base path); + completion and abort remove it. - **`.sc/git-remotes//marks` carries `git_oid ↔ sc_id` identity — deleting it is destructive.** Deleting the sibling `mirror.git` is always safe (reconstructed on next use). Stale marks self-heal on push/export. diff --git a/ROADMAP.md b/ROADMAP.md index 230deb65..33fb252d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -743,6 +743,13 @@ scale-&-reach horizon): `sc sparse set` on a full clone is refused with a pointer to `sc sparse disable` (a partial clone keeps its `sc backfill` refusal); and `.sc/sparse` parsing ignores blank lines. +- **Sparse follow-ups from #210 — shipped (#224).** A partial clone treats a + binary conflict's `.theirs` sidecar as in view when its conflicted path is, so + a file-exact spec no longer refuses it. `sc sparse set` may widen (never + narrow) while a merge, cherry-pick or rebase is stopped: the files coming into + view are laid out from the stop's decided root, so `rebase --continue`'s + out-of-view refusal no longer needs an abort. Protected files that come into + view mid-stop stay absent (not decrypted) until completion. - **Transparent lazy-fetch for partial clones (P27).** `sc backfill` is explicit-only by design (ADR-0037's "no network in read paths" choice); the identical filtered-walk/gap infra could instead dial the promisor diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 6c02acd7..8781f18e 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -308,6 +308,69 @@ fn a_conflict_in_the_view_completes_with_the_carried_subtree_grafted_from_the_de fx.cleanup(); } +/// A stopped merge on a lazy clone can be widened past its filter: the carried +/// subtree that comes into view is filled by id, laid out from the decided +/// root, and the completion equals the full merge (#224). A clone that cannot +/// fetch refuses the widen with the gap error and changes nothing. +#[test] +fn widening_a_stopped_partial_merge_fills_and_lays_out_the_carried_subtree() { + let fx = Fx::origin("widen-stopped", |_| {}); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", b"ONE\ntwo\nthree\n"), + ("docs/a.md", b"a changed\n"), + ], + ); + let expect = full_merge_root( + &fx, + "b1", + &[("src/a.txt", b"uno\ntwo\nthree\n")], + &["src/a.txt"], + ); + let clone = fx.clone(false); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + let wide = ["src/".to_string(), "docs/".to_string()]; + // Sideways and narrowing keep the old refusal; a widen past the filter on + // a clone that cannot fetch is the gap error, with nothing changed. + assert!(matches!( + clone.set_sparse(&["docs/".to_string()], None), + Err(Error::MergeInProgress) + )); + assert!(matches!( + clone.set_sparse(&wide, None), + Err(Error::GapOutsideFilter(_)) + )); + assert_eq!( + clone.sparse_spec().unwrap().prefixes(), + ["src/".to_string()] + ); + assert!(!fx.clone_root.join("docs").exists()); + + crate::promisor::set_lazy(clone.layout(), true).unwrap(); + clone.set_sparse(&wide, None).unwrap(); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/a.md")).unwrap(), + b"a changed\n", + "theirs' content, from the decided root" + ); + assert!(std::fs::read_to_string(fx.clone_root.join("src/a.txt")) + .unwrap() + .contains("<<<<<<<")); + clone + .resolve_path("src/a.txt", crate::conflicts::ResolveSide::Ours, None) + .unwrap(); + let merged = clone.commit("t", "merge").unwrap(); + assert_eq!(clone.snapshot(&merged).unwrap().root, expect); + assert!(clone.status().unwrap().modified.is_empty()); + drop(clone); + fx.cleanup(); +} + /// Aborting restores ours and removes the markers; the carried subtree was /// never on disk and the ref never moved. #[test] @@ -852,31 +915,35 @@ fn a_protected_conflict_in_the_view_seals_at_completion() { } /// A binary conflict writes `.theirs` beside the file. When the view is -/// the file itself the sidecar falls outside it, so the merge is refused before -/// any write instead of leaving plaintext where completion does not look. +/// the file itself the sidecar does not match the spec, yet it sits beside an +/// in-view conflict: the stop writes it, abort removes it, and completion +/// leaves it out of the tree (#224). #[test] -fn a_sidecar_outside_the_view_is_refused_before_any_write() { - let fx = Fx::origin("sidecar-out", |_| {}); +fn a_sidecar_beside_an_in_view_conflict_is_written_and_removed_under_a_file_exact_view() { + let fx = Fx::origin("sidecar-exact", |_| {}); fx.origin_branch("b1", &[("src/a.txt", b"\xff\xfeTHEIRS\n")]); let clone = fx.clone(false); clone.set_sparse(&["src/a.txt".to_string()], None).unwrap(); clone_edit_src(&fx, &clone, b"\xff\xfeOURS\n"); - let before = refs::read_branch_tip(clone.layout(), "main").unwrap(); + let sidecar = fx.clone_root.join("src/a.txt.theirs"); + let err = clone.merge("origin/b1", "t").unwrap_err(); - assert!( - matches!(&err, Error::InvalidArgument(m) if m.contains("sidecar")), - "got {err:?}" - ); - assert!(!clone.merge_in_progress()); - assert!(!fx.clone_root.join("src/a.txt.theirs").exists()); - assert_eq!( - refs::read_branch_tip(clone.layout(), "main").unwrap(), - before - ); - assert_eq!( - std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), - b"\xff\xfeOURS\n" - ); + assert!(matches!(err, Error::MergeConflicts(1)), "got {err:?}"); + assert_eq!(std::fs::read(&sidecar).unwrap(), b"\xff\xfeTHEIRS\n"); + assert!(!fx.clone_root.join("docs").exists()); + clone.merge_abort().unwrap(); + assert!(!sidecar.exists(), "abort removes the sidecar"); + assert!(!fx.clone_root.join(".sc/CONFLICT_SIDECARS").exists()); + + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + std::fs::write(fx.clone_root.join("src/a.txt"), b"\xff\xferesolved\n").unwrap(); + let merged = clone.commit("t", "merge").unwrap(); + assert!(!sidecar.exists(), "completion removes the sidecar"); + assert!(!clone.tracked_paths().unwrap().contains("src/a.txt.theirs")); + assert_eq!(clone.snapshot(&merged).unwrap().parents.len(), 2); drop(clone); fx.cleanup(); } diff --git a/crates/repo/src/partial_replay.rs b/crates/repo/src/partial_replay.rs index 59aa6d52..d6997a0d 100644 --- a/crates/repo/src/partial_replay.rs +++ b/crates/repo/src/partial_replay.rs @@ -381,6 +381,74 @@ fn a_rebase_stops_on_a_conflict_in_the_view_and_continues() { fx.cleanup(); } +/// A stopped rebase on a lazy clone can be widened past its filter: the +/// subtree that comes into view is filled by id and laid out from the decided +/// root, and the rebase ends at the tree the full rebase gives (#224). +#[test] +fn widening_a_stopped_partial_rebase_fills_the_carried_subtree() { + let fx = Fx::origin("rebase-widen", |_| {}); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", SRC_ONE), + ("docs/api/guide.md", b"g changed\n"), + ], + ); + let two = b"one\nTWO\nthree\n"; + let local2 = b"uno\nTWO\nthree\n"; + fx.origin.branch("loc").unwrap(); + fx.origin.switch("loc").unwrap(); + std::fs::write(fx.origin_root.join("src/a.txt"), two).unwrap(); + fx.origin.commit("t", "local 1").unwrap(); + std::fs::write(fx.origin_root.join("src/a.txt"), local2).unwrap(); + fx.origin.commit("t", "local 2").unwrap(); + assert!(matches!( + fx.origin.rebase("b1", "t", None).unwrap(), + RebaseResult::Stopped { .. } + )); + fx.origin + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let RebaseResult::Rebased { new_tip, .. } = fx.origin.rebase_continue("t", None).unwrap() + else { + panic!("expected a rebase"); + }; + let expect = root_of(&fx.origin, &new_tip); + fx.origin.switch("main").unwrap(); + + let clone = fx.clone(true); + std::fs::write(fx.clone_root.join("src/a.txt"), two).unwrap(); + clone.commit("t", "local 1").unwrap(); + std::fs::write(fx.clone_root.join("src/a.txt"), local2).unwrap(); + clone.commit("t", "local 2").unwrap(); + assert!(matches!( + clone.rebase("origin/b1", "t", None).unwrap(), + RebaseResult::Stopped { .. } + )); + + clone + .set_sparse(&["src/".to_string(), "docs/".to_string()], None) + .unwrap(); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/api/guide.md")).unwrap(), + b"g changed\n", + "the target's content, from the decided root" + ); + assert!(std::fs::read_to_string(fx.clone_root.join("src/a.txt")) + .unwrap() + .contains("<<<<<<<")); + clone + .resolve_path("src/a.txt", ResolveSide::Ours, None) + .unwrap(); + let RebaseResult::Rebased { new_tip, .. } = clone.rebase_continue("t", None).unwrap() else { + panic!("expected a rebase"); + }; + assert_eq!(root_of(&clone, &new_tip), expect); + assert!(clone.status().unwrap().modified.is_empty()); + drop(clone); + fx.cleanup(); +} + /// SRC_A is the first commit's content: a pick that changes nothing there is /// already applied. #[test] diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 34eb722c..846203a1 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -2934,10 +2934,186 @@ mod tests { remove_root(&root); } - /// `sc sparse set` is refused while a rebase is stopped, so a refusal - /// reached from `--continue` must send the user to abort first. + /// The rebase of `work` onto `main` with the view narrowed to `src/`: + /// commit 1 conflicts on `src/a.txt` and changes `docs/y.txt` cleanly (out + /// of view), commit 2 conflicts on `docs/x.txt` (out of view). Returns the + /// repo after commit 1 was resolved and `--continue` refused commit 2. + fn rebase_refused_at_an_out_of_view_conflict(name: &str) -> (std::path::PathBuf, Repo) { + let root = tmp_root(name); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs", "lib"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"x1\nx2\nx3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"y\n").unwrap(); + std::fs::write(root.join("lib/z.txt"), b"z\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"OURS\nx2\nx3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"Y-work\n").unwrap(); + repo.commit("me", "work 1").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"THEIRS\nx2\nx3\n").unwrap(); + repo.commit("me", "work 2").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + + assert!(matches!( + repo.rebase("main", "me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + std::fs::write(root.join("src/a.txt"), b"RESOLVED\nl2\nl3\n").unwrap(); + let err = repo.rebase_continue("me", None).unwrap_err(); + assert_widen_refusal(&err, "docs/x.txt"); + let Error::InvalidArgument(m) = &err else { + unreachable!() + }; + assert!( + m.contains("sc sparse set") && m.contains("--continue"), + "{m}" + ); + assert!(repo.rebase_in_progress(), "the rebase is still stopped"); + assert!(!root.join("docs/x.txt").exists(), "nothing out of view"); + (root, repo) + } + + /// Mid-rebase `sc sparse set` only widens: a narrower or sideways spec + /// keeps the old refusal and changes nothing. + #[test] + fn a_stopped_rebase_refuses_a_spec_that_is_not_a_widening() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-narrow"); + for spec in [vec!["docs/"], vec!["src/a.txt"], vec!["src/a.txt", "docs/"]] { + let spec: Vec = spec.into_iter().map(String::from).collect(); + assert!(matches!( + repo.set_sparse(&spec, None), + Err(Error::RebaseInProgress) + )); + } + assert!(matches!( + repo.set_sparse(&[], None), + Err(Error::RebaseInProgress) + )); + assert!(matches!( + repo.disable_sparse(None), + Err(Error::RebaseInProgress) + )); + assert_eq!( + repo.sparse_spec().unwrap().prefixes(), + &["src/".to_string()] + ); + assert!(!root.join("docs/x.txt").exists(), "nothing laid out"); + repo.rebase_abort().unwrap(); + drop(repo); + remove_root(&root); + } + + /// The refusal from `--continue` is fixed by widening in place: the files + /// that come into view are laid out from the stop's decided root (not + /// HEAD), `--continue` then stops at the now in-view conflict, and the + /// finished rebase keeps both resolved commits. + #[test] + fn widening_a_stopped_rebase_lets_continue_finish() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-widen"); + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + assert_eq!( + std::fs::read(root.join("src/a.txt")).unwrap(), + b"RESOLVED\nl2\nl3\n", + "the resolution is untouched" + ); + assert_eq!( + std::fs::read(root.join("docs/y.txt")).unwrap(), + b"Y-work\n", + "laid from the decided root, which holds work 1's clean change" + ); + assert_eq!( + std::fs::read(root.join("docs/x.txt")).unwrap(), + b"OURS\nx2\nx3\n" + ); + assert!(!root.join("lib/z.txt").exists(), "still out of view"); + + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + assert!(std::fs::read_to_string(root.join("docs/x.txt")) + .unwrap() + .contains("<<<<<<<")); + std::fs::write(root.join("docs/x.txt"), b"MERGED\nx2\nx3\n").unwrap(); + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Rebased { replayed: 2, .. } + )); + assert!(!repo.rebase_in_progress()); + repo.disable_sparse(None).unwrap(); + for (path, want) in [ + ("src/a.txt", &b"RESOLVED\nl2\nl3\n"[..]), + ("docs/x.txt", b"MERGED\nx2\nx3\n"), + ("docs/y.txt", b"Y-work\n"), + ("lib/z.txt", b"z\n"), + ] { + assert_eq!(std::fs::read(root.join(path)).unwrap(), want, "{path}"); + } + assert!(repo.status().unwrap().modified.is_empty()); + drop(repo); + remove_root(&root); + } + + /// Aborting after a widen restores the original tip over the wider view + /// and writes nothing outside it. + #[test] + fn aborting_a_widened_rebase_leaves_nothing_out_of_view() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-widen-abort"); + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + repo.rebase_abort().unwrap(); + assert!(!repo.rebase_in_progress()); + for (path, want) in [ + ("src/a.txt", &b"THEIRS\nl2\nl3\n"[..]), + ("docs/x.txt", b"THEIRS\nx2\nx3\n"), + ("docs/y.txt", b"Y-work\n"), + ] { + assert_eq!(std::fs::read(root.join(path)).unwrap(), want, "{path}"); + } + assert!( + !root.join("lib/z.txt").exists(), + "nothing out of the new view" + ); + assert!(repo.status().unwrap().modified.is_empty()); + assert!(repo.status().unwrap().deleted.is_empty()); + drop(repo); + remove_root(&root); + } + + /// A widen lays no file over one the user owns: it is refused before the + /// spec or any file changes. + #[test] + fn widening_a_stopped_rebase_refuses_over_an_untracked_file() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-untracked"); + std::fs::create_dir_all(root.join("docs")).unwrap(); + std::fs::write(root.join("docs/y.txt"), b"MINE\n").unwrap(); + let err = repo + .set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "got {err:?}"); + assert_eq!( + repo.sparse_spec().unwrap().prefixes(), + &["src/".to_string()] + ); + assert!(!root.join("docs/x.txt").exists()); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"MINE\n"); + assert!(repo.rebase_in_progress()); + drop(repo); + remove_root(&root); + } + + /// A protected file that comes into view mid-rebase is not decrypted: it + /// stays absent, and the finished rebase still carries it. #[test] - fn a_rebase_continue_refusal_points_at_abort_not_at_sparse_set() { + fn widening_a_stopped_rebase_over_a_protected_sealing_input_finishes() { let (root, repo, sk) = mixed_conflict_fixture("sparse-rebase-vault"); repo.set_sparse(&["src/".into()], Some(&sk)).unwrap(); // Commit 1 only touches src/a.txt: an in-view conflict, no sealing. @@ -2949,18 +3125,174 @@ mod tests { // Commit 2 conflicts on src/b.txt and merges vault/s.txt out of view. let err = repo.rebase_continue("me", Some(&sk)).unwrap_err(); assert_widen_refusal(&err, "vault/s.txt"); - let Error::InvalidArgument(m) = &err else { - unreachable!() - }; - assert!(m.contains("sc rebase --abort"), "{m}"); - assert!(repo.rebase_in_progress(), "the rebase is still stopped"); + let skipped = repo + .set_sparse(&["src/".into(), "vault/".into()], Some(&sk)) + .unwrap(); + assert_eq!(skipped, vec!["vault/s.txt".to_string()]); + assert!(!root.join("vault/s.txt").exists(), "not decrypted mid-op"); assert!(matches!( - repo.set_sparse(&["src/".into(), "vault/".into()], Some(&sk)), - Err(Error::RebaseInProgress) + repo.rebase_continue("me", Some(&sk)).unwrap(), + RebaseResult::Stopped { .. } )); - assert!(!root.join("vault/s.txt").exists(), "nothing out of view"); - repo.rebase_abort().unwrap(); - assert!(!root.join("vault/s.txt").exists(), "nothing after abort"); + std::fs::write(root.join("src/b.txt"), b"RESOLVED\nl2\nl3\n").unwrap(); + assert!(matches!( + repo.rebase_continue("me", Some(&sk)).unwrap(), + RebaseResult::Rebased { replayed: 2, .. } + )); + assert_eq!( + std::fs::read(root.join("vault/s.txt")).unwrap(), + b"OURS\ns2\ns3\ns4\nTHEIRS\n", + "both sides' protected edits survive" + ); + drop(repo); + remove_root(&root); + } + + /// A stopped cherry-pick widens like a merge: the newly in-view file is + /// the pick's decided content, completion keeps it, and abort after a + /// widen restores ours over the wider view. + #[test] + fn widening_a_stopped_pick_keeps_the_decided_content_and_abort_restores_ours() { + let root = tmp_root("sparse-pick-widen"); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs", "lib"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"y\n").unwrap(); + std::fs::write(root.join("lib/z.txt"), b"z\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"Y-work\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + let wide = ["src/".to_string(), "docs/".to_string()]; + + for abort in [true, false] { + assert!(matches!( + repo.cherry_pick("work", "me", None, None).unwrap_err(), + Error::PickConflicts(1) + )); + assert!(matches!( + repo.set_sparse(&["docs/".into()], None), + Err(Error::PickInProgress) + )); + repo.set_sparse(&wide, None).unwrap(); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"Y-work\n"); + if abort { + repo.cherry_pick_abort().unwrap(); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"y\n"); + assert_eq!( + std::fs::read(root.join("src/a.txt")).unwrap(), + b"OURS\nl2\nl3\n" + ); + assert!(!root.join("lib/z.txt").exists(), "outside the new view"); + assert!(repo.status().unwrap().modified.is_empty()); + } else { + std::fs::write(root.join("src/a.txt"), b"MERGED\nl2\nl3\n").unwrap(); + repo.commit("me", "pick").unwrap(); + } + } + repo.disable_sparse(None).unwrap(); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"Y-work\n"); + assert_eq!(std::fs::read(root.join("lib/z.txt")).unwrap(), b"z\n"); + assert!(repo.status().unwrap().modified.is_empty()); + drop(repo); + remove_root(&root); + } + + /// The untracked-file guard also covers paths only the tip tracks: the + /// merge deleted `docs/old.txt`, so the decided root lacks it, yet a + /// user's own file there would be read back by the completion and undo + /// the deletion. + #[test] + fn widening_a_stopped_merge_refuses_over_an_untracked_file_at_a_tip_path() { + let root = tmp_root("sparse-merge-widen-tip"); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/old.txt"), b"old\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::remove_file(root.join("docs/old.txt")).unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + std::fs::create_dir_all(root.join("docs")).unwrap(); + std::fs::write(root.join("docs/old.txt"), b"MINE\n").unwrap(); + + let err = repo + .set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "got {err:?}"); + assert_eq!( + repo.sparse_spec().unwrap().prefixes(), + &["src/".to_string()] + ); + assert_eq!(std::fs::read(root.join("docs/old.txt")).unwrap(), b"MINE\n"); + repo.merge_abort().unwrap(); + drop(repo); + remove_root(&root); + } + + /// A merge that is stopped can be widened too: the files that come into + /// view are the merge's decided content, and the completion keeps them. + #[test] + fn widening_a_stopped_merge_keeps_the_decided_content() { + let root = tmp_root("sparse-merge-widen"); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs", "lib"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"y\n").unwrap(); + std::fs::write(root.join("lib/z.txt"), b"z\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/y.txt"), b"Y-work\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + + assert!(matches!( + repo.set_sparse(&["lib/".into()], None), + Err(Error::MergeInProgress) + )); + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"Y-work\n"); + assert!(std::fs::read_to_string(root.join("src/a.txt")) + .unwrap() + .contains("<<<<<<<")); + std::fs::write(root.join("src/a.txt"), b"MERGED\nl2\nl3\n").unwrap(); + repo.commit("me", "merge").unwrap(); + repo.disable_sparse(None).unwrap(); + assert_eq!(std::fs::read(root.join("docs/y.txt")).unwrap(), b"Y-work\n"); + assert_eq!(std::fs::read(root.join("lib/z.txt")).unwrap(), b"z\n"); + assert!(repo.status().unwrap().modified.is_empty()); drop(repo); remove_root(&root); } diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 666b9419..c995f573 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -1807,10 +1807,12 @@ impl Repo { conflicted_paths: &[String], ) -> Result { let sparse = self.sparse_spec()?; - // `sc sparse set` is refused while a rebase is stopped, so a refusal - // reached from `rebase --continue` cannot be fixed by widening in place. + // A stopped op can be widened in place (`sc sparse set`, additive + // only; ADR-0034), so the stopped-op refusal from `--continue` names + // that instead of an abort. let widen = if crate::rebase_state::in_progress(&self.layout) { - "abort the rebase (`sc rebase --abort`), widen with `sc sparse set`, then rerun the rebase" + "add it to the current prefixes with `sc sparse set` (it may only widen the view \ + while the rebase is stopped), then rerun `sc rebase --continue`" } else { "run `sc sparse set` to include it, then retry" }; @@ -1841,14 +1843,15 @@ impl Repo { ))); } // A sidecar is `.theirs`, and the conflicted path - // passed the gate above, so on a full clone it is beside an in-view - // file (a file-exact spec such as `src/a.bin` does not match + // passed the gate above, so it is beside an in-view file (a + // file-exact spec such as `src/a.bin` does not match // `src/a.bin.theirs`, yet that sidecar is in view) and `abort` and - // completion remove it. A partial clone keeps the stricter ADR-0049 - // refusal: outside the view its sidecar would sit where completion - // neither expects nor protects it. + // completion remove it. A partial clone still checks it, by its base + // path: outside the view it would sit where completion neither + // expects nor protects it (ADR-0049). if self.promisor()?.is_some() { - if let Some((rel, _)) = sidecars.iter().find(|(r, _)| !sparse.matches(r)) { + let in_view = |r: &str| sparse.matches(r.strip_suffix(".theirs").unwrap_or(r)); + if let Some((rel, _)) = sidecars.iter().find(|(r, _)| !in_view(r)) { return Err(Error::InvalidArgument(format!( "{op} has conflicts, and its sidecar {rel} would fall outside your sparse checkout; \ {widen}" diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 57de9173..3b78d0de 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -50,6 +50,19 @@ impl Sparse { }) } + /// Whether every path `other` matches is also matched here: the widen-only + /// test for a spec change while an op is stopped. A prefix names its bare + /// path and everything under it, so `other`'s prefix is covered exactly + /// when this spec matches that bare path. + pub fn covers(&self, other: &Sparse) -> bool { + self.is_full() + || (!other.is_full() + && other + .prefixes + .iter() + .all(|p| self.matches(p.trim_end_matches('/')))) + } + /// The recorded prefixes, in the order they were set. pub fn prefixes(&self) -> &[String] { &self.prefixes @@ -226,6 +239,11 @@ impl Repo { /// spec. An unborn HEAD has no working tree to re-lay; the spec is still /// persisted so it takes effect on the first commit/checkout. /// + /// While a merge, pick or rebase is stopped it refuses with that op's + /// `*InProgress` error unless `prefixes` cover the current spec (a strict + /// widen) and the stop recorded its decided root; a widen then lays out + /// only the files that come into view (see `widen_while_stopped`). + /// /// Refuses (same as [`Repo::switch_with_identity`]) when the working tree /// has uncommitted modifications or deletions: `materialize`'s write loop /// rewrites every in-sparse target entry from HEAD's blob unconditionally, @@ -243,15 +261,20 @@ impl Repo { self.refuse_on_private("sc sparse set")?; // Before any side effect (the shared `store` checks again). validate_prefixes(prefixes)?; - if crate::merge_state::in_progress(&self.layout) { - return Err(Error::MergeInProgress); - } - if crate::pick_state::in_progress(&self.layout) { - return Err(Error::PickInProgress); - } - if crate::rebase_state::in_progress(&self.layout) { - return Err(Error::RebaseInProgress); - } + let spec = Sparse::new(prefixes.to_vec()); + // The view in effect now, read before it is replaced: it bounds the + // removal walk in the re-lay below. + let old_spec = self.sparse_spec()?; + // A stopped merge, pick or rebase refuses a narrowing or sideways + // change (the re-lay would clobber its markers). A strict widen is + // allowed when the stop recorded its decided root: see + // `widen_while_stopped`. + let stopped = match self.stopped_op()? { + None => None, + // A full spec is `disable`, which stays refused. + Some((_, Some(decided))) if !spec.is_full() && spec.covers(&old_spec) => Some(decided), + Some((refusal, _)) => return Err(refusal), + }; // Gap error on sparse-widen (P27 Task 5): a partial clone never // fetched anything outside its promisor filter, so narrowing the // sparse view to a prefix that isn't itself covered by the filter @@ -281,6 +304,9 @@ impl Repo { } } } + if let Some(decided) = stopped { + return self.widen_while_stopped(decided, &old_spec, &spec); + } let dirty = self.status()?; if !dirty.modified.is_empty() || !dirty.deleted.is_empty() { return Err(crate::error::Error::InvalidArgument( @@ -288,10 +314,6 @@ impl Repo { .into(), )); } - let spec = Sparse::new(prefixes.to_vec()); - // The view in effect now, read before it is replaced: it bounds the - // removal walk in the re-lay below. - let old_spec = self.sparse_spec()?; let tip = self.head_tip()?; let snap = match tip { Some(tip) => Some(self.snapshot(&tip)?), @@ -358,6 +380,80 @@ impl Repo { Ok(skipped) } + /// The in-progress merge, pick or rebase, if any, as the refusal a + /// spec change gets and the decided root the stop recorded (the tree its + /// working tree was laid out from). + fn stopped_op(&self) -> Result)>> { + Ok(if crate::merge_state::in_progress(&self.layout) { + Some(( + Error::MergeInProgress, + crate::merge_state::read_decided_root(&self.layout)?, + )) + } else if crate::pick_state::in_progress(&self.layout) { + Some(( + Error::PickInProgress, + crate::pick_state::read_decided_root(&self.layout)?, + )) + } else if crate::rebase_state::in_progress(&self.layout) { + Some(( + Error::RebaseInProgress, + crate::rebase_state::read_decided_root(&self.layout)?, + )) + } else { + None + }) + } + + /// Widen the view (`new` covers `old`) while a merge, pick or rebase is + /// stopped, laying out only the files that come into view, from the + /// stop's decided root. A normal `set_sparse` re-lays HEAD, which would + /// overwrite the conflict markers and the user's resolution, and its + /// dirty-tree check reads those as uncommitted edits. + /// + /// The decided root is right for every path the stop did not write: it is + /// what completion carries an out-of-view path from, so laying a path out + /// from it changes nothing the completion would compute, while leaving + /// it off disk would read as a deletion. Marker, plaintext and sidecar + /// paths are all in the old view (the stop gates them), so none is + /// touched. Protected entries are not decrypted here: they stay absent, + /// and completion carries them from the decided root as it does for any + /// absent protected file. Files are written before the spec, so a crash + /// leaves only extra out-of-view files, never a view with files missing. + fn widen_while_stopped( + &self, + decided: scl_core::ObjectId, + old: &Sparse, + new: &Sparse, + ) -> Result> { + // Before any write, for every tree the stopped op still reads over + // the new view (the decided root; the tip, which `--abort` and the + // completion's `tracked_paths_at` read; a rebase's accumulator): + // fill the closure (lazy partial clones), check it is present, and + // refuse to lay a file over one the user owns. + let mut roots = vec![decided]; + if let Some(tip) = self.head_tip()? { + roots.push(self.snapshot(&tip)?.root); + } + if let Some(st) = crate::rebase_state::read(&self.layout)? { + roots.push(self.snapshot(&st.acc_tip)?.root); + } + let store_arc = self.vfs().store(); + for root in roots { + self.ensure_view(root, new)?; + { + let mut s = store_arc.lock().unwrap(); + crate::worktree::preflight_view(self.layout(), &mut s, root, new)?; + } + self.refuse_untracked_for_view_change("sparse set", root, old, new)?; + } + let skipped = { + let mut s = store_arc.lock().unwrap(); + crate::worktree::lay_out_added_view(self.layout(), &mut s, decided, old, new)? + }; + store(self.layout(), new)?; + Ok(skipped) + } + /// The prefixes in `prefixes` that match no tracked path at HEAD, so a /// typo or a path that does not exist is not silently taken for a narrow /// view that happens to be empty. Each prefix is one path-directed lookup @@ -528,6 +624,20 @@ mod tests { std::fs::remove_dir_all(&layout.root).unwrap(); } + #[test] + fn covers_is_a_strict_superset_test_on_path_boundaries() { + let sp = |v: &[&str]| Sparse::new(v.iter().map(|s| s.to_string()).collect()); + assert!(sp(&["src/", "docs/"]).covers(&sp(&["src"]))); + assert!(sp(&["src"]).covers(&sp(&["src/a/", "src/b.txt"]))); + assert!(sp(&["src"]).covers(&sp(&["src"]))); + assert!(sp(&[]).covers(&sp(&["src"])), "full covers everything"); + assert!(sp(&[]).covers(&sp(&[]))); + assert!(!sp(&["src"]).covers(&sp(&[])), "full is wider than any set"); + assert!(!sp(&["src/a.txt"]).covers(&sp(&["src"])), "narrower"); + assert!(!sp(&["docs"]).covers(&sp(&["src"])), "sideways"); + assert!(!sp(&["src"]).covers(&sp(&["srcfoo"])), "boundary, not text"); + } + #[test] fn sparse_set_refused_during_in_progress() { use crate::repo::Repo; diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 83716f6d..329bd00b 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -774,6 +774,39 @@ pub(crate) fn preflight_view( Ok(()) } +/// Lay out the files that a widened view (`new`, which covers `old`) brings +/// in from `root`, and nothing else: no removal pass, and no rewrite of a path +/// `old` already laid out (it may hold conflict markers or the user's +/// resolution). A protected entry is not decrypted: it is left absent and +/// returned, since the caller has no wrap map for `root`. +pub(crate) fn lay_out_added_view( + layout: &Layout, + store: &mut Store, + root: ObjectId, + old: &Sparse, + new: &Sparse, +) -> Result> { + let mut skipped = Vec::new(); + for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { + if old.matches(&path) { + continue; + } + if perms & PROTECTED != 0 { + skipped.push(path); + continue; + } + let Object::Blob(bytes) = store.get(&blob_id)? else { + continue; + }; + let full = safe_join(&layout.root, &path)?; + if let Some(parent) = full.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::write(&full, &bytes[..])?; + } + Ok(skipped) +} + /// Materialize a snapshot's file tree into the working dir. /// /// `sparse` filters the disk view: a target entry outside `sparse` is never diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index ce0a3218..1972c0c7 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -63,12 +63,13 @@ review fix — see Consequences). (`sc sparse set`) before anything is written, exactly like an out-of-sparse conflicted path, so a stop, its completion and its abort leave nothing out of view on disk. From `rebase --continue` the hint - says to abort the rebase first, because `sparse set` is refused while a - rebase is stopped. Sidecars need no gate on a full clone: each is - `.theirs` beside a conflicted path that already passed - the gate (so it is in view even under a file-exact spec such as - `src/a.bin`), and abort and completion remove it; a partial clone keeps - its stricter sidecar refusal (ADR-0049). The earlier build wrote that + says to widen with `sc sparse set` and rerun `--continue` (a widen is + allowed mid-stop since #224, see refinement 5). Sidecars need no gate on + a full clone: each is `.theirs` beside a conflicted path + that already passed the gate (so it is in view even under a file-exact + spec such as `src/a.bin`), and abort and completion remove it; a partial + clone checks the sidecar by its base path (#224), so a file-exact spec no + longer refuses a valid in-view binary conflict (ADR-0049). The earlier build wrote that plaintext outside the view and left it there after completion (only abort removed it); this was never data loss or a new disclosure, only a view wider than advertised. The cost is that such a merge now needs the @@ -161,10 +162,36 @@ every claim below is checked against the shipped code, not the plan. narrow view during harvest and silently revert an agent's genuine deletion of an out-of-sparse path, pinned by `sc_work_full_agent_deletion_survives_host_sparse`. -5. **`set_sparse`/`disable_sparse` refuse mid-conflict.** Re-laying the - working tree while a merge/pick/rebase is in progress would either - destroy in-progress conflict markers or narrow away a path a - conflicted operation still needs on disk — both refuse with the same - `MergeInProgress`/`PickInProgress`/`RebaseInProgress` errors the P21 - policy-op guards already use elsewhere, added in review before Task 4 - landed rather than discovered after. +5. **`set_sparse`/`disable_sparse` refuse mid-conflict, except a strict + widen.** Re-laying the working tree while a merge/pick/rebase is in + progress would either destroy in-progress conflict markers or narrow + away a path a conflicted operation still needs on disk — both refuse + with the same `MergeInProgress`/`PickInProgress`/`RebaseInProgress` + errors the P21 policy-op guards already use elsewhere, added in review + before Task 4 landed rather than discovered after. `disable_sparse`, + narrowing and sideways specs still refuse. Since #224 `set_sparse` accepts + a spec that `Sparse::covers` the current one (every old prefix is matched + by a new one) while the stop has a recorded decided root + (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`), so `rebase --continue`'s + out-of-view refusal can be fixed without losing the resolved commits. + `Repo::widen_while_stopped` (`crates/repo/src/sparse.rs`) does not + re-lay HEAD. For the decided root, the tip and (rebase) the accumulator + it fills the new view's closure (lazy partial clones), preflights the + view and runs `refuse_untracked_for_view_change`, then writes + only the files `old` did not match (`worktree::lay_out_added_view`) and + stores the spec last, so a crash leaves extra out-of-view files, never a + view with files missing (a retry of the same widen after such a crash is + refused by the untracked-file guard; delete the laid files and rerun). The + decided root is the right source: it is what completion carries an + out-of-view path from. For a resumed rebase (`resolved`) the accumulator + can differ from it outside the view, but the next `--continue` re-lays + the tree from the accumulator under the new spec, so what the widen laid + is overwritten there. A full spec is `disable` and stays refused. Markers, the resolution, + `CONFLICT_PLAINTEXT` and sidecars are all in the old view, so they are not + touched; the dirty-tree check is skipped (HEAD is stale mid-op). A + protected file that comes into view is not decrypted (the stop does not + persist its wrap map): it stays absent, is reported as skipped, and + completion carries it from the decided root as for any absent protected + path. Abort restores the original tip over the widened view and the spec + stays widened. No oplog record is written (the spec change moves no + ref); the repo lock is held by the CLI as for any mutation. diff --git a/docs/adr/0049-merge-on-partial-clones.md b/docs/adr/0049-merge-on-partial-clones.md index 84df85ce..d31f1834 100644 --- a/docs/adr/0049-merge-on-partial-clones.md +++ b/docs/adr/0049-merge-on-partial-clones.md @@ -103,12 +103,12 @@ Flatten drops empty directories and directory metadata and rebuilds directories **Three refusals, all before any merge-output, merge-state or working-tree write** (fetched objects persist, as in decision 5): 1. A conflicted path outside the sparse view. This is the existing `materialize_conflict_state` refusal, and the widen hint stays: `sc sparse set` on a lazy clone, `sc backfill` on a non-lazy one. -2. A sidecar (`.theirs`, written for a binary conflict) whose path is outside the view: it is plaintext beside the conflicted file, and outside the view completion neither expects nor protects it. +2. A sidecar (`.theirs`, written for a binary conflict) whose conflicted path is outside the view: it is plaintext beside the conflicted file, and outside the view completion neither expects nor protects it. The sidecar counts as in view when its base path is, so a file-exact spec such as `src/a.bin` no longer refuses an in-view binary conflict (#224; the conflicted path itself already passed refusal 1, so this check is a guard and does not fire in practice). 3. A result that must be sealed at completion (protected plaintext from a clean content merge, or a plain file under a union rule) at a path outside the view. Completion reads that plaintext back from disk. A partial clone completes from the view only, so plaintext left outside it would be silently dropped, and on any clone writing it outside the view leaves plaintext on disk where the user asked for none (and `commit` never sweeps it). The combination is refused with a message that names the path, on full and partial clones alike (#210; a full clone was first allowed to write it, which left the plaintext behind after completion, ADR-0034). Sealing it into the decided root at conflict time would lift the refusal, but it needs the fresh wraps persisted (a fourth state file), and nothing yet asks for it. **Completion** (`sc commit` with `MERGE_HEAD` set) is allowed on a partial clone only when a decided root is recorded, which every partial conflict writes. It builds `all` from the working tree and the in-view carry exactly as today, then splices every out-of-view subtree back in **from the decided root** by id (`graft_out_of_sparse` with the decided root as the parent), where a plain commit grafts from the tip. The wrap map keeps the complete union of both parents' wraps (decision 4's coarse rule: nothing is pruned when a subtree was grafted), since a grafted subtree's blobs never pass through the seal loop. A merge without a decided root, and every cherry-pick or rebase completion, still refuse; replay is 37c-iv. -**The view is fixed across the merge.** `sc sparse set` already refuses while a merge is in progress, so the spec the conflict was checked against is the spec completion runs under. +**The view only widens across the merge.** `sc sparse set` refuses while a merge, pick or rebase is stopped unless the new spec is a strict superset of the current one and the stop recorded its decided root (#224, ADR-0034). A widen fills the new view's closure of the decided root, the tip and a rebase's accumulator (lazy clones; a non-lazy clone gets the usual gap error from the preflight), then lays out only the files that come into view, from the decided root. The spec the conflict was checked against therefore stays inside the spec completion runs under, and nothing the stop wrote is rewritten. **The conflict materialization fills first.** `materialize_conflict_state` runs `ensure_view(conflict_root)` before the first working-tree write, as every other materializer does, because a carried subtree can sit inside the view. From 559474ee2c4493bd4d9e008f1d5fe73653a69a7d Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 12:26:22 +0300 Subject: [PATCH 02/10] fix(repo): safe retry and old-view paths for the stopped-op sparse widen (#224) The widen's untracked-file check now covers only paths the old view did not match, so a directory a replayed commit created under the old view is no longer an obstacle. A file already on disk with the decided root's bytes counts as an earlier write of the same widen, so a retry after an interruption succeeds; on a partial clone the new view is recorded in the sparse_prev marker before the writes so abort removes what an interrupted widen laid. Widening to the same spec is a quiet no-op. Adds tests for the file-to-directory rebase, the retry, equality with a run without sparse (merge, pick), and abort residue on a partial clone. Docs: stale CLAUDE.md sidecar clause, precise ROADMAP wording for protected files, ADR-0034 pointer to #238. Refs #224 --- CLAUDE.md | 2 +- ROADMAP.md | 6 +- crates/repo/src/partial_merge.rs | 60 +++++++++++ crates/repo/src/replay.rs | 173 ++++++++++++++++++++++++++++++ crates/repo/src/repo.rs | 20 +++- crates/repo/src/sparse.rs | 48 +++++++-- crates/repo/src/worktree.rs | 28 +++++ docs/adr/0034-sparse-checkouts.md | 18 +++- 8 files changed, 337 insertions(+), 18 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 045a5894..c3976cf8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -174,7 +174,7 @@ transport-adjacent. The rest, imperatively: capability, and no DTO field capable of carrying protected ciphertext or identity material. - **Partial clones refuse `sc export`, `sparse disable`, an empty `sparse set` (and, on a non-lazy clone, one outside the filter), `sc rewrap`, - `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge, cherry-pick or rebase whose conflict lies outside the view or whose `.theirs` sidecar would lie outside the view (partial clones only: on a full clone the sidecar is exempt as it sits beside an in-view file) (no ref, state or working-tree change; on every clone, a conflict stop that needs sealing outside the view is refused the same way), `sc push` to a git remote, and + `sc grant`/`revoke`, creating, switching to or leaving a private branch, a merge, cherry-pick or rebase whose conflict lies outside the view (a `.theirs` sidecar sits beside its conflicted path, so it is in view whenever that path is; ADR-0049) (no ref, state or working-tree change; on every clone, a conflict stop that needs sealing outside the view is refused the same way), `sc push` to a git remote, and `sc fetch` from any native remote but the promisor origin.** `sc merge`, `sc cherry-pick` and `sc rebase` work, including conflicts inside the view (P37c). `sc ws` and `sc work` work too (ADR-0050): workspaces and agents see the clone's view only, and harvest lands through the same merge. `sc backfill --all` converts to a genuine full clone and re-enables them. The filter records what a clone *asked for*, not what is present: after a fetch an in-filter object diff --git a/ROADMAP.md b/ROADMAP.md index 6c701618..1e4a2aa1 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -749,7 +749,11 @@ scale-&-reach horizon): narrow) while a merge, cherry-pick or rebase is stopped: the files coming into view are laid out from the stop's decided root, so `rebase --continue`'s out-of-view refusal no longer needs an abort. Protected files that come into - view mid-stop stay absent (not decrypted) until completion. + view mid-stop are not decrypted: they stay absent from disk and completion + carries them. A rebase's final fold re-lays the tree and decrypts them with + `--identity`; a merge or cherry-pick finished with `sc commit` writes no + working tree, so they stay absent until the next operation that does (a + `switch`, or `sparse set` again). - **Transparent lazy-fetch for partial clones (P27).** `sc backfill` is explicit-only by design (ADR-0037's "no network in read paths" choice); the identical filtered-walk/gap infra could instead dial the promisor diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 8781f18e..b4e3a821 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -371,6 +371,66 @@ fn widening_a_stopped_partial_merge_fills_and_lays_out_the_carried_subtree() { fx.cleanup(); } +/// Aborting a stopped merge after a widen removes what the widen laid out, +/// and leaves no marker: a file only theirs added is gone, ours' files are back. +/// A widen interrupted after its writes (simulated: files laid, spec not +/// stored, marker as the widen records it) is cleaned by the abort too, which +/// the spec's view alone could not see on a partial clone. +#[test] +fn aborting_a_widened_partial_merge_leaves_no_residue() { + for interrupted in [false, true] { + let fx = Fx::origin( + if interrupted { + "abort-widen-int" + } else { + "abort-widen" + }, + |_| {}, + ); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", b"ONE\ntwo\nthree\n"), + ("docs/new.md", b"added by theirs\n"), + ], + ); + let clone = fx.clone(true); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + let old = clone.sparse_spec().unwrap(); + let wide = crate::Sparse::new(vec!["src/".to_string(), "docs/".to_string()]); + if interrupted { + let decided = crate::merge_state::read_decided_root(clone.layout()) + .unwrap() + .unwrap(); + clone.ensure_view(decided, &wide).unwrap(); + crate::sparse::store_prev(clone.layout(), &wide).unwrap(); + let store = clone.vfs().store(); + let mut s = store.lock().unwrap(); + crate::worktree::lay_out_added_view(clone.layout(), &mut s, decided, &old, &wide) + .unwrap(); + } else { + clone.set_sparse(wide.prefixes(), None).unwrap(); + assert!(!fx.clone_root.join(".sc/sparse_prev").exists()); + } + assert!(fx.clone_root.join("docs/new.md").exists(), "laid"); + + clone.merge_abort().unwrap(); + assert!(!fx.clone_root.join("docs/new.md").exists(), "no residue"); + assert!(!fx.clone_root.join(".sc/sparse_prev").exists()); + assert_eq!( + std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), + b"uno\ntwo\nthree\n" + ); + assert!(clone.status().unwrap().modified.is_empty()); + drop(clone); + fx.cleanup(); + } +} + /// Aborting restores ours and removes the markers; the carried subtree was /// never on disk and the ref never moved. #[test] diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 846203a1..f7b5bf89 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -1341,6 +1341,7 @@ pub enum RebaseResult { mod tests { use super::*; use crate::worktree::tree_file_ids; + use crate::Sparse; fn tmp_root(tag: &str) -> std::path::PathBuf { let p = std::env::temp_dir().join(format!("scl-repo-replay-{tag}-{}", std::process::id())); @@ -3250,6 +3251,178 @@ mod tests { remove_root(&root); } + /// A replayed commit turned `src/x` (a file) into the directory `src/x/`. + /// The widen never writes under `src/` (the old view laid it out), so that + /// directory is not an obstacle, and the rebase finishes after it. + #[test] + fn widening_a_stopped_rebase_ignores_a_directory_the_old_view_laid_out() { + let root = tmp_root("sparse-rebase-file-to-dir"); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("src/x"), b"x\n").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"x1\nx2\nx3\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"OURS\nx2\nx3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::remove_file(root.join("src/x")).unwrap(); + std::fs::create_dir_all(root.join("src/x")).unwrap(); + std::fs::write(root.join("src/x/y"), b"y\n").unwrap(); + repo.commit("me", "work 1").unwrap(); + std::fs::write(root.join("docs/x.txt"), b"THEIRS\nx2\nx3\n").unwrap(); + repo.commit("me", "work 2").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!(matches!( + repo.rebase("main", "me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + assert!(root.join("src/x/y").is_file(), "src/x is a directory now"); + std::fs::write(root.join("src/a.txt"), b"RESOLVED\nl2\nl3\n").unwrap(); + let err = repo.rebase_continue("me", None).unwrap_err(); + assert_widen_refusal(&err, "docs/x.txt"); + + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + std::fs::write(root.join("docs/x.txt"), b"MERGED\nx2\nx3\n").unwrap(); + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Rebased { replayed: 2, .. } + )); + assert_eq!(std::fs::read(root.join("src/x/y")).unwrap(), b"y\n"); + drop(repo); + remove_root(&root); + } + + /// A widen interrupted after it wrote the files but before it stored the + /// spec: the retry rewrites them instead of refusing, and the rebase + /// finishes. + #[test] + fn a_widen_interrupted_after_laying_files_can_be_retried() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-retry"); + let old = repo.sparse_spec().unwrap(); + let new = Sparse::new(vec!["src/".into(), "docs/".into()]); + let decided = crate::rebase_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + { + let store = repo.vfs().store(); + let mut s = store.lock().unwrap(); + crate::worktree::lay_out_added_view(repo.layout(), &mut s, decided, &old, &new) + .unwrap(); + } + assert!(root.join("docs/y.txt").exists(), "laid, spec not stored"); + assert_eq!(repo.sparse_spec().unwrap(), old); + + // A file the user changed since is still theirs and still refused. + std::fs::write(root.join("docs/y.txt"), b"MINE\n").unwrap(); + assert!(repo.set_sparse(new.prefixes(), None).is_err()); + std::fs::write(root.join("docs/y.txt"), b"Y-work\n").unwrap(); + + repo.set_sparse(new.prefixes(), None).unwrap(); + // The same spec again is a quiet no-op. + assert!(repo.set_sparse(new.prefixes(), None).unwrap().is_empty()); + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Stopped { .. } + )); + std::fs::write(root.join("docs/x.txt"), b"MERGED\nx2\nx3\n").unwrap(); + assert!(matches!( + repo.rebase_continue("me", None).unwrap(), + RebaseResult::Rebased { .. } + )); + drop(repo); + remove_root(&root); + } + + /// `main` and `work` both edit `src/a.txt` (a conflict); `work` also + /// deletes `docs/del.txt` and adds `docs/new.txt`, both cleanly and in + /// view only after a widen. Runs `op` with the view narrowed to `src/` and + /// widened mid-stop (or never narrowed), and returns the resulting tree. + fn deletion_and_add_fixture( + name: &str, + narrow: bool, + op: impl Fn(&Repo, &std::path::Path), + ) -> scl_core::ObjectId { + let root = tmp_root(name); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/del.txt"), b"del\n").unwrap(); + std::fs::write(root.join("docs/keep.txt"), b"keep\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::remove_file(root.join("docs/del.txt")).unwrap(); + std::fs::write(root.join("docs/new.txt"), b"new\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + if narrow { + repo.set_sparse(&["src/".into()], None).unwrap(); + } + op(&repo, &root); + let tip = repo.head_tip().unwrap().unwrap(); + let tree = repo.snapshot(&tip).unwrap().root; + drop(repo); + remove_root(&root); + tree + } + + /// Widening mid-stop gives the tree a run without sparse gives, for a + /// clean deletion and a clean add that only come into view with the widen. + #[test] + fn a_widened_merge_equals_the_merge_without_sparse() { + let run = |narrow: bool| { + deletion_and_add_fixture("sparse-eq-merge", narrow, |repo, root| { + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + if narrow { + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"MERGED\nl2\nl3\n").unwrap(); + repo.commit("me", "merge").unwrap(); + }) + }; + assert_eq!(run(true), run(false)); + } + + /// The same for a cherry-pick. + #[test] + fn a_widened_pick_equals_the_pick_without_sparse() { + let run = |narrow: bool| { + deletion_and_add_fixture("sparse-eq-pick", narrow, |repo, root| { + assert!(matches!( + repo.cherry_pick("work", "me", None, None).unwrap_err(), + Error::PickConflicts(1) + )); + if narrow { + repo.set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"MERGED\nl2\nl3\n").unwrap(); + repo.commit("me", "pick").unwrap(); + }) + }; + assert_eq!(run(true), run(false)); + } + /// A merge that is stopped can be widened too: the files that come into /// view are the merge's decided content, and the completion keeps them. #[test] diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 06b506f5..153a8866 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -2090,9 +2090,12 @@ impl Repo { } /// Refuse a sparse-spec change (`op`) that would lay a tracked file onto an - /// untracked or ignored one: a path newly in view (`new_view`) that the - /// tree `root` tracks, with something already standing there and the old - /// view (`old_view`) not having laid it out. Same rules as + /// untracked or ignored one: a path newly in view (`new_view` and not + /// `old_view`) that the tree `root` tracks, with something already standing + /// there. Paths the old view matched are not checked: they are not written + /// (a stopped op may have turned one into a directory, say). `already_laid` + /// names paths that count as tracked because an earlier, interrupted run of + /// the same change wrote them. Same rules as /// [`Repo::refuse_untracked_overwrite_vs`]. pub(crate) fn refuse_untracked_for_view_change( &self, @@ -2100,9 +2103,10 @@ impl Repo { root: ObjectId, old_view: &crate::Sparse, new_view: &crate::Sparse, + already_laid: &std::collections::BTreeSet, ) -> Result<()> { let partial = self.promisor()?.is_some(); - let (laid_out, target) = { + let (mut laid_out, target) = { let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); let mut files = |view: &crate::Sparse| -> Result> { @@ -2116,7 +2120,13 @@ impl Repo { }; (files(old_view)?, files(new_view)?) }; - self.refuse_untracked_paths_in(op, &laid_out, target.iter(), new_view) + laid_out.extend(already_laid.iter().cloned()); + self.refuse_untracked_paths_in( + op, + &laid_out, + target.iter().filter(|p| !old_view.matches(p)), + new_view, + ) } /// The path-list core of [`Repo::refuse_untracked_overwrite_vs`], for a diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 3b78d0de..eef1faa4 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -332,7 +332,13 @@ impl Repo { } // Widening lays tracked files onto disk: refuse before anything is // stored if one would replace a file the user owns. - self.refuse_untracked_for_view_change("sparse set", snap.root, &old_spec, &spec)?; + self.refuse_untracked_for_view_change( + "sparse set", + snap.root, + &old_spec, + &spec, + &Default::default(), + )?; } // Crash safety: record the spec being replaced before the new one is // stored. If the process dies before the re-lay finishes, the marker @@ -425,6 +431,10 @@ impl Repo { old: &Sparse, new: &Sparse, ) -> Result> { + // Same view: nothing comes into view, so there is nothing to write. + if new.covers(old) && old.covers(new) { + return Ok(Vec::new()); + } // Before any write, for every tree the stopped op still reads over // the new view (the decided root; the tip, which `--abort` and the // completion's `tracked_paths_at` read; a rebase's accumulator): @@ -438,19 +448,42 @@ impl Repo { roots.push(self.snapshot(&st.acc_tip)?.root); } let store_arc = self.vfs().store(); - for root in roots { + for &root in &roots { self.ensure_view(root, new)?; - { - let mut s = store_arc.lock().unwrap(); - crate::worktree::preflight_view(self.layout(), &mut s, root, new)?; - } - self.refuse_untracked_for_view_change("sparse set", root, old, new)?; + let mut s = store_arc.lock().unwrap(); + crate::worktree::preflight_view(self.layout(), &mut s, root, new)?; + } + // Files an interrupted run of this same widen already wrote (same + // bytes as the decided root): a retry may rewrite them, not refuse. + let already_laid = { + let mut s = store_arc.lock().unwrap(); + crate::worktree::added_view_already_laid(self.layout(), &mut s, decided, old, new)? + }; + for root in roots { + self.refuse_untracked_for_view_change("sparse set", root, old, new, &already_laid)?; + } + // A partial clone's abort removes only what its removal walk can see, + // which is the spec's view plus this marker. The files are laid before + // the spec is stored, so record the new view first; a crash then + // leaves a bound that covers them. An earlier interrupted `sparse set` + // may have left its own marker: keep it. + let partial = self.promisor()?.is_some(); + let prev = load_prev(self.layout())?; + if partial { + let bound = prev.as_ref().map_or_else(|| new.clone(), |p| p.union(new)); + store_prev(self.layout(), &bound)?; } let skipped = { let mut s = store_arc.lock().unwrap(); crate::worktree::lay_out_added_view(self.layout(), &mut s, decided, old, new)? }; store(self.layout(), new)?; + if partial { + match prev { + Some(p) => store_prev(self.layout(), &p)?, + None => clear_prev(self.layout())?, + } + } Ok(skipped) } @@ -523,6 +556,7 @@ impl Repo { root, &self.sparse_spec()?, &Sparse::default(), + &Default::default(), )?; } clear(self.layout())?; diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 329bd00b..06ad04df 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -774,6 +774,34 @@ pub(crate) fn preflight_view( Ok(()) } +/// The paths a widened view (`new`, which covers `old`) brings in from `root` +/// that are already on disk with exactly the bytes [`lay_out_added_view`] +/// would write: what an interrupted run of the same widen left behind. +pub(crate) fn added_view_already_laid( + layout: &Layout, + store: &mut Store, + root: ObjectId, + old: &Sparse, + new: &Sparse, +) -> Result> { + let mut laid = std::collections::BTreeSet::new(); + for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { + if old.matches(&path) || perms & PROTECTED != 0 { + continue; + } + let 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[..]) { + laid.insert(path); + } + } + Ok(laid) +} + /// Lay out the files that a widened view (`new`, which covers `old`) brings /// in from `root`, and nothing else: no removal pass, and no rewrite of a path /// `old` already laid out (it may hold conflict markers or the user's diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 1972c0c7..8d0da587 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -180,10 +180,20 @@ every claim below is checked against the shipped code, not the plan. view and runs `refuse_untracked_for_view_change`, then writes only the files `old` did not match (`worktree::lay_out_added_view`) and stores the spec last, so a crash leaves extra out-of-view files, never a - view with files missing (a retry of the same widen after such a crash is - refused by the untracked-file guard; delete the laid files and rerun). The - decided root is the right source: it is what completion carries an - out-of-view path from. For a resumed rebase (`resolved`) the accumulator + view with files missing. A retry treats a file already on disk with the + decided root's bytes as its own earlier write (modes are not compared: + `lay_out_added_view` writes none, #237) and a changed one still refuses. On a + partial clone the new view is recorded in `.sc/sparse_prev` before the + writes, so the abort's removal walk covers files an interrupted widen + laid; the marker is restored to its prior state once the spec is stored. + The untracked-file guard checks only paths the old view did not match + (those it laid out are not written, and a stopped op may have turned one + into a directory). Widening to the same spec is a quiet no-op. The + decided root is the right source: it is what completion is meant to carry + an out-of-view path from (not always true today: a clean out-of-view + deletion by the other side can be resurrected from the tip on a full + clone, #238, which is why a widen *before* completing yields the right tree + and a narrow view does not). For a resumed rebase (`resolved`) the accumulator can differ from it outside the view, but the next `--continue` re-lays the tree from the accumulator under the new spec, so what the widen laid is overwritten there. A full spec is `disable` and stays refused. Markers, the resolution, From 4f779c8eacc2c38c3b49e301e8c762b95f59c51b Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 12:46:14 +0300 Subject: [PATCH 03/10] fix(repo): record a stopped-op widen's writes so a retry cannot adopt user files (#224) The retry exemption now needs durable evidence: the widen records the paths it is about to write in .sc/sparse-widen, and only a recorded file with the decided root's bytes counts as its own earlier write. Files are written atomically. The record is removed with the spec write or with the stopped op. Test assertions use the real sparse-prev path. Refs #224 --- crates/repo/src/conflict_plaintext.rs | 4 +- crates/repo/src/layout.rs | 7 ++++ crates/repo/src/partial_merge.rs | 4 +- crates/repo/src/replay.rs | 10 ++++- crates/repo/src/sparse.rs | 48 +++++++++++++++++++++-- crates/repo/src/worktree.rs | 56 +++++++++++++++++++-------- docs/adr/0034-sparse-checkouts.md | 10 +++-- 7 files changed, 112 insertions(+), 27 deletions(-) diff --git a/crates/repo/src/conflict_plaintext.rs b/crates/repo/src/conflict_plaintext.rs index bdc6b2c4..d3a33ac2 100644 --- a/crates/repo/src/conflict_plaintext.rs +++ b/crates/repo/src/conflict_plaintext.rs @@ -48,8 +48,10 @@ pub(crate) fn read(layout: &Layout) -> Result> { } } -/// Drop both records. +/// Drop both records, and the stopped-op widen's (`.sc/sparse-widen`): all of +/// them end with the stopped merge, pick or rebase. pub(crate) fn clear(layout: &Layout) -> Result<()> { + remove_if_present(&layout.sparse_widen_path())?; remove_if_present(&path(layout))?; remove_if_present(&sidecar_path(layout)) } diff --git a/crates/repo/src/layout.rs b/crates/repo/src/layout.rs index 6ba9db37..607a63f0 100644 --- a/crates/repo/src/layout.rs +++ b/crates/repo/src/layout.rs @@ -124,6 +124,13 @@ impl Layout { pub fn sparse_prev_path(&self) -> PathBuf { self.dot_sc.join("sparse-prev") } + /// `.sc/sparse-widen` — the paths a `sparse set` widen during a stopped + /// merge, pick or rebase is writing, recorded before the writes. It is the + /// evidence that a file found at one of them on a retry is the widen's own + /// earlier write and not the user's; removed once the spec is stored. + pub fn sparse_widen_path(&self) -> PathBuf { + self.dot_sc.join("sparse-widen") + } /// `.sc/serve-tokens.toml` — server access-control tokens (P29): each entry is /// `{label, hash = BLAKE3(raw token), scope}`. Presence of ≥1 entry turns on /// bearer auth for `sc serve --http`. Distinct from `recipients.toml` diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index b4e3a821..6aff510f 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -414,13 +414,13 @@ fn aborting_a_widened_partial_merge_leaves_no_residue() { .unwrap(); } else { clone.set_sparse(wide.prefixes(), None).unwrap(); - assert!(!fx.clone_root.join(".sc/sparse_prev").exists()); + assert!(!clone.layout().sparse_prev_path().exists()); } assert!(fx.clone_root.join("docs/new.md").exists(), "laid"); clone.merge_abort().unwrap(); assert!(!fx.clone_root.join("docs/new.md").exists(), "no residue"); - assert!(!fx.clone_root.join(".sc/sparse_prev").exists()); + assert!(!clone.layout().sparse_prev_path().exists()); assert_eq!( std::fs::read(fx.clone_root.join("src/a.txt")).unwrap(), b"uno\ntwo\nthree\n" diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index f7b5bf89..c7ccd0cb 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -3070,8 +3070,10 @@ mod tests { let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-widen-abort"); repo.set_sparse(&["src/".into(), "docs/".into()], None) .unwrap(); + crate::sparse::store_widen(repo.layout(), &["docs/y.txt".to_string()].into()).unwrap(); repo.rebase_abort().unwrap(); assert!(!repo.rebase_in_progress()); + assert!(!repo.layout().sparse_widen_path().exists()); for (path, want) in [ ("src/a.txt", &b"THEIRS\nl2\nl3\n"[..]), ("docs/x.txt", b"THEIRS\nx2\nx3\n"), @@ -3323,12 +3325,18 @@ mod tests { assert!(root.join("docs/y.txt").exists(), "laid, spec not stored"); assert_eq!(repo.sparse_spec().unwrap(), old); - // A file the user changed since is still theirs and still refused. + // Without the widen's record these byte-equal files are the user's. + assert!(repo.set_sparse(new.prefixes(), None).is_err()); + let paths: std::collections::BTreeSet = + ["docs/x.txt".to_string(), "docs/y.txt".to_string()].into(); + crate::sparse::store_widen(repo.layout(), &paths).unwrap(); + // A recorded file the user changed since is still refused. std::fs::write(root.join("docs/y.txt"), b"MINE\n").unwrap(); assert!(repo.set_sparse(new.prefixes(), None).is_err()); std::fs::write(root.join("docs/y.txt"), b"Y-work\n").unwrap(); repo.set_sparse(new.prefixes(), None).unwrap(); + assert!(!repo.layout().sparse_widen_path().exists()); // The same spec again is a quiet no-op. assert!(repo.set_sparse(new.prefixes(), None).unwrap().is_empty()); assert!(matches!( diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index eef1faa4..fbfb3491 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -173,6 +173,29 @@ pub(crate) fn clear_prev(layout: &Layout) -> Result<()> { remove_if_exists(&layout.sparse_prev_path()) } +/// The paths an in-flight stopped-op widen recorded writing (empty if none). +pub(crate) fn load_widen(layout: &Layout) -> Result> { + match std::fs::read_to_string(layout.sparse_widen_path()) { + Ok(text) => Ok(text + .lines() + .filter(|l| !l.is_empty()) + .map(String::from) + .collect()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Default::default()), + Err(e) => Err(e.into()), + } +} + +/// Record `paths` as the files a widen is about to write. +pub(crate) fn store_widen( + layout: &Layout, + paths: &std::collections::BTreeSet, +) -> Result<()> { + let text: String = paths.iter().map(|p| format!("{p}\n")).collect(); + scl_core::fsutil::atomic_write_durable(&layout.sparse_widen_path(), text.as_bytes())?; + Ok(()) +} + impl Sparse { /// A spec that matches whatever either input matches. A full (empty) spec /// is full in the union. @@ -453,15 +476,33 @@ impl Repo { let mut s = store_arc.lock().unwrap(); crate::worktree::preflight_view(self.layout(), &mut s, root, new)?; } - // Files an interrupted run of this same widen already wrote (same - // bytes as the decided root): a retry may rewrite them, not refuse. + // Files an interrupted run of this same widen already wrote (recorded + // in `.sc/sparse-widen`, bytes equal to the decided root's): a retry + // may rewrite them. Anything else at those paths is the user's. + let recorded = load_widen(self.layout())?; let already_laid = { let mut s = store_arc.lock().unwrap(); - crate::worktree::added_view_already_laid(self.layout(), &mut s, decided, old, new)? + crate::worktree::added_view_already_laid( + self.layout(), + &mut s, + decided, + old, + new, + &recorded, + )? }; for root in roots { self.refuse_untracked_for_view_change("sparse set", root, old, new, &already_laid)?; } + // Every guard passed, so each path below is the widen's to write: + // record them (keeping an earlier interrupted run's) before the first. + let mut written = recorded; + { + let mut s = store_arc.lock().unwrap(); + let (plain, _) = crate::worktree::added_view_files(&mut s, decided, old, new)?; + written.extend(plain.into_iter().map(|(p, _)| p)); + } + store_widen(self.layout(), &written)?; // A partial clone's abort removes only what its removal walk can see, // which is the spec's view plus this marker. The files are laid before // the spec is stored, so record the new view first; a crash then @@ -478,6 +519,7 @@ impl Repo { crate::worktree::lay_out_added_view(self.layout(), &mut s, decided, old, new)? }; store(self.layout(), new)?; + remove_if_exists(&self.layout().sparse_widen_path())?; if partial { match prev { Some(p) => store_prev(self.layout(), &p)?, diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 06ad04df..fba68017 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -774,19 +774,47 @@ pub(crate) fn preflight_view( Ok(()) } -/// The paths a widened view (`new`, which covers `old`) brings in from `root` -/// that are already on disk with exactly the bytes [`lay_out_added_view`] -/// would write: what an interrupted run of the same widen left behind. +/// `(plain files with their blob ids, protected paths)` a widened view adds. +type AddedViewFiles = (Vec<(String, ObjectId)>, Vec); + +/// The plain (unprotected) files a widened view (`new`, which covers `old`) +/// brings in from `root`, with their blob ids: what [`lay_out_added_view`] +/// writes. Protected entries are returned apart, as `(plain, protected)`. +pub(crate) fn added_view_files( + store: &mut Store, + root: ObjectId, + old: &Sparse, + new: &Sparse, +) -> Result { + let (mut plain, mut protected) = (Vec::new(), Vec::new()); + for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { + if old.matches(&path) { + continue; + } + if perms & PROTECTED != 0 { + protected.push(path); + } else { + plain.push((path, blob_id)); + } + } + Ok((plain, protected)) +} + +/// Of the paths an interrupted widen recorded in `.sc/sparse-widen` +/// (`recorded`), those whose file on disk has exactly the bytes +/// [`lay_out_added_view`] would write: the widen's own earlier writes. A file +/// that merely has the same bytes but was never recorded is the user's. pub(crate) fn added_view_already_laid( layout: &Layout, store: &mut Store, root: ObjectId, old: &Sparse, new: &Sparse, + recorded: &std::collections::BTreeSet, ) -> Result> { let mut laid = std::collections::BTreeSet::new(); - for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { - if old.matches(&path) || perms & PROTECTED != 0 { + for (path, blob_id) in added_view_files(store, root, old, new)?.0 { + if !recorded.contains(&path) { continue; } let Object::Blob(bytes) = store.get(&blob_id)? else { @@ -806,7 +834,8 @@ pub(crate) fn added_view_already_laid( /// in from `root`, and nothing else: no removal pass, and no rewrite of a path /// `old` already laid out (it may hold conflict markers or the user's /// resolution). A protected entry is not decrypted: it is left absent and -/// returned, since the caller has no wrap map for `root`. +/// returned, since the caller has no wrap map for `root`. Each file is written +/// atomically, so an interruption never leaves a half-written one. pub(crate) fn lay_out_added_view( layout: &Layout, store: &mut Store, @@ -814,15 +843,8 @@ pub(crate) fn lay_out_added_view( old: &Sparse, new: &Sparse, ) -> Result> { - let mut skipped = Vec::new(); - for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { - if old.matches(&path) { - continue; - } - if perms & PROTECTED != 0 { - skipped.push(path); - continue; - } + let (plain, protected) = added_view_files(store, root, old, new)?; + for (path, blob_id) in plain { let Object::Blob(bytes) = store.get(&blob_id)? else { continue; }; @@ -830,9 +852,9 @@ pub(crate) fn lay_out_added_view( if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } - std::fs::write(&full, &bytes[..])?; + scl_core::fsutil::atomic_write_durable(&full, &bytes[..])?; } - Ok(skipped) + Ok(protected) } /// Materialize a snapshot's file tree into the working dir. diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 8d0da587..9985bf94 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -180,9 +180,13 @@ every claim below is checked against the shipped code, not the plan. view and runs `refuse_untracked_for_view_change`, then writes only the files `old` did not match (`worktree::lay_out_added_view`) and stores the spec last, so a crash leaves extra out-of-view files, never a - view with files missing. A retry treats a file already on disk with the - decided root's bytes as its own earlier write (modes are not compared: - `lay_out_added_view` writes none, #237) and a changed one still refuses. On a + view with files missing. The paths are recorded in `.sc/sparse-widen` before + the first write (each file is written atomically) and the record is + removed with the spec write or with the stopped op. A retry treats a + recorded file already on disk with the decided root's bytes as its own + earlier write; an unrecorded file, even with equal bytes, or a changed one + still refuses (modes are not compared: `lay_out_added_view` writes none, + #237). On a partial clone the new view is recorded in `.sc/sparse_prev` before the writes, so the abort's removal walk covers files an interrupted widen laid; the marker is restored to its prior state once the spec is stored. From cdcf0a9078cde8958997b3a4c84f5a6d4f8565d8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 13:27:10 +0300 Subject: [PATCH 04/10] fix(repo): remove what an interrupted stopped-op widen laid on abort and completion (#224) The abort's removal walk follows the other side's tree and the restored view, so it left ours-only files and never-fetched subtrees an interrupted widen had written. Abort (merge, pick, rebase) and completion now delete the files the .sc/sparse-widen record names that lie outside the view and still have the decided tree's bytes. The record carries the decided-root id and is ignored when it does not match. Docs: sparse-prev name, CLAUDE.md note. Refs #224 --- CLAUDE.md | 4 + crates/repo/src/partial_merge.rs | 38 +++++++++ crates/repo/src/replay.rs | 131 +++++++++++++++++++++++++++++- crates/repo/src/repo.rs | 5 ++ crates/repo/src/sparse.rs | 93 ++++++++++++++++++--- crates/repo/src/worktree.rs | 49 ++++++++--- docs/adr/0034-sparse-checkouts.md | 12 ++- 7 files changed, 302 insertions(+), 30 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 0f0205bb..0c1f9f66 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -264,6 +264,10 @@ transport-adjacent. The rest, imperatively: sidecars it wrote are listed apart in `.sc/CONFLICT_SIDECARS`; abort, `rebase --continue` and `sc resolve` remove only those, never a `.theirs` file the stop did not write; the completing commit leaves them out of the tree and removes them. + A stopped-op `sparse set` widen records the files it is writing in + `.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. - **A merge, switch, cherry-pick, rebase, undo or sparse-spec change refuses, before any write, to lay a file or directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it away and retry. Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded. diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 6aff510f..83c93573 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -431,6 +431,44 @@ fn aborting_a_widened_partial_merge_leaves_no_residue() { } } +/// On a partial clone an interrupted widen laid a subtree only ours has (theirs' +/// docs tree was never fetched, so the abort's walk over theirs cannot see it). +/// The abort removes those files using the widen's record, and a fresh widen +/// then succeeds. +#[test] +fn abort_after_an_interrupted_partial_widen_removes_the_subtree_it_laid() { + let fx = Fx::origin("abort-widen-ours-only", |_| {}); + // Cut before main gains its own docs file, so theirs never touches docs/. + fx.origin_branch("b1", &[("src/a.txt", b"ONE\ntwo\nthree\n")]); + std::fs::write(fx.origin_root.join("docs/mine.md"), b"ours only\n").unwrap(); + fx.origin.commit("t", "ours docs").unwrap(); + let clone = fx.clone(true); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + let old = clone.sparse_spec().unwrap(); + let wide = crate::Sparse::new(vec!["src/".to_string(), "docs/".to_string()]); + let decided = crate::merge_state::read_decided_root(clone.layout()) + .unwrap() + .unwrap(); + clone.lay_out_widened_view(decided, &old, &wide).unwrap(); + assert!(fx.clone_root.join("docs/mine.md").exists(), "laid"); + + clone.merge_abort().unwrap(); + assert!(!fx.clone_root.join("docs/mine.md").exists(), "no residue"); + assert!(!clone.layout().sparse_widen_path().exists()); + assert!(!clone.layout().sparse_prev_path().exists()); + clone.set_sparse(wide.prefixes(), None).unwrap(); + assert_eq!( + std::fs::read(fx.clone_root.join("docs/mine.md")).unwrap(), + b"ours only\n" + ); + drop(clone); + fx.cleanup(); +} + /// Aborting restores ours and removes the markers; the carried subtree was /// never on disk and the ref never moved. #[test] diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index c7ccd0cb..dd701449 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -663,6 +663,7 @@ impl Repo { self.remove_conflict_plaintext(*root)?; } self.remove_conflict_sidecars(&sidecar_roots)?; + self.remove_widen_residue(decided)?; crate::pick_state::clear(&self.layout)?; Ok(skipped) } @@ -1086,6 +1087,8 @@ impl Repo { Some(&mut cache), )?; } + // Files an interrupted widen of the last stop laid outside the view. + self.remove_widen_residue(crate::rebase_state::read_decided_root(&self.layout)?)?; refs::write_branch_tip(&self.layout, &head, &acc_tip)?; cache.save_best_effort(); crate::oplog::record( @@ -1306,6 +1309,7 @@ impl Repo { }; 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) } @@ -3070,7 +3074,11 @@ mod tests { let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-widen-abort"); repo.set_sparse(&["src/".into(), "docs/".into()], None) .unwrap(); - crate::sparse::store_widen(repo.layout(), &["docs/y.txt".to_string()].into()).unwrap(); + let decided = crate::rebase_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + crate::sparse::store_widen(repo.layout(), decided, &["lib/z.txt".to_string()].into()) + .unwrap(); repo.rebase_abort().unwrap(); assert!(!repo.rebase_in_progress()); assert!(!repo.layout().sparse_widen_path().exists()); @@ -3329,7 +3337,7 @@ mod tests { assert!(repo.set_sparse(new.prefixes(), None).is_err()); let paths: std::collections::BTreeSet = ["docs/x.txt".to_string(), "docs/y.txt".to_string()].into(); - crate::sparse::store_widen(repo.layout(), &paths).unwrap(); + crate::sparse::store_widen(repo.layout(), decided, &paths).unwrap(); // A recorded file the user changed since is still refused. std::fs::write(root.join("docs/y.txt"), b"MINE\n").unwrap(); assert!(repo.set_sparse(new.prefixes(), None).is_err()); @@ -3431,6 +3439,125 @@ mod tests { assert_eq!(run(true), run(false)); } + /// `main` has `docs/mine.md` (ours-only); `work` edits `src/a.txt` against + /// `main`'s edit. Returns the repo, narrowed to `src/`, with the merge + /// stopped on `src/a.txt`. + fn merge_stopped_with_an_ours_only_file(name: &str) -> (std::path::PathBuf, Repo) { + let root = tmp_root(name); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/base.md"), b"base\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/mine.md"), b"ours only\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + (root, repo) + } + + /// Stop after a widen's writes, before its spec write: the state a crash + /// or an I/O error leaves. + fn interrupt_a_widen(repo: &Repo, decided: ObjectId, new: &Sparse) { + repo.lay_out_widened_view(decided, &repo.sparse_spec().unwrap(), new) + .unwrap(); + } + + /// Abort after an interrupted widen removes the files it wrote, including + /// one only ours has (the abort's walk follows theirs' tree), so a fresh + /// widen is not blocked by them. A file the user edited since stays. + #[test] + fn abort_after_an_interrupted_widen_removes_what_it_laid() { + for edited in [false, true] { + let (root, repo) = merge_stopped_with_an_ours_only_file("sparse-widen-int-abort"); + let wide = Sparse::new(vec!["src/".into(), "docs/".into()]); + let decided = crate::merge_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + interrupt_a_widen(&repo, decided, &wide); + assert!(root.join("docs/mine.md").exists(), "laid"); + if edited { + std::fs::write(root.join("docs/mine.md"), b"user edit\n").unwrap(); + } + + repo.merge_abort().unwrap(); + assert!(!repo.layout().sparse_widen_path().exists()); + assert_eq!( + repo.sparse_spec().unwrap().prefixes(), + &["src/".to_string()] + ); + assert!(!root.join("docs/base.md").exists()); + if edited { + assert_eq!( + std::fs::read(root.join("docs/mine.md")).unwrap(), + b"user edit\n", + "a changed file is the user's" + ); + remove_root(&root); + continue; + } + assert!(!root.join("docs/mine.md").exists(), "no residue"); + // A fresh widen is not blocked, and brings the files in. + repo.set_sparse(wide.prefixes(), None).unwrap(); + assert_eq!( + std::fs::read(root.join("docs/mine.md")).unwrap(), + b"ours only\n" + ); + drop(repo); + remove_root(&root); + } + } + + /// Completing the stopped merge after an interrupted widen removes the + /// files it laid outside the view too. + #[test] + fn completion_after_an_interrupted_widen_removes_what_it_laid() { + let (root, repo) = merge_stopped_with_an_ours_only_file("sparse-widen-int-commit"); + let wide = Sparse::new(vec!["src/".into(), "docs/".into()]); + let decided = crate::merge_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + interrupt_a_widen(&repo, decided, &wide); + std::fs::write(root.join("src/a.txt"), b"MERGED\nl2\nl3\n").unwrap(); + repo.commit("me", "merge").unwrap(); + assert!(!root.join("docs/mine.md").exists(), "no residue"); + assert!(!repo.layout().sparse_widen_path().exists()); + repo.set_sparse(wide.prefixes(), None).unwrap(); + assert!(root.join("docs/mine.md").exists()); + drop(repo); + remove_root(&root); + } + + /// A record naming another stop's decided root is stale: nothing is + /// removed on its account. + #[test] + fn a_stale_widen_record_is_ignored() { + let (root, repo) = merge_stopped_with_an_ours_only_file("sparse-widen-stale"); + let wide = Sparse::new(vec!["src/".into(), "docs/".into()]); + let decided = crate::merge_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + interrupt_a_widen(&repo, decided, &wide); + let record = std::fs::read_to_string(repo.layout().sparse_widen_path()).unwrap(); + 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"); + drop(repo); + remove_root(&root); + } + /// A merge that is stopped can be widened too: the files that come into /// view are the merge's decided content, and the completion keeps them. #[test] diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 153a8866..f77ca7e8 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -977,6 +977,8 @@ impl Repo { if merging || picking { let root = self.snapshot(&id)?.root; self.remove_conflict_sidecars(&[root])?; + // Likewise the files an interrupted widen laid outside the view. + self.remove_widen_residue(decided_root)?; } refs::write_branch_tip(&self.layout, &head, &id)?; crate::merge_state::clear(&self.layout)?; @@ -2333,6 +2335,9 @@ impl Repo { self.remove_conflict_plaintext(*root)?; } self.remove_conflict_sidecars(&sidecar_roots)?; + // What an interrupted stopped-op widen laid, which the walk above + // (over theirs' tree) cannot see. + self.remove_widen_residue(crate::merge_state::read_decided_root(&self.layout)?)?; crate::merge_state::clear(&self.layout)?; Ok(skipped) } diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index fbfb3491..e2b26451 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -173,25 +173,35 @@ pub(crate) fn clear_prev(layout: &Layout) -> Result<()> { remove_if_exists(&layout.sparse_prev_path()) } -/// The paths an in-flight stopped-op widen recorded writing (empty if none). -pub(crate) fn load_widen(layout: &Layout) -> Result> { +/// The paths an in-flight stopped-op widen recorded writing, if the record +/// belongs to the stop whose decided root is `decided` (its first line); a +/// record from another stop is stale and reads as empty. +pub(crate) fn load_widen( + layout: &Layout, + decided: scl_core::ObjectId, +) -> Result> { match std::fs::read_to_string(layout.sparse_widen_path()) { - Ok(text) => Ok(text - .lines() - .filter(|l| !l.is_empty()) - .map(String::from) - .collect()), + Ok(text) => { + let mut lines = text.lines(); + if lines.next() != Some(decided.to_hex().as_str()) { + return Ok(Default::default()); + } + Ok(lines.filter(|l| !l.is_empty()).map(String::from).collect()) + } Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Default::default()), Err(e) => Err(e.into()), } } -/// Record `paths` as the files a widen is about to write. +/// Record `paths` as the files a widen over the stop with decided root +/// `decided` is about to write. pub(crate) fn store_widen( layout: &Layout, + decided: scl_core::ObjectId, paths: &std::collections::BTreeSet, ) -> Result<()> { - let text: String = paths.iter().map(|p| format!("{p}\n")).collect(); + let mut text = format!("{}\n", decided.to_hex()); + text.extend(paths.iter().map(|p| format!("{p}\n"))); scl_core::fsutil::atomic_write_durable(&layout.sparse_widen_path(), text.as_bytes())?; Ok(()) } @@ -458,6 +468,22 @@ impl Repo { if new.covers(old) && old.covers(new) { return Ok(Vec::new()); } + let (skipped, prev) = self.lay_out_widened_view(decided, old, new)?; + self.finish_widen(new, prev)?; + Ok(skipped) + } + + /// The first half of [`Repo::widen_while_stopped`]: every guard, the + /// `.sc/sparse-widen` record, and the writes, but not the spec. Returns + /// the protected paths left absent and the `sparse-prev` marker to + /// restore. A crash or error after this leaves exactly the state the + /// abort and completion cleanup (`remove_widen_residue`) handles. + pub(crate) fn lay_out_widened_view( + &self, + decided: scl_core::ObjectId, + old: &Sparse, + new: &Sparse, + ) -> Result<(Vec, Option)> { // Before any write, for every tree the stopped op still reads over // the new view (the decided root; the tip, which `--abort` and the // completion's `tracked_paths_at` read; a rebase's accumulator): @@ -479,7 +505,7 @@ impl Repo { // Files an interrupted run of this same widen already wrote (recorded // in `.sc/sparse-widen`, bytes equal to the decided root's): a retry // may rewrite them. Anything else at those paths is the user's. - let recorded = load_widen(self.layout())?; + let recorded = load_widen(self.layout(), decided)?; let already_laid = { let mut s = store_arc.lock().unwrap(); crate::worktree::added_view_already_laid( @@ -502,7 +528,7 @@ impl Repo { let (plain, _) = crate::worktree::added_view_files(&mut s, decided, old, new)?; written.extend(plain.into_iter().map(|(p, _)| p)); } - store_widen(self.layout(), &written)?; + store_widen(self.layout(), decided, &written)?; // A partial clone's abort removes only what its removal walk can see, // which is the spec's view plus this marker. The files are laid before // the spec is stored, so record the new view first; a crash then @@ -518,15 +544,56 @@ impl Repo { let mut s = store_arc.lock().unwrap(); crate::worktree::lay_out_added_view(self.layout(), &mut s, decided, old, new)? }; + Ok((skipped, prev)) + } + + /// The second half: store the spec, drop the record, and put back any + /// `sparse-prev` marker an earlier interrupted `sparse set` left. + fn finish_widen(&self, new: &Sparse, prev: Option) -> Result<()> { store(self.layout(), new)?; remove_if_exists(&self.layout().sparse_widen_path())?; - if partial { + if self.promisor()?.is_some() { match prev { Some(p) => store_prev(self.layout(), &p)?, None => clear_prev(self.layout())?, } } - Ok(skipped) + Ok(()) + } + + /// Delete the files an interrupted stopped-op widen wrote, when the stop + /// ends (abort or completion) with the old view still in force. Abort's + /// removal walk follows the other side's tree and the restored view, so it + /// cannot see an ours-only file or a subtree it never fetched; this uses + /// the `.sc/sparse-widen` record instead. A path is removed only if the + /// record names `decided` (a stale record from another stop is ignored), + /// the path is outside the current view, and its bytes still equal the + /// decided tree's: a file the user edited since is theirs. Call it before + /// the stopped op's state, which removes the record, is cleared. + pub(crate) fn remove_widen_residue(&self, decided: Option) -> Result<()> { + let Some(decided) = decided else { + return Ok(()); + }; + let recorded = load_widen(self.layout(), decided)?; + if recorded.is_empty() { + return Ok(()); + } + let view = self.sparse_spec()?; + let outside: std::collections::BTreeSet = + recorded.into_iter().filter(|p| !view.matches(p)).collect(); + let laid = { + let store_arc = self.vfs().store(); + let mut s = store_arc.lock().unwrap(); + crate::worktree::recorded_paths_laid(self.layout(), &mut s, decided, &outside)? + }; + for path in laid { + match std::fs::remove_file(crate::worktree::safe_join(&self.layout.root, &path)?) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + } + Ok(()) } /// The prefixes in `prefixes` that match no tracked path at HEAD, so a diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index fba68017..b3c33a2e 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -800,36 +800,59 @@ pub(crate) fn added_view_files( Ok((plain, protected)) } -/// Of the paths an interrupted widen recorded in `.sc/sparse-widen` -/// (`recorded`), those whose file on disk has exactly the bytes -/// [`lay_out_added_view`] would write: the widen's own earlier writes. A file -/// that merely has the same bytes but was never recorded is the user's. -pub(crate) fn added_view_already_laid( +/// 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 +/// never recorded is the user's, which is why callers pass only recorded paths. +pub(crate) fn recorded_paths_laid( layout: &Layout, store: &mut Store, root: ObjectId, - old: &Sparse, - new: &Sparse, - recorded: &std::collections::BTreeSet, + paths: &std::collections::BTreeSet, ) -> Result> { + let partial = crate::promisor::load(layout)?.is_some(); let mut laid = std::collections::BTreeSet::new(); - for (path, blob_id) in added_view_files(store, root, old, new)?.0 { - if !recorded.contains(&path) { + for path in paths { + // A lookup that fails (a gap) reads as "not laid". + let Ok(Some((blob_id, _, perms))) = tree_entry_at(store, root, path, partial) else { + continue; + }; + if perms & PROTECTED != 0 { continue; } - let Object::Blob(bytes) = store.get(&blob_id)? else { + let Ok(Object::Blob(bytes)) = store.get(&blob_id) else { continue; }; - let full = safe_join(&layout.root, &path)?; + 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[..]) { - laid.insert(path); + laid.insert(path.clone()); } } Ok(laid) } +/// Of the paths an interrupted widen recorded (`recorded`), those the widen +/// from `old` to `new` would write that are already on disk with the decided +/// root's bytes: the widen's own earlier writes. +pub(crate) fn added_view_already_laid( + layout: &Layout, + store: &mut Store, + root: ObjectId, + old: &Sparse, + new: &Sparse, + recorded: &std::collections::BTreeSet, +) -> Result> { + let added: std::collections::BTreeSet = added_view_files(store, root, old, new)? + .0 + .into_iter() + .map(|(p, _)| p) + .filter(|p| recorded.contains(p)) + .collect(); + recorded_paths_laid(layout, store, root, &added) +} + /// Lay out the files that a widened view (`new`, which covers `old`) brings /// in from `root`, and nothing else: no removal pass, and no rewrite of a path /// `old` already laid out (it may hold conflict markers or the user's diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 9985bf94..896266af 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -186,8 +186,16 @@ every claim below is checked against the shipped code, not the plan. recorded file already on disk with the decided root's bytes as its own earlier write; an unrecorded file, even with equal bytes, or a changed one still refuses (modes are not compared: `lay_out_added_view` writes none, - #237). On a - partial clone the new view is recorded in `.sc/sparse_prev` before the + #237). The record starts with the stop's decided-root id and is ignored + when that does not match (a stale record). When the stop ends with the old + view still in force (abort of a merge, pick or rebase, or the completion + after an interrupted widen) `Repo::remove_widen_residue` deletes the + recorded files that lie outside the view and still have the decided tree's + 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. On a + partial clone the new view is recorded in `.sc/sparse-prev` before the writes, so the abort's removal walk covers files an interrupted widen laid; the marker is restored to its prior state once the spec is stored. The untracked-file guard checks only paths the old view did not match From ca3cf04803f6b0c69da26140c609d227c7156003 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 13:56:39 +0300 Subject: [PATCH 05/10] fix(repo): clean widen residue before completion reads the tree; keep a changed retry's record (#224) Completion removes the files an interrupted stopped-op widen laid outside the view before it reads the working tree, so they are never taken as input. A retry of the widen with a different target keeps the earlier target's paths in .sc/sparse-widen, so abort and completion still clean them. Refs #224 --- crates/repo/src/replay.rs | 30 ++++++++++++++++++++++++++++++ crates/repo/src/repo.rs | 7 +++++-- crates/repo/src/sparse.rs | 23 ++++++++++++++++++++++- docs/adr/0034-sparse-checkouts.md | 3 ++- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index dd701449..e15793c2 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -1208,6 +1208,8 @@ impl Repo { // construction; `rebase` refuses up front if a merge commit is // in the replayed range), so the registry base is always the // conflicted commit's own first parent. + // Before the working tree is read (see `commit`). + self.remove_widen_residue(decided)?; let new_tip = self.assemble_completion_snapshot( st.acc_tip, st.conflicted, @@ -3360,6 +3362,34 @@ mod tests { remove_root(&root); } + /// A widen interrupted after its writes, retried with a different target: + /// the first target's files stay recorded, and the abort removes them. + #[test] + fn a_changed_retry_keeps_the_first_widens_record_for_the_abort() { + let (root, repo) = rebase_refused_at_an_out_of_view_conflict("sparse-rebase-retry-other"); + let old = repo.sparse_spec().unwrap(); + let first = Sparse::new(vec!["src/".into(), "docs/".into()]); + let decided = crate::rebase_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + repo.lay_out_widened_view(decided, &old, &first).unwrap(); + assert!(root.join("docs/y.txt").exists()); + + repo.set_sparse(&["src/".into(), "lib/".into()], None) + .unwrap(); + assert!(root.join("lib/z.txt").exists()); + assert!( + repo.layout().sparse_widen_path().exists(), + "the docs/ files are still recorded" + ); + repo.rebase_abort().unwrap(); + assert!(!root.join("docs/y.txt").exists()); + assert!(!root.join("docs/x.txt").exists()); + assert!(!repo.layout().sparse_widen_path().exists()); + drop(repo); + remove_root(&root); + } + /// `main` and `work` both edit `src/a.txt` (a conflict); `work` also /// deletes `docs/del.txt` and adds `docs/new.txt`, both cleanly and in /// view only after a widen. Runs `op` with the view narrowed to `src/` and diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 68067a2b..356d6ccc 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -938,6 +938,11 @@ impl Repo { // in the pick arm avoids double-managing (and clobbering) the cache // `assemble_completion_snapshot` already saved. let mut cache: Option = None; + // Files an interrupted widen laid outside the view go before the + // working tree is read, or they would be read as completion input. + if merging || picking { + self.remove_widen_residue(decided_root)?; + } let id = match (tip, merge_head, pick_head) { (Some(t), None, Some(ph)) => self.assemble_completion_snapshot( t, @@ -975,8 +980,6 @@ impl Repo { if merging || picking { let root = self.snapshot(&id)?.root; self.remove_conflict_sidecars(&[root])?; - // Likewise the files an interrupted widen laid outside the view. - self.remove_widen_residue(decided_root)?; } refs::write_branch_tip(&self.layout, &head, &id)?; crate::merge_state::clear(&self.layout)?; diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index e2b26451..47601d32 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -551,7 +551,28 @@ impl Repo { /// `sparse-prev` marker an earlier interrupted `sparse set` left. fn finish_widen(&self, new: &Sparse, prev: Option) -> Result<()> { store(self.layout(), new)?; - remove_if_exists(&self.layout().sparse_widen_path())?; + // Paths an earlier interrupted widen (a different target) laid that + // the new view does not cover stay recorded, so abort and completion + // still clean them up. + let record = self.layout().sparse_widen_path(); + match std::fs::read_to_string(&record) { + Ok(text) => { + let mut lines = text.lines(); + let header = lines.next().unwrap_or_default(); + let kept: Vec<&str> = lines.filter(|p| !p.is_empty() && !new.matches(p)).collect(); + if kept.is_empty() { + remove_if_exists(&record)?; + } else { + let body: String = kept.iter().map(|p| format!("{p}\n")).collect(); + scl_core::fsutil::atomic_write_durable( + &record, + format!("{header}\n{body}").as_bytes(), + )?; + } + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } if self.promisor()?.is_some() { match prev { Some(p) => store_prev(self.layout(), &p)?, diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 4c513f92..9304cf1a 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -194,7 +194,8 @@ 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. On a + left. The cleanup runs before the working tree is read for completion, and a retry with a + different target keeps the earlier target's paths recorded. On a partial clone the new view is recorded in `.sc/sparse-prev` before the writes, so the abort's removal walk covers files an interrupted widen laid; the marker is restored to its prior state once the spec is stored. From f935113c6e0d08acb0949914fc06a0ce5c859c7b Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:16:29 +0300 Subject: [PATCH 06/10] fix(repo): widen-laid files keep the recorded executable bit (#224, #237) lay_out_added_view applies the decided root's mode after its atomic write, via a shared apply_file_mode split out of worktree::write_file. Adds an integration test that a stopped merge widen lays an EXEC file with its bit and that the completing commit records EXEC. Refs #224 --- crates/repo/src/sparse.rs | 2 +- crates/repo/src/worktree.rs | 21 ++++++++++++++------- crates/repo/tests/exec_bit.rs | 28 ++++++++++++++++++++++++++++ docs/adr/0034-sparse-checkouts.md | 4 ++-- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 47601d32..0547ef52 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -526,7 +526,7 @@ impl Repo { { let mut s = store_arc.lock().unwrap(); let (plain, _) = crate::worktree::added_view_files(&mut s, decided, old, new)?; - written.extend(plain.into_iter().map(|(p, _)| p)); + written.extend(plain.into_iter().map(|(p, ..)| p)); } store_widen(self.layout(), decided, &written)?; // A partial clone's abort removes only what its removal walk can see, diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 75ab0ee0..be17f841 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -144,6 +144,12 @@ fn tracked_under(tracked: &BTreeSet, dir: &str) -> bool { /// a filesystem that refuses the chmod (no mode bits) is not an error. pub(crate) fn write_file(path: &Path, bytes: &[u8], mode: FileMode) -> std::io::Result<()> { std::fs::write(path, bytes)?; + apply_file_mode(path, mode) +} + +/// The mode half of [`write_file`], for a caller that wrote the bytes itself +/// (the stopped-op widen writes atomically). +pub(crate) fn apply_file_mode(path: &Path, mode: FileMode) -> std::io::Result<()> { #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; @@ -808,11 +814,11 @@ pub(crate) fn preflight_view( Ok(()) } -/// `(plain files with their blob ids, protected paths)` a widened view adds. -type AddedViewFiles = (Vec<(String, ObjectId)>, Vec); +/// `(plain files with blob id and mode, protected paths)` a widened view adds. +type AddedViewFiles = (Vec<(String, ObjectId, FileMode)>, Vec); /// The plain (unprotected) files a widened view (`new`, which covers `old`) -/// brings in from `root`, with their blob ids: what [`lay_out_added_view`] +/// brings in from `root`, with blob ids and modes: what [`lay_out_added_view`] /// writes. Protected entries are returned apart, as `(plain, protected)`. pub(crate) fn added_view_files( store: &mut Store, @@ -821,14 +827,14 @@ pub(crate) fn added_view_files( new: &Sparse, ) -> Result { let (mut plain, mut protected) = (Vec::new(), Vec::new()); - for (path, (blob_id, _mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { + for (path, (blob_id, mode, perms)) in tree_file_entries_with_perms_sparse(store, root, new)? { if old.matches(&path) { continue; } if perms & PROTECTED != 0 { protected.push(path); } else { - plain.push((path, blob_id)); + plain.push((path, blob_id, mode)); } } Ok((plain, protected)) @@ -881,7 +887,7 @@ pub(crate) fn added_view_already_laid( let added: std::collections::BTreeSet = added_view_files(store, root, old, new)? .0 .into_iter() - .map(|(p, _)| p) + .map(|(p, ..)| p) .filter(|p| recorded.contains(p)) .collect(); recorded_paths_laid(layout, store, root, &added) @@ -901,7 +907,7 @@ pub(crate) fn lay_out_added_view( new: &Sparse, ) -> Result> { let (plain, protected) = added_view_files(store, root, old, new)?; - for (path, blob_id) in plain { + for (path, blob_id, mode) in plain { let Object::Blob(bytes) = store.get(&blob_id)? else { continue; }; @@ -910,6 +916,7 @@ pub(crate) fn lay_out_added_view( std::fs::create_dir_all(parent)?; } scl_core::fsutil::atomic_write_durable(&full, &bytes[..])?; + apply_file_mode(&full, mode)?; } Ok(protected) } diff --git a/crates/repo/tests/exec_bit.rs b/crates/repo/tests/exec_bit.rs index 457b64ed..ff679a32 100644 --- a/crates/repo/tests/exec_bit.rs +++ b/crates/repo/tests/exec_bit.rs @@ -163,6 +163,34 @@ fn sparse_set_lays_out_exec_files_with_their_bit() { assert_round_trips(&repo, &root, "a/run.sh", true, "b/x.txt"); } +/// A stopped merge widened mid-stop (#224): the executable that comes into +/// view keeps its bit, and the completing commit still records EXEC. +#[test] +fn stopped_merge_widen_lays_out_exec_files_with_their_bit() { + let (repo, root) = base("widen"); + put(&root, "seed.txt", "l1\nl2\nl3\n", false); + repo.commit("t", "base").unwrap(); + repo.branch("feat").unwrap(); + put(&root, "seed.txt", "MAIN\nl2\nl3\n", false); + repo.commit("t", "main").unwrap(); + repo.switch("feat").unwrap(); + put(&root, "seed.txt", "FEAT\nl2\nl3\n", false); + put(&root, "bin/run.sh", "#!/bin/sh\n", true); + repo.commit("t", "feat").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["seed.txt".to_string()], None).unwrap(); + assert!(repo.merge("feat", "t").is_err(), "conflict on seed.txt"); + assert!(!root.join("bin/run.sh").exists()); + + repo.set_sparse(&["seed.txt".to_string(), "bin".to_string()], None) + .unwrap(); + assert!(on_disk_exec(&root, "bin/run.sh"), "bit laid by the widen"); + put(&root, "seed.txt", "MERGED\nl2\nl3\n", false); + repo.commit("t", "merge").unwrap(); + assert_eq!(committed(&repo, "bin/run.sh"), FileMode::EXEC); + assert!(on_disk_exec(&root, "bin/run.sh")); +} + #[test] fn clone_keeps_exec_bit() { let (repo, root) = base("clone-src"); diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index 9304cf1a..aa303a50 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -185,8 +185,8 @@ every claim below is checked against the shipped code, not the plan. removed with the spec write or with the stopped op. A retry treats a recorded file already on disk with the decided root's bytes as its own earlier write; an unrecorded file, even with equal bytes, or a changed one - still refuses (modes are not compared: `lay_out_added_view` writes none, - #237). The record starts with the stop's decided-root id and is ignored + still refuses (the file's executable bit is not compared, but + `lay_out_added_view` applies the recorded mode after each atomic write, #237). The record starts with the stop's decided-root id and is ignored when that does not match (a stale record). When the stop ends with the old view still in force (abort of a merge, pick or rebase, or the completion after an interrupted widen) `Repo::remove_widen_residue` deletes the From 4ceb4b6367e75335a46366f882f55063fce6824d Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:31:07 +0300 Subject: [PATCH 07/10] fix(repo): refuse a stopped-op widen over a path with a newline (#224) The widen record is one path per line; refuse before any write. Refs #224 --- crates/repo/src/replay.rs | 42 +++++++++++++++++++++++++++++++++++++++ crates/repo/src/sparse.rs | 7 +++++++ 2 files changed, 49 insertions(+) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index e15793c2..13d603e1 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -3588,6 +3588,48 @@ mod tests { remove_root(&root); } + /// The widen record is one path per line, so a path with a newline is + /// refused before anything is written. + #[cfg(unix)] + #[test] + fn widening_a_stopped_merge_refuses_a_path_with_a_newline() { + let root = tmp_root("sparse-widen-newline"); + let repo = Repo::init(&root).unwrap(); + for d in ["src", "docs"] { + std::fs::create_dir_all(root.join(d)).unwrap(); + } + std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); + repo.commit("me", "base").unwrap(); + repo.branch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + repo.commit("me", "main").unwrap(); + repo.switch("work").unwrap(); + std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); + std::fs::write(root.join("docs/odd\nname.txt"), b"x\n").unwrap(); + repo.commit("me", "work").unwrap(); + repo.switch("main").unwrap(); + repo.set_sparse(&["src/".into()], None).unwrap(); + assert!(matches!( + repo.merge("work", "me").unwrap_err(), + Error::MergeConflicts(1) + )); + let err = repo + .set_sparse(&["src/".into(), "docs/".into()], None) + .unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("newline")), + "{err:?}" + ); + assert_eq!( + repo.sparse_spec().unwrap().prefixes(), + &["src/".to_string()] + ); + assert!(!repo.layout().sparse_widen_path().exists()); + repo.merge_abort().unwrap(); + drop(repo); + remove_root(&root); + } + /// A merge that is stopped can be widened too: the files that come into /// view are the merge's decided content, and the completion keeps them. #[test] diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 0547ef52..3c57d005 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -526,6 +526,13 @@ impl Repo { { let mut s = store_arc.lock().unwrap(); let (plain, _) = crate::worktree::added_view_files(&mut s, decided, old, new)?; + // The record is one path per line. + if let Some((p, ..)) = plain.iter().find(|(p, ..)| p.contains('\n')) { + return Err(Error::InvalidArgument(format!( + "cannot widen while an operation is stopped: {p:?} contains a newline; \ + finish or abort the operation first" + ))); + } written.extend(plain.into_iter().map(|(p, ..)| p)); } store_widen(self.layout(), decided, &written)?; From f1915124a8dd971008140c321fbad40f5fa4eaa7 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 14:48:05 +0300 Subject: [PATCH 08/10] fix(repo): NUL-separate the stopped-op widen record (#224) The record was newline-delimited and read with lines(), which also strips a trailing carriage return, so a path ending in one round-tripped differently than it was written. It is now NUL-separated and every reader and rewriter uses the same splitter, like CONFLICT_PLAINTEXT. The newline refusal is dropped: such paths are recorded exactly. Test: odd names round-trip, abort removes them and leaves the user's lookalike files. Refs #224 --- crates/repo/src/replay.rs | 56 +++++++++++++++++++++---------- crates/repo/src/sparse.rs | 54 ++++++++++++++++++----------- docs/adr/0034-sparse-checkouts.md | 4 ++- 3 files changed, 75 insertions(+), 39 deletions(-) diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 13d603e1..764e781f 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -3588,12 +3588,14 @@ mod tests { remove_root(&root); } - /// The widen record is one path per line, so a path with a newline is - /// refused before anything is written. + /// The widen record is NUL-separated and read with the splitter that wrote + /// it: paths holding a newline, a carriage return, or ending in one + /// round-trip exactly, so the abort removes those files and never a + /// lookalike the user owns. #[cfg(unix)] #[test] - fn widening_a_stopped_merge_refuses_a_path_with_a_newline() { - let root = tmp_root("sparse-widen-newline"); + fn the_widen_record_round_trips_paths_with_newlines_and_carriage_returns() { + let root = tmp_root("sparse-widen-odd-names"); let repo = Repo::init(&root).unwrap(); for d in ["src", "docs"] { std::fs::create_dir_all(root.join(d)).unwrap(); @@ -3601,11 +3603,14 @@ mod tests { std::fs::write(root.join("src/a.txt"), b"l1\nl2\nl3\n").unwrap(); repo.commit("me", "base").unwrap(); repo.branch("work").unwrap(); + let odd = ["docs/a\rb.txt", "docs/c\nd.txt", "docs/e\r", "docs/f\r\n"]; std::fs::write(root.join("src/a.txt"), b"OURS\nl2\nl3\n").unwrap(); + for p in odd { + std::fs::write(root.join(p), b"ours only\n").unwrap(); + } repo.commit("me", "main").unwrap(); repo.switch("work").unwrap(); std::fs::write(root.join("src/a.txt"), b"THEIRS\nl2\nl3\n").unwrap(); - std::fs::write(root.join("docs/odd\nname.txt"), b"x\n").unwrap(); repo.commit("me", "work").unwrap(); repo.switch("main").unwrap(); repo.set_sparse(&["src/".into()], None).unwrap(); @@ -3613,21 +3618,36 @@ mod tests { repo.merge("work", "me").unwrap_err(), Error::MergeConflicts(1) )); - let err = repo - .set_sparse(&["src/".into(), "docs/".into()], None) - .unwrap_err(); - assert!( - matches!(&err, Error::InvalidArgument(m) if m.contains("newline")), - "{err:?}" - ); - assert_eq!( - repo.sparse_spec().unwrap().prefixes(), - &["src/".to_string()] - ); - assert!(!repo.layout().sparse_widen_path().exists()); + // The user's own files, named like the odd ones with the control + // characters cut off. + let mine = ["docs/e", "docs/f", "docs/c", "docs/a"]; + for p in mine { + std::fs::write(root.join(p), b"mine\n").unwrap(); + } + let decided = crate::merge_state::read_decided_root(repo.layout()) + .unwrap() + .unwrap(); + interrupt_a_widen( + &repo, + decided, + &Sparse::new(vec!["src/".into(), "docs/".into()]), + ); + let recorded = crate::sparse::load_widen(repo.layout(), decided).unwrap(); + let want: std::collections::BTreeSet = odd.iter().map(|p| p.to_string()).collect(); + assert_eq!(recorded, want, "exactly the laid paths, byte for byte"); + for p in odd { + assert!(root.join(p).exists(), "{p:?} laid"); + } + repo.merge_abort().unwrap(); + for p in odd { + assert!(!root.join(p).exists(), "{p:?} removed"); + } + for p in mine { + assert_eq!(std::fs::read(root.join(p)).unwrap(), b"mine\n", "{p}"); + } drop(repo); - remove_root(&root); + std::fs::remove_dir_all(&root).unwrap(); } /// A merge that is stopped can be widened too: the files that come into diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 3c57d005..9354f07d 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -182,17 +182,37 @@ pub(crate) fn load_widen( ) -> Result> { match std::fs::read_to_string(layout.sparse_widen_path()) { Ok(text) => { - let mut lines = text.lines(); - if lines.next() != Some(decided.to_hex().as_str()) { + let mut fields = widen_fields(&text); + if fields.next() != Some(decided.to_hex().as_str()) { return Ok(Default::default()); } - Ok(lines.filter(|l| !l.is_empty()).map(String::from).collect()) + Ok(fields.map(String::from).collect()) } Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Default::default()), Err(e) => Err(e.into()), } } +/// The record's fields: the decided-root id, then one path each. NUL-separated +/// (a path can hold a newline or a carriage return, never a NUL), and the one +/// splitter every reader and rewriter of the record uses. +fn widen_fields(text: &str) -> impl Iterator { + text.split('\0').filter(|f| !f.is_empty()) +} + +/// Serialize a record: `decided`, then `paths`, each followed by a NUL. +fn widen_record<'a>( + decided: scl_core::ObjectId, + paths: impl IntoIterator, +) -> String { + let mut text = format!("{}\0", decided.to_hex()); + for p in paths { + text.push_str(p); + text.push('\0'); + } + text +} + /// Record `paths` as the files a widen over the stop with decided root /// `decided` is about to write. pub(crate) fn store_widen( @@ -200,8 +220,7 @@ pub(crate) fn store_widen( decided: scl_core::ObjectId, paths: &std::collections::BTreeSet, ) -> Result<()> { - let mut text = format!("{}\n", decided.to_hex()); - text.extend(paths.iter().map(|p| format!("{p}\n"))); + let text = widen_record(decided, paths.iter().map(String::as_str)); scl_core::fsutil::atomic_write_durable(&layout.sparse_widen_path(), text.as_bytes())?; Ok(()) } @@ -526,13 +545,6 @@ impl Repo { { let mut s = store_arc.lock().unwrap(); let (plain, _) = crate::worktree::added_view_files(&mut s, decided, old, new)?; - // The record is one path per line. - if let Some((p, ..)) = plain.iter().find(|(p, ..)| p.contains('\n')) { - return Err(Error::InvalidArgument(format!( - "cannot widen while an operation is stopped: {p:?} contains a newline; \ - finish or abort the operation first" - ))); - } written.extend(plain.into_iter().map(|(p, ..)| p)); } store_widen(self.layout(), decided, &written)?; @@ -564,17 +576,19 @@ impl Repo { let record = self.layout().sparse_widen_path(); match std::fs::read_to_string(&record) { Ok(text) => { - let mut lines = text.lines(); - let header = lines.next().unwrap_or_default(); - let kept: Vec<&str> = lines.filter(|p| !p.is_empty() && !new.matches(p)).collect(); + let mut fields = widen_fields(&text); + let header = fields.next().unwrap_or_default(); + let kept: Vec<&str> = fields.filter(|p| !new.matches(p)).collect(); if kept.is_empty() { remove_if_exists(&record)?; } else { - let body: String = kept.iter().map(|p| format!("{p}\n")).collect(); - scl_core::fsutil::atomic_write_durable( - &record, - format!("{header}\n{body}").as_bytes(), - )?; + // The header is the decided root's hex; write it back as is. + let mut out = format!("{header}\0"); + for p in kept { + out.push_str(p); + out.push('\0'); + } + scl_core::fsutil::atomic_write_durable(&record, out.as_bytes())?; } } Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index aa303a50..f44ed0a6 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -186,7 +186,9 @@ every claim below is checked against the shipped code, not the plan. recorded file already on disk with the decided root's bytes as its own earlier write; an unrecorded file, even with equal bytes, or a changed one still refuses (the file's executable bit is not compared, but - `lay_out_added_view` applies the recorded mode after each atomic write, #237). The record starts with the stop's decided-root id and is ignored + `lay_out_added_view` applies the recorded mode after each atomic write, #237). The record is NUL-separated (a path may hold a newline or a carriage + return, never a NUL), read and rewritten with one splitter, like + `CONFLICT_PLAINTEXT`. It starts with the stop's decided-root id and is ignored when that does not match (a stale record). When the stop ends with the old view still in force (abort of a merge, pick or rebase, or the completion after an interrupted widen) `Repo::remove_widen_residue` deletes the From fef619a7c4eb55e89870649e2e8db89c4821aac7 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 15:19:37 +0300 Subject: [PATCH 09/10] fix(repo): widen guard ignores case aliases; completion drops the stale sparse-prev bound (#224) A stopped-op widen no longer counts a case alias of a laid-out path as tracked, so a decided SRC/a.txt cannot overwrite the user's resolution of src/a.txt on a case-insensitive filesystem. Completing a stopped op after an interrupted widen on a partial clone clears the sparse-prev bound the widen left, and refuses (naming the file) when a user-edited residue file outside the view remains. Docs: abort semantics, the kept-residue decision, the temp-file note. Refs #224 --- crates/repo/src/partial_merge.rs | 60 ++++++++++++++++++++++++ crates/repo/src/replay.rs | 4 +- crates/repo/src/repo.rs | 44 +++++++++++++++-- crates/repo/src/sparse.rs | 78 ++++++++++++++++++++++++++----- docs/adr/0034-sparse-checkouts.md | 16 ++++++- 5 files changed, 183 insertions(+), 19 deletions(-) diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 83c93573..137164b9 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -469,6 +469,66 @@ fn abort_after_an_interrupted_partial_widen_removes_the_subtree_it_laid() { fx.cleanup(); } +/// Completing a stopped merge after an interrupted widen on a partial clone +/// removes what the widen laid and the `sparse-prev` bound it left, so a later +/// materialize cannot delete tracked files under that bound unchecked. A file +/// the user edited since is kept (and the bound is still dropped). +#[test] +fn completing_after_an_interrupted_partial_widen_clears_the_bound() { + for edited in [false, true] { + let fx = Fx::origin( + if edited { + "complete-widen-ed" + } else { + "complete-widen" + }, + |_| {}, + ); + fx.origin_branch( + "b1", + &[ + ("src/a.txt", b"ONE\ntwo\nthree\n"), + ("docs/new.md", b"added by theirs\n"), + ], + ); + let clone = fx.clone(true); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + let old = clone.sparse_spec().unwrap(); + let wide = crate::Sparse::new(vec!["src/".to_string(), "docs/".to_string()]); + let decided = crate::merge_state::read_decided_root(clone.layout()) + .unwrap() + .unwrap(); + clone.lay_out_widened_view(decided, &old, &wide).unwrap(); + assert!(clone.layout().sparse_prev_path().exists(), "premise"); + if edited { + std::fs::write(fx.clone_root.join("docs/new.md"), b"my edit\n").unwrap(); + } + + std::fs::write(fx.clone_root.join("src/a.txt"), b"MERGED\ntwo\nthree\n").unwrap(); + if edited { + // Refused with the path named, nothing moved; moving it away lets + // the commit through. + let err = clone.commit("t", "merge").unwrap_err(); + assert!( + matches!(&err, Error::InvalidArgument(m) if m.contains("docs/new.md")), + "got {err:?}" + ); + assert!(clone.merge_in_progress()); + std::fs::remove_file(fx.clone_root.join("docs/new.md")).unwrap(); + } + clone.commit("t", "merge").unwrap(); + assert!(!clone.layout().sparse_prev_path().exists()); + assert!(!clone.layout().sparse_widen_path().exists()); + assert!(!fx.clone_root.join("docs/new.md").exists()); + drop(clone); + fx.cleanup(); + } +} + /// Aborting restores ours and removes the markers; the carried subtree was /// never on disk and the ref never moved. #[test] diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 764e781f..dfb16139 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -1088,7 +1088,7 @@ impl Repo { )?; } // Files an interrupted widen of the last stop laid outside the view. - self.remove_widen_residue(crate::rebase_state::read_decided_root(&self.layout)?)?; + self.finish_widen_at_completion(crate::rebase_state::read_decided_root(&self.layout)?)?; refs::write_branch_tip(&self.layout, &head, &acc_tip)?; cache.save_best_effort(); crate::oplog::record( @@ -1209,7 +1209,7 @@ impl Repo { // in the replayed range), so the registry base is always the // conflicted commit's own first parent. // Before the working tree is read (see `commit`). - self.remove_widen_residue(decided)?; + self.finish_widen_at_completion(decided)?; let new_tip = self.assemble_completion_snapshot( st.acc_tip, st.conflicted, diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 8de74e9d..87db0585 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -941,7 +941,7 @@ impl Repo { // Files an interrupted widen laid outside the view go before the // working tree is read, or they would be read as completion input. if merging || picking { - self.remove_widen_residue(decided_root)?; + self.finish_widen_at_completion(decided_root)?; } let id = match (tip, merge_head, pick_head) { (Some(t), None, Some(ph)) => self.assemble_completion_snapshot( @@ -2098,7 +2098,9 @@ impl Repo { /// there. Paths the old view matched are not checked: they are not written /// (a stopped op may have turned one into a directory, say). `already_laid` /// names paths that count as tracked because an earlier, interrupted run of - /// the same change wrote them. Same rules as + /// the same change wrote them. `alias_ok` lets a case alias of a laid-out + /// path count as tracked; a stopped-op widen passes false, because writing + /// a newly visible `SRC/a.txt` would overwrite the user's `src/a.txt`. Same rules as /// [`Repo::refuse_untracked_overwrite_vs`]. pub(crate) fn refuse_untracked_for_view_change( &self, @@ -2107,6 +2109,7 @@ impl Repo { old_view: &crate::Sparse, new_view: &crate::Sparse, already_laid: &std::collections::BTreeSet, + alias_ok: bool, ) -> Result<()> { let partial = self.promisor()?.is_some(); let (mut laid_out, target) = { @@ -2129,6 +2132,7 @@ impl Repo { &laid_out, target.iter().filter(|p| !old_view.matches(p)), new_view, + alias_ok, ) } @@ -2141,7 +2145,7 @@ impl Repo { tracked: &std::collections::BTreeSet, target: impl Iterator, ) -> Result<()> { - self.refuse_untracked_paths_in(op, tracked, target, &self.sparse_spec()?) + self.refuse_untracked_paths_in(op, tracked, target, &self.sparse_spec()?, true) } /// [`Repo::refuse_untracked_paths`] for the view the write will happen @@ -2153,6 +2157,7 @@ impl Repo { tracked: &std::collections::BTreeSet, target: impl Iterator, sparse: &crate::Sparse, + alias_ok: bool, ) -> Result<()> { // A path outside the view was never laid out: whatever is on disk // there is the user's, even if the tree tracks it. On a @@ -2161,7 +2166,8 @@ impl Repo { // provably the same file. let root = &self.layout.root; let mut is_tracked = |p: &str| { - sparse.matches(p) && (tracked.contains(p) || is_tracked_case_alias(root, tracked, p)) + sparse.matches(p) + && (tracked.contains(p) || (alias_ok && is_tracked_case_alias(root, tracked, p))) }; for rel in target.filter(|p| sparse.matches(p)) { match stop_write_blocker(&self.layout.root, rel, &mut is_tracked)? { @@ -2759,6 +2765,36 @@ mod tests { p } + /// On a case-insensitive filesystem `SRC/a.txt` is the tracked `src/a.txt`. + /// A normal checkout counts that alias as tracked, but a stopped-op widen + /// would write the decided `SRC/a.txt` over the user's `src/a.txt`, so it + /// must not (`alias_ok = false`). Skipped where the filesystem is + /// case-sensitive and no alias exists. + #[cfg(unix)] + #[test] + fn a_widen_does_not_treat_a_case_alias_as_its_own_file() { + let root = tmp_root("widen-case-alias"); + let repo = Repo::init(&root).unwrap(); + if root.join(".SC").symlink_metadata().is_err() { + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + return; + } + std::fs::create_dir_all(root.join("src")).unwrap(); + std::fs::write(root.join("src/a.txt"), b"my resolution\n").unwrap(); + let tracked: std::collections::BTreeSet = ["src/a.txt".to_string()].into(); + let target = ["SRC/a.txt".to_string()]; + let sparse = crate::Sparse::new(vec!["src/".into(), "SRC/".into()]); + repo.refuse_untracked_paths_in("t", &tracked, target.iter(), &sparse, true) + .unwrap(); + let err = repo + .refuse_untracked_paths_in("t", &tracked, target.iter(), &sparse, false) + .unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "got {err:?}"); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn scignore_hides_untracked_from_status_and_commit_but_not_tracked() { let root = tmp_root("scignore"); diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 9354f07d..013bfa87 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -390,6 +390,7 @@ impl Repo { &old_spec, &spec, &Default::default(), + true, )?; } // Crash safety: record the spec being replaced before the new one is @@ -537,7 +538,14 @@ impl Repo { )? }; for root in roots { - self.refuse_untracked_for_view_change("sparse set", root, old, new, &already_laid)?; + self.refuse_untracked_for_view_change( + "sparse set", + root, + old, + new, + &already_laid, + false, + )?; } // Every guard passed, so each path below is the widen's to write: // record them (keeping an earlier interrupted run's) before the first. @@ -604,21 +612,33 @@ impl Repo { } /// Delete the files an interrupted stopped-op widen wrote, when the stop - /// ends (abort or completion) with the old view still in force. Abort's - /// removal walk follows the other side's tree and the restored view, so it - /// cannot see an ours-only file or a subtree it never fetched; this uses - /// the `.sc/sparse-widen` record instead. A path is removed only if the + /// ends with the old view still in force. A path is removed only if the /// record names `decided` (a stale record from another stop is ignored), /// the path is outside the current view, and its bytes still equal the - /// decided tree's: a file the user edited since is theirs. Call it before + /// decided tree's, so a file the user edited since is left. Call it before /// the stopped op's state, which removes the record, is cleared. - pub(crate) fn remove_widen_residue(&self, decided: Option) -> Result<()> { + /// + /// 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. + pub(crate) fn remove_widen_residue(&self, decided: Option) -> Result { + Ok(self.remove_widen_residue_keeping(decided)?.is_some()) + } + + /// [`Repo::remove_widen_residue`], returning the recorded out-of-view + /// paths it kept (`None`: no valid record). + fn remove_widen_residue_keeping( + &self, + decided: Option, + ) -> Result>> { let Some(decided) = decided else { - return Ok(()); + return Ok(None); }; let recorded = load_widen(self.layout(), decided)?; if recorded.is_empty() { - return Ok(()); + return Ok(None); } let view = self.sparse_spec()?; let outside: std::collections::BTreeSet = @@ -628,13 +648,48 @@ impl Repo { let mut s = store_arc.lock().unwrap(); crate::worktree::recorded_paths_laid(self.layout(), &mut s, decided, &outside)? }; - for path in laid { - match std::fs::remove_file(crate::worktree::safe_join(&self.layout.root, &path)?) { + for path in &laid { + match std::fs::remove_file(crate::worktree::safe_join(&self.layout.root, path)?) { Ok(()) => {} Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => return Err(e.into()), } } + Ok(Some( + outside.into_iter().filter(|p| !laid.contains(p)).collect(), + )) + } + + /// [`Repo::remove_widen_residue`] for the completion of a stopped op. A + /// partial clone's interrupted widen also left its wider bound in + /// `.sc/sparse-prev`; with the residue handled that bound is stale, and + /// the next `materialize` with an old root would delete tracked files + /// under it without a byte check (an edited kept residue file included), + /// so it goes too. + pub(crate) fn finish_widen_at_completion( + &self, + decided: Option, + ) -> Result<()> { + let Some(kept) = self.remove_widen_residue_keeping(decided)? else { + return Ok(()); + }; + let partial = self.promisor()?.is_some(); + // On a full clone a kept (user-edited) file outside the view is read + // like any tracked file there and committed with the user's edit. A + // partial clone cannot graft it into the result, so say what to do. + if partial { + if let Some(p) = kept + .iter() + .find(|p| std::fs::symlink_metadata(self.layout.root.join(p)).is_ok()) + { + return Err(Error::InvalidArgument(format!( + "{p} lies outside your sparse checkout and differs from what the merge \ + decided (an interrupted `sc sparse set` laid it out): move your copy \ + away or restore it, then retry" + ))); + } + clear_prev(self.layout())?; + } Ok(()) } @@ -708,6 +763,7 @@ impl Repo { &self.sparse_spec()?, &Sparse::default(), &Default::default(), + true, )?; } clear(self.layout())?; diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index f44ed0a6..ee23cc85 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -196,14 +196,26 @@ 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. The cleanup runs before the working tree is read for completion, and a retry with a + 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 + 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 + the file, because the result cannot graft it, until it is moved away. On a partial + clone, completion also drops the `.sc/sparse-prev` bound the interrupted widen + left, so a later materialize cannot delete tracked files under it unchecked. The cleanup runs before the working tree is read for completion, and a retry with a different target keeps the earlier target's paths recorded. On a partial clone the new view is recorded in `.sc/sparse-prev` before the writes, so the abort's removal walk covers files an interrupted widen laid; the marker is restored to its prior state once the spec is stored. The untracked-file guard checks only paths the old view did not match (those it laid out are not written, and a stopped op may have turned one - into a directory). Widening to the same spec is a quiet no-op. The + into a directory). Widening to the same spec is a quiet no-op. The guard does not count a case + alias of a laid-out path as tracked (`alias_ok = false`): on a case-insensitive + filesystem a decided `SRC/a.txt` would otherwise be written over the user's + resolution of `src/a.txt`. A crash between an atomic write's temp file + (`...tmp`) and its rename leaves that temp file beside the target; + it is not recorded, delete it. The decided root is the right source: it is what completion carries an out-of-view path from (refinement 6). For a resumed rebase (`resolved`) the accumulator can differ from it outside the view, but the next `--continue` re-lays From 9a4328b367e35b2b86b2e5a101a32c8f0ba07b7f Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Tue, 20 Oct 2026 15:57:21 +0300 Subject: [PATCH 10/10] fix(repo): a header-only widen journal is a pending widen (#224) A widen whose newly included paths are all protected writes no file but still records the wider sparse-prev bound on a partial clone. load_widen now tells a header-only record (Some, empty) from none, so completion clears the bound. Refs #224 --- crates/repo/src/partial_merge.rs | 38 ++++++++++++++++++++++++++++++++ crates/repo/src/replay.rs | 4 +++- crates/repo/src/sparse.rs | 22 +++++++++--------- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/crates/repo/src/partial_merge.rs b/crates/repo/src/partial_merge.rs index 137164b9..02082422 100644 --- a/crates/repo/src/partial_merge.rs +++ b/crates/repo/src/partial_merge.rs @@ -529,6 +529,44 @@ fn completing_after_an_interrupted_partial_widen_clears_the_bound() { } } +/// A widen whose newly included paths are all protected writes no file, so its +/// record has only a header, yet on a partial clone it still left the wider +/// `sparse-prev` bound. Completion treats that record as pending too. +#[test] +fn completing_after_an_interrupted_protected_only_widen_clears_the_bound() { + let (_sk, pk) = scl_crypto::generate_keypair(); + let fx = protected_origin("complete-widen-prot", &pk); + fx.origin_branch("b1", &[("src/a.txt", b"ONE\ntwo\nthree\n")]); + let clone = fx.clone(true); + clone_edit_src(&fx, &clone, b"uno\ntwo\nthree\n"); + assert!(matches!( + clone.merge("origin/b1", "t").unwrap_err(), + Error::MergeConflicts(1) + )); + let old = clone.sparse_spec().unwrap(); + let wide = crate::Sparse::new(vec!["src/".to_string(), "vault/".to_string()]); + let decided = crate::merge_state::read_decided_root(clone.layout()) + .unwrap() + .unwrap(); + let (skipped, _) = clone.lay_out_widened_view(decided, &old, &wide).unwrap(); + assert_eq!(skipped, vec!["vault/s.txt".to_string()], "protected only"); + assert_eq!( + crate::sparse::load_widen(clone.layout(), decided).unwrap(), + Some(Default::default()), + "a header-only record" + ); + assert!(clone.layout().sparse_prev_path().exists(), "premise"); + + clone + .resolve_path("src/a.txt", crate::conflicts::ResolveSide::Theirs, None) + .unwrap(); + clone.commit("t", "merge").unwrap(); + assert!(!clone.layout().sparse_prev_path().exists()); + assert!(!clone.layout().sparse_widen_path().exists()); + drop(clone); + fx.cleanup(); +} + /// Aborting restores ours and removes the markers; the carried subtree was /// never on disk and the ref never moved. #[test] diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index dfb16139..d503d9c5 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -3632,7 +3632,9 @@ mod tests { decided, &Sparse::new(vec!["src/".into(), "docs/".into()]), ); - let recorded = crate::sparse::load_widen(repo.layout(), decided).unwrap(); + let recorded = crate::sparse::load_widen(repo.layout(), decided) + .unwrap() + .unwrap(); let want: std::collections::BTreeSet = odd.iter().map(|p| p.to_string()).collect(); assert_eq!(recorded, want, "exactly the laid paths, byte for byte"); for p in odd { diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 013bfa87..f6bee3a2 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -174,21 +174,23 @@ pub(crate) fn clear_prev(layout: &Layout) -> Result<()> { } /// The paths an in-flight stopped-op widen recorded writing, if the record -/// belongs to the stop whose decided root is `decided` (its first line); a -/// record from another stop is stale and reads as empty. +/// belongs to the stop whose decided root is `decided` (its header); a record +/// from another stop is stale and reads as `None`. `Some` of an empty set is a +/// header-only record: a widen that added no plain file, which still left a +/// pending widen (its `sparse-prev` bound). pub(crate) fn load_widen( layout: &Layout, decided: scl_core::ObjectId, -) -> Result> { +) -> Result>> { match std::fs::read_to_string(layout.sparse_widen_path()) { Ok(text) => { let mut fields = widen_fields(&text); if fields.next() != Some(decided.to_hex().as_str()) { - return Ok(Default::default()); + return Ok(None); } - Ok(fields.map(String::from).collect()) + Ok(Some(fields.map(String::from).collect())) } - Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Default::default()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), Err(e) => Err(e.into()), } } @@ -525,7 +527,7 @@ impl Repo { // Files an interrupted run of this same widen already wrote (recorded // in `.sc/sparse-widen`, bytes equal to the decided root's): a retry // may rewrite them. Anything else at those paths is the user's. - let recorded = load_widen(self.layout(), decided)?; + let recorded = load_widen(self.layout(), decided)?.unwrap_or_default(); let already_laid = { let mut s = store_arc.lock().unwrap(); crate::worktree::added_view_already_laid( @@ -636,10 +638,10 @@ impl Repo { let Some(decided) = decided else { return Ok(None); }; - let recorded = load_widen(self.layout(), decided)?; - if recorded.is_empty() { + // A header-only record is a pending widen too. + let Some(recorded) = load_widen(self.layout(), decided)? else { return Ok(None); - } + }; let view = self.sparse_spec()?; let outside: std::collections::BTreeSet = recorded.into_iter().filter(|p| !view.matches(p)).collect();