|
| 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