Skip to content

Commit 9801cc3

Browse files
fix(repo): stop resurrecting out-of-view deletions on sparse completion (#239)
* fix(repo): do not resurrect out-of-view deletions on sparse completion With a decided root recorded, completion of a stopped merge, cherry-pick or rebase no longer reads the parents for the out-of-sparse carry. A path missing from the decided root was decided deleted; the tip used to copy it back. Parents still supply still-protected paths and remain the fallback for state without a decided root. Ref #238 * test(repo): assert the sparse completion temp repo is removed (ref #238) * fix(repo): complete from the decided root alone, pin protected, dir and mode cases With a decided root, parents no longer supply still-protected paths either: a protected file the other side deleted cleanly came back after a conflicted completion. Tests cover that, a file replaced by a directory out of view, and an out-of-view exec bit. ADR-0034 refinement 6 now scopes the decided root to public branches. Ref #238
1 parent 8b67386 commit 9801cc3

4 files changed

Lines changed: 360 additions & 31 deletions

File tree

‎crates/repo/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod secrets;
4444
pub mod serve_tokens;
4545
pub mod signatures;
4646
pub mod sparse;
47+
#[cfg(test)]
48+
mod sparse_completion;
4749
pub mod stdio_transport;
4850
pub mod sync;
4951
pub mod textdiff;

‎crates/repo/src/repo.rs‎

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,13 @@ impl Repo {
658658
// committed the stale ours blob.) A path PRESENT in the decided
659659
// tree is decided, period: parents are never consulted for it.
660660
// 2. The parents — `tip` only on a non-merge commit (unchanged
661-
// behavior), both parents on merge completion for paths the
662-
// decided tree doesn't know (and as full fallback when the merge
663-
// state predates the decided-root record, i.e. was written by an
664-
// older code path). Ours wins across parents. A pick's non-parent
661+
// behavior), both parents on a merge completion only as the full
662+
// fallback when the state predates the decided-root record (written
663+
// by an older code path). Ours wins across parents. With a decided
664+
// root the parents are NOT consulted at all: it holds every
665+
// surviving path, so a path missing from it (protected or
666+
// out-of-sparse) was decided deleted and must not come back
667+
// (#238). A pick's non-parent
665668
// `pick_head` is NOT a source: any picked-side content the pick
666669
// kept is in the decided tree — a protected path in the picked
667670
// commit but absent from the decided tree was decided *deleted*
@@ -698,37 +701,32 @@ impl Repo {
698701
let store_arc = self.vfs.store();
699702
let mut store = store_arc.lock().unwrap();
700703

701-
// Decided tree first (merge completion only), then the parents
702-
// restricted to paths outside the decided tree.
703-
let mut sources: Vec<
704-
std::collections::BTreeMap<String, (ObjectId, scl_core::FileMode, u8)>,
705-
> = Vec::new();
706-
let mut decided_paths: std::collections::BTreeSet<String> = Default::default();
704+
// The decided tree (merge, pick or rebase completion) is the only
705+
// source when one exists; the parents are the fallback for a plain
706+
// commit and for state written without a decided root.
707+
type Entries = std::collections::BTreeMap<String, (ObjectId, scl_core::FileMode, u8)>;
708+
let mut sources: Vec<Entries> = Vec::new();
707709
if let Some(dr) = decided_root {
708-
let decided = if partial {
710+
sources.push(if partial {
709711
worktree::tree_file_entries_with_perms_sparse(&mut store, dr, sparse)?
710712
} else {
711713
worktree::tree_file_entries_with_perms(&mut store, dr)?
712-
};
713-
decided_paths = decided.keys().cloned().collect();
714-
sources.push(decided);
715-
}
716-
for parent in tip.into_iter().chain(merge_head) {
717-
let parent_root = store.get_snapshot(&parent)?.root;
718-
let mut entries = if partial {
719-
// A parent is only a fallback behind the decided root
720-
// (merge completion); a subtree it lacks was never the
721-
// only record of a path, so skip it, not fail.
722-
worktree::tree_file_entries_with_perms_sparse_present(
723-
&mut store,
724-
parent_root,
725-
sparse,
726-
)?
727-
} else {
728-
worktree::tree_file_entries_with_perms(&mut store, parent_root)?
729-
};
730-
entries.retain(|p, _| !decided_paths.contains(p));
731-
sources.push(entries);
714+
});
715+
} else {
716+
for parent in tip.into_iter().chain(merge_head) {
717+
let parent_root = store.get_snapshot(&parent)?.root;
718+
sources.push(if partial {
719+
// A subtree a parent lacks was never the only record
720+
// of a path, so skip it, not fail.
721+
worktree::tree_file_entries_with_perms_sparse_present(
722+
&mut store,
723+
parent_root,
724+
sparse,
725+
)?
726+
} else {
727+
worktree::tree_file_entries_with_perms(&mut store, parent_root)?
728+
});
729+
}
732730
}
733731

734732
// Paths the stopped operation wrote in plaintext. One of them that
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
//! A stopped merge, cherry-pick or rebase on a FULL clone must complete from
2+
//! its decided root (#238). With a sparse spec an out-of-view path the other
3+
//! side deleted stays deleted while its clean adds and modifies land and
4+
//! ours-side-only out-of-view changes are kept; a protected path the other
5+
//! side deleted stays deleted too.
6+
7+
use std::collections::BTreeMap;
8+
use std::path::Path;
9+
10+
use scl_core::{FileMode, Object};
11+
12+
use crate::conflicts::ResolveSide;
13+
use crate::repo::Repo;
14+
use crate::worktree;
15+
16+
#[derive(Clone, Copy, Debug)]
17+
enum Op {
18+
Merge,
19+
Pick,
20+
Rebase,
21+
}
22+
23+
/// A working-tree edit applied before a commit.
24+
enum Edit<'a> {
25+
Write(&'a str, &'a [u8]),
26+
Exec(&'a str, &'a [u8]),
27+
Remove(&'a str),
28+
}
29+
30+
fn apply(root: &Path, edits: &[Edit]) {
31+
for e in edits {
32+
match e {
33+
Edit::Write(p, b) | Edit::Exec(p, b) => {
34+
let full = root.join(p);
35+
std::fs::create_dir_all(full.parent().unwrap()).unwrap();
36+
std::fs::write(&full, b).unwrap();
37+
#[cfg(unix)]
38+
if matches!(e, Edit::Exec(..)) {
39+
use std::os::unix::fs::PermissionsExt;
40+
std::fs::set_permissions(&full, std::fs::Permissions::from_mode(0o755))
41+
.unwrap();
42+
}
43+
}
44+
Edit::Remove(p) => std::fs::remove_file(root.join(p)).unwrap(),
45+
}
46+
}
47+
}
48+
49+
/// Stop `op` of `feature` against `main`, resolve `src/a.txt` to the other
50+
/// side, complete, and return the tip's `path -> (content, mode)`. Cleans up.
51+
fn finish(
52+
root: &Path,
53+
repo: Repo,
54+
op: Op,
55+
identity: Option<&scl_crypto::SecretKey>,
56+
conflict: &str,
57+
) -> BTreeMap<String, (Vec<u8>, FileMode)> {
58+
match op {
59+
Op::Merge => {
60+
repo.merge_with_identity("feature", "me", identity)
61+
.unwrap_err();
62+
}
63+
Op::Pick => {
64+
repo.cherry_pick("feature", "me", identity, None)
65+
.unwrap_err();
66+
}
67+
Op::Rebase => {
68+
let out = repo.rebase("main", "me", identity).unwrap();
69+
assert!(matches!(out, crate::replay::RebaseResult::Stopped { .. }));
70+
}
71+
}
72+
repo.resolve_path(conflict, ResolveSide::Theirs, identity)
73+
.unwrap();
74+
match op {
75+
Op::Rebase => {
76+
repo.rebase_continue("me", identity).unwrap();
77+
}
78+
_ => {
79+
repo.commit("me", "complete").unwrap();
80+
}
81+
}
82+
let tip = repo.head_tip().unwrap().unwrap();
83+
let mut out = BTreeMap::new();
84+
{
85+
let store_arc = repo.vfs().store();
86+
let mut s = store_arc.lock().unwrap();
87+
let tree = s.get_snapshot(&tip).unwrap().root;
88+
for (p, (id, mode, _)) in worktree::tree_file_entries_with_perms(&mut s, tree).unwrap() {
89+
match s.get(&id).unwrap() {
90+
Object::Blob(b) => out.insert(p, (b.to_vec(), mode)),
91+
_ => unreachable!(),
92+
};
93+
}
94+
}
95+
drop(repo);
96+
std::fs::remove_dir_all(root).unwrap();
97+
assert!(!root.exists(), "temp repo must be removed");
98+
out
99+
}
100+
101+
/// base -> `main` edits and `feature` edits (both touch `src/a.txt`, so the
102+
/// operation stops), optionally under sparse `src/`. Everything under
103+
/// `docs/` is outside the view.
104+
fn run(
105+
tag: &str,
106+
op: Op,
107+
sparse: bool,
108+
base: &[Edit],
109+
main: &[Edit],
110+
feature: &[Edit],
111+
) -> BTreeMap<String, (Vec<u8>, FileMode)> {
112+
let root = std::env::temp_dir().join(format!(
113+
"scl-sparse-completion-{tag}-{}",
114+
std::process::id()
115+
));
116+
let _ = std::fs::remove_dir_all(&root);
117+
std::fs::create_dir_all(&root).unwrap();
118+
let repo = Repo::init(&root).unwrap();
119+
apply(&root, base);
120+
repo.commit("me", "base").unwrap();
121+
repo.branch("feature").unwrap();
122+
apply(&root, main);
123+
repo.commit("me", "main").unwrap();
124+
repo.switch("feature").unwrap();
125+
apply(&root, feature);
126+
repo.commit("me", "feature").unwrap();
127+
// Narrow before leaving `feature`, so its out-of-view files are not left
128+
// on disk to block the switch (a directory where `main` has a file).
129+
if sparse {
130+
repo.set_sparse(&["src/".into()], None).unwrap();
131+
// Drop what the narrowing left behind: empty directories.
132+
for d in ["docs/x", "docs"] {
133+
let _ = std::fs::remove_dir(root.join(d));
134+
}
135+
assert!(!root.join("docs").exists(), "setup: docs/ is out of view");
136+
}
137+
if !matches!(op, Op::Rebase) {
138+
repo.switch("main").unwrap();
139+
}
140+
finish(&root, repo, op, None, "src/a.txt")
141+
}
142+
143+
fn ops() -> [Op; 3] {
144+
[Op::Merge, Op::Pick, Op::Rebase]
145+
}
146+
147+
fn tag(op: Op, what: &str) -> String {
148+
format!("{}-{what}", format!("{op:?}").to_lowercase())
149+
}
150+
151+
const BASE_A: Edit = Edit::Write("src/a.txt", b"a\nb\nc\n");
152+
const MAIN_A: Edit = Edit::Write("src/a.txt", b"a\nMAIN\nc\n");
153+
const FEAT_A: Edit = Edit::Write("src/a.txt", b"a\nFEAT\nc\n");
154+
155+
fn text(m: &BTreeMap<String, (Vec<u8>, FileMode)>) -> BTreeMap<&str, &[u8]> {
156+
m.iter().map(|(p, (b, _))| (p.as_str(), &b[..])).collect()
157+
}
158+
159+
#[test]
160+
fn a_sparse_completion_keeps_the_other_sides_out_of_view_deletion_and_lands_the_rest() {
161+
for op in ops() {
162+
let args = |sparse| {
163+
run(
164+
&tag(op, if sparse { "del-sparse" } else { "del-full" }),
165+
op,
166+
sparse,
167+
&[
168+
BASE_A,
169+
Edit::Write("docs/old.txt", b"v1\n"),
170+
Edit::Write("docs/mod.txt", b"v1\n"),
171+
Edit::Write("docs/ours.txt", b"v1\n"),
172+
Edit::Write("docs/ours_del.txt", b"v1\n"),
173+
],
174+
&[
175+
MAIN_A,
176+
Edit::Write("docs/ours.txt", b"ours v2\n"),
177+
Edit::Remove("docs/ours_del.txt"),
178+
],
179+
&[
180+
FEAT_A,
181+
Edit::Remove("docs/old.txt"),
182+
Edit::Write("docs/mod.txt", b"theirs v2\n"),
183+
Edit::Write("docs/new.txt", b"new\n"),
184+
],
185+
)
186+
};
187+
let (full, sparse) = (args(false), args(true));
188+
assert!(!full.contains_key("docs/old.txt"), "reference: deleted");
189+
assert!(
190+
!sparse.contains_key("docs/old.txt"),
191+
"{op:?}: the other side's clean out-of-view deletion must not resurrect"
192+
);
193+
let s = text(&sparse);
194+
assert_eq!(
195+
s["docs/mod.txt"], b"theirs v2\n",
196+
"{op:?}: clean modify lands"
197+
);
198+
assert_eq!(s["docs/new.txt"], b"new\n", "{op:?}: clean add lands");
199+
assert_eq!(s["docs/ours.txt"], b"ours v2\n", "{op:?}: ours' edit kept");
200+
assert!(
201+
!s.contains_key("docs/ours_del.txt"),
202+
"{op:?}: ours' delete kept"
203+
);
204+
assert_eq!(
205+
s,
206+
text(&full),
207+
"{op:?}: sparse completion equals the full one"
208+
);
209+
}
210+
}
211+
212+
/// The other side turns the out-of-view file `docs/x` into a directory. The
213+
/// parents used to carry `docs/x` back beside `docs/x/y`, a corrupt tree.
214+
#[test]
215+
fn a_sparse_completion_accepts_a_file_replaced_by_a_directory_out_of_view() {
216+
for op in ops() {
217+
let sparse = run(
218+
&tag(op, "dir"),
219+
op,
220+
true,
221+
&[BASE_A, Edit::Write("docs/x", b"file\n")],
222+
&[MAIN_A],
223+
&[
224+
FEAT_A,
225+
Edit::Remove("docs/x"),
226+
Edit::Write("docs/x/y", b"y\n"),
227+
],
228+
);
229+
let s = text(&sparse);
230+
assert!(!s.contains_key("docs/x"), "{op:?}: the file is gone");
231+
assert_eq!(s["docs/x/y"], b"y\n", "{op:?}: the directory's file landed");
232+
}
233+
}
234+
235+
/// The other side adds the exec bit to an out-of-view file. Compared apart
236+
/// from the equality check: the non-sparse reference drops exec bits (#237).
237+
#[cfg(unix)]
238+
#[test]
239+
fn a_sparse_completion_lands_an_out_of_view_mode_change() {
240+
for op in ops() {
241+
let sparse = run(
242+
&tag(op, "mode"),
243+
op,
244+
true,
245+
&[BASE_A, Edit::Write("docs/run.sh", b"#!/bin/sh\n")],
246+
&[MAIN_A],
247+
&[FEAT_A, Edit::Exec("docs/run.sh", b"#!/bin/sh\n")],
248+
);
249+
assert_eq!(sparse["docs/run.sh"].1, FileMode::EXEC, "{op:?}");
250+
}
251+
}
252+
253+
/// The other side deletes a protected file while an unrelated conflict stops
254+
/// the operation: the decided root dropped it, so completion must too. The
255+
/// deletion is committed as a raw snapshot because a plain commit carries an
256+
/// absent protected path forward.
257+
#[test]
258+
fn a_completion_keeps_the_other_sides_protected_deletion() {
259+
for op in ops() {
260+
let root = std::env::temp_dir().join(format!(
261+
"scl-sparse-completion-{}-{}",
262+
tag(op, "prot"),
263+
std::process::id()
264+
));
265+
let _ = std::fs::remove_dir_all(&root);
266+
std::fs::create_dir_all(&root).unwrap();
267+
let repo = Repo::init(&root).unwrap();
268+
let (sk, pk) = scl_crypto::generate_keypair();
269+
repo.protect("secret/", &[pk], None).unwrap();
270+
apply(&root, &[BASE_A, Edit::Write("secret/a.txt", b"s\n")]);
271+
repo.commit("me", "base").unwrap();
272+
repo.branch("feature").unwrap();
273+
apply(&root, &[MAIN_A]);
274+
repo.commit("me", "main").unwrap();
275+
276+
repo.switch_with_identity("feature", Some(&sk)).unwrap();
277+
let tip = repo.head_tip().unwrap().unwrap();
278+
let protection = repo.snapshot(&tip).unwrap().protection;
279+
let tree = repo
280+
.vfs()
281+
.write_tree_with_perms(&[(
282+
"src/a.txt".to_string(),
283+
b"a\nFEAT\nc\n".to_vec(),
284+
FileMode::FILE,
285+
0,
286+
)])
287+
.unwrap();
288+
repo.commit_snapshot(
289+
tree,
290+
vec![tip],
291+
Default::default(),
292+
protection,
293+
"me",
294+
"feature deletes secret/a.txt",
295+
)
296+
.unwrap();
297+
// Match the disk to that tree before leaving the branch.
298+
apply(&root, &[FEAT_A, Edit::Remove("secret/a.txt")]);
299+
if !matches!(op, Op::Rebase) {
300+
repo.switch_with_identity("main", Some(&sk)).unwrap();
301+
}
302+
let done = finish(&root, repo, op, Some(&sk), "src/a.txt");
303+
let s = text(&done);
304+
assert!(
305+
!s.contains_key("secret/a.txt"),
306+
"{op:?}: the protected deletion must not be undone"
307+
);
308+
assert_eq!(s["src/a.txt"], b"a\nFEAT\nc\n");
309+
}
310+
}

0 commit comments

Comments
 (0)