| 98286406 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::*; |
| 75167672 Andy McCall |
4 |
use crate::protocol::SignedPolicy; |
| 957fee30 Andy McCall ↶ |
5 |
use crate::repo::{BisectStep, Selection}; |
| 98286406 Andy McCall ↶ |
6 |
|
| 7 |
|
| 8 |
|
| 9 |
fn hermetic() { |
| 10 |
static ONCE: std::sync::Once = std::sync::Once::new(); |
| 11 |
static DIR: std::sync::OnceLock<tempfile::TempDir> = std::sync::OnceLock::new(); |
| 12 |
ONCE.call_once(|| { |
| 13 |
let d = tempfile::tempdir().unwrap(); |
| 14 |
std::env::set_var("OXY_HOME", d.path()); |
| 15 |
DIR.set(d).ok(); |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
fn write(root: &Path, rel: &str, content: &[u8]) { |
| 20 |
let p = root.join(rel); |
| 21 |
fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 22 |
fs::write(p, content).unwrap(); |
| 23 |
} |
| 24 |
|
| 25 |
fn statuses(s: &SnapshotResult) -> Vec<(String, ChangeStatus)> { |
| 75167672 Andy McCall |
26 |
s.statuses |
| 27 |
.iter() |
| 28 |
.map(|p| (p.path.clone(), p.status)) |
| 29 |
.collect() |
| 98286406 Andy McCall ↶ |
30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn init_snapshot_commit_log_undo() { |
| 34 |
hermetic(); |
| 35 |
let dir = tempfile::tempdir().unwrap(); |
| 36 |
let root = dir.path(); |
| 37 |
let repo = Repo::init(root).unwrap(); |
| 38 |
assert!(Repo::init(root).is_err()); |
| 39 |
let ops = repo.op_log().unwrap(); |
| 40 |
assert_eq!(ops.len(), 1); |
| 41 |
assert_eq!(ops[0].1.command, "oxy init"); |
| 42 |
|
| 43 |
|
| 44 |
let s = repo.snapshot("oxy status").unwrap(); |
| 45 |
assert!(!s.changed); |
| 46 |
assert_eq!(repo.op_log().unwrap().len(), 1); |
| 47 |
|
| 48 |
write(root, "src/main.rs", b"fn main() {}\n"); |
| 49 |
write(root, "README.md", b"# Hello\n"); |
| 50 |
write(root, ".oxyignore", b"/target\n"); |
| 51 |
write(root, "target/junk.o", b"\0\0\0"); |
| 52 |
let s = repo.snapshot("oxy status").unwrap(); |
| 53 |
assert!(s.changed); |
| 54 |
assert_eq!( |
| 55 |
statuses(&s), |
| 56 |
vec![ |
| 57 |
(".oxyignore".into(), ChangeStatus::Added), |
| 58 |
("README.md".into(), ChangeStatus::Added), |
| 59 |
("src/main.rs".into(), ChangeStatus::Added), |
| 60 |
] |
| 61 |
); |
| 62 |
assert_eq!(repo.op_log().unwrap().len(), 2); |
| 63 |
|
| 64 |
let s2 = repo.snapshot("oxy status").unwrap(); |
| 65 |
assert!(!s2.changed); |
| 66 |
assert_eq!(s2.wc_hash, s.wc_hash); |
| 67 |
assert_eq!(repo.op_log().unwrap().len(), 2); |
| 68 |
|
| 69 |
let h1 = repo.commit("Initial import", &[]).unwrap(); |
| 70 |
let c1 = repo.store().get_change(&h1).unwrap(); |
| 71 |
assert_eq!(c1.kind, ChangeKind::Normal); |
| 72 |
assert!(c1.deps.is_empty(), "first change has nothing to depend on"); |
| 73 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h1]); |
| 74 |
let (wc_hash, wc) = repo.working_copy().unwrap(); |
| 75 |
assert_eq!(wc.deps, vec![h1]); |
| 76 |
assert!(wc.edits.is_empty()); |
| 77 |
assert!(repo.commit("nothing", &[]).is_err()); |
| 78 |
|
| 79 |
|
| 75167672 Andy McCall |
80 |
write( |
| 81 |
root, |
| 82 |
"src/main.rs", |
| 83 |
b"fn main() {\n println!(\"hi\");\n}\n", |
| 84 |
); |
| 98286406 Andy McCall ↶ |
85 |
fs::remove_file(root.join("README.md")).unwrap(); |
| 86 |
write(root, "logo.png", b"\x89PNG\0\0\x01"); |
| 87 |
let s = repo.snapshot("oxy status").unwrap(); |
| 88 |
assert_eq!( |
| 89 |
statuses(&s), |
| 90 |
vec![ |
| 91 |
("README.md".into(), ChangeStatus::Removed), |
| 92 |
("logo.png".into(), ChangeStatus::Added), |
| 93 |
("src/main.rs".into(), ChangeStatus::Modified), |
| 94 |
] |
| 95 |
); |
| 96 |
assert_ne!(s.wc_hash, wc_hash); |
| 97 |
|
| 98 |
assert_eq!(s.wc.change_id, wc.change_id); |
| 99 |
assert!(s.wc.deps.contains(&h1)); |
| 100 |
|
| 101 |
let h2 = repo.commit("Second", &[]).unwrap(); |
| 102 |
let c2 = repo.store().get_change(&h2).unwrap(); |
| 75167672 Andy McCall |
103 |
assert_eq!( |
| 104 |
c2.deps, |
| 105 |
vec![h1], |
| 106 |
"everything it touches was introduced by h1" |
| 107 |
); |
| 98286406 Andy McCall ↶ |
108 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2]); |
| 109 |
|
| 110 |
|
| 111 |
let st = repo.load_state(&[h2]).unwrap(); |
| 112 |
let snap = st.materialize(); |
| 113 |
assert!(!snap.is_conflicted()); |
| 75167672 Andy McCall |
114 |
assert_eq!( |
| 115 |
snap.files.keys().cloned().collect::<Vec<_>>(), |
| 116 |
[".oxyignore", "logo.png", "src", "src/main.rs"] |
| 117 |
); |
| 118 |
assert!( |
| 119 |
matches!(&snap.files["src/main.rs"].content, FileContent::Text { bytes, .. } if bytes == b"fn main() {\n println!(\"hi\");\n}\n") |
| 120 |
); |
| 98286406 Andy McCall ↶ |
121 |
assert!(snap.files["logo.png"].mode.opaque); |
| 122 |
if let FileContent::Opaque { blobs } = &snap.files["logo.png"].content { |
| 123 |
assert_eq!(repo.read_blob(&blobs[0]).unwrap(), b"\x89PNG\0\0\x01"); |
| 124 |
} else { |
| 125 |
panic!("logo should be opaque"); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
let log = repo.log().unwrap(); |
| 130 |
assert_eq!(log.len(), 3); |
| 131 |
assert!(log[0].is_working_copy); |
| 132 |
assert_eq!(log[1].hash, h2); |
| 133 |
assert!(log[1].is_head); |
| 134 |
assert_eq!(log[2].hash, h1); |
| 135 |
let d = repo.describe_change(&h2).unwrap(); |
| 136 |
assert!(d.contains("D README.md"), "{d}"); |
| 137 |
assert!(d.contains("+ println!(\"hi\");"), "{d}"); |
| 138 |
assert!(d.contains("-fn main() {}"), "{d}"); |
| 139 |
|
| 140 |
|
| 141 |
assert_eq!(repo.resolve_change(&c2.change_id.short(6)).unwrap(), h2); |
| 75167672 Andy McCall |
142 |
assert_eq!( |
| 143 |
repo.resolve_change(&format!("#{}", h1.short(8))).unwrap(), |
| 144 |
h1 |
| 145 |
); |
| 98286406 Andy McCall ↶ |
146 |
assert!(repo.resolve_change("zzzz").is_err()); |
| 147 |
|
| 148 |
|
| 149 |
repo.undo().unwrap(); |
| 150 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h1]); |
| 151 |
let s = repo.snapshot("oxy status").unwrap(); |
| 75167672 Andy McCall |
152 |
assert!( |
| 153 |
!s.changed, |
| 154 |
"files on disk still match the pre-commit working copy" |
| 155 |
); |
| 98286406 Andy McCall ↶ |
156 |
assert_eq!(s.statuses.len(), 3); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
fn conflicted_repo() -> (tempfile::TempDir, Repo, ChangeHash, ChangeHash, ChangeHash) { |
| 161 |
let dir = tempfile::tempdir().unwrap(); |
| 162 |
let root = dir.path(); |
| 163 |
let repo = Repo::init(root).unwrap(); |
| 164 |
write(root, "f.txt", b"a\nb\nc\n"); |
| 165 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 166 |
write(root, "f.txt", b"a\nB\nc\n"); |
| 167 |
let h2 = repo.commit("upper", &[]).unwrap(); |
| 168 |
repo.checkout(&[h1], false, "checkout").unwrap(); |
| 169 |
write(root, "f.txt", b"a\nX\nc\n"); |
| 170 |
let h3 = repo.commit("ex", &[]).unwrap(); |
| 171 |
(dir, repo, h1, h2, h3) |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn concurrent_edits_conflict_and_can_be_resolved() { |
| 176 |
hermetic(); |
| 177 |
let (dir, repo, h1, h2, h3) = conflicted_repo(); |
| 178 |
let root = dir.path(); |
| 179 |
let heads: BTreeSet<_> = repo.bookmark_heads().unwrap().into_iter().collect(); |
| 180 |
assert_eq!(heads, [h2, h3].into_iter().collect()); |
| 181 |
let st = repo.load_state(&[h2, h3]).unwrap(); |
| 182 |
assert!(st.materialize().is_conflicted()); |
| 183 |
|
| 184 |
|
| 75167672 Andy McCall |
185 |
let r = repo |
| 186 |
.checkout(&repo.bookmark_heads().unwrap(), false, "checkout main") |
| 187 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
188 |
assert!(r.conflicted); |
| 189 |
let marked = fs::read_to_string(root.join("f.txt")).unwrap(); |
| 75167672 Andy McCall |
190 |
assert!( |
| 191 |
marked.starts_with("a\n<<<<<<< oxy conflict 1 of 1"), |
| 192 |
"{marked}" |
| 193 |
); |
| 194 |
assert!( |
| 195 |
marked.contains("B\n") && marked.contains("X\n") && marked.ends_with("end\nc\n"), |
| 196 |
"{marked}" |
| 197 |
); |
| 98286406 Andy McCall ↶ |
198 |
assert!(has_conflict_markers(marked.as_bytes())); |
| 199 |
|
| 200 |
let s = repo.snapshot("status").unwrap(); |
| 201 |
assert!(s.statuses.is_empty()); |
| 202 |
assert!(!s.resolves); |
| 203 |
|
| 204 |
|
| 205 |
write(root, "f.txt", marked.replace("B\n", "BB\n").as_bytes()); |
| 206 |
let s = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
207 |
assert_eq!( |
| 208 |
statuses(&s), |
| 209 |
vec![("f.txt".into(), ChangeStatus::ConflictEdited)] |
| 210 |
); |
| 98286406 Andy McCall ↶ |
211 |
assert!(repo.commit("no", &[]).is_err()); |
| 212 |
|
| 213 |
|
| 214 |
write(root, "f.txt", b"a\nX\nc\nd\n"); |
| 215 |
let s = repo.snapshot("status").unwrap(); |
| 216 |
assert_eq!(statuses(&s), vec![("f.txt".into(), ChangeStatus::Resolved)]); |
| 217 |
assert!(s.resolves); |
| 218 |
let h4 = repo.commit("resolve", &[]).unwrap(); |
| 219 |
let c4 = repo.store().get_change(&h4).unwrap(); |
| 220 |
assert_eq!(c4.kind, ChangeKind::Resolution); |
| 221 |
let mut want = vec![h1, h2, h3]; |
| 222 |
want.sort(); |
| 75167672 Andy McCall |
223 |
assert_eq!( |
| 224 |
c4.deps, want, |
| 225 |
"a resolution depends on exactly the contributors and what it references" |
| 226 |
); |
| 98286406 Andy McCall ↶ |
227 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h4]); |
| 228 |
let snap = repo.load_state(&[h4]).unwrap().materialize(); |
| 229 |
assert!(!snap.is_conflicted()); |
| 75167672 Andy McCall |
230 |
assert!( |
| 231 |
matches!(&snap.files["f.txt"].content, FileContent::Text { bytes, .. } if bytes == b"a\nX\nc\nd\n") |
| 232 |
); |
| 98286406 Andy McCall ↶ |
233 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 234 |
|
| 235 |
|
| 236 |
let mut st = State::new(); |
| 237 |
for h in [h4, h3, h1, h2] { |
| 238 |
st.add_unchecked(repo.store().get_change(&h).unwrap()); |
| 239 |
} |
| 240 |
st.validate().unwrap(); |
| 75167672 Andy McCall |
241 |
assert!( |
| 242 |
matches!(&st.materialize().files["f.txt"].content, FileContent::Text { bytes, .. } if bytes == b"a\nX\nc\nd\n") |
| 243 |
); |
| 98286406 Andy McCall ↶ |
244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn resolution_may_keep_both_sides_or_neither() { |
| 248 |
hermetic(); |
| 249 |
let (dir, repo, _h1, _h2, _h3) = conflicted_repo(); |
| 250 |
let root = dir.path(); |
| 75167672 Andy McCall |
251 |
repo.checkout(&repo.bookmark_heads().unwrap(), false, "checkout main") |
| 252 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
253 |
|
| 254 |
write(root, "f.txt", b"a\nX\nB\nc\n"); |
| 255 |
let h = repo.commit("both", &[]).unwrap(); |
| 256 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 257 |
assert!(!snap.is_conflicted()); |
| 75167672 Andy McCall |
258 |
assert!( |
| 259 |
matches!(&snap.files["f.txt"].content, FileContent::Text { bytes, .. } if bytes == b"a\nX\nB\nc\n") |
| 260 |
); |
| 98286406 Andy McCall ↶ |
261 |
|
| 262 |
repo.undo().unwrap(); |
| 263 |
write(root, "f.txt", b"a\nc\n"); |
| 264 |
let h = repo.commit("neither", &[]).unwrap(); |
| 265 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 266 |
assert!(!snap.is_conflicted()); |
| 75167672 Andy McCall |
267 |
assert!( |
| 268 |
matches!(&snap.files["f.txt"].content, FileContent::Text { bytes, .. } if bytes == b"a\nc\n") |
| 269 |
); |
| 98286406 Andy McCall ↶ |
270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn name_conflict_resolved_by_removing_a_candidate() { |
| 274 |
hermetic(); |
| 275 |
let dir = tempfile::tempdir().unwrap(); |
| 276 |
let root = dir.path(); |
| 277 |
let repo = Repo::init(root).unwrap(); |
| 278 |
write(root, "keep.txt", b"k\n"); |
| 279 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 280 |
write(root, "new.txt", b"one\n"); |
| 281 |
repo.commit("one", &[]).unwrap(); |
| 282 |
repo.checkout(&[h1], false, "checkout").unwrap(); |
| 283 |
write(root, "new.txt", b"two\n"); |
| 284 |
repo.commit("two", &[]).unwrap(); |
| 75167672 Andy McCall |
285 |
let r = repo |
| 286 |
.checkout(&repo.bookmark_heads().unwrap(), false, "checkout main") |
| 287 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
288 |
assert!(r.conflicted); |
| 75167672 Andy McCall |
289 |
assert!( |
| 290 |
root.join("new.txt.oxy-conflict-1").exists() |
| 291 |
&& root.join("new.txt.oxy-conflict-2").exists() |
| 292 |
); |
| 98286406 Andy McCall ↶ |
293 |
assert!(!root.join("new.txt").exists()); |
| 294 |
|
| 295 |
|
| 75167672 Andy McCall |
296 |
let (keep, drop) = if fs::read(root.join("new.txt.oxy-conflict-1")).unwrap() == b"two\n" { |
| 297 |
(1, 2) |
| 298 |
} else { |
| 299 |
(2, 1) |
| 300 |
}; |
| 98286406 Andy McCall ↶ |
301 |
fs::remove_file(root.join(format!("new.txt.oxy-conflict-{drop}"))).unwrap(); |
| 75167672 Andy McCall |
302 |
fs::rename( |
| 303 |
root.join(format!("new.txt.oxy-conflict-{keep}")), |
| 304 |
root.join("new.txt"), |
| 305 |
) |
| 306 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
307 |
let h = repo.commit("pick two", &[]).unwrap(); |
| 75167672 Andy McCall |
308 |
assert_eq!( |
| 309 |
repo.store().get_change(&h).unwrap().kind, |
| 310 |
ChangeKind::Resolution |
| 311 |
); |
| 98286406 Andy McCall ↶ |
312 |
|
| 313 |
|
| 314 |
let heads: BTreeSet<_> = repo.bookmark_heads().unwrap().into_iter().collect(); |
| 315 |
assert_eq!(heads, [h1, h].into_iter().collect()); |
| 75167672 Andy McCall |
316 |
let snap = repo |
| 317 |
.load_state(&repo.bookmark_heads().unwrap()) |
| 318 |
.unwrap() |
| 319 |
.materialize(); |
| 98286406 Andy McCall ↶ |
320 |
assert!(!snap.is_conflicted(), "{:?}", snap.tree.conflicts); |
| 75167672 Andy McCall |
321 |
assert_eq!( |
| 322 |
snap.files.keys().cloned().collect::<Vec<_>>(), |
| 323 |
["keep.txt", "new.txt"] |
| 324 |
); |
| 325 |
assert!( |
| 326 |
matches!(&snap.files["new.txt"].content, FileContent::Text { bytes, .. } if bytes == b"two\n") |
| 327 |
); |
| 98286406 Andy McCall ↶ |
328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn identities_and_signed_commits() { |
| 332 |
hermetic(); |
| 333 |
let dir = tempfile::tempdir().unwrap(); |
| 334 |
let root = dir.path(); |
| 335 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 336 |
write(root, "a.txt", b"a\n"); |
| 337 |
let h0 = repo.commit("unsigned", &[]).unwrap(); |
| 338 |
assert_eq!(repo.verify_change(&h0).unwrap(), SignatureStatus::Unsigned); |
| 75167672 Andy McCall |
339 |
assert_eq!( |
| 340 |
repo.store().get_change(&h0).unwrap().author, |
| 341 |
ObjectRef::ZERO |
| 342 |
); |
| 98286406 Andy McCall ↶ |
343 |
|
| 75167672 Andy McCall |
344 |
let (key, id) = repo |
| 345 |
.create_identity("Ada", "[email protected]", IdentityScope::Repo) |
| 346 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
347 |
assert_eq!(id.hash(), key); |
| 348 |
assert_eq!(repo.config.author_key, Some(key)); |
| 349 |
|
| 350 |
let reopened = Repo::open(root).unwrap().with_home(root.join("home")); |
| 351 |
assert_eq!(reopened.config.author_key, Some(key)); |
| 352 |
assert_eq!(reopened.secret_key().unwrap().unwrap().0, key); |
| 353 |
|
| 354 |
|
| 355 |
write(root, "a.txt", b"a\nb\n"); |
| 356 |
let h1 = repo.commit("authored", &[]).unwrap(); |
| 357 |
assert_eq!(repo.store().get_change(&h1).unwrap().author, key); |
| 358 |
assert_eq!(repo.verify_change(&h1).unwrap(), SignatureStatus::Unsigned); |
| 359 |
|
| 360 |
repo.sign_change(&h1).unwrap(); |
| 75167672 Andy McCall |
361 |
assert!( |
| 362 |
matches!(repo.verify_change(&h1).unwrap(), SignatureStatus::Valid { key: k, .. } if k == key) |
| 363 |
); |
| 98286406 Andy McCall ↶ |
364 |
|
| 365 |
repo.config.set("core.sign", "change").unwrap(); |
| 366 |
repo.save_config().unwrap(); |
| 367 |
write(root, "a.txt", b"a\nb\nc\n"); |
| 368 |
let h2 = repo.commit("signed", &[]).unwrap(); |
| 75167672 Andy McCall |
369 |
assert_eq!( |
| 370 |
repo.verify_change(&h2).unwrap(), |
| 371 |
SignatureStatus::Valid { |
| 372 |
key, |
| 373 |
name: "Ada".into(), |
| 374 |
email: "[email protected]".into(), |
| 375 |
sealed: false, |
| 376 |
revoked: false |
| 377 |
} |
| 378 |
); |
| 98286406 Andy McCall ↶ |
379 |
|
| 05e71ed6 Andy McCall ↶ |
380 |
|
| 381 |
|
| 382 |
|
| 98286406 Andy McCall ↶ |
383 |
let other = SecretKey::generate(); |
| 384 |
let other_id = other.identity("Mallory", "[email protected]", 0); |
| 385 |
let other_key = repo.store().put_identity(&other_id).unwrap(); |
| 05e71ed6 Andy McCall ↶ |
386 |
write(root, "a.txt", b"a\nb\nc\nd\n"); |
| 387 |
repo.config.set("core.sign", "seal").unwrap(); |
| 388 |
let h3 = repo.commit("ada's, unsigned", &[]).unwrap(); |
| 389 |
let forged = other.sign(Domain::Change, &other_key, &h3); |
| 390 |
repo.store().put_signature(&h3, &forged).unwrap(); |
| 75167672 Andy McCall |
391 |
assert!( |
| 05e71ed6 Andy McCall ↶ |
392 |
matches!(repo.verify_change(&h3).unwrap(), SignatureStatus::Invalid(m) if m.contains("neither the author")) |
| 75167672 Andy McCall |
393 |
); |
| 05e71ed6 Andy McCall ↶ |
394 |
let adopted = other.sign(Domain::Change, &other_key, &h0); |
| 395 |
repo.store().put_signature(&h0, &adopted).unwrap(); |
| 396 |
assert!( |
| 397 |
matches!(repo.verify_change(&h0).unwrap(), SignatureStatus::Valid { name, .. } if name == "Mallory") |
| 398 |
); |
| 98286406 Andy McCall ↶ |
399 |
|
| 400 |
|
| 401 |
let mut sig = repo.store().signatures(&h2).unwrap().remove(0); |
| 402 |
sig.ed[0] ^= 1; |
| 403 |
repo.store().put_signature(&h2, &sig).unwrap(); |
| 75167672 Andy McCall |
404 |
assert!(matches!( |
| 405 |
repo.verify_change(&h2).unwrap(), |
| 406 |
SignatureStatus::Invalid(_) |
| 407 |
)); |
| 98286406 Andy McCall ↶ |
408 |
|
| 409 |
|
| 410 |
let no_keys = Repo::open(root).unwrap().with_home(root.join("nowhere")); |
| 411 |
write(root, "a.txt", b"z\n"); |
| 412 |
assert!(no_keys.commit("cannot sign", &[]).is_err()); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn checkout_moves_the_working_copy() { |
| 417 |
hermetic(); |
| 418 |
let dir = tempfile::tempdir().unwrap(); |
| 419 |
let root = dir.path(); |
| 420 |
let repo = Repo::init(root).unwrap(); |
| 421 |
write(root, "a.txt", b"one\n"); |
| 422 |
write(root, "sub/deep/b.txt", b"b\n"); |
| 423 |
let h1 = repo.commit("first", &[]).unwrap(); |
| 424 |
write(root, "a.txt", b"two\n"); |
| 425 |
fs::remove_file(root.join("sub/deep/b.txt")).unwrap(); |
| 426 |
write(root, "bin.dat", &[0, 1, 2, 3]); |
| 427 |
let h2 = repo.commit("second", &[]).unwrap(); |
| 428 |
|
| 429 |
let r = repo.checkout(&[h1], false, "oxy checkout").unwrap(); |
| 75167672 Andy McCall |
430 |
assert_eq!( |
| 431 |
r, |
| 432 |
CheckoutReport { |
| 433 |
written: 2, |
| 434 |
removed: 1, |
| 435 |
conflicted: false, |
| 436 |
notes: vec![] |
| 437 |
} |
| 438 |
); |
| 98286406 Andy McCall ↶ |
439 |
assert_eq!(fs::read(root.join("a.txt")).unwrap(), b"one\n"); |
| 440 |
assert_eq!(fs::read(root.join("sub/deep/b.txt")).unwrap(), b"b\n"); |
| 441 |
assert!(!root.join("bin.dat").exists()); |
| 442 |
let (_, wc) = repo.working_copy().unwrap(); |
| 443 |
assert_eq!(wc.deps, vec![h1]); |
| 75167672 Andy McCall |
444 |
assert!( |
| 445 |
!repo.snapshot("status").unwrap().changed, |
| 446 |
"checkout leaves a clean working copy" |
| 447 |
); |
| 98286406 Andy McCall ↶ |
448 |
|
| 449 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2]); |
| 450 |
|
| 451 |
|
| 452 |
write(root, "a.txt", b"dirty\n"); |
| 453 |
assert!(repo.checkout(&[h2], false, "oxy checkout").is_err()); |
| 75167672 Andy McCall |
454 |
let r = repo |
| 455 |
.checkout(&[h2], true, "oxy checkout --discard") |
| 456 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
457 |
assert_eq!(fs::read(root.join("a.txt")).unwrap(), b"two\n"); |
| 458 |
assert_eq!(fs::read(root.join("bin.dat")).unwrap(), [0, 1, 2, 3]); |
| 459 |
assert!(!root.join("sub").exists(), "empty directories are pruned"); |
| 460 |
assert_eq!(r.removed, 1); |
| 461 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 462 |
|
| 463 |
|
| 464 |
#[cfg(unix)] |
| 465 |
{ |
| 466 |
fs::remove_dir_all(root.join("sub")).ok(); |
| 467 |
std::os::unix::fs::symlink("/tmp", root.join("sub")).unwrap(); |
| 468 |
assert!(repo.checkout(&[h1], true, "oxy checkout").is_err()); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn large_files_are_chunked_and_read_back() { |
| 474 |
hermetic(); |
| 475 |
let dir = tempfile::tempdir().unwrap(); |
| 476 |
let repo = Repo::init(dir.path()).unwrap(); |
| 477 |
let mut big = vec![0u8; 700 * 1024]; |
| 478 |
for (i, b) in big.iter_mut().enumerate() { |
| 479 |
*b = (i as u32).wrapping_mul(2654435761).to_le_bytes()[0]; |
| 480 |
} |
| 481 |
let r = repo.store_blob(&big).unwrap(); |
| 482 |
let blob = Blob::decode(&repo.store().get_typed(&r, ObjectType::Blob).unwrap()).unwrap(); |
| 483 |
assert!(blob.chunks.len() >= 3, "{}", blob.chunks.len()); |
| 484 |
assert_eq!(repo.read_blob(&r).unwrap(), big); |
| 485 |
assert_eq!(Repo::blob_ref(&big), r); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn clone_push_pull_with_seals_and_attestations() { |
| 490 |
hermetic(); |
| 491 |
let dir = tempfile::tempdir().unwrap(); |
| 492 |
let root = dir.path(); |
| 493 |
let origin_path = root.join("origin"); |
| 494 |
let origin = Repo::init(&origin_path).unwrap(); |
| 495 |
let origin_url = origin_path.to_string_lossy().to_string(); |
| 496 |
|
| 497 |
|
| 75167672 Andy McCall |
498 |
let mut a = Repo::clone(&origin_url, &root.join("a")) |
| 499 |
.unwrap() |
| 500 |
.with_home(root.join("home-a")); |
| 98286406 Andy McCall ↶ |
501 |
assert_eq!(a.id().unwrap(), origin.id().unwrap()); |
| 75167672 Andy McCall |
502 |
let (key_a, _) = a |
| 503 |
.create_identity("Ada", "[email protected]", IdentityScope::Repo) |
| 504 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
505 |
write(&root.join("a"), "f.txt", b"a\n"); |
| 506 |
let h1 = a.commit("first", &[]).unwrap(); |
| 507 |
let results = a.push("origin", false).unwrap(); |
| 508 |
assert!(results[0].ok, "{}", results[0].reason); |
| 509 |
assert_eq!(origin.bookmark_heads().unwrap(), vec![h1]); |
| 510 |
|
| 75167672 Andy McCall |
511 |
assert!( |
| 512 |
matches!(origin.authenticate_change(&h1).unwrap(), SignatureStatus::Valid { key, sealed: true, .. } if key == key_a) |
| 513 |
); |
| 514 |
assert_eq!( |
| 515 |
origin.verify_change(&h1).unwrap(), |
| 516 |
SignatureStatus::Unsigned |
| 517 |
); |
| 98286406 Andy McCall ↶ |
518 |
let (_, att) = origin.latest_ref_attest("main").unwrap().unwrap(); |
| 519 |
assert!(matches!(att.attest.body, AttestBody::Ref { serial: 1, .. })); |
| 520 |
let again = a.push("origin", false).unwrap(); |
| 521 |
assert!(again[0].ok && again[0].reason.contains("up to date")); |
| 522 |
let (_, att) = origin.latest_ref_attest("main").unwrap().unwrap(); |
| 75167672 Andy McCall |
523 |
assert!( |
| 524 |
matches!(att.attest.body, AttestBody::Ref { serial: 1, .. }), |
| 525 |
"a no-op push does not attest again" |
| 526 |
); |
| 98286406 Andy McCall ↶ |
527 |
|
| 528 |
|
| 529 |
let b = Repo::clone(&origin_url, &root.join("b")).unwrap(); |
| 530 |
assert_eq!(fs::read(root.join("b/f.txt")).unwrap(), b"a\n"); |
| 531 |
assert_eq!(b.bookmark_heads().unwrap(), vec![h1]); |
| 75167672 Andy McCall |
532 |
assert!(matches!( |
| 533 |
b.authenticate_change(&h1).unwrap(), |
| 534 |
SignatureStatus::Valid { sealed: true, .. } |
| 535 |
)); |
| 98286406 Andy McCall ↶ |
536 |
|
| 537 |
|
| 538 |
write(&root.join("b"), "f.txt", b"a\nb\n"); |
| 539 |
let h2 = b.commit("second", &[]).unwrap(); |
| 540 |
assert!(b.push("origin", false).unwrap()[0].ok); |
| 541 |
assert_eq!(origin.bookmark_heads().unwrap(), vec![h2]); |
| 542 |
|
| 543 |
|
| 544 |
write(&root.join("a"), "g.txt", b"g\n"); |
| 545 |
let h3 = a.commit("third", &[]).unwrap(); |
| 546 |
assert!(a.push("origin", false).is_err()); |
| 547 |
let pr = a.pull("origin").unwrap(); |
| 548 |
assert!(pr.changed && pr.checked_out); |
| 549 |
let mut want = vec![h2, h3]; |
| 550 |
want.sort(); |
| 551 |
assert_eq!(pr.heads, want); |
| 552 |
assert_eq!(fs::read(root.join("a/f.txt")).unwrap(), b"a\nb\n"); |
| 553 |
assert_eq!(fs::read(root.join("a/g.txt")).unwrap(), b"g\n"); |
| 554 |
let results = a.push("origin", false).unwrap(); |
| 555 |
assert!(results[0].ok, "{}", results[0].reason); |
| 556 |
let mut origin_heads = origin.bookmark_heads().unwrap(); |
| 557 |
origin_heads.sort(); |
| 558 |
assert_eq!(origin_heads, want); |
| 559 |
let (_, att) = origin.latest_ref_attest("main").unwrap().unwrap(); |
| 560 |
assert!(matches!(att.attest.body, AttestBody::Ref { serial: 2, .. })); |
| 561 |
|
| 562 |
|
| 563 |
let pr = b.pull("origin").unwrap(); |
| 564 |
assert_eq!(pr.heads, want); |
| 565 |
assert_eq!(fs::read(root.join("b/g.txt")).unwrap(), b"g\n"); |
| 566 |
assert!(!b.snapshot("status").unwrap().changed); |
| 567 |
|
| 568 |
|
| 569 |
write(&root.join("a"), "f.txt", b"a\nA\n"); |
| 570 |
a.commit("a-edit", &[]).unwrap(); |
| 571 |
a.push("origin", false).unwrap(); |
| 572 |
write(&root.join("b"), "f.txt", b"a\nB\n"); |
| 573 |
b.commit("b-edit", &[]).unwrap(); |
| 574 |
let pr = b.pull("origin").unwrap(); |
| 575 |
assert!(pr.changed && pr.checked_out); |
| 576 |
let text = fs::read_to_string(root.join("b/f.txt")).unwrap(); |
| 577 |
assert!(text.contains("<<<<<<< oxy conflict"), "{text}"); |
| 578 |
write(&root.join("b"), "f.txt", b"a\nAB\n"); |
| 579 |
let hr = b.commit("resolve", &[]).unwrap(); |
| 75167672 Andy McCall |
580 |
assert_eq!( |
| 581 |
b.store().get_change(&hr).unwrap().kind, |
| 582 |
ChangeKind::Resolution |
| 583 |
); |
| 98286406 Andy McCall ↶ |
584 |
assert!(b.push("origin", false).unwrap()[0].ok); |
| 585 |
a.pull("origin").unwrap(); |
| 586 |
assert_eq!(fs::read(root.join("a/f.txt")).unwrap(), b"a\nAB\n"); |
| 587 |
|
| 588 |
|
| 589 |
let mut want = vec![hr, h3]; |
| 590 |
want.sort(); |
| 591 |
assert_eq!(a.bookmark_heads().unwrap(), want); |
| 592 |
assert!(!a.load_state(&want).unwrap().materialize().is_conflicted()); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn receive_push_rejects_bad_updates() { |
| 597 |
hermetic(); |
| 598 |
let dir = tempfile::tempdir().unwrap(); |
| 599 |
let root = dir.path(); |
| 600 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 601 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
602 |
let mut a = Repo::clone(&url, &root.join("a")) |
| 603 |
.unwrap() |
| 604 |
.with_home(root.join("home")); |
| 605 |
let (key, _) = a |
| 606 |
.create_identity("Ada", "a@b", IdentityScope::Repo) |
| 607 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
608 |
write(&root.join("a"), "f.txt", b"x\n"); |
| 609 |
let h1 = a.commit("one", &[]).unwrap(); |
| 610 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 611 |
let empty = origin.store().put_state(&StateObject::new(vec![])).unwrap(); |
| 75167672 Andy McCall |
612 |
let s1 = origin |
| 613 |
.store() |
| 614 |
.put_state(&StateObject::new(vec![h1])) |
| 615 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
616 |
|
| 617 |
|
| 75167672 Andy McCall |
618 |
let ghost = origin |
| 619 |
.store() |
| 620 |
.put_state(&StateObject::new(vec![ObjectRef([9; 32])])) |
| 621 |
.unwrap(); |
| 622 |
let r = origin |
| 623 |
.receive_push( |
| 624 |
Bundle::default(), |
| 625 |
vec![RefUpdate { |
| 626 |
name: "main".into(), |
| 627 |
old_state: s1, |
| 628 |
new_state: ghost, |
| 629 |
force: false, |
| 630 |
attestation: None, |
| 631 |
}], |
| 632 |
None, |
| 633 |
vec![], |
| 634 |
) |
| 635 |
.unwrap(); |
| 636 |
assert!( |
| 637 |
!r[0].ok && r[0].reason.contains("missing"), |
| 638 |
"{}", |
| 639 |
r[0].reason |
| 640 |
); |
| 98286406 Andy McCall ↶ |
641 |
|
| 642 |
|
| 75167672 Andy McCall |
643 |
let r = origin |
| 644 |
.receive_push( |
| 645 |
Bundle::default(), |
| 646 |
vec![RefUpdate { |
| 647 |
name: "main".into(), |
| 648 |
old_state: empty, |
| 649 |
new_state: s1, |
| 650 |
force: false, |
| 651 |
attestation: None, |
| 652 |
}], |
| 653 |
None, |
| 654 |
vec![], |
| 655 |
) |
| 656 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
657 |
assert!(!r[0].ok && r[0].reason.contains("stale"), "{}", r[0].reason); |
| 658 |
|
| 659 |
|
| 75167672 Andy McCall |
660 |
let r = origin |
| 661 |
.receive_push( |
| 662 |
Bundle::default(), |
| 663 |
vec![RefUpdate { |
| 664 |
name: "main".into(), |
| 665 |
old_state: s1, |
| 666 |
new_state: empty, |
| 667 |
force: false, |
| 668 |
attestation: None, |
| 669 |
}], |
| 670 |
None, |
| 671 |
vec![], |
| 672 |
) |
| 673 |
.unwrap(); |
| 674 |
assert!( |
| 675 |
!r[0].ok && r[0].reason.contains("fast-forward"), |
| 676 |
"{}", |
| 677 |
r[0].reason |
| 678 |
); |
| 679 |
let r = origin |
| 680 |
.receive_push( |
| 681 |
Bundle::default(), |
| 682 |
vec![RefUpdate { |
| 683 |
name: "main".into(), |
| 684 |
old_state: s1, |
| 685 |
new_state: empty, |
| 686 |
force: true, |
| 687 |
attestation: None, |
| 688 |
}], |
| 689 |
None, |
| 690 |
vec![], |
| 691 |
) |
| 692 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
693 |
assert!(r[0].ok); |
| 694 |
assert_eq!(origin.bookmark_heads().unwrap(), vec![]); |
| 695 |
|
| 696 |
|
| 75167672 Andy McCall |
697 |
let sa = a |
| 698 |
.sign_attest(&Attest { |
| 699 |
repo: a.id().unwrap(), |
| 700 |
hlc: Hlc::now(), |
| 701 |
body: AttestBody::Ref { |
| 702 |
name: "main".into(), |
| 703 |
old_state: empty, |
| 704 |
new_state: s1, |
| 705 |
serial: 1, |
| 706 |
}, |
| 707 |
}) |
| 708 |
.unwrap(); |
| 709 |
let r = origin |
| 710 |
.receive_push( |
| 711 |
Bundle::default(), |
| 712 |
vec![RefUpdate { |
| 713 |
name: "main".into(), |
| 714 |
old_state: empty, |
| 715 |
new_state: s1, |
| 716 |
force: false, |
| 717 |
attestation: Some(sa.clone()), |
| 718 |
}], |
| 719 |
None, |
| 720 |
vec![], |
| 721 |
) |
| 722 |
.unwrap(); |
| 723 |
assert!( |
| 724 |
!r[0].ok && r[0].reason.contains("serial"), |
| 725 |
"{}", |
| 726 |
r[0].reason |
| 727 |
); |
| 98286406 Andy McCall ↶ |
728 |
|
| 75167672 Andy McCall |
729 |
let sa2 = a |
| 730 |
.sign_attest(&Attest { |
| 731 |
repo: a.id().unwrap(), |
| 732 |
hlc: Hlc::now(), |
| 733 |
body: AttestBody::Ref { |
| 734 |
name: "main".into(), |
| 735 |
old_state: s1, |
| 736 |
new_state: empty, |
| 737 |
serial: 5, |
| 738 |
}, |
| 739 |
}) |
| 740 |
.unwrap(); |
| 741 |
let r = origin |
| 742 |
.receive_push( |
| 743 |
Bundle::default(), |
| 744 |
vec![RefUpdate { |
| 745 |
name: "main".into(), |
| 746 |
old_state: empty, |
| 747 |
new_state: s1, |
| 748 |
force: false, |
| 749 |
attestation: Some(sa2), |
| 750 |
}], |
| 751 |
None, |
| 752 |
vec![], |
| 753 |
) |
| 754 |
.unwrap(); |
| 755 |
assert!( |
| 756 |
!r[0].ok && r[0].reason.contains("describe"), |
| 757 |
"{}", |
| 758 |
r[0].reason |
| 759 |
); |
| 98286406 Andy McCall ↶ |
760 |
let _ = key; |
| 761 |
} |
| 762 |
|
| 763 |
#[test] |
| 764 |
fn user_config_supplies_defaults_and_clones_sign() { |
| 765 |
hermetic(); |
| 766 |
let dir = tempfile::tempdir().unwrap(); |
| 767 |
let root = dir.path(); |
| 768 |
let home = root.join("home"); |
| 769 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 770 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 771 |
|
| 772 |
|
| 773 |
let mut a = Repo::init(&root.join("a")).unwrap().with_home(&home); |
| 75167672 Andy McCall |
774 |
let (key, _) = a |
| 775 |
.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 776 |
.unwrap(); |
| 98286406 Andy McCall ↶ |
777 |
assert!(!a.config.repo_layer().contains_key("author.key")); |
| 778 |
assert_eq!(a.config.author_key, Some(key)); |
| 779 |
assert!(home.join("config.toml").exists()); |
| 780 |
a.set_user_config("core.sign", "change").unwrap(); |
| 781 |
assert!(a.config.sign_changes); |
| 782 |
|
| 783 |
|
| 784 |
a.config.set("core.sign", "seal").unwrap(); |
| 785 |
a.save_config().unwrap(); |
| 786 |
let again = Repo::open(&root.join("a")).unwrap().with_home(&home); |
| 787 |
assert!(!again.config.sign_changes); |
| 788 |
assert_eq!(again.config.author_key, Some(key)); |
| 75167672 Andy McCall |
789 |
assert_eq!( |
| 790 |
read_config_file(&root.join("a/.oxy/config")) |
| 791 |
.unwrap() |
| 792 |
.get("core.sign") |
| 793 |
.map(String::as_str), |
| 794 |
Some("seal") |
| 795 |
); |
| 98286406 Andy McCall ↶ |
796 |
|
| 797 |
|
| 798 |
|
| 799 |
let b = Repo::clone(&url, &root.join("b")).unwrap().with_home(&home); |
| 800 |
assert!(b.config.sign_changes); |
| 801 |
write(&root.join("b"), "f.txt", b"f\n"); |
| 802 |
let h = b.commit("signed from a clone", &[]).unwrap(); |
| 75167672 Andy McCall |
803 |
assert!( |
| 804 |
matches!(b.verify_change(&h).unwrap(), SignatureStatus::Valid { key: k, .. } if k == key) |
| 805 |
); |
| 98286406 Andy McCall ↶ |
806 |
assert!(b.push("origin", false).unwrap()[0].ok); |
| 75167672 Andy McCall |
807 |
assert!(matches!( |
| 808 |
origin.verify_change(&h).unwrap(), |
| 809 |
SignatureStatus::Valid { .. } |
| 810 |
)); |
| 98286406 Andy McCall ↶ |
811 |
} |
| 812 |
|
| 813 |
#[test] |
| 814 |
fn format1_repositories_are_migrated_on_open() { |
| 815 |
hermetic(); |
| 816 |
use crate::oplog::{Format1Op, Format1View}; |
| 817 |
let dir = tempfile::tempdir().unwrap(); |
| 818 |
let root = dir.path(); |
| 819 |
let repo = Repo::init(root).unwrap(); |
| 820 |
write(root, "a.txt", b"a\n"); |
| 821 |
let h1 = repo.commit("one", &[]).unwrap(); |
| 822 |
let (wc_hash, _) = repo.working_copy().unwrap(); |
| 823 |
|
| 824 |
let mut v1 = Format1View::default(); |
| 825 |
v1.bookmarks.insert("main".into(), vec![]); |
| 826 |
v1.workspaces.insert(repo.workspace(), wc_hash); |
| 75167672 Andy McCall |
827 |
let init = Format1Op { |
| 828 |
parents: vec![], |
| 829 |
hlc: Hlc::default(), |
| 830 |
command: "oxy init".into(), |
| 831 |
view: v1.clone(), |
| 832 |
}; |
| 98286406 Andy McCall ↶ |
833 |
let init_ref = repo.store().put(ObjectType::Op, &init.encode()).unwrap(); |
| 834 |
v1.bookmarks.insert("main".into(), vec![h1]); |
| 75167672 Andy McCall |
835 |
let commit = Format1Op { |
| 836 |
parents: vec![init_ref], |
| 837 |
hlc: Hlc::default(), |
| 838 |
command: "oxy commit".into(), |
| 839 |
view: v1, |
| 840 |
}; |
| 98286406 Andy McCall ↶ |
841 |
let commit_ref = repo.store().put(ObjectType::Op, &commit.encode()).unwrap(); |
| 842 |
write_atomic(&root.join(".oxy/ops/head"), commit_ref.to_hex().as_bytes()).unwrap(); |
| 843 |
write_atomic(&root.join(".oxy/format"), FORMAT1_LINE.as_bytes()).unwrap(); |
| 844 |
drop(repo); |
| 845 |
|
| 846 |
let repo = Repo::open(root).unwrap(); |
| 75167672 Andy McCall |
847 |
assert_eq!( |
| 848 |
fs::read_to_string(root.join(".oxy/format")).unwrap(), |
| 849 |
FORMAT_LINE |
| 850 |
); |
| 98286406 Andy McCall ↶ |
851 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h1]); |
| 852 |
let ops = repo.op_log().unwrap(); |
| 75167672 Andy McCall |
853 |
assert_eq!( |
| 854 |
ops.iter() |
| 855 |
.map(|(_, o)| o.command.as_str()) |
| 856 |
.collect::<Vec<_>>(), |
| 857 |
["oxy commit", "oxy init"] |
| 858 |
); |
| 98286406 Andy McCall ↶ |
859 |
assert_eq!(repo.heads_of(&ops[1].1.view, "main").unwrap(), vec![]); |
| 860 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 861 |
|
| 862 |
assert_eq!(Repo::open(root).unwrap().op_log().unwrap().len(), 2); |
| 863 |
} |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn undo_skips_snapshot_operations() { |
| 867 |
hermetic(); |
| 868 |
let dir = tempfile::tempdir().unwrap(); |
| 869 |
let root = dir.path(); |
| 870 |
let repo = Repo::init(root).unwrap(); |
| 871 |
write(root, "a.txt", b"a\n"); |
| 872 |
let h1 = repo.commit("a", &[]).unwrap(); |
| 873 |
|
| 874 |
write(root, "b.txt", b"b\n"); |
| 875 |
let s = repo.snapshot("oxy status").unwrap(); |
| 876 |
assert!(s.changed); |
| 877 |
assert!(repo.head_op().unwrap().1.is_snapshot()); |
| 878 |
assert_eq!(repo.last_command().unwrap().command, "oxy commit -m \"a\""); |
| 879 |
|
| 880 |
repo.undo().unwrap(); |
| 881 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![]); |
| 882 |
let s = repo.snapshot("oxy status").unwrap(); |
| 883 |
let paths: Vec<_> = s.statuses.iter().map(|p| p.path.as_str()).collect(); |
| 75167672 Andy McCall |
884 |
assert_eq!( |
| 885 |
paths, |
| 886 |
["a.txt", "b.txt"], |
| 887 |
"both files are back in the working copy" |
| 888 |
); |
| 98286406 Andy McCall ↶ |
889 |
assert!(repo.store().get_change(&h1).unwrap().deps.is_empty()); |
| 890 |
|
| 891 |
assert_eq!(repo.last_command().unwrap().command, "oxy init"); |
| 892 |
assert!(repo.undo().is_err()); |
| 893 |
} |
| 894 |
|
| 895 |
#[test] |
| 896 |
fn independent_changes_commute_and_make_multiple_heads() { |
| 897 |
hermetic(); |
| 898 |
let dir = tempfile::tempdir().unwrap(); |
| 899 |
let root = dir.path(); |
| 900 |
let repo = Repo::init(root).unwrap(); |
| 901 |
write(root, "a.txt", b"a\n"); |
| 902 |
let h1 = repo.commit("a", &[]).unwrap(); |
| 903 |
write(root, "b.txt", b"b\n"); |
| 904 |
let h2 = repo.commit("b", &[]).unwrap(); |
| 905 |
let c2 = repo.store().get_change(&h2).unwrap(); |
| 906 |
assert!(c2.deps.is_empty(), "b.txt touches nothing from h1"); |
| 907 |
let mut heads = repo.bookmark_heads().unwrap(); |
| 908 |
heads.sort(); |
| 909 |
let mut want = vec![h1, h2]; |
| 910 |
want.sort(); |
| 911 |
assert_eq!(heads, want); |
| 912 |
|
| 913 |
write(root, "a.txt", b"a\na2\n"); |
| 914 |
let h3 = repo.commit("a2", &[]).unwrap(); |
| 915 |
assert_eq!(repo.store().get_change(&h3).unwrap().deps, vec![h1]); |
| 916 |
let heads: BTreeSet<_> = repo.bookmark_heads().unwrap().into_iter().collect(); |
| 917 |
assert_eq!(heads, [h2, h3].into_iter().collect()); |
| 918 |
|
| 919 |
write(root, "c.txt", b"c\n"); |
| 920 |
let h4 = repo.commit("c", &[h2]).unwrap(); |
| 921 |
assert_eq!(repo.store().get_change(&h4).unwrap().deps, vec![h2]); |
| 922 |
} |
| 923 |
|
| 924 |
#[test] |
| 925 |
fn mode_change_and_text_binary_switch() { |
| 926 |
hermetic(); |
| 927 |
let dir = tempfile::tempdir().unwrap(); |
| 928 |
let root = dir.path(); |
| 929 |
let repo = Repo::init(root).unwrap(); |
| 930 |
write(root, "run.sh", b"#!/bin/sh\n"); |
| 931 |
repo.commit("add script", &[]).unwrap(); |
| 932 |
#[cfg(unix)] |
| 933 |
{ |
| 934 |
use std::os::unix::fs::PermissionsExt; |
| 935 |
fs::set_permissions(root.join("run.sh"), fs::Permissions::from_mode(0o755)).unwrap(); |
| 936 |
let s = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
937 |
assert_eq!( |
| 938 |
statuses(&s), |
| 939 |
vec![("run.sh".into(), ChangeStatus::ModeChanged)] |
| 940 |
); |
| 98286406 Andy McCall ↶ |
941 |
let h = repo.commit("exec", &[]).unwrap(); |
| 942 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 943 |
assert!(snap.files["run.sh"].mode.exec); |
| 944 |
} |
| 945 |
|
| 946 |
write(root, "run.sh", b"\0binary"); |
| 947 |
let h = repo.commit("binary", &[]).unwrap(); |
| 948 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 949 |
assert!(snap.files["run.sh"].mode.opaque); |
| 950 |
write(root, "run.sh", b"text again\n"); |
| 951 |
let h = repo.commit("text", &[]).unwrap(); |
| 952 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 953 |
assert!(!snap.files["run.sh"].mode.opaque); |
| 75167672 Andy McCall |
954 |
assert!( |
| 955 |
matches!(&snap.files["run.sh"].content, FileContent::Text { bytes, .. } if bytes == b"text again\n") |
| 956 |
); |
| 98286406 Andy McCall ↶ |
957 |
assert!(!snap.is_conflicted()); |
| 958 |
} |
| 83e0677b Andy McCall ↶ |
959 |
|
| 960 |
#[test] |
| 961 |
fn trust_policy_governs_pushes_and_fetches() { |
| 962 |
hermetic(); |
| 963 |
let dir = tempfile::tempdir().unwrap(); |
| 964 |
let root = dir.path(); |
| 965 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 966 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 967 |
|
| 968 |
|
| 75167672 Andy McCall |
969 |
let mut a = Repo::clone(&url, &root.join("a")) |
| 970 |
.unwrap() |
| 971 |
.with_home(root.join("home-a")); |
| 972 |
let (key_a, _) = a |
| 973 |
.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 974 |
.unwrap(); |
| 975 |
let mut b = Repo::clone(&url, &root.join("b")) |
| 976 |
.unwrap() |
| 977 |
.with_home(root.join("home-b")); |
| 83e0677b Andy McCall ↶ |
978 |
write(&root.join("b"), "b.txt", b"b\n"); |
| 979 |
b.commit("unsigned before policy", &[]).unwrap(); |
| 980 |
assert!(b.push("origin", false).unwrap()[0].ok); |
| 981 |
|
| 982 |
|
| 983 |
write(&root.join("a"), "a.txt", b"a\n"); |
| 984 |
a.commit("a", &[]).unwrap(); |
| 985 |
let sp = a.policy_init().unwrap(); |
| 986 |
assert_eq!(sp.policy.serial, 1); |
| 987 |
a.pull("origin").unwrap(); |
| 988 |
let results = a.push("origin", false).unwrap(); |
| 989 |
assert!(results.iter().all(|r| r.ok), "{results:?}"); |
| 990 |
assert_eq!(origin.current_policy().unwrap().unwrap().1.policy.serial, 1); |
| 991 |
|
| 992 |
|
| 993 |
b.pull("origin").unwrap(); |
| 75167672 Andy McCall |
994 |
assert!( |
| 995 |
b.current_policy().unwrap().is_some(), |
| 996 |
"the policy arrived with the fetch" |
| 997 |
); |
| 83e0677b Andy McCall ↶ |
998 |
assert!(b.push("origin", false).is_err()); |
| 999 |
|
| 75167672 Andy McCall |
1000 |
let (key_b, id_b) = b |
| 1001 |
.create_identity("Bob", "[email protected]", IdentityScope::User) |
| 1002 |
.unwrap(); |
| 83e0677b Andy McCall ↶ |
1003 |
write(&root.join("b"), "b.txt", b"b2\n"); |
| 1004 |
let h_b2 = b.commit("b2 as Bob", &[]).unwrap(); |
| 1005 |
assert_eq!(b.store().get_change(&h_b2).unwrap().author, key_b); |
| 1006 |
let r = b.push("origin", false).unwrap(); |
| 75167672 Andy McCall |
1007 |
assert!( |
| 1008 |
!r[0].ok && r[0].reason.contains("may not update"), |
| 1009 |
"{:?}", |
| 1010 |
r |
| 1011 |
); |
| 83e0677b Andy McCall ↶ |
1012 |
|
| 1013 |
|
| 1014 |
let sp = a.policy_grant(id_b.clone(), None).unwrap(); |
| 1015 |
assert_eq!(sp.policy.serial, 2); |
| 1016 |
let r = a.push("origin", false).unwrap(); |
| 1017 |
assert!(r.iter().any(|x| x.name == "policy" && x.ok), "{r:?}"); |
| 1018 |
|
| 75167672 Andy McCall |
1019 |
assert!(b |
| 1020 |
.push("origin", false) |
| 1021 |
.unwrap_or_else(|e| vec![RefResult { |
| 1022 |
name: "main".into(), |
| 1023 |
ok: false, |
| 1024 |
reason: e.to_string() |
| 1025 |
}]) |
| 1026 |
.iter() |
| 1027 |
.all(|x| !x.ok)); |
| 83e0677b Andy McCall ↶ |
1028 |
b.pull("origin").unwrap(); |
| 1029 |
assert_eq!(b.current_policy().unwrap().unwrap().1.policy.serial, 2); |
| 1030 |
let r = b.push("origin", false).unwrap(); |
| 1031 |
assert!(r[0].ok, "{:?}", r); |
| 1032 |
|
| 1033 |
let heads = origin.bookmark_heads().unwrap(); |
| 1034 |
for h in &heads { |
| 1035 |
let c = origin.store().get_change(h).unwrap(); |
| 1036 |
if c.author == key_b { |
| 75167672 Andy McCall |
1037 |
origin |
| 1038 |
.change_allowed( |
| 1039 |
h, |
| 1040 |
&origin.current_policy().unwrap().unwrap().1.policy, |
| 1041 |
"main", |
| 1042 |
) |
| 1043 |
.unwrap(); |
| 83e0677b Andy McCall ↶ |
1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
let mut forged = b.current_policy().unwrap().unwrap().1.policy.clone(); |
| 1049 |
forged.serial = 3; |
| 1050 |
forged.roots = vec![key_b]; |
| 1051 |
let fh = forged.hash(); |
| 1052 |
let (_, sk_b) = b.secret_key().unwrap().unwrap(); |
| 75167672 Andy McCall |
1053 |
let forged = SignedPolicy { |
| 1054 |
policy: forged, |
| 1055 |
sigs: vec![sk_b.sign(Domain::Policy, &key_b, &fh)], |
| 1056 |
}; |
| 1057 |
let r = origin |
| 1058 |
.receive_push(Bundle::default(), vec![], Some(forged), vec![]) |
| 1059 |
.unwrap(); |
| 83e0677b Andy McCall ↶ |
1060 |
assert!(!r[0].ok && r[0].reason.contains("root"), "{:?}", r); |
| 1061 |
let stale = a.current_policy().unwrap().unwrap().1; |
| 1062 |
let mut old = stale.clone(); |
| 1063 |
old.policy.serial = 1; |
| 75167672 Andy McCall |
1064 |
let r = origin |
| 1065 |
.receive_push(Bundle::default(), vec![], Some(old), vec![]) |
| 1066 |
.unwrap(); |
| 83e0677b Andy McCall ↶ |
1067 |
assert!(!r[0].ok && r[0].reason.contains("serial"), "{:?}", r); |
| 1068 |
|
| 1069 |
|
| 1070 |
a.pull("origin").unwrap(); |
| 1071 |
a.policy_revoke(&key_b).unwrap(); |
| 1072 |
let r = a.push("origin", false).unwrap(); |
| 1073 |
assert!(r.iter().any(|x| x.name == "policy" && x.ok), "{r:?}"); |
| 1074 |
b.pull("origin").unwrap(); |
| 1075 |
write(&root.join("b"), "b.txt", b"b3\n"); |
| 1076 |
b.commit("b3", &[]).unwrap(); |
| 1077 |
let r = b.push("origin", false).unwrap(); |
| 1078 |
assert!(!r[0].ok, "{:?}", r); |
| 1079 |
|
| 1080 |
|
| 75167672 Andy McCall |
1081 |
let c = Repo::clone(&url, &root.join("c")) |
| 1082 |
.unwrap() |
| 1083 |
.with_home(root.join("home-c")); |
| 83e0677b Andy McCall ↶ |
1084 |
assert_eq!(c.current_policy().unwrap().unwrap().1.policy.serial, 3); |
| 75167672 Andy McCall |
1085 |
assert!(c |
| 1086 |
.current_policy() |
| 1087 |
.unwrap() |
| 1088 |
.unwrap() |
| 1089 |
.1 |
| 1090 |
.policy |
| 1091 |
.roots |
| 1092 |
.contains(&key_a)); |
| 83e0677b Andy McCall ↶ |
1093 |
assert!(!c.snapshot("status").unwrap().changed); |
| 1094 |
} |
| d02e545d Andy McCall ↶ |
1095 |
|
| 1096 |
#[test] |
| 1097 |
fn gc_packs_objects_and_history_still_works() { |
| 1098 |
hermetic(); |
| 1099 |
let dir = tempfile::tempdir().unwrap(); |
| 1100 |
let root = dir.path(); |
| 1101 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
1102 |
repo.create_identity("Ada", "a@b", IdentityScope::User) |
| 1103 |
.unwrap(); |
| d02e545d Andy McCall ↶ |
1104 |
repo.set_user_config("core.sign", "change").unwrap(); |
| 1105 |
write(root, "a.txt", b"one\n"); |
| 1106 |
let h1 = repo.commit("one", &[]).unwrap(); |
| 1107 |
write(root, "a.txt", b"two\n"); |
| 1108 |
write(root, "big.bin", &vec![0u8; 100_000]); |
| 1109 |
let h2 = repo.commit("two", &[]).unwrap(); |
| ba160f60 Andy McCall ↶ |
1110 |
|
| 1111 |
for i in 0..5 { |
| 1112 |
write(root, "a.txt", format!("two\n{i}\n").as_bytes()); |
| 1113 |
repo.snapshot("status").unwrap(); |
| 1114 |
} |
| 1115 |
write(root, "a.txt", b"two\n"); |
| 1116 |
repo.snapshot("status").unwrap(); |
| d02e545d Andy McCall ↶ |
1117 |
let before = repo.store().list().unwrap(); |
| 1118 |
assert!(repo.store().loose_count().unwrap() > 0); |
| ba160f60 Andy McCall ↶ |
1119 |
|
| 1120 |
|
| 1121 |
let r = repo.gc(true).unwrap(); |
| 1122 |
assert_eq!(r.packed.objects, before.len()); |
| 1123 |
assert_eq!(r.pruned.objects, 0); |
| d02e545d Andy McCall ↶ |
1124 |
assert_eq!(repo.store().loose_count().unwrap(), 0); |
| 1125 |
assert_eq!(repo.store().list().unwrap(), before); |
| ba160f60 Andy McCall ↶ |
1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
repo.config.set("core.op-retention", "0").unwrap(); |
| 1130 |
repo.save_config().unwrap(); |
| d02e545d Andy McCall ↶ |
1131 |
let repo = Repo::open(root).unwrap().with_home(root.join("home")); |
| ba160f60 Andy McCall ↶ |
1132 |
let r = repo.gc(true).unwrap(); |
| 1133 |
assert!(r.pruned.objects >= 5, "{r:?}"); |
| 1134 |
let after = repo.store().list().unwrap(); |
| 1135 |
assert!(after.len() < before.len()); |
| 1136 |
assert!(after.contains(&h1) && after.contains(&h2)); |
| 1137 |
for (_, op) in repo.op_log().unwrap() { |
| 1138 |
assert!(!op.command.is_empty(), "operation objects are all kept"); |
| 1139 |
} |
| 1140 |
|
| d02e545d Andy McCall ↶ |
1141 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2]); |
| 75167672 Andy McCall |
1142 |
assert!(matches!( |
| 1143 |
repo.verify_change(&h2).unwrap(), |
| 1144 |
SignatureStatus::Valid { .. } |
| 1145 |
)); |
| d02e545d Andy McCall ↶ |
1146 |
assert_eq!(repo.log().unwrap().len(), 3); |
| 1147 |
repo.checkout(&[h1], false, "checkout").unwrap(); |
| 1148 |
assert_eq!(fs::read(root.join("a.txt")).unwrap(), b"one\n"); |
| 1149 |
repo.checkout(&[h2], false, "checkout").unwrap(); |
| 1150 |
assert_eq!(fs::read(root.join("big.bin")).unwrap().len(), 100_000); |
| 1151 |
assert!(!repo.snapshot("status").unwrap().changed); |
| ba160f60 Andy McCall ↶ |
1152 |
|
| d02e545d Andy McCall ↶ |
1153 |
write(root, "a.txt", b"three\n"); |
| 1154 |
repo.commit("three", &[]).unwrap(); |
| ba160f60 Andy McCall ↶ |
1155 |
let r = repo.gc(false).unwrap(); |
| 1156 |
assert_eq!(r.pruned.objects, 0); |
| 1157 |
assert!(r.pruned.deferred > 0); |
| 1158 |
let r = repo.gc(true).unwrap(); |
| 1159 |
assert!(r.pruned.objects > 0); |
| 1160 |
assert_eq!(repo.log().unwrap().len(), 4); |
| d02e545d Andy McCall ↶ |
1161 |
} |
| 614541c3 Andy McCall ↶ |
1162 |
|
| 1163 |
#[test] |
| 1164 |
fn describe_amend_abandon_heads() { |
| 1165 |
hermetic(); |
| 1166 |
let dir = tempfile::tempdir().unwrap(); |
| 1167 |
let root = dir.path(); |
| 1168 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
1169 |
repo.create_identity("Ada", "a@b", IdentityScope::User) |
| 1170 |
.unwrap(); |
| 614541c3 Andy McCall ↶ |
1171 |
repo.set_user_config("core.sign", "change").unwrap(); |
| 1172 |
write(root, "a.txt", b"one\n"); |
| 1173 |
let h1 = repo.commit("first", &[]).unwrap(); |
| 1174 |
write(root, "a.txt", b"one\ntwo\n"); |
| 1175 |
let h2 = repo.commit("secnod", &[]).unwrap(); |
| 1176 |
let id2 = repo.store().get_change(&h2).unwrap().change_id; |
| 1177 |
|
| 1178 |
|
| 1179 |
let h2b = repo.describe(&h2, "second").unwrap(); |
| 1180 |
assert_ne!(h2b, h2); |
| 1181 |
let c = repo.store().get_change(&h2b).unwrap(); |
| 75167672 Andy McCall |
1182 |
assert_eq!( |
| 1183 |
(c.change_id, c.message.as_str(), c.deps.clone()), |
| 1184 |
(id2, "second", vec![h1]) |
| 1185 |
); |
| 614541c3 Andy McCall ↶ |
1186 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2b]); |
| 75167672 Andy McCall |
1187 |
assert!(matches!( |
| 1188 |
repo.verify_change(&h2b).unwrap(), |
| 1189 |
SignatureStatus::Valid { .. } |
| 1190 |
)); |
| 614541c3 Andy McCall ↶ |
1191 |
let s = repo.snapshot("status").unwrap(); |
| 1192 |
assert!(s.statuses.is_empty(), "{:?}", s.statuses); |
| 75167672 Andy McCall |
1193 |
assert_eq!( |
| 1194 |
repo.resolve_change(&id2.short(8)).unwrap(), |
| 1195 |
h2b, |
| 1196 |
"the old object is no longer reachable from the log" |
| 1197 |
); |
| c07e0e7d Andy McCall ↶ |
1198 |
|
| 1199 |
let h1b = repo.describe(&h1, "first, described").unwrap(); |
| 1200 |
assert_ne!(h1b, h1); |
| 1201 |
let heads = repo.bookmark_heads().unwrap(); |
| 1202 |
assert_eq!(heads.len(), 1); |
| 1203 |
let top = repo.store().get_change(&heads[0]).unwrap(); |
| 1204 |
assert_eq!((top.change_id, top.deps.clone()), (id2, vec![h1b])); |
| 75167672 Andy McCall |
1205 |
assert!( |
| 1206 |
matches!( |
| 1207 |
repo.verify_change(&heads[0]).unwrap(), |
| 1208 |
SignatureStatus::Valid { .. } |
| 1209 |
), |
| 1210 |
"rewritten own change is re-signed" |
| 1211 |
); |
| c07e0e7d Andy McCall ↶ |
1212 |
let (_, wc) = repo.working_copy().unwrap(); |
| 75167672 Andy McCall |
1213 |
assert_eq!( |
| 1214 |
wc.deps, |
| 1215 |
vec![heads[0]], |
| 1216 |
"the working copy follows the rewrite" |
| 1217 |
); |
| c07e0e7d Andy McCall ↶ |
1218 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 1219 |
let snap = repo.load_state(&heads).unwrap().materialize(); |
| 75167672 Andy McCall |
1220 |
assert!( |
| 1221 |
matches!(&snap.files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\ntwo\n") |
| 1222 |
); |
| 614541c3 Andy McCall ↶ |
1223 |
|
| 1224 |
|
| 1225 |
write(root, "a.txt", b"one\ntwo\nthree\n"); |
| 1226 |
write(root, "b.txt", b"b\n"); |
| 1227 |
let h2c = repo.amend(None).unwrap(); |
| 1228 |
let c = repo.store().get_change(&h2c).unwrap(); |
| 1229 |
assert_eq!(c.change_id, id2); |
| 1230 |
assert_eq!(c.message, "second"); |
| 1231 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2c]); |
| 75167672 Andy McCall |
1232 |
assert_eq!( |
| 1233 |
repo.log().unwrap().len(), |
| 1234 |
3, |
| 1235 |
"working copy, amended head, first" |
| 1236 |
); |
| 614541c3 Andy McCall ↶ |
1237 |
let snap = repo.load_state(&[h2c]).unwrap().materialize(); |
| 75167672 Andy McCall |
1238 |
assert!( |
| 1239 |
matches!(&snap.files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\ntwo\nthree\n") |
| 1240 |
); |
| 614541c3 Andy McCall ↶ |
1241 |
assert!(snap.files.contains_key("b.txt")); |
| 1242 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 75167672 Andy McCall |
1243 |
assert!( |
| 1244 |
repo.amend(None).is_err(), |
| 1245 |
"clean working copy and no message" |
| 1246 |
); |
| 614541c3 Andy McCall ↶ |
1247 |
let h2d = repo.amend(Some("second, amended")).unwrap(); |
| 75167672 Andy McCall |
1248 |
assert_eq!( |
| 1249 |
repo.store().get_change(&h2d).unwrap().message, |
| 1250 |
"second, amended" |
| 1251 |
); |
| 614541c3 Andy McCall ↶ |
1252 |
|
| 1253 |
|
| 1254 |
repo.abandon(&h2d).unwrap(); |
| 75167672 Andy McCall |
1255 |
assert_eq!( |
| 1256 |
repo.bookmark_heads().unwrap(), |
| 1257 |
vec![h1b], |
| 1258 |
"the described first change is the head again" |
| 1259 |
); |
| 614541c3 Andy McCall ↶ |
1260 |
let s = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
1261 |
let paths: Vec<_> = s |
| 1262 |
.statuses |
| 1263 |
.iter() |
| 1264 |
.map(|p| (p.path.as_str(), p.status)) |
| 1265 |
.collect(); |
| 1266 |
assert_eq!( |
| 1267 |
paths, |
| 1268 |
vec![ |
| 1269 |
("a.txt", ChangeStatus::Modified), |
| 1270 |
("b.txt", ChangeStatus::Added) |
| 1271 |
] |
| 1272 |
); |
| 614541c3 Andy McCall ↶ |
1273 |
assert_eq!(fs::read(root.join("a.txt")).unwrap(), b"one\ntwo\nthree\n"); |
| 1274 |
let h3 = repo.commit("recommitted", &[]).unwrap(); |
| 1275 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h3]); |
| 1276 |
|
| 1277 |
repo.undo().unwrap(); |
| 1278 |
repo.undo().unwrap(); |
| 1279 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![h2d]); |
| c07e0e7d Andy McCall ↶ |
1280 |
|
| 6706bdcf Andy McCall ↶ |
1281 |
|
| 1282 |
|
| 1283 |
|
| c07e0e7d Andy McCall ↶ |
1284 |
let base = repo.bookmark_heads().unwrap()[0]; |
| 1285 |
let base_change = repo.store().get_change(&base).unwrap(); |
| 1286 |
let h1_now = base_change.deps[0]; |
| 6706bdcf Andy McCall ↶ |
1287 |
let notes = repo.abandon(&h1_now).unwrap(); |
| 75167672 Andy McCall |
1288 |
assert!( |
| 1289 |
!notes.is_empty(), |
| 1290 |
"the dependent's edit of a.txt was reified" |
| 1291 |
); |
| 6706bdcf Andy McCall ↶ |
1292 |
let heads = repo.bookmark_heads().unwrap(); |
| 1293 |
let after = repo.load_state(&heads).unwrap().materialize(); |
| 75167672 Andy McCall |
1294 |
assert!( |
| 1295 |
!after.files.contains_key("a.txt") && after.files.contains_key("b.txt"), |
| 1296 |
"{:?}", |
| 1297 |
after.files.keys() |
| 1298 |
); |
| 6706bdcf Andy McCall ↶ |
1299 |
repo.undo().unwrap(); |
| 1300 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![base]); |
| c07e0e7d Andy McCall ↶ |
1301 |
write(root, "c.txt", b"c\n"); |
| 1302 |
let hc = repo.commit("c", &[]).unwrap(); |
| 1303 |
write(root, "d.txt", b"d\n"); |
| 1304 |
let hd = repo.commit("d", &[hc]).unwrap(); |
| 1305 |
assert_eq!(repo.store().get_change(&hd).unwrap().deps, vec![hc]); |
| 1306 |
repo.abandon(&hc).unwrap(); |
| 1307 |
let heads: BTreeSet<_> = repo.bookmark_heads().unwrap().into_iter().collect(); |
| 1308 |
assert!(!heads.contains(&hc)); |
| 75167672 Andy McCall |
1309 |
let d_now = heads |
| 1310 |
.iter() |
| 1311 |
.find(|h| repo.store().get_change(h).unwrap().message == "d") |
| 1312 |
.copied() |
| 1313 |
.unwrap(); |
| 1314 |
assert!( |
| 1315 |
repo.store().get_change(&d_now).unwrap().deps.is_empty(), |
| 1316 |
"d inherited c's (empty) dependencies" |
| 1317 |
); |
| c07e0e7d Andy McCall ↶ |
1318 |
let s = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
1319 |
assert_eq!( |
| 1320 |
s.statuses |
| 1321 |
.iter() |
| 1322 |
.map(|p| p.path.as_str()) |
| 1323 |
.collect::<Vec<_>>(), |
| 1324 |
vec!["c.txt"], |
| 1325 |
"c's content is now uncommitted work" |
| 1326 |
); |
| 614541c3 Andy McCall ↶ |
1327 |
} |
| 79ab8528 Andy McCall ↶ |
1328 |
|
| 1329 |
#[test] |
| 1330 |
fn git_export_mirrors_history() { |
| 1331 |
hermetic(); |
| 75167672 Andy McCall |
1332 |
if std::process::Command::new("git") |
| 1333 |
.arg("--version") |
| 1334 |
.output() |
| 1335 |
.is_err() |
| 1336 |
{ |
| 79ab8528 Andy McCall ↶ |
1337 |
eprintln!("git not installed; skipping"); |
| 1338 |
return; |
| 1339 |
} |
| 1340 |
let dir = tempfile::tempdir().unwrap(); |
| 1341 |
let root = dir.path(); |
| 75167672 Andy McCall |
1342 |
let mut repo = Repo::init(&root.join("r")) |
| 1343 |
.unwrap() |
| 1344 |
.with_home(root.join("home")); |
| 1345 |
repo.create_identity("Ada Lovelace", "[email protected]", IdentityScope::User) |
| 1346 |
.unwrap(); |
| 79ab8528 Andy McCall ↶ |
1347 |
let r = root.join("r"); |
| 1348 |
write(&r, "README.md", b"hello\n"); |
| 1349 |
write(&r, "src/main.rs", b"fn main() {}\n"); |
| 1350 |
let h1 = repo.commit("first", &[]).unwrap(); |
| 1351 |
write(&r, "src/main.rs", b"fn main() { run(); }\n"); |
| 1352 |
write(&r, "logo.png", b"\x89PNG\0\0"); |
| 1353 |
let h2 = repo.commit("second", &[]).unwrap(); |
| 1354 |
|
| 1355 |
let git_dir = root.join("mirror.git"); |
| 1356 |
let rep = repo.git_export(&git_dir).unwrap(); |
| 1357 |
assert_eq!((rep.exported, rep.skipped, rep.heads), (2, 0, 1)); |
| 1358 |
let git = |args: &[&str]| -> String { |
| 75167672 Andy McCall |
1359 |
let out = std::process::Command::new("git") |
| 1360 |
.arg("-C") |
| 1361 |
.arg(&git_dir) |
| 1362 |
.args(args) |
| 1363 |
.output() |
| 1364 |
.unwrap(); |
| 1365 |
assert!( |
| 1366 |
out.status.success(), |
| 1367 |
"{}", |
| 1368 |
String::from_utf8_lossy(&out.stderr) |
| 1369 |
); |
| 79ab8528 Andy McCall ↶ |
1370 |
String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 1371 |
}; |
| 1372 |
assert_eq!(git(&["log", "--format=%s", "main"]), "second\nfirst"); |
| 1373 |
assert_eq!(git(&["show", "main:src/main.rs"]), "fn main() { run(); }"); |
| 1374 |
assert_eq!(git(&["show", "main:README.md"]), "hello"); |
| 1375 |
let body = git(&["log", "-1", "--format=%B", "main"]); |
| 75167672 Andy McCall |
1376 |
assert!( |
| 1377 |
body.contains(&format!("Oxy-Hash: {}", h2.to_hex())), |
| 1378 |
"{body}" |
| 1379 |
); |
| 79ab8528 Andy McCall ↶ |
1380 |
assert!(body.contains("Change-Id: "), "{body}"); |
| 75167672 Andy McCall |
1381 |
assert_eq!( |
| 1382 |
git(&["log", "-1", "--format=%an <%ae>", "main"]), |
| 1383 |
"Ada Lovelace <[email protected]>" |
| 1384 |
); |
| 1385 |
assert_eq!( |
| 1386 |
git(&["ls-tree", "--name-only", "main"]), |
| 1387 |
"README.md\nlogo.png\nsrc" |
| 1388 |
); |
| 79ab8528 Andy McCall ↶ |
1389 |
|
| 1390 |
|
| 1391 |
let rep = repo.git_export(&git_dir).unwrap(); |
| 1392 |
assert_eq!((rep.exported, rep.skipped), (0, 2)); |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
repo.checkout(&[h1], false, "checkout").unwrap(); |
| 1397 |
write(&r, "other.txt", b"o\n"); |
| 1398 |
let h3 = repo.commit("third", &[]).unwrap(); |
| 1399 |
let rep = repo.git_export(&git_dir).unwrap(); |
| 1400 |
assert_eq!((rep.exported, rep.heads), (1, 2)); |
| 1401 |
assert_eq!(git(&["log", "--format=%s", "main"]), "third\nsecond\nfirst"); |
| 1402 |
let parents = git(&["rev-list", "--parents", "-1", "main"]); |
| 1403 |
assert_eq!(parents.split(' ').count(), 2, "one parent: {parents}"); |
| 1404 |
assert_eq!(git(&["show", "main:other.txt"]), "o"); |
| 1405 |
assert_eq!(git(&["show", "main:src/main.rs"]), "fn main() { run(); }"); |
| 75167672 Andy McCall |
1406 |
assert_eq!( |
| 1407 |
git(&["ls-tree", "--name-only", "main"]), |
| 1408 |
"README.md\nlogo.png\nother.txt\nsrc" |
| 1409 |
); |
| 79ab8528 Andy McCall ↶ |
1410 |
let body = git(&["log", "-1", "--format=%B", "main"]); |
| 75167672 Andy McCall |
1411 |
assert!( |
| 1412 |
body.contains(&format!("Oxy-Hash: {}", h3.to_hex())) && body.contains("Oxy-State: "), |
| 1413 |
"{body}" |
| 1414 |
); |
| 79ab8528 Andy McCall ↶ |
1415 |
|
| 1416 |
repo.describe(&h3, "third, described").unwrap(); |
| 1417 |
let rep = repo.git_export(&git_dir).unwrap(); |
| 1418 |
assert_eq!(rep.exported, 1); |
| 75167672 Andy McCall |
1419 |
assert_eq!( |
| 1420 |
git(&["log", "-1", "--format=%s", "main"]), |
| 1421 |
"third, described" |
| 1422 |
); |
| 79ab8528 Andy McCall ↶ |
1423 |
assert_eq!(git(&["log", "--format=%s", "main"]).lines().count(), 4); |
| 1424 |
let _ = h2; |
| 1425 |
} |
| c3069537 Andy McCall ↶ |
1426 |
|
| 1427 |
#[test] |
| 1428 |
fn git_import_reproduces_trees() { |
| 1429 |
hermetic(); |
| 75167672 Andy McCall |
1430 |
if std::process::Command::new("git") |
| 1431 |
.arg("--version") |
| 1432 |
.output() |
| 1433 |
.is_err() |
| 1434 |
{ |
| c3069537 Andy McCall ↶ |
1435 |
eprintln!("git not installed; skipping"); |
| 1436 |
return; |
| 1437 |
} |
| 1438 |
let dir = tempfile::tempdir().unwrap(); |
| 1439 |
let root = dir.path(); |
| 1440 |
let g = root.join("src.git"); |
| 1441 |
fs::create_dir_all(&g).unwrap(); |
| 1442 |
let git = |args: &[&str]| -> String { |
| 1443 |
let out = std::process::Command::new("git") |
| 1444 |
.arg("-C") |
| 1445 |
.arg(&g) |
| 75167672 Andy McCall |
1446 |
.args([ |
| 1447 |
"-c", |
| 1448 |
"user.name=Grace", |
| 1449 |
"-c", |
| 1450 |
"[email protected]", |
| 1451 |
"-c", |
| 1452 |
"commit.gpgsign=false", |
| 1453 |
]) |
| c3069537 Andy McCall ↶ |
1454 |
.args(args) |
| 1455 |
.output() |
| 1456 |
.unwrap(); |
| 75167672 Andy McCall |
1457 |
assert!( |
| 1458 |
out.status.success(), |
| 1459 |
"git {args:?}: {}", |
| 1460 |
String::from_utf8_lossy(&out.stderr) |
| 1461 |
); |
| c3069537 Andy McCall ↶ |
1462 |
String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 1463 |
}; |
| 1464 |
git(&["init", "-q", "-b", "main"]); |
| 1465 |
write(&g, "README.md", b"# Hi\n"); |
| 1466 |
write(&g, "src/lib.rs", b"pub fn a() {}\n"); |
| 1467 |
git(&["add", "."]); |
| 1468 |
git(&["commit", "-q", "-m", "initial"]); |
| 1469 |
write(&g, "src/lib.rs", b"pub fn a() {}\npub fn b() {}\n"); |
| 1470 |
write(&g, "run.sh", b"#!/bin/sh\n"); |
| 1471 |
#[cfg(unix)] |
| 1472 |
{ |
| 1473 |
use std::os::unix::fs::PermissionsExt; |
| 1474 |
fs::set_permissions(g.join("run.sh"), fs::Permissions::from_mode(0o755)).unwrap(); |
| 1475 |
} |
| 1476 |
write(&g, "logo.png", b"\x89PNG\0\0\x01"); |
| 1477 |
git(&["add", "."]); |
| 75167672 Andy McCall |
1478 |
git(&[ |
| 1479 |
"commit", |
| 1480 |
"-q", |
| 1481 |
"-m", |
| 1482 |
"second\n\nChange-Id: 0123456789abcdef0123456789abcdef", |
| 1483 |
]); |
| c3069537 Andy McCall ↶ |
1484 |
|
| 1485 |
git(&["checkout", "-q", "-b", "topic", "HEAD~1"]); |
| 1486 |
write(&g, "README.md", b"# Hi\n\nMore.\n"); |
| 1487 |
git(&["commit", "-q", "-am", "readme"]); |
| 1488 |
git(&["checkout", "-q", "main"]); |
| 1489 |
git(&["merge", "-q", "--no-ff", "-m", "merge topic", "topic"]); |
| 1490 |
|
| 1491 |
|
| 1492 |
let initial = git(&["rev-list", "--max-parents=0", "HEAD"]); |
| 1493 |
git(&["checkout", "-q", "-b", "clash", &initial]); |
| 1494 |
write(&g, "src/lib.rs", b"pub fn a() {}\npub fn c() {}\n"); |
| 1495 |
git(&["commit", "-q", "-am", "clash"]); |
| 1496 |
git(&["checkout", "-q", "main"]); |
| 75167672 Andy McCall |
1497 |
let _ = std::process::Command::new("git") |
| 1498 |
.arg("-C") |
| 1499 |
.arg(&g) |
| 1500 |
.args(["merge", "-q", "clash"]) |
| 1501 |
.output(); |
| 1502 |
write( |
| 1503 |
&g, |
| 1504 |
"src/lib.rs", |
| 1505 |
b"pub fn a() {}\npub fn b() {}\npub fn c() {}\n", |
| 1506 |
); |
| c3069537 Andy McCall ↶ |
1507 |
git(&["add", "."]); |
| 1508 |
git(&["commit", "-q", "-m", "resolve clash"]); |
| 1509 |
git(&["rm", "-q", "logo.png"]); |
| 1510 |
git(&["commit", "-q", "-m", "drop logo"]); |
| 6e723b81 Andy McCall ↶ |
1511 |
|
| 1512 |
git(&["mv", "src/lib.rs", "src/lib2.rs"]); |
| 75167672 Andy McCall |
1513 |
write( |
| 1514 |
&g, |
| 1515 |
"src/lib2.rs", |
| 1516 |
b"pub fn a() {}\npub fn b() {}\npub fn c() {}\npub fn d() {}\n", |
| 1517 |
); |
| 6e723b81 Andy McCall ↶ |
1518 |
git(&["add", "."]); |
| 1519 |
git(&["commit", "-q", "-m", "rename lib"]); |
| c3069537 Andy McCall ↶ |
1520 |
let expected_count: usize = git(&["rev-list", "--count", "main"]).parse().unwrap(); |
| 1521 |
|
| 1522 |
let dest = root.join("oxy"); |
| 1523 |
let repo = Repo::init(&dest).unwrap().with_home(root.join("home")); |
| 1524 |
let rep = repo.import_git(&g, "main").unwrap(); |
| 1525 |
assert_eq!(rep.commits, expected_count); |
| 1526 |
assert_eq!(rep.merges, 2); |
| 1527 |
|
| 75167672 Andy McCall |
1528 |
assert_eq!( |
| 1529 |
fs::read(dest.join("src/lib2.rs")).unwrap(), |
| 1530 |
b"pub fn a() {}\npub fn b() {}\npub fn c() {}\npub fn d() {}\n" |
| 1531 |
); |
| 6e723b81 Andy McCall ↶ |
1532 |
assert!(!dest.join("src/lib.rs").exists()); |
| 75167672 Andy McCall |
1533 |
assert_eq!( |
| 1534 |
fs::read(dest.join("README.md")).unwrap(), |
| 1535 |
b"# Hi\n\nMore.\n" |
| 1536 |
); |
| c3069537 Andy McCall ↶ |
1537 |
assert!(!dest.join("logo.png").exists()); |
| 1538 |
#[cfg(unix)] |
| 1539 |
{ |
| 1540 |
use std::os::unix::fs::PermissionsExt; |
| 75167672 Andy McCall |
1541 |
assert!( |
| 1542 |
fs::metadata(dest.join("run.sh")) |
| 1543 |
.unwrap() |
| 1544 |
.permissions() |
| 1545 |
.mode() |
| 1546 |
& 0o111 |
| 1547 |
!= 0 |
| 1548 |
); |
| c3069537 Andy McCall ↶ |
1549 |
} |
| 75167672 Andy McCall |
1550 |
assert!( |
| 1551 |
!repo.snapshot("status").unwrap().changed, |
| 1552 |
"clean after import" |
| 1553 |
); |
| c3069537 Andy McCall ↶ |
1554 |
let heads = repo.bookmark_heads().unwrap(); |
| 1555 |
let st = repo.load_state(&heads).unwrap(); |
| 1556 |
assert!(!st.materialize().is_conflicted()); |
| 1557 |
assert_eq!(st.len(), expected_count); |
| 75167672 Andy McCall |
1558 |
let tip = repo |
| 1559 |
.log() |
| 1560 |
.unwrap() |
| 1561 |
.into_iter() |
| 1562 |
.find(|e| e.change.message.starts_with("rename lib")) |
| 1563 |
.unwrap(); |
| 6e723b81 Andy McCall ↶ |
1564 |
let c = repo.store().get_change(&tip.hash).unwrap(); |
| 75167672 Andy McCall |
1565 |
assert!( |
| 1566 |
c.tree_ops |
| 1567 |
.iter() |
| 1568 |
.any(|op| matches!(op, TreeOp::Move { name, .. } if name == "lib2.rs")), |
| 1569 |
"{:?}", |
| 1570 |
c.tree_ops |
| 1571 |
); |
| 1572 |
assert!(!c |
| 1573 |
.tree_ops |
| 1574 |
.iter() |
| 1575 |
.any(|op| matches!(op, TreeOp::AddNode { .. }))); |
| c3069537 Andy McCall ↶ |
1576 |
|
| 1577 |
let log = repo.log().unwrap(); |
| 75167672 Andy McCall |
1578 |
let second = log |
| 1579 |
.iter() |
| 1580 |
.find(|e| e.change.message.starts_with("second")) |
| 1581 |
.unwrap(); |
| 1582 |
assert_eq!( |
| 1583 |
second.change.change_id, |
| 1584 |
ChangeId::from_hex("0123456789abcdef0123456789abcdef").unwrap() |
| 1585 |
); |
| 1586 |
assert!(second |
| 1587 |
.change |
| 1588 |
.message |
| 1589 |
.contains("Git-Author: Grace <[email protected]>")); |
| 1590 |
let resolve = log |
| 1591 |
.iter() |
| 1592 |
.find(|e| e.change.message.starts_with("resolve clash")) |
| 1593 |
.unwrap(); |
| 1594 |
assert_eq!( |
| 1595 |
resolve.change.kind, |
| 1596 |
ChangeKind::Resolution, |
| 1597 |
"a conflicting Git merge becomes a resolution" |
| 1598 |
); |
| c3069537 Andy McCall ↶ |
1599 |
|
| 75167672 Andy McCall |
1600 |
let repo2 = Repo::init(&root.join("oxy2")) |
| 1601 |
.unwrap() |
| 1602 |
.with_home(root.join("home")); |
| c3069537 Andy McCall ↶ |
1603 |
repo2.import_git(&g, "main").unwrap(); |
| 75167672 Andy McCall |
1604 |
let ids: BTreeSet<ChangeId> = repo |
| 1605 |
.log() |
| 1606 |
.unwrap() |
| 1607 |
.iter() |
| 1608 |
.filter(|e| !e.is_working_copy) |
| 1609 |
.map(|e| e.change.change_id) |
| 1610 |
.collect(); |
| 1611 |
let ids2: BTreeSet<ChangeId> = repo2 |
| 1612 |
.log() |
| 1613 |
.unwrap() |
| 1614 |
.iter() |
| 1615 |
.filter(|e| !e.is_working_copy) |
| 1616 |
.map(|e| e.change.change_id) |
| 1617 |
.collect(); |
| c3069537 Andy McCall ↶ |
1618 |
assert_eq!(ids, ids2); |
| 1619 |
|
| 1620 |
let mirror = root.join("mirror.git"); |
| 1621 |
repo.git_export(&mirror).unwrap(); |
| 75167672 Andy McCall |
1622 |
let out = std::process::Command::new("git") |
| 1623 |
.arg("-C") |
| 1624 |
.arg(&mirror) |
| 1625 |
.args(["show", "main:src/lib2.rs"]) |
| 1626 |
.output() |
| 1627 |
.unwrap(); |
| 1628 |
assert_eq!( |
| 1629 |
String::from_utf8_lossy(&out.stdout), |
| 1630 |
"pub fn a() {}\npub fn b() {}\npub fn c() {}\npub fn d() {}\n" |
| 1631 |
); |
| 1632 |
let out = std::process::Command::new("git") |
| 1633 |
.arg("-C") |
| 1634 |
.arg(&mirror) |
| 1635 |
.args(["show", "main:src/lib.rs"]) |
| 1636 |
.output() |
| 1637 |
.unwrap(); |
| 1638 |
assert!( |
| 1639 |
!out.status.success(), |
| 1640 |
"the old path is gone from the export" |
| 1641 |
); |
| c3069537 Andy McCall ↶ |
1642 |
} |
| 252e8cb2 Andy McCall ↶ |
1643 |
|
| 1644 |
#[test] |
| 1645 |
fn checkpoints_accelerate_loading_and_stay_correct() { |
| 1646 |
hermetic(); |
| 1647 |
let dir = tempfile::tempdir().unwrap(); |
| 1648 |
let root = dir.path(); |
| 1649 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 1650 |
repo.config.set("core.checkpoint-interval", "5").unwrap(); |
| 1651 |
repo.save_config().unwrap(); |
| 1652 |
let repo = Repo::open(root).unwrap().with_home(root.join("home")); |
| 1653 |
let mut hashes = Vec::new(); |
| 1654 |
for i in 0..12 { |
| 75167672 Andy McCall |
1655 |
write( |
| 1656 |
root, |
| 1657 |
"a.txt", |
| 1658 |
format!( |
| 1659 |
"{}\n", |
| 1660 |
(0..=i) |
| 1661 |
.map(|k| k.to_string()) |
| 1662 |
.collect::<Vec<_>>() |
| 1663 |
.join("\n") |
| 1664 |
) |
| 1665 |
.as_bytes(), |
| 1666 |
); |
| 252e8cb2 Andy McCall ↶ |
1667 |
write(root, &format!("f{i}.txt"), b"x\n"); |
| 1668 |
hashes.push(repo.commit(&format!("c{i}"), &[]).unwrap()); |
| 1669 |
} |
| 75167672 Andy McCall |
1670 |
let (_, cp) = repo |
| 1671 |
.latest_checkpoint("main") |
| 1672 |
.unwrap() |
| 1673 |
.expect("automatic checkpoint"); |
| 252e8cb2 Andy McCall ↶ |
1674 |
assert!(cp.count >= 5 && cp.count <= 12, "{}", cp.count); |
| 1675 |
|
| 1676 |
let heads = repo.bookmark_heads().unwrap(); |
| 1677 |
let fast = repo.load_state(&heads).unwrap(); |
| 1678 |
let full = repo.load_state_full(&heads).unwrap(); |
| 1679 |
assert_eq!(fast.len(), full.len()); |
| 1680 |
assert_eq!(fast.heads(), full.heads()); |
| 1681 |
let (a, b) = (fast.materialize(), full.materialize()); |
| 75167672 Andy McCall |
1682 |
assert_eq!( |
| 1683 |
a.files.keys().collect::<Vec<_>>(), |
| 1684 |
b.files.keys().collect::<Vec<_>>() |
| 1685 |
); |
| 252e8cb2 Andy McCall ↶ |
1686 |
for (p, f) in &a.files { |
| 1687 |
assert_eq!(f.content, b.files[p].content, "{p}"); |
| 1688 |
} |
| 1689 |
assert!(repo.load_state_checkpointed(&heads).unwrap().is_some()); |
| 1690 |
|
| 75167672 Andy McCall |
1691 |
assert!(repo |
| 1692 |
.load_state_checkpointed(&[hashes[0]]) |
| 1693 |
.unwrap() |
| 1694 |
.is_none()); |
| 252e8cb2 Andy McCall ↶ |
1695 |
assert_eq!(repo.load_state(&[hashes[0]]).unwrap().len(), 1); |
| 1696 |
|
| 1697 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 1698 |
let h = repo.describe(&hashes[3], "c3 described").unwrap(); |
| 1699 |
assert_eq!(repo.store().get_change(&h).unwrap().message, "c3 described"); |
| 1700 |
let (r, n, bytes) = repo.checkpoint().unwrap(); |
| 1701 |
assert!(n >= 12 && bytes > 0); |
| 1702 |
assert_eq!(repo.latest_checkpoint("main").unwrap().unwrap().0, r); |
| 1703 |
write(root, "a.txt", b"after\n"); |
| 1704 |
let h_after = repo.commit("after", &[]).unwrap(); |
| 1705 |
let st = repo.load_state(&repo.bookmark_heads().unwrap()).unwrap(); |
| 1706 |
assert!(st.contains(&h_after)); |
| 75167672 Andy McCall |
1707 |
assert!( |
| 1708 |
matches!(&st.materialize().files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"after\n") |
| 1709 |
); |
| 252e8cb2 Andy McCall ↶ |
1710 |
assert_eq!(repo.log().unwrap().len(), 14); |
| 1711 |
|
| 1712 |
repo.gc(true).unwrap(); |
| 1713 |
assert!(repo.latest_checkpoint("main").unwrap().is_some()); |
| 75167672 Andy McCall |
1714 |
assert!(repo |
| 1715 |
.load_state_checkpointed(&repo.bookmark_heads().unwrap()) |
| 1716 |
.unwrap() |
| 1717 |
.is_some()); |
| 252e8cb2 Andy McCall ↶ |
1718 |
} |
| 135212de Andy McCall ↶ |
1719 |
|
| 1720 |
#[test] |
| 1721 |
fn quick_status_for_prompts() { |
| 1722 |
hermetic(); |
| 1723 |
let dir = tempfile::tempdir().unwrap(); |
| 1724 |
let root = dir.path(); |
| 1725 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 1726 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
1727 |
let a = Repo::clone(&url, &root.join("a")) |
| 1728 |
.unwrap() |
| 1729 |
.with_home(root.join("home")); |
| 135212de Andy McCall ↶ |
1730 |
let ops_before = a.op_log().unwrap().len(); |
| 1731 |
let info = a.quick_status().unwrap(); |
| 75167672 Andy McCall |
1732 |
assert_eq!( |
| 1733 |
( |
| 1734 |
info.bookmark.as_str(), |
| 1735 |
info.heads, |
| 1736 |
info.dirty, |
| 1737 |
info.conflicted, |
| 1738 |
info.ahead, |
| 1739 |
info.behind |
| 1740 |
), |
| 1741 |
("main", 0, false, false, Some(0), Some(0)) |
| 1742 |
); |
| 135212de Andy McCall ↶ |
1743 |
write(&root.join("a"), "f.txt", b"f\n"); |
| 1744 |
assert!(a.quick_status().unwrap().dirty, "a new file is dirty"); |
| 75167672 Andy McCall |
1745 |
assert_eq!( |
| 1746 |
a.op_log().unwrap().len(), |
| 1747 |
ops_before, |
| 1748 |
"quick_status records nothing" |
| 1749 |
); |
| 135212de Andy McCall ↶ |
1750 |
|
| 1751 |
a.snapshot("status").unwrap(); |
| 1752 |
assert!(a.quick_status().unwrap().dirty); |
| 1753 |
a.commit("f", &[]).unwrap(); |
| 1754 |
let info = a.quick_status().unwrap(); |
| 1755 |
assert!(!info.dirty, "clean right after a commit"); |
| 1756 |
assert_eq!((info.ahead, info.behind), (Some(1), Some(0))); |
| 75167672 Andy McCall |
1757 |
assert_eq!( |
| 1758 |
info.format("%b %c%d%x%A%D"), |
| 1759 |
format!("main {}+1", info.change_id) |
| 1760 |
); |
| 135212de Andy McCall ↶ |
1761 |
assert_eq!(info.format("[%h heads] %a/%B %%"), "[1 heads] 1/0 %"); |
| 1762 |
|
| 1763 |
|
| 1764 |
let p = root.join("a/f.txt"); |
| 1765 |
let content = fs::read(&p).unwrap(); |
| 1766 |
fs::write(&p, &content).unwrap(); |
| 1767 |
assert!(!a.quick_status().unwrap().dirty); |
| 1768 |
fs::write(&p, b"g\n").unwrap(); |
| 1769 |
assert!(a.quick_status().unwrap().dirty); |
| 1770 |
fs::write(&p, b"f\n").unwrap(); |
| 1771 |
|
| 1772 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 1773 |
assert_eq!(a.quick_status().unwrap().ahead, Some(0)); |
| 75167672 Andy McCall |
1774 |
let b = Repo::clone(&url, &root.join("b")) |
| 1775 |
.unwrap() |
| 1776 |
.with_home(root.join("home-b")); |
| 135212de Andy McCall ↶ |
1777 |
write(&root.join("b"), "g.txt", b"g\n"); |
| 1778 |
b.commit("g", &[]).unwrap(); |
| 1779 |
b.push("origin", false).unwrap(); |
| 1780 |
a.fetch("origin").unwrap(); |
| 1781 |
let info = a.quick_status().unwrap(); |
| 1782 |
assert_eq!((info.ahead, info.behind), (Some(0), Some(1))); |
| 1783 |
assert_eq!(info.format("%A%D"), "-1"); |
| 1784 |
let _ = origin; |
| 1785 |
} |
| b3c5a59a Andy McCall ↶ |
1786 |
|
| 1787 |
#[test] |
| 1788 |
fn lazy_blobs_are_fetched_on_first_read() { |
| 1789 |
hermetic(); |
| 1790 |
let dir = tempfile::tempdir().unwrap(); |
| 1791 |
let root = dir.path(); |
| 1792 |
let origin_dir = root.join("origin"); |
| 75167672 Andy McCall |
1793 |
let origin = Repo::init(&origin_dir) |
| 1794 |
.unwrap() |
| 1795 |
.with_home(root.join("home")); |
| b3c5a59a Andy McCall ↶ |
1796 |
let url = origin_dir.to_string_lossy().to_string(); |
| 1797 |
let big: Vec<u8> = (0..200_000u32).map(|i| (i % 251) as u8).collect(); |
| 1798 |
write(&origin_dir, "big.bin", &big); |
| 1799 |
write(&origin_dir, "small.bin", &[0u8, 1, 2]); |
| 1800 |
write(&origin_dir, "t.txt", b"t\n"); |
| 1801 |
let h = origin.commit("blobs", &[]).unwrap(); |
| 1802 |
let big_ref = Repo::blob_ref(&big); |
| 1803 |
|
| 1804 |
|
| 75167672 Andy McCall |
1805 |
let a = Repo::init_with_id(&root.join("a"), origin.id().unwrap()) |
| 1806 |
.unwrap() |
| 1807 |
.with_home(root.join("home")); |
| b3c5a59a Andy McCall ↶ |
1808 |
let a = { |
| 1809 |
let mut a = a; |
| 1810 |
a.config.set("remote.origin.url", &url).unwrap(); |
| 1811 |
a.config.set("filter.max-blob", "100K").unwrap(); |
| 1812 |
a.save_config().unwrap(); |
| 1813 |
a |
| 1814 |
}; |
| 1815 |
assert_eq!(a.config.max_blob, Some(100 * 1024)); |
| 1816 |
a.fetch("origin").unwrap(); |
| 1817 |
assert!(a.store().has(&h)); |
| 1818 |
assert!(!a.store().has(&big_ref), "withheld"); |
| 75167672 Andy McCall |
1819 |
assert!( |
| 1820 |
a.store().has(&Repo::blob_ref(&[0u8, 1, 2])), |
| 1821 |
"small blob came along" |
| 1822 |
); |
| b3c5a59a Andy McCall ↶ |
1823 |
assert_eq!(a.lazy_remote(&big_ref).as_deref(), Some("origin")); |
| 1824 |
|
| 1825 |
|
| 1826 |
let (op_ref, op) = a.head_op().unwrap(); |
| 1827 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
1828 |
view.bookmarks.insert( |
| 1829 |
"main".into(), |
| 1830 |
a.store().put_state(&StateObject::new(vec![h])).unwrap(), |
| 1831 |
); |
| b3c5a59a Andy McCall ↶ |
1832 |
a.record_op(&[op_ref], "adopt", view).unwrap(); |
| 1833 |
|
| 1834 |
a.checkout(&[h], true, "checkout").unwrap(); |
| 1835 |
assert!(a.store().has(&big_ref), "fetched on first read"); |
| 1836 |
assert_eq!(fs::read(root.join("a/big.bin")).unwrap(), big); |
| 1837 |
assert!(a.lazy_remote(&big_ref).is_none(), "no longer registered"); |
| 1838 |
write(&root.join("a"), "t.txt", b"t2\n"); |
| 1839 |
a.commit("text edit", &[]).unwrap(); |
| 1840 |
|
| 1841 |
|
| 7421309f Andy McCall ↶ |
1842 |
let b = Repo::clone_with(&url, &root.join("b"), Some(1024), &[]).unwrap(); |
| b3c5a59a Andy McCall ↶ |
1843 |
assert_eq!(b.config.max_blob, Some(1024)); |
| 1844 |
assert_eq!(fs::read(root.join("b/big.bin")).unwrap(), big); |
| 1845 |
assert_eq!(parse_size("2M").unwrap(), 2 << 20); |
| 1846 |
assert!(parse_size("lots").is_err()); |
| 1847 |
} |
| 7421309f Andy McCall ↶ |
1848 |
|
| 1849 |
#[test] |
| 1850 |
fn sparse_checkout_materializes_only_chosen_paths() { |
| 1851 |
hermetic(); |
| 1852 |
let dir = tempfile::tempdir().unwrap(); |
| 1853 |
let root = dir.path(); |
| 1854 |
let origin_dir = root.join("origin"); |
| 75167672 Andy McCall |
1855 |
let origin = Repo::init(&origin_dir) |
| 1856 |
.unwrap() |
| 1857 |
.with_home(root.join("home")); |
| 7421309f Andy McCall ↶ |
1858 |
write(&origin_dir, "src/main.rs", b"fn main() {}\n"); |
| 1859 |
write(&origin_dir, "src/lib.rs", b"pub fn a() {}\n"); |
| 1860 |
write(&origin_dir, "docs/guide.md", b"# guide\n"); |
| 1861 |
let logo: Vec<u8> = (0..50_000u32).map(|i| (i % 7) as u8).collect(); |
| 1862 |
write(&origin_dir, "docs/logo.png", &logo); |
| 1863 |
write(&origin_dir, "README.md", b"# top\n"); |
| 1864 |
let h1 = origin.commit("tree", &[]).unwrap(); |
| 1865 |
let url = origin_dir.to_string_lossy().to_string(); |
| 1866 |
|
| 75167672 Andy McCall |
1867 |
let a = Repo::clone_with(&url, &root.join("a"), None, &["src".into()]) |
| 1868 |
.unwrap() |
| 1869 |
.with_home(root.join("home")); |
| 7421309f Andy McCall ↶ |
1870 |
assert_eq!(a.config.sparse_paths, vec!["src"]); |
| 1871 |
assert_eq!(a.config.max_blob, Some(0), "sparse implies lazy blobs"); |
| 1872 |
assert!(root.join("a/src/main.rs").exists()); |
| 1873 |
assert!(!root.join("a/docs").exists() && !root.join("a/README.md").exists()); |
| 75167672 Andy McCall |
1874 |
assert!( |
| 1875 |
!a.store().has(&Repo::blob_ref(&logo)), |
| 1876 |
"out-of-scope blob never fetched" |
| 1877 |
); |
| 1878 |
assert!( |
| 1879 |
!a.snapshot("status").unwrap().changed, |
| 1880 |
"out-of-scope files are not removals" |
| 1881 |
); |
| 05e71ed6 Andy McCall ↶ |
1882 |
assert!(!a.quick_status().unwrap().dirty); |
| 7421309f Andy McCall ↶ |
1883 |
|
| 1884 |
|
| 75167672 Andy McCall |
1885 |
write( |
| 1886 |
&root.join("a"), |
| 1887 |
"src/lib.rs", |
| 1888 |
b"pub fn a() {}\npub fn b() {}\n", |
| 1889 |
); |
| 7421309f Andy McCall ↶ |
1890 |
let h2 = a.commit("edit lib", &[]).unwrap(); |
| 1891 |
let st = a.load_state(&[h2]).unwrap().materialize(); |
| 75167672 Andy McCall |
1892 |
assert!( |
| 1893 |
st.files.contains_key("docs/guide.md"), |
| 1894 |
"the state still has everything" |
| 1895 |
); |
| 7421309f Andy McCall ↶ |
1896 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 1897 |
|
| 1898 |
write(&root.join("a"), "docs/new.md", b"ignored\n"); |
| 1899 |
assert!(a.snapshot("status").unwrap().statuses.is_empty()); |
| 1900 |
|
| 1901 |
|
| 1902 |
let mut a = a; |
| 1903 |
a.config.set("filter.paths", "src,docs").unwrap(); |
| 1904 |
a.save_config().unwrap(); |
| 75167672 Andy McCall |
1905 |
let r = a |
| 1906 |
.checkout(&a.bookmark_heads().unwrap(), true, "widen") |
| 1907 |
.unwrap(); |
| 7421309f Andy McCall ↶ |
1908 |
assert!(r.written >= 2); |
| 75167672 Andy McCall |
1909 |
assert_eq!( |
| 1910 |
fs::read(root.join("a/docs/logo.png")).unwrap(), |
| 1911 |
logo, |
| 1912 |
"now fetched lazily" |
| 1913 |
); |
| 7421309f Andy McCall ↶ |
1914 |
assert!(!root.join("a/README.md").exists()); |
| 1915 |
|
| 1916 |
origin.pull("origin").err(); |
| 75167672 Andy McCall |
1917 |
let full = origin |
| 1918 |
.load_state(&origin.bookmark_heads().unwrap()) |
| 1919 |
.unwrap() |
| 1920 |
.materialize(); |
| 1921 |
assert!( |
| 1922 |
matches!(&full.files["src/lib.rs"].content, FileContent::Text { bytes, .. } if bytes == b"pub fn a() {}\npub fn b() {}\n") |
| 1923 |
); |
| 7421309f Andy McCall ↶ |
1924 |
assert!(full.files.contains_key("README.md")); |
| 1925 |
let _ = h1; |
| 1926 |
} |
| 2863456a Andy McCall ↶ |
1927 |
|
| 1928 |
#[test] |
| 1929 |
fn shallow_clone_and_deepen() { |
| 1930 |
hermetic(); |
| 1931 |
let dir = tempfile::tempdir().unwrap(); |
| 1932 |
let root = dir.path(); |
| 1933 |
let origin_dir = root.join("origin"); |
| 75167672 Andy McCall |
1934 |
let origin = Repo::init(&origin_dir) |
| 1935 |
.unwrap() |
| 1936 |
.with_home(root.join("home")); |
| 2863456a Andy McCall ↶ |
1937 |
let mut hashes = Vec::new(); |
| 1938 |
for i in 0..8 { |
| 75167672 Andy McCall |
1939 |
write( |
| 1940 |
&origin_dir, |
| 1941 |
"a.txt", |
| 1942 |
format!( |
| 1943 |
"{}\n", |
| 1944 |
(0..=i) |
| 1945 |
.map(|k| k.to_string()) |
| 1946 |
.collect::<Vec<_>>() |
| 1947 |
.join("\n") |
| 1948 |
) |
| 1949 |
.as_bytes(), |
| 1950 |
); |
| 2863456a Andy McCall ↶ |
1951 |
hashes.push(origin.commit(&format!("c{i}"), &[]).unwrap()); |
| 1952 |
} |
| 1953 |
let url = origin_dir.to_string_lossy().to_string(); |
| 75167672 Andy McCall |
1954 |
let a = Repo::clone_shallow(&url, &root.join("a"), 3, None, &[]) |
| 1955 |
.unwrap() |
| 1956 |
.with_home(root.join("home")); |
| 2863456a Andy McCall ↶ |
1957 |
assert!(a.is_shallow()); |
| 75167672 Andy McCall |
1958 |
assert_eq!( |
| 1959 |
fs::read_to_string(root.join("a/a.txt")).unwrap(), |
| 1960 |
"0\n1\n2\n3\n4\n5\n6\n7\n" |
| 1961 |
); |
| 1962 |
assert!( |
| 1963 |
a.store().has(&hashes[7]) && a.store().has(&hashes[5]), |
| 1964 |
"three levels present" |
| 1965 |
); |
| 2863456a Andy McCall ↶ |
1966 |
assert!(!a.store().has(&hashes[4]), "beyond the depth is absent"); |
| 1967 |
assert_eq!(a.boundary(), [hashes[4]].into_iter().collect()); |
| 75167672 Andy McCall |
1968 |
assert_eq!( |
| 1969 |
a.log().unwrap().len(), |
| 1970 |
9, |
| 1971 |
"the log shows every change from the checkpoint's metadata" |
| 1972 |
); |
| 2863456a Andy McCall ↶ |
1973 |
assert!(!a.snapshot("status").unwrap().changed); |
| 1974 |
assert_eq!(a.quick_status().unwrap().heads, 1); |
| 1975 |
|
| 1976 |
|
| 1977 |
write(&root.join("a"), "a.txt", b"0\n1\n2\n3\n4\n5\n6\n7\n8\n"); |
| 1978 |
let h_new = a.commit("c8", &[]).unwrap(); |
| 1979 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 1980 |
assert_eq!(origin.bookmark_heads().unwrap(), vec![h_new]); |
| 1981 |
|
| 1982 |
write(&origin_dir, "b.txt", b"b\n"); |
| 1983 |
origin.pull("origin").err(); |
| 1984 |
let (_, wc) = origin.working_copy().unwrap(); |
| 1985 |
let _ = wc; |
| 75167672 Andy McCall |
1986 |
origin |
| 1987 |
.checkout(&origin.bookmark_heads().unwrap(), true, "co") |
| 1988 |
.unwrap(); |
| 2863456a Andy McCall ↶ |
1989 |
write(&origin_dir, "b.txt", b"b\n"); |
| 1990 |
let h_b = origin.commit("b", &[]).unwrap(); |
| 1991 |
let pr = a.pull("origin").unwrap(); |
| 1992 |
assert!(pr.changed); |
| 1993 |
assert!(a.store().has(&h_b)); |
| 1994 |
assert!(root.join("a/b.txt").exists()); |
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
assert!(a.deepen("origin", 2).unwrap() >= 2); |
| 2000 |
assert!(a.store().has(&hashes[3])); |
| 2001 |
assert!(a.is_shallow()); |
| 2002 |
a.deepen("origin", 100).unwrap(); |
| 2003 |
assert!(!a.is_shallow(), "boundary reached the root"); |
| 2004 |
assert!(a.store().has(&hashes[0])); |
| 75167672 Andy McCall |
2005 |
assert_eq!( |
| 2006 |
a.load_state_full(&a.bookmark_heads().unwrap()) |
| 2007 |
.unwrap() |
| 2008 |
.len(), |
| 2009 |
10 |
| 2010 |
); |
| 2863456a Andy McCall ↶ |
2011 |
} |
| 132b884b Andy McCall ↶ |
2012 |
|
| 2013 |
#[test] |
| 2014 |
fn tags_are_immutable_named_states_and_travel() { |
| 2015 |
hermetic(); |
| 2016 |
let dir = tempfile::tempdir().unwrap(); |
| 2017 |
let root = dir.path(); |
| 2018 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 2019 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
2020 |
let mut a = Repo::clone(&url, &root.join("a")) |
| 2021 |
.unwrap() |
| 2022 |
.with_home(root.join("home-a")); |
| 2023 |
let (key_a, _) = a |
| 2024 |
.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 2025 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2026 |
write(&root.join("a"), "f.txt", b"1\n"); |
| 2027 |
let h1 = a.commit("one", &[]).unwrap(); |
| 2028 |
write(&root.join("a"), "f.txt", b"1\n2\n"); |
| 2029 |
let h2 = a.commit("two", &[]).unwrap(); |
| 2030 |
|
| 2031 |
let t = a.tag_create("v1", &[h1], "first release").unwrap(); |
| 75167672 Andy McCall |
2032 |
assert_eq!( |
| 2033 |
(t.heads.clone(), t.signer.as_deref()), |
| 2034 |
(vec![h1], Some("Ada")) |
| 2035 |
); |
| 132b884b Andy McCall ↶ |
2036 |
assert!(a.tag_create("v1", &[h2], "").is_err(), "immutable"); |
| 2037 |
assert!(a.tag_create("bad/name", &[h2], "").is_err()); |
| 2038 |
assert_eq!(a.tag_heads("v1").unwrap(), Some(vec![h1])); |
| 2039 |
assert_eq!(a.resolve_heads("v1").unwrap(), vec![h1]); |
| 2040 |
assert_eq!(a.resolve_heads("main").unwrap(), vec![h2]); |
| 2041 |
let (_, sa) = a.tag_attest("v1").unwrap().unwrap(); |
| 2042 |
assert_eq!(sa.sigs[0].key, key_a); |
| 2043 |
|
| 75167672 Andy McCall |
2044 |
a.checkout(&a.resolve_heads("v1").unwrap(), false, "co") |
| 2045 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2046 |
assert_eq!(fs::read(root.join("a/f.txt")).unwrap(), b"1\n"); |
| 75167672 Andy McCall |
2047 |
a.checkout(&a.bookmark_heads().unwrap(), false, "co") |
| 2048 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2049 |
|
| 2050 |
|
| 2051 |
let r = a.push("origin", false).unwrap(); |
| 2052 |
assert!(r.iter().any(|x| x.name == "tags/v1" && x.ok), "{r:?}"); |
| 2053 |
assert_eq!(origin.tag_heads("v1").unwrap(), Some(vec![h1])); |
| 75167672 Andy McCall |
2054 |
let mut forged = a |
| 2055 |
.tag_refs() |
| 2056 |
.unwrap() |
| 2057 |
.into_iter() |
| 2058 |
.find(|t| t.name == "v1") |
| 2059 |
.unwrap(); |
| 2060 |
forged.state = origin |
| 2061 |
.store() |
| 2062 |
.put_state(&StateObject::new(vec![h2])) |
| 2063 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2064 |
forged.heads = vec![h2]; |
| 2065 |
forged.attestation = None; |
| 75167672 Andy McCall |
2066 |
let r = origin |
| 2067 |
.receive_push(Bundle::default(), vec![], None, vec![forged]) |
| 2068 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2069 |
assert!(!r[0].ok && r[0].reason.contains("immutable"), "{r:?}"); |
| 2070 |
|
| 2071 |
|
| 75167672 Andy McCall |
2072 |
let b = Repo::clone(&url, &root.join("b")) |
| 2073 |
.unwrap() |
| 2074 |
.with_home(root.join("home-b")); |
| 132b884b Andy McCall ↶ |
2075 |
assert_eq!(b.tag_heads("v1").unwrap(), Some(vec![h1])); |
| 2076 |
assert!(b.tags().unwrap()[0].signer.as_deref() == Some("Ada")); |
| 2077 |
a.tag_create("v2", &[h2], "second").unwrap(); |
| 2078 |
a.push("origin", false).unwrap(); |
| 2079 |
let rep = b.fetch("origin").unwrap(); |
| 2080 |
assert_eq!(rep.new_tags, 1); |
| 2081 |
assert_eq!(b.tag_heads("v2").unwrap(), Some(vec![h2])); |
| 2082 |
|
| 2083 |
|
| 2084 |
a.policy_init().unwrap(); |
| 2085 |
a.push("origin", false).unwrap(); |
| 75167672 Andy McCall |
2086 |
let unattested = crate::protocol::TagRef { |
| 2087 |
name: "v3".into(), |
| 2088 |
state: origin |
| 2089 |
.store() |
| 2090 |
.put_state(&StateObject::new(vec![h2])) |
| 2091 |
.unwrap(), |
| 2092 |
heads: vec![h2], |
| 2093 |
attestation: None, |
| 2094 |
}; |
| 2095 |
let r = origin |
| 2096 |
.receive_push(Bundle::default(), vec![], None, vec![unattested]) |
| 2097 |
.unwrap(); |
| 132b884b Andy McCall ↶ |
2098 |
assert!(!r[0].ok && r[0].reason.contains("attestation"), "{r:?}"); |
| 2099 |
a.tag_create("v3", &[h2], "").unwrap(); |
| 2100 |
let r = a.push("origin", false).unwrap(); |
| 2101 |
assert!(r.iter().any(|x| x.name == "tags/v3" && x.ok), "{r:?}"); |
| 2102 |
assert!(a.gc(true).is_ok()); |
| 2103 |
assert_eq!(a.tags().unwrap().len(), 3, "tags survive gc"); |
| 2104 |
} |
| 4e88e774 Andy McCall ↶ |
2105 |
|
| 2106 |
#[test] |
| 2107 |
fn multiple_workspaces_share_a_repository() { |
| 2108 |
hermetic(); |
| 2109 |
let dir = tempfile::tempdir().unwrap(); |
| 2110 |
let root = dir.path(); |
| 2111 |
let main_dir = root.join("main"); |
| 2112 |
let repo = Repo::init(&main_dir).unwrap().with_home(root.join("home")); |
| 2113 |
write(&main_dir, "a.txt", b"a\n"); |
| 2114 |
let h1 = repo.commit("a", &[]).unwrap(); |
| 2115 |
assert_eq!(repo.workspaces().unwrap().len(), 1); |
| 2116 |
|
| 2117 |
|
| 2118 |
let ws_dir = root.join("ws2"); |
| 2119 |
let ws = repo.workspace_add(&ws_dir, None).unwrap(); |
| 2120 |
assert!(ws_dir.join(".oxy").is_file()); |
| 2121 |
assert_eq!(fs::read(ws_dir.join("a.txt")).unwrap(), b"a\n"); |
| 2122 |
assert_ne!(ws.workspace(), repo.workspace()); |
| 2123 |
let list = repo.workspaces().unwrap(); |
| 2124 |
assert_eq!(list.len(), 2); |
| 75167672 Andy McCall |
2125 |
assert!(list |
| 2126 |
.iter() |
| 2127 |
.any(|w| w.id == ws.workspace() && w.path == ws_dir.canonicalize().unwrap() && !w.current)); |
| 4e88e774 Andy McCall ↶ |
2128 |
assert!(!ws.snapshot("status").unwrap().changed); |
| 2129 |
|
| 2130 |
|
| 2131 |
write(&ws_dir, "b.txt", b"b\n"); |
| 2132 |
let s = ws.snapshot("status").unwrap(); |
| 2133 |
assert_eq!(s.statuses.len(), 1); |
| 75167672 Andy McCall |
2134 |
assert!( |
| 2135 |
!repo.snapshot("status").unwrap().changed, |
| 2136 |
"the first workspace is untouched" |
| 2137 |
); |
| 4e88e774 Andy McCall ↶ |
2138 |
let h2 = ws.commit("b from ws2", &[]).unwrap(); |
| 2139 |
let mut heads = repo.bookmark_heads().unwrap(); |
| 2140 |
heads.sort(); |
| 2141 |
let mut want = vec![h1, h2]; |
| 2142 |
want.sort(); |
| 2143 |
assert_eq!(heads, want, "both workspaces see the bookmark move"); |
| 2144 |
assert!(!main_dir.join("b.txt").exists()); |
| 75167672 Andy McCall |
2145 |
repo.checkout(&repo.bookmark_heads().unwrap(), false, "co") |
| 2146 |
.unwrap(); |
| 4e88e774 Andy McCall ↶ |
2147 |
assert!(main_dir.join("b.txt").exists()); |
| 2148 |
|
| 2149 |
|
| 2150 |
let reopened = Repo::open(&ws_dir).unwrap(); |
| 2151 |
assert_eq!(reopened.workspace(), ws.workspace()); |
| 2152 |
assert_eq!(reopened.id().unwrap(), repo.id().unwrap()); |
| 2153 |
assert_eq!(reopened.quick_status().unwrap().heads, 2); |
| 2154 |
|
| 2155 |
|
| 2156 |
assert!(repo.workspace_add(&main_dir.join("inner"), None).is_err()); |
| 2157 |
assert!(repo.workspace_forget(&repo.workspace()).is_err()); |
| 2158 |
repo.workspace_forget(&ws.workspace()).unwrap(); |
| 2159 |
assert_eq!(repo.workspaces().unwrap().len(), 1); |
| 2160 |
assert!(ws_dir.join("b.txt").exists()); |
| 75167672 Andy McCall |
2161 |
assert!( |
| 2162 |
ws.snapshot("status").is_err(), |
| 2163 |
"a forgotten workspace is no longer in the view" |
| 2164 |
); |
| 4e88e774 Andy McCall ↶ |
2165 |
|
| 2166 |
|
| 2167 |
let lock = repo.lock_snapshot().unwrap(); |
| 2168 |
drop(lock); |
| 75167672 Andy McCall |
2169 |
let lock_path = main_dir |
| 2170 |
.join(".oxy/workspaces") |
| 2171 |
.join(repo.workspace().to_hex()) |
| 2172 |
.join("lock"); |
| 4e88e774 Andy McCall ↶ |
2173 |
fs::write(&lock_path, "1").unwrap(); |
| 2174 |
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(120); |
| 75167672 Andy McCall |
2175 |
fs::File::open(&lock_path) |
| 2176 |
.unwrap() |
| 2177 |
.set_modified(old) |
| 2178 |
.unwrap(); |
| 4e88e774 Andy McCall ↶ |
2179 |
assert!(repo.lock_snapshot().is_ok(), "stale lock taken over"); |
| 2180 |
} |
| cdb5beba Andy McCall ↶ |
2181 |
|
| 2182 |
#[test] |
| 2183 |
fn attributes_binary_eol_nocompress_lockable() { |
| 2184 |
hermetic(); |
| 2185 |
let dir = tempfile::tempdir().unwrap(); |
| 2186 |
let root = dir.path(); |
| 2187 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
2188 |
write( |
| 2189 |
root, |
| 2190 |
".oxyattributes", |
| 2191 |
b"*.dat binary nocompress\n*.txt eol=crlf\nassets/** lockable\n", |
| 2192 |
); |
| cdb5beba Andy McCall ↶ |
2193 |
write(root, "plain.dat", b"no nul bytes here\n"); |
| 2194 |
write(root, "notes.txt", b"one\r\ntwo\r\n"); |
| 2195 |
write(root, "assets/model.bin", b"\0model"); |
| 2196 |
let h = repo.commit("attrs", &[]).unwrap(); |
| 2197 |
let snap = repo.load_state(&[h]).unwrap().materialize(); |
| 75167672 Andy McCall |
2198 |
assert!( |
| 2199 |
snap.files["plain.dat"].mode.opaque, |
| 2200 |
"binary attribute forces opaque without NUL bytes" |
| 2201 |
); |
| 2202 |
assert!( |
| 2203 |
matches!(&snap.files["notes.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\ntwo\n"), |
| 2204 |
"stored with LF" |
| 2205 |
); |
| 2206 |
assert!( |
| 2207 |
!repo.snapshot("status").unwrap().changed, |
| 2208 |
"CRLF on disk equals LF in the state" |
| 2209 |
); |
| cdb5beba Andy McCall ↶ |
2210 |
|
| 2211 |
let r = repo.gc(true).unwrap(); |
| 2212 |
assert!(r.packed.objects > 0); |
| 2213 |
|
| 2214 |
fs::remove_file(root.join("notes.txt")).unwrap(); |
| 2215 |
repo.checkout(&[h], true, "co").unwrap(); |
| 2216 |
assert_eq!(fs::read(root.join("notes.txt")).unwrap(), b"one\r\ntwo\r\n"); |
| 75167672 Andy McCall |
2217 |
assert!(fs::metadata(root.join("assets/model.bin")) |
| 2218 |
.unwrap() |
| 2219 |
.permissions() |
| 2220 |
.readonly()); |
| cdb5beba Andy McCall ↶ |
2221 |
assert!(repo.lock_path("plain.dat").is_err(), "not lockable"); |
| 2222 |
repo.lock_path("assets/model.bin").unwrap(); |
| 75167672 Andy McCall |
2223 |
assert!(!fs::metadata(root.join("assets/model.bin")) |
| 2224 |
.unwrap() |
| 2225 |
.permissions() |
| 2226 |
.readonly()); |
| cdb5beba Andy McCall ↶ |
2227 |
assert!(repo.is_locked("assets/model.bin")); |
| 2228 |
write(root, "assets/model.bin", b"\0model2"); |
| 2229 |
repo.commit("edit model", &[]).unwrap(); |
| 2230 |
repo.unlock_path("assets/model.bin").unwrap(); |
| 75167672 Andy McCall |
2231 |
assert!(fs::metadata(root.join("assets/model.bin")) |
| 2232 |
.unwrap() |
| 2233 |
.permissions() |
| 2234 |
.readonly()); |
| cdb5beba Andy McCall ↶ |
2235 |
|
| 2236 |
repo.checkout(&[h], true, "co").unwrap(); |
| 2237 |
assert_eq!(fs::read(root.join("assets/model.bin")).unwrap(), b"\0model"); |
| 2238 |
} |
| 6d3eefbc Andy McCall ↶ |
2239 |
|
| 2240 |
#[cfg(unix)] |
| 2241 |
#[test] |
| 2242 |
fn hooks_can_veto_observe_and_add_dependencies() { |
| 2243 |
use std::os::unix::fs::PermissionsExt; |
| 2244 |
hermetic(); |
| 2245 |
let dir = tempfile::tempdir().unwrap(); |
| 2246 |
let root = dir.path(); |
| 2247 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 2248 |
let hooks = root.join(".oxy/hooks"); |
| 2249 |
fs::create_dir_all(&hooks).unwrap(); |
| 2250 |
let hook = |name: &str, body: &str| { |
| 2251 |
let p = hooks.join(name); |
| 2252 |
fs::write(&p, format!("#!/bin/sh\n{body}\n")).unwrap(); |
| 2253 |
fs::set_permissions(&p, fs::Permissions::from_mode(0o755)).unwrap(); |
| 2254 |
}; |
| 2255 |
hook("pre-commit", "cat > \"$OXY_ROOT/.last-pre-commit\"; grep -q WIP \"$OXY_ROOT/.last-pre-commit\" && { echo 'no WIP commits' >&2; exit 1; }; exit 0"); |
| 2256 |
hook("post-commit", "cat > \"$OXY_ROOT/.last-post-commit\""); |
| 2257 |
hook("post-checkout", "cat > \"$OXY_ROOT/.last-post-checkout\""); |
| 2258 |
hook("pre-push", "echo pushing blocked >&2; exit 3"); |
| 2259 |
write(root, ".oxyignore", b".last-*\n"); |
| 2260 |
write(root, "a.txt", b"a\n"); |
| 2261 |
let err = repo.commit("WIP: nope", &[]).unwrap_err().to_string(); |
| 2262 |
assert!(err.contains("pre-commit refused: no WIP commits"), "{err}"); |
| 2263 |
let h1 = repo.commit("first", &[]).unwrap(); |
| 2264 |
let pre = fs::read_to_string(root.join(".last-pre-commit")).unwrap(); |
| 75167672 Andy McCall |
2265 |
assert!( |
| 2266 |
pre.contains("\"event\":\"pre-commit\"") |
| 2267 |
&& pre.contains("\"path\":\"a.txt\"") |
| 2268 |
&& pre.contains("\"message\":\"first\""), |
| 2269 |
"{pre}" |
| 2270 |
); |
| 6d3eefbc Andy McCall ↶ |
2271 |
let post = fs::read_to_string(root.join(".last-post-commit")).unwrap(); |
| 2272 |
assert!(post.contains(&h1.to_hex()), "{post}"); |
| 2273 |
repo.checkout(&[h1], false, "co").unwrap(); |
| 75167672 Andy McCall |
2274 |
assert!(fs::read_to_string(root.join(".last-post-checkout")) |
| 2275 |
.unwrap() |
| 2276 |
.contains("post-checkout")); |
| 6d3eefbc Andy McCall ↶ |
2277 |
repo.config.set("remote.origin.url", "/nowhere").unwrap(); |
| 2278 |
repo.save_config().unwrap(); |
| 2279 |
let err = repo.push("origin", false).unwrap_err().to_string(); |
| 2280 |
assert!(err.contains("pre-push refused: pushing blocked"), "{err}"); |
| 2281 |
|
| 2282 |
|
| 2283 |
write(root, "b.txt", b"b\n"); |
| 75167672 Andy McCall |
2284 |
hook( |
| 2285 |
"semantic-deps", |
| 2286 |
&format!( |
| 2287 |
"cat > \"$OXY_ROOT/.last-semantic\"; echo '{{\"deps\": [\"{}\"]}}'", |
| 2288 |
h1.to_hex() |
| 2289 |
), |
| 2290 |
); |
| 6d3eefbc Andy McCall ↶ |
2291 |
let h2 = repo.commit("b without hook", &[]).unwrap(); |
| 75167672 Andy McCall |
2292 |
assert!( |
| 2293 |
repo.store().get_change(&h2).unwrap().deps.is_empty(), |
| 2294 |
"hook disabled by default" |
| 2295 |
); |
| 6d3eefbc Andy McCall ↶ |
2296 |
repo.config.set("hooks.semantic-deps", "true").unwrap(); |
| 2297 |
repo.save_config().unwrap(); |
| 2298 |
write(root, "c.txt", b"c\n"); |
| 2299 |
let h3 = repo.commit("c with hook", &[]).unwrap(); |
| 2300 |
assert_eq!(repo.store().get_change(&h3).unwrap().deps, vec![h1]); |
| 2301 |
let sem = fs::read_to_string(root.join(".last-semantic")).unwrap(); |
| 75167672 Andy McCall |
2302 |
assert!( |
| 2303 |
sem.contains("\"touched_paths\":[\"c.txt\"]") && sem.contains("candidate_deps"), |
| 2304 |
"{sem}" |
| 2305 |
); |
| 6d3eefbc Andy McCall ↶ |
2306 |
assert_eq!(json_str("a\"b\\c\n"), "\"a\\\"b\\\\c\\n\""); |
| 2307 |
} |
| 23b217d7 Andy McCall ↶ |
2308 |
|
| 2309 |
#[test] |
| 2310 |
fn amend_into_older_change_and_split() { |
| 2311 |
hermetic(); |
| 2312 |
let dir = tempfile::tempdir().unwrap(); |
| 2313 |
let root = dir.path(); |
| 2314 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
2315 |
repo.create_identity("Ada", "a@b", IdentityScope::User) |
| 2316 |
.unwrap(); |
| 23b217d7 Andy McCall ↶ |
2317 |
repo.set_user_config("core.sign", "change").unwrap(); |
| 2318 |
write(root, "a.txt", b"one\ntwo\n"); |
| 2319 |
let h0 = repo.commit("base", &[]).unwrap(); |
| 2320 |
write(root, "a.txt", b"one\nTWO\n"); |
| 2321 |
let h1 = repo.commit("edit two", &[]).unwrap(); |
| 2322 |
write(root, "a.txt", b"one\nTWO\nthree\n"); |
| 2323 |
let h2 = repo.commit("add three", &[]).unwrap(); |
| 2324 |
let id1 = repo.store().get_change(&h1).unwrap().change_id; |
| 2325 |
|
| 2326 |
|
| 2327 |
write(root, "a.txt", b"one\nTWO!\nthree\n"); |
| 2328 |
let h1b = repo.amend_into(&h1, None).unwrap(); |
| 2329 |
assert_ne!(h1b, h1); |
| 2330 |
let c1b = repo.store().get_change(&h1b).unwrap(); |
| 2331 |
assert_eq!(c1b.change_id, id1); |
| 2332 |
assert_eq!(c1b.deps, vec![h0]); |
| 75167672 Andy McCall |
2333 |
assert!(matches!( |
| 2334 |
repo.verify_change(&h1b).unwrap(), |
| 2335 |
SignatureStatus::Valid { .. } |
| 2336 |
)); |
| 23b217d7 Andy McCall ↶ |
2337 |
let heads = repo.bookmark_heads().unwrap(); |
| 2338 |
assert_eq!(heads.len(), 1); |
| 2339 |
let top = repo.store().get_change(&heads[0]).unwrap(); |
| 2340 |
assert_eq!(top.message, "add three"); |
| 75167672 Andy McCall |
2341 |
assert_eq!( |
| 2342 |
top.deps.iter().copied().collect::<BTreeSet<_>>(), |
| 2343 |
[h0, h1b].into_iter().collect(), |
| 2344 |
"depends on the file's creator and the rewritten line" |
| 2345 |
); |
| 23b217d7 Andy McCall ↶ |
2346 |
let snap = repo.load_state(&heads).unwrap().materialize(); |
| 75167672 Andy McCall |
2347 |
assert!( |
| 2348 |
matches!(&snap.files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\nTWO!\nthree\n") |
| 2349 |
); |
| 2350 |
assert!( |
| 2351 |
!repo.snapshot("status").unwrap().changed, |
| 2352 |
"the working copy is clean after folding" |
| 2353 |
); |
| 23b217d7 Andy McCall ↶ |
2354 |
|
| 2355 |
let only = repo.load_state(&[h1b]).unwrap().materialize(); |
| 75167672 Andy McCall |
2356 |
assert!( |
| 2357 |
matches!(&only.files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\nTWO!\n") |
| 2358 |
); |
| 23b217d7 Andy McCall ↶ |
2359 |
assert_eq!(repo.log().unwrap().len(), 4); |
| 2360 |
|
| 2361 |
|
| 2362 |
write(root, "a.txt", b"one\nTWO!\nthree!\n"); |
| 2363 |
let err = repo.amend_into(&h1b, None).unwrap_err().to_string(); |
| 2364 |
assert!(err.contains("introduced after"), "{err}"); |
| 75167672 Andy McCall |
2365 |
assert!( |
| 2366 |
repo.snapshot("status").unwrap().statuses.len() == 1, |
| 2367 |
"nothing was changed by the refusal" |
| 2368 |
); |
| 23b217d7 Andy McCall ↶ |
2369 |
write(root, "a.txt", b"one\nTWO!\nthree\n"); |
| 2370 |
|
| 2371 |
|
| 2372 |
write(root, "b.txt", b"b\n"); |
| 2373 |
write(root, "c.txt", b"c\n"); |
| 2374 |
let h3 = repo.commit("b and c", &[]).unwrap(); |
| 2375 |
let id3 = repo.store().get_change(&h3).unwrap().change_id; |
| 2376 |
let (p1, p2) = repo.split(&h3, &["b.txt".into()]).unwrap(); |
| 2377 |
let c_1 = repo.store().get_change(&p1).unwrap(); |
| 2378 |
let c_2 = repo.store().get_change(&p2).unwrap(); |
| 2379 |
assert!(c_1.message.contains("part 1: b.txt")); |
| 2380 |
assert_eq!(c_2.change_id, id3); |
| 2381 |
let heads: BTreeSet<_> = repo.bookmark_heads().unwrap().into_iter().collect(); |
| 75167672 Andy McCall |
2382 |
assert!( |
| 2383 |
heads.contains(&p1) && heads.contains(&p2), |
| 2384 |
"independent parts are both heads" |
| 2385 |
); |
| 2386 |
let snap = repo |
| 2387 |
.load_state(&repo.bookmark_heads().unwrap()) |
| 2388 |
.unwrap() |
| 2389 |
.materialize(); |
| 23b217d7 Andy McCall ↶ |
2390 |
assert!(snap.files.contains_key("b.txt") && snap.files.contains_key("c.txt")); |
| 2391 |
let only1 = repo.load_state(&[p1]).unwrap().materialize(); |
| 2392 |
assert!(only1.files.contains_key("b.txt") && !only1.files.contains_key("c.txt")); |
| 2393 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 2394 |
assert!(repo.split(&p2, &["nope.txt".into()]).is_err()); |
| 2395 |
let _ = h2; |
| 2396 |
} |
| 28fc23d7 Andy McCall ↶ |
2397 |
|
| 2398 |
#[test] |
| 2399 |
fn identity_lifecycle() { |
| 2400 |
hermetic(); |
| 2401 |
let dir = tempfile::tempdir().unwrap(); |
| 2402 |
let root = dir.path(); |
| 2403 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 2404 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
2405 |
let mut a = Repo::clone(&url, &root.join("a")) |
| 2406 |
.unwrap() |
| 2407 |
.with_home(root.join("home-a")); |
| 28fc23d7 Andy McCall ↶ |
2408 |
a.set_user_config("core.sign", "change").unwrap(); |
| 75167672 Andy McCall |
2409 |
let (key_a, _) = a |
| 2410 |
.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 2411 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2412 |
write(&root.join("a"), "a.txt", b"a\n"); |
| 2413 |
let h1 = a.commit("one", &[]).unwrap(); |
| 2414 |
assert!(a.policy_init().unwrap().applied); |
| 2415 |
assert!(a.push("origin", false).unwrap().iter().all(|r| r.ok)); |
| 2416 |
|
| 2417 |
|
| 75167672 Andy McCall |
2418 |
let mut e = Repo::init(&root.join("e")) |
| 2419 |
.unwrap() |
| 2420 |
.with_home(root.join("home-e")); |
| 28fc23d7 Andy McCall ↶ |
2421 |
e.set_user_config("core.sign", "change").unwrap(); |
| 75167672 Andy McCall |
2422 |
e.create_identity_with( |
| 2423 |
"Eve", |
| 2424 |
"[email protected]", |
| 2425 |
IdentityScope::User, |
| 2426 |
Some(Hlc::now().wall_ms - 1), |
| 2427 |
) |
| 2428 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2429 |
write(&root.join("e"), "e.txt", b"e\n"); |
| 2430 |
let he = e.commit("late", &[]).unwrap(); |
| 75167672 Andy McCall |
2431 |
assert!( |
| 2432 |
matches!(e.verify_change(&he).unwrap(), SignatureStatus::Invalid(m) if m.contains("expired")) |
| 2433 |
); |
| 28fc23d7 Andy McCall ↶ |
2434 |
|
| 75167672 Andy McCall |
2435 |
let r = e |
| 2436 |
.renew_identity(Some(Hlc::now().wall_ms + 86_400_000)) |
| 2437 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2438 |
assert_eq!(e.identity(&r.new).unwrap().previous, Some(r.old)); |
| 2439 |
write(&root.join("e"), "e.txt", b"e2\n"); |
| 2440 |
let he2 = e.commit("in time", &[]).unwrap(); |
| 75167672 Andy McCall |
2441 |
assert!( |
| 2442 |
matches!(e.verify_change(&he2).unwrap(), SignatureStatus::Valid { key, .. } if key == r.new) |
| 2443 |
); |
| 28fc23d7 Andy McCall ↶ |
2444 |
|
| 2445 |
|
| 2446 |
let r = a.rotate_identity().unwrap(); |
| 2447 |
assert_eq!(r.old, key_a); |
| 2448 |
assert_eq!(a.config.author_key, Some(r.new)); |
| 2449 |
assert_eq!(a.identity(&r.new).unwrap().previous, Some(key_a)); |
| 2450 |
assert!(r.policy.as_ref().unwrap().applied); |
| 2451 |
let (_, sp) = a.current_policy().unwrap().unwrap(); |
| 75167672 Andy McCall |
2452 |
assert_eq!( |
| 2453 |
(sp.policy.serial, sp.policy.roots.clone()), |
| 2454 |
(2, vec![r.new]) |
| 2455 |
); |
| 2456 |
assert!( |
| 2457 |
!sp.policy.revoked.contains(&key_a) && sp.policy.authorizes(&r.new, "main"), |
| 2458 |
"superseded, not revoked" |
| 2459 |
); |
| 28fc23d7 Andy McCall ↶ |
2460 |
assert!(sp.policy.succeeds(&r.new, &key_a) && !sp.policy.succeeds(&key_a, &r.new)); |
| 75167672 Andy McCall |
2461 |
assert!( |
| 2462 |
matches!(a.verify_change(&h1).unwrap(), SignatureStatus::Valid { key, revoked: false, .. } if key == key_a) |
| 2463 |
); |
| 28fc23d7 Andy McCall ↶ |
2464 |
write(&root.join("a"), "a.txt", b"a2\n"); |
| 2465 |
let h2 = a.commit("two", &[]).unwrap(); |
| 75167672 Andy McCall |
2466 |
assert!( |
| 2467 |
matches!(a.verify_change(&h2).unwrap(), SignatureStatus::Valid { key, revoked: false, .. } if key == r.new) |
| 2468 |
); |
| 28fc23d7 Andy McCall ↶ |
2469 |
|
| 2470 |
let res = a.push("origin", false).unwrap(); |
| 2471 |
assert!(res.iter().all(|x| x.ok), "{res:?}"); |
| 2472 |
assert_eq!(origin.current_policy().unwrap().unwrap().1.policy.serial, 2); |
| 2473 |
let key_a = r.new; |
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 75167672 Andy McCall |
2477 |
let mut b = Repo::clone(&url, &root.join("b")) |
| 2478 |
.unwrap() |
| 2479 |
.with_home(root.join("home-b")); |
| 2480 |
let (key_b, id_b) = b |
| 2481 |
.create_identity("Bob", "[email protected]", IdentityScope::User) |
| 2482 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2483 |
let r = a.delegate_add(id_b.clone()).unwrap(); |
| 2484 |
assert_eq!(r.old, key_a); |
| 2485 |
let key_a2 = r.new; |
| 2486 |
assert_eq!(a.identity(&key_a2).unwrap().delegates, vec![key_b]); |
| 75167672 Andy McCall |
2487 |
assert_eq!( |
| 2488 |
a.identity(&key_a2).unwrap().ed25519_pk, |
| 2489 |
a.identity(&key_a).unwrap().ed25519_pk |
| 2490 |
); |
| 28fc23d7 Andy McCall ↶ |
2491 |
assert!(a.delegate_add(id_b.clone()).is_err(), "already a delegate"); |
| 2492 |
assert!(a.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 2493 |
write(&root.join("a"), "a.txt", b"a3\n"); |
| 2494 |
let h3 = a.commit("three", &[]).unwrap(); |
| 75167672 Andy McCall |
2495 |
assert!( |
| 2496 |
matches!(a.verify_change(&h3).unwrap(), SignatureStatus::Valid { key, .. } if key == key_a2), |
| 2497 |
"the same secret signs under the new KeyId" |
| 2498 |
); |
| 28fc23d7 Andy McCall ↶ |
2499 |
|
| 2500 |
a.set_user_config("core.sign", "seal").unwrap(); |
| 2501 |
write(&root.join("a"), "a.txt", b"a4\n"); |
| 2502 |
let h4 = a.commit("four", &[]).unwrap(); |
| 2503 |
assert_eq!(a.verify_change(&h4).unwrap(), SignatureStatus::Unsigned); |
| 2504 |
let (_, sk_b) = b.secret_key().unwrap().unwrap(); |
| 75167672 Andy McCall |
2505 |
a.store() |
| 2506 |
.put_signature(&h4, &sk_b.sign(Domain::Change, &key_b, &h4)) |
| 2507 |
.unwrap(); |
| 2508 |
assert!( |
| 2509 |
matches!(a.verify_change(&h4).unwrap(), SignatureStatus::Valid { key, .. } if key == key_b) |
| 2510 |
); |
| 28fc23d7 Andy McCall ↶ |
2511 |
a.set_user_config("core.sign", "change").unwrap(); |
| 2512 |
|
| 2513 |
let r = a.delegate_remove(&key_b).unwrap(); |
| 2514 |
assert!(a.identity(&r.new).unwrap().delegates.is_empty()); |
| 2515 |
assert!(a.delegate_remove(&key_b).is_err()); |
| 2516 |
let key_a3 = r.new; |
| 2517 |
let res = a.push("origin", false).unwrap(); |
| 2518 |
assert!(res.iter().all(|x| x.ok), "{res:?}"); |
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
assert!(a.policy_root_add(id_b.clone()).unwrap().applied); |
| 2523 |
assert!(a.policy_root_add(id_b.clone()).is_err()); |
| 2524 |
assert!(a.policy_set_threshold(3).is_err()); |
| 2525 |
assert!(a.policy_set_threshold(2).unwrap().applied); |
| 2526 |
assert!(a.push("origin", false).unwrap().iter().all(|x| x.ok)); |
| 2527 |
b.pull("origin").unwrap(); |
| 75167672 Andy McCall |
2528 |
assert_eq!( |
| 2529 |
b.current_policy().unwrap().unwrap().1.policy.root_threshold, |
| 2530 |
2 |
| 2531 |
); |
| 2532 |
let mut c = Repo::init(&root.join("c")) |
| 2533 |
.unwrap() |
| 2534 |
.with_home(root.join("home-c")); |
| 2535 |
let (key_c, id_c) = c |
| 2536 |
.create_identity("Cy", "[email protected]", IdentityScope::User) |
| 2537 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2538 |
let serial_before = a.current_policy().unwrap().unwrap().1.policy.serial; |
| 2539 |
let proposal = a.policy_grant(id_c, None).unwrap(); |
| 2540 |
assert!(!proposal.applied && proposal.sigs.len() == 1); |
| 2541 |
assert_eq!(proposal.policy.serial, serial_before + 1); |
| 75167672 Andy McCall |
2542 |
assert_eq!( |
| 2543 |
a.current_policy().unwrap().unwrap().1.policy.serial, |
| 2544 |
serial_before, |
| 2545 |
"a proposal changes nothing yet" |
| 2546 |
); |
| 28fc23d7 Andy McCall ↶ |
2547 |
|
| 2548 |
let bytes = proposal.signed().encode(); |
| 75167672 Andy McCall |
2549 |
let signed = b |
| 2550 |
.policy_sign(SignedPolicy::decode(&bytes).unwrap()) |
| 2551 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2552 |
assert!(signed.applied && signed.sigs.len() == 2); |
| 75167672 Andy McCall |
2553 |
assert!(b |
| 2554 |
.current_policy() |
| 2555 |
.unwrap() |
| 2556 |
.unwrap() |
| 2557 |
.1 |
| 2558 |
.policy |
| 2559 |
.authorizes(&key_c, "main")); |
| 2560 |
assert!( |
| 2561 |
b.policy_sign(signed.signed()).is_err(), |
| 2562 |
"a stale proposal is refused" |
| 2563 |
); |
| 28fc23d7 Andy McCall ↶ |
2564 |
|
| 75167672 Andy McCall |
2565 |
assert!(b |
| 2566 |
.push("origin", false) |
| 2567 |
.unwrap() |
| 2568 |
.iter() |
| 2569 |
.any(|x| x.name == "policy" && x.ok)); |
| 28fc23d7 Andy McCall ↶ |
2570 |
a.pull("origin").unwrap(); |
| 75167672 Andy McCall |
2571 |
assert_eq!( |
| 2572 |
a.current_policy().unwrap().unwrap().1.policy.serial, |
| 2573 |
serial_before + 1 |
| 2574 |
); |
| 28fc23d7 Andy McCall ↶ |
2575 |
|
| 2576 |
assert!(a.policy_revoke(&key_a3).is_err()); |
| 75167672 Andy McCall |
2577 |
let u = a |
| 2578 |
.revoke_identity(&key_c, "left the project") |
| 2579 |
.unwrap() |
| 2580 |
.unwrap(); |
| 28fc23d7 Andy McCall ↶ |
2581 |
assert!(!u.applied); |
| 2582 |
assert!(a.is_revoked(&key_c).unwrap()); |
| 2583 |
assert!(!b.is_revoked(&key_c).unwrap()); |
| 2584 |
let u = b.policy_sign(u.signed()).unwrap(); |
| 2585 |
assert!(u.applied && u.policy.is_revoked(&key_c)); |
| 2586 |
assert!(b.is_revoked(&key_c).unwrap()); |
| 2587 |
|
| 75167672 Andy McCall |
2588 |
assert!( |
| 2589 |
b.policy_revoke(&key_a3).is_err(), |
| 2590 |
"would leave one root for a threshold of 2" |
| 2591 |
); |
| 28fc23d7 Andy McCall ↶ |
2592 |
|
| 2593 |
let key_c_own = c.config.author_key.unwrap(); |
| 75167672 Andy McCall |
2594 |
assert!(c |
| 2595 |
.revoke_identity(&key_c_own, "lost laptop") |
| 2596 |
.unwrap() |
| 2597 |
.is_none()); |
| 28fc23d7 Andy McCall ↶ |
2598 |
assert!(c.is_revoked(&key_c_own).unwrap()); |
| 2599 |
assert_eq!(key_c_own, key_c); |
| 2600 |
} |
| 2601 |
|
| 6e723b81 Andy McCall ↶ |
2602 |
#[test] |
| 2603 |
fn renames_are_detected_and_tracked_as_moves() { |
| 2604 |
hermetic(); |
| 2605 |
let dir = tempfile::tempdir().unwrap(); |
| 2606 |
let root = dir.path(); |
| 2607 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
2608 |
write( |
| 2609 |
root, |
| 2610 |
"src/old.rs", |
| 2611 |
b"fn a() {}\nfn b() {}\nfn c() {}\nfn d() {}\n", |
| 2612 |
); |
| 6e723b81 Andy McCall ↶ |
2613 |
write(root, "img.bin", b"\x89PNG\0\0\x01\x02"); |
| 2614 |
write(root, "tiny.txt", b"x\n"); |
| 2615 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 2616 |
let node_old = repo.load_state(&[h1]).unwrap().materialize().files["src/old.rs"].node; |
| 2617 |
let node_img = repo.load_state(&[h1]).unwrap().materialize().files["img.bin"].node; |
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
fs::remove_file(root.join("src/old.rs")).unwrap(); |
| 75167672 Andy McCall |
2622 |
write( |
| 2623 |
root, |
| 2624 |
"lib/new.rs", |
| 2625 |
b"fn a() {}\nfn b() {}\nfn c() {}\nfn e() {}\n", |
| 2626 |
); |
| 6e723b81 Andy McCall ↶ |
2627 |
fs::remove_file(root.join("img.bin")).unwrap(); |
| 2628 |
write(root, "assets/img.bin", b"\x89PNG\0\0\x01\x02"); |
| 2629 |
fs::remove_file(root.join("tiny.txt")).unwrap(); |
| 2630 |
write(root, "other.txt", b"completely different\n"); |
| 2631 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
2632 |
let statuses: Vec<(&str, ChangeStatus)> = snap |
| 2633 |
.statuses |
| 2634 |
.iter() |
| 2635 |
.map(|s| (s.path.as_str(), s.status)) |
| 2636 |
.collect(); |
| 6e723b81 Andy McCall ↶ |
2637 |
assert_eq!( |
| 2638 |
statuses, |
| 75167672 Andy McCall |
2639 |
vec![ |
| 2640 |
("assets/img.bin", ChangeStatus::Renamed), |
| 2641 |
("lib/new.rs", ChangeStatus::Renamed), |
| 2642 |
("other.txt", ChangeStatus::Added), |
| 2643 |
("src", ChangeStatus::Removed), |
| 2644 |
("tiny.txt", ChangeStatus::Removed) |
| 2645 |
] |
| 6e723b81 Andy McCall ↶ |
2646 |
); |
| 2647 |
let h2 = repo.commit("move things", &[]).unwrap(); |
| 2648 |
let c = repo.store().get_change(&h2).unwrap(); |
| 75167672 Andy McCall |
2649 |
let moves: Vec<_> = c |
| 2650 |
.tree_ops |
| 2651 |
.iter() |
| 2652 |
.filter(|op| matches!(op, TreeOp::Move { .. })) |
| 2653 |
.collect(); |
| 6e723b81 Andy McCall ↶ |
2654 |
assert_eq!(moves.len(), 2, "{:?}", c.tree_ops); |
| 75167672 Andy McCall |
2655 |
assert!(c.tree_ops.iter().any( |
| 2656 |
|op| matches!(op, TreeOp::Move { node, name, .. } if *node == node_old && name == "new.rs") |
| 2657 |
)); |
| 2658 |
assert!(c.tree_ops.iter().any( |
| 2659 |
|op| matches!(op, TreeOp::Move { node, name, .. } if *node == node_img && name == "img.bin") |
| 2660 |
)); |
| 6e723b81 Andy McCall ↶ |
2661 |
let snap = repo.load_state(&[h2]).unwrap().materialize(); |
| 75167672 Andy McCall |
2662 |
assert_eq!( |
| 2663 |
snap.files["lib/new.rs"].node, node_old, |
| 2664 |
"the node survives the rename" |
| 2665 |
); |
| 2666 |
assert!( |
| 2667 |
matches!(&snap.files["lib/new.rs"].content, FileContent::Text { bytes, .. } if bytes == b"fn a() {}\nfn b() {}\nfn c() {}\nfn e() {}\n") |
| 2668 |
); |
| 6e723b81 Andy McCall ↶ |
2669 |
assert!(!snap.files.contains_key("src/old.rs") && !snap.files.contains_key("src")); |
| 75167672 Andy McCall |
2670 |
assert!(render_change( |
| 2671 |
&c, |
| 2672 |
&repo.load_state(&[h1]).unwrap(), |
| 2673 |
&repo.load_state(&[h1]).unwrap().materialize() |
| 2674 |
) |
| 2675 |
.contains("R ")); |
| 6e723b81 Andy McCall ↶ |
2676 |
|
| 2677 |
let other = tempfile::tempdir().unwrap(); |
| 2678 |
let clone = Repo::clone(&root.to_string_lossy(), other.path()).unwrap(); |
| 75167672 Andy McCall |
2679 |
assert_eq!( |
| 2680 |
fs::read(other.path().join("lib/new.rs")).unwrap(), |
| 2681 |
b"fn a() {}\nfn b() {}\nfn c() {}\nfn e() {}\n" |
| 2682 |
); |
| 6e723b81 Andy McCall ↶ |
2683 |
assert!(!other.path().join("src").exists()); |
| 2684 |
assert!(!clone.snapshot("status").unwrap().changed); |
| 2685 |
|
| 2686 |
assert_eq!(similarity(b"a\nb\n", b"a\nb\n"), 100); |
| 2687 |
assert_eq!(similarity(b"a\nb\nc\nd\n", b"a\nb\nx\ny\n"), 50); |
| 2688 |
assert_eq!(similarity(b"hello\n", b"world\n"), 0); |
| 2689 |
assert_eq!(similarity(b"", b"x\n"), 0); |
| 2690 |
} |
| 2691 |
|
| 30ea9c1e Andy McCall ↶ |
2692 |
#[test] |
| 2693 |
fn git_fetch_and_push_round_trip() { |
| 2694 |
hermetic(); |
| 75167672 Andy McCall |
2695 |
if std::process::Command::new("git") |
| 2696 |
.arg("--version") |
| 2697 |
.output() |
| 2698 |
.is_err() |
| 2699 |
{ |
| 30ea9c1e Andy McCall ↶ |
2700 |
eprintln!("git not installed; skipping"); |
| 2701 |
return; |
| 2702 |
} |
| 2703 |
let dir = tempfile::tempdir().unwrap(); |
| 2704 |
let root = dir.path(); |
| 2705 |
let git_in = |d: &Path, args: &[&str]| -> String { |
| 2706 |
let out = std::process::Command::new("git") |
| 2707 |
.arg("-C") |
| 2708 |
.arg(d) |
| 75167672 Andy McCall |
2709 |
.args([ |
| 2710 |
"-c", |
| 2711 |
"user.name=Grace", |
| 2712 |
"-c", |
| 2713 |
"[email protected]", |
| 2714 |
"-c", |
| 2715 |
"commit.gpgsign=false", |
| 2716 |
]) |
| 30ea9c1e Andy McCall ↶ |
2717 |
.args(args) |
| 2718 |
.output() |
| 2719 |
.unwrap(); |
| 75167672 Andy McCall |
2720 |
assert!( |
| 2721 |
out.status.success(), |
| 2722 |
"git {args:?}: {}", |
| 2723 |
String::from_utf8_lossy(&out.stderr) |
| 2724 |
); |
| 30ea9c1e Andy McCall ↶ |
2725 |
String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 2726 |
}; |
| 2727 |
|
| 2728 |
let remote = root.join("remote.git"); |
| 2729 |
fs::create_dir_all(&remote).unwrap(); |
| 2730 |
git_in(&remote, &["init", "-q", "--bare", "-b", "main"]); |
| 2731 |
let work = root.join("work"); |
| 75167672 Andy McCall |
2732 |
git_in( |
| 2733 |
root, |
| 2734 |
&[ |
| 2735 |
"clone", |
| 2736 |
"-q", |
| 2737 |
remote.to_str().unwrap(), |
| 2738 |
work.to_str().unwrap(), |
| 2739 |
], |
| 2740 |
); |
| 30ea9c1e Andy McCall ↶ |
2741 |
git_in(&work, &["checkout", "-q", "-b", "main"]); |
| 2742 |
write(&work, "README.md", b"# Project\n"); |
| 2743 |
write(&work, "src/lib.rs", b"pub fn a() {}\n"); |
| 2744 |
git_in(&work, &["add", "."]); |
| 2745 |
git_in(&work, &["commit", "-q", "-m", "initial"]); |
| 2746 |
write(&work, "src/lib.rs", b"pub fn a() {}\npub fn b() {}\n"); |
| 2747 |
git_in(&work, &["commit", "-q", "-am", "second"]); |
| 2748 |
git_in(&work, &["push", "-q", "origin", "main"]); |
| 2749 |
let tip1 = git_in(&work, &["rev-parse", "HEAD"]); |
| 2750 |
|
| 2751 |
|
| 2752 |
let o = root.join("oxy"); |
| 2753 |
let mut repo = Repo::init(&o).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
2754 |
repo.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 2755 |
.unwrap(); |
| 30ea9c1e Andy McCall ↶ |
2756 |
let url = remote.to_string_lossy().to_string(); |
| 2757 |
let r = repo.git_fetch(&url, "main").unwrap(); |
| 2758 |
assert_eq!((r.commits, r.heads), (2, 1)); |
| 75167672 Andy McCall |
2759 |
assert_eq!( |
| 2760 |
fs::read(o.join("src/lib.rs")).unwrap(), |
| 2761 |
b"pub fn a() {}\npub fn b() {}\n" |
| 2762 |
); |
| 30ea9c1e Andy McCall ↶ |
2763 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 75167672 Andy McCall |
2764 |
assert_eq!( |
| 2765 |
repo.git_fetch(&url, "main").unwrap().commits, |
| 2766 |
0, |
| 2767 |
"nothing new" |
| 2768 |
); |
| 30ea9c1e Andy McCall ↶ |
2769 |
|
| 2770 |
|
| 75167672 Andy McCall |
2771 |
write( |
| 2772 |
&o, |
| 2773 |
"src/lib.rs", |
| 2774 |
b"pub fn a() {}\npub fn b() {}\npub fn c() {}\n", |
| 2775 |
); |
| 30ea9c1e Andy McCall ↶ |
2776 |
write(&o, "NOTES.md", b"from oxy\n"); |
| 2777 |
let h_oxy = repo.commit("add c and notes", &[]).unwrap(); |
| 2778 |
let r = repo.git_push(&url, "main").unwrap(); |
| 2779 |
assert_eq!(r.exported, 1, "{r:?}"); |
| 2780 |
git_in(&work, &["pull", "-q", "--ff-only", "origin", "main"]); |
| 75167672 Andy McCall |
2781 |
assert_eq!( |
| 2782 |
git_in(&work, &["log", "--format=%s", "main"]), |
| 2783 |
"add c and notes\nsecond\ninitial" |
| 2784 |
); |
| 30ea9c1e Andy McCall ↶ |
2785 |
assert_eq!(git_in(&work, &["rev-parse", "HEAD~1"]), tip1); |
| 2786 |
assert_eq!(fs::read(work.join("NOTES.md")).unwrap(), b"from oxy\n"); |
| 2787 |
assert_eq!(git_in(&work, &["log", "-1", "--format=%an", "main"]), "Ada"); |
| 75167672 Andy McCall |
2788 |
assert!(git_in(&work, &["log", "-1", "--format=%B", "main"]) |
| 2789 |
.contains(&format!("Oxy-Hash: {}", h_oxy.to_hex()))); |
| 2790 |
assert_eq!( |
| 2791 |
repo.git_push(&url, "main").unwrap().exported, |
| 2792 |
0, |
| 2793 |
"already pushed" |
| 2794 |
); |
| 30ea9c1e Andy McCall ↶ |
2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
write(&work, "README.md", b"# Project\n\nMore.\n"); |
| 2799 |
git_in(&work, &["commit", "-q", "-am", "readme from git"]); |
| 2800 |
git_in(&work, &["push", "-q", "origin", "main"]); |
| 2801 |
let tip2 = git_in(&work, &["rev-parse", "HEAD"]); |
| 75167672 Andy McCall |
2802 |
assert!(repo |
| 2803 |
.git_push(&url, "main") |
| 2804 |
.unwrap_err() |
| 2805 |
.to_string() |
| 2806 |
.contains("fetch")); |
| 30ea9c1e Andy McCall ↶ |
2807 |
let r = repo.git_fetch(&url, "main").unwrap(); |
| 75167672 Andy McCall |
2808 |
assert_eq!( |
| 2809 |
(r.commits, r.heads), |
| 2810 |
(1, 2), |
| 2811 |
"the Git change and the oxy change commute" |
| 2812 |
); |
| 2813 |
assert_eq!( |
| 2814 |
fs::read(o.join("README.md")).unwrap(), |
| 2815 |
b"# Project\n\nMore.\n" |
| 2816 |
); |
| 30ea9c1e Andy McCall ↶ |
2817 |
assert_eq!(fs::read(o.join("NOTES.md")).unwrap(), b"from oxy\n"); |
| 2818 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 2819 |
|
| 2820 |
write(&o, "NOTES.md", b"from oxy\nagain\n"); |
| 2821 |
repo.commit("notes again", &[]).unwrap(); |
| 2822 |
assert_eq!(repo.git_push(&url, "main").unwrap().exported, 1); |
| 2823 |
git_in(&work, &["pull", "-q", "--ff-only", "origin", "main"]); |
| 2824 |
assert_eq!(git_in(&work, &["rev-parse", "HEAD~1"]), tip2); |
| 75167672 Andy McCall |
2825 |
assert_eq!( |
| 2826 |
fs::read(work.join("NOTES.md")).unwrap(), |
| 2827 |
b"from oxy\nagain\n" |
| 2828 |
); |
| 2829 |
assert_eq!( |
| 2830 |
git_in(&work, &["log", "--format=%s", "main"]) |
| 2831 |
.lines() |
| 2832 |
.count(), |
| 2833 |
5 |
| 2834 |
); |
| 30ea9c1e Andy McCall ↶ |
2835 |
} |
| 2836 |
|
| 2b872d07 Andy McCall ↶ |
2837 |
|
| 2838 |
|
| 2839 |
#[test] |
| 2840 |
#[ignore] |
| 2841 |
fn bench_one_gibibyte_file() { |
| 2842 |
hermetic(); |
| 2843 |
|
| 2844 |
let dir = match std::env::var_os("OXY_BENCH_DIR") { |
| 2845 |
Some(d) => tempfile::tempdir_in(d).unwrap(), |
| 2846 |
None => tempfile::tempdir().unwrap(), |
| 2847 |
}; |
| 2848 |
let root = dir.path(); |
| 75167672 Andy McCall |
2849 |
let repo = Repo::init(&root.join("r")) |
| 2850 |
.unwrap() |
| 2851 |
.with_home(root.join("home")); |
| 2b872d07 Andy McCall ↶ |
2852 |
let size = 1usize << 30; |
| 2853 |
let mut data = vec![0u8; size]; |
| 2854 |
let mut x = 0x9e3779b97f4a7c15u64; |
| 2855 |
for chunk in data.chunks_mut(8) { |
| 2856 |
x ^= x << 13; |
| 2857 |
x ^= x >> 7; |
| 2858 |
x ^= x << 17; |
| 2859 |
let b = x.to_le_bytes(); |
| 2860 |
chunk.copy_from_slice(&b[..chunk.len()]); |
| 2861 |
} |
| 2862 |
let t = std::time::Instant::now(); |
| 2863 |
write(&root.join("r"), "big.bin", &data); |
| 2864 |
eprintln!("write 1 GiB to disk: {:.2?}", t.elapsed()); |
| 2865 |
let t = std::time::Instant::now(); |
| 2866 |
let h = repo.commit("big", &[]).unwrap(); |
| 2867 |
eprintln!("commit (chunk + hash + store): {:.2?}", t.elapsed()); |
| 2868 |
let t = std::time::Instant::now(); |
| 2869 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 2870 |
eprintln!("status on a clean 1 GiB working copy: {:.2?}", t.elapsed()); |
| 2871 |
let t = std::time::Instant::now(); |
| 2872 |
let objects = repo.store().list().unwrap().len(); |
| 2873 |
eprintln!("objects stored: {objects} ({:.2?} to list)", t.elapsed()); |
| 2874 |
let t = std::time::Instant::now(); |
| 2875 |
let g = repo.gc(true).unwrap(); |
| 2876 |
eprintln!("gc (pack): {:.2?} {g:?}", t.elapsed()); |
| 2877 |
let t = std::time::Instant::now(); |
| 2878 |
let clone = Repo::clone(&root.join("r").to_string_lossy(), &root.join("c")).unwrap(); |
| 2879 |
eprintln!("clone (fetch + checkout): {:.2?}", t.elapsed()); |
| 2880 |
let t = std::time::Instant::now(); |
| 2881 |
let back = fs::read(root.join("c/big.bin")).unwrap(); |
| 2882 |
assert_eq!(blake3::hash(&back), blake3::hash(&data)); |
| 2883 |
eprintln!("read back and verify: {:.2?}", t.elapsed()); |
| 2884 |
assert_eq!(clone.bookmark_heads().unwrap(), vec![h]); |
| 2885 |
let store_bytes: u64 = walkdir_size(&root.join("r/.oxy/objects")); |
| 75167672 Andy McCall |
2886 |
eprintln!( |
| 2887 |
"store size: {:.1} MiB for 1 GiB of data", |
| 2888 |
store_bytes as f64 / (1024.0 * 1024.0) |
| 2889 |
); |
| 2b872d07 Andy McCall ↶ |
2890 |
} |
| 2891 |
|
| 2892 |
fn walkdir_size(p: &Path) -> u64 { |
| 2893 |
let mut total = 0; |
| 2894 |
if let Ok(rd) = fs::read_dir(p) { |
| 2895 |
for e in rd.flatten() { |
| 2896 |
let path = e.path(); |
| 2897 |
if path.is_dir() { |
| 2898 |
total += walkdir_size(&path); |
| 2899 |
} else if let Ok(m) = fs::metadata(&path) { |
| 2900 |
total += m.len(); |
| 2901 |
} |
| 2902 |
} |
| 2903 |
} |
| 2904 |
total |
| 2905 |
} |
| 2906 |
|
| b7f102fc Andy McCall ↶ |
2907 |
|
| 2908 |
|
| 2909 |
#[cfg(unix)] |
| 2910 |
#[test] |
| 2911 |
fn stat_cache_skips_reading_unchanged_opaque_files() { |
| 2912 |
use std::os::unix::fs::PermissionsExt; |
| 2913 |
hermetic(); |
| 2914 |
let dir = tempfile::tempdir().unwrap(); |
| 2915 |
let root = dir.path(); |
| 2916 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 2917 |
let mut big = vec![0u8; 300 * 1024]; |
| 2918 |
for (i, b) in big.iter_mut().enumerate() { |
| 2919 |
*b = (i as u32).wrapping_mul(2654435761).to_le_bytes()[1]; |
| 2920 |
} |
| 2921 |
big[0] = 0; |
| 2922 |
write(root, "big.bin", &big); |
| 2923 |
write(root, "a.txt", b"text\n"); |
| 2924 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 2925 |
let (_, _, m) = repo.read_manifest().unwrap().unwrap(); |
| 75167672 Andy McCall |
2926 |
assert!( |
| 2927 |
m.entries["big.bin"].blob.is_some(), |
| 2928 |
"opaque files record their blob" |
| 2929 |
); |
| b7f102fc Andy McCall ↶ |
2930 |
assert!(m.entries["a.txt"].blob.is_none()); |
| 2931 |
let blob = m.entries["big.bin"].blob.unwrap(); |
| 2932 |
assert!(repo.store().has(&blob)); |
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(30); |
| 75167672 Andy McCall |
2937 |
fs::File::options() |
| 2938 |
.write(true) |
| 2939 |
.open(root.join("big.bin")) |
| 2940 |
.unwrap() |
| 2941 |
.set_modified(old) |
| 2942 |
.unwrap(); |
| b7f102fc Andy McCall ↶ |
2943 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 2944 |
|
| 2945 |
fs::set_permissions(root.join("big.bin"), fs::Permissions::from_mode(0o000)).unwrap(); |
| 2946 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
2947 |
assert!( |
| 2948 |
!snap.changed && snap.statuses.is_empty(), |
| 2949 |
"{:?}", |
| 2950 |
snap.statuses |
| 2951 |
); |
| b7f102fc Andy McCall ↶ |
2952 |
fs::set_permissions(root.join("big.bin"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 2953 |
|
| 2954 |
let mut big2 = big.clone(); |
| 2955 |
big2[10] ^= 0xff; |
| 2956 |
write(root, "big.bin", &big2); |
| 2957 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
2958 |
assert_eq!( |
| 2959 |
snap.statuses |
| 2960 |
.iter() |
| 2961 |
.map(|s| (s.path.as_str(), s.status)) |
| 2962 |
.collect::<Vec<_>>(), |
| 2963 |
vec![("big.bin", ChangeStatus::Modified)] |
| 2964 |
); |
| b7f102fc Andy McCall ↶ |
2965 |
let h2 = repo.commit("edit", &[]).unwrap(); |
| 2966 |
let files = repo.load_state(&[h2]).unwrap().materialize().files; |
| 75167672 Andy McCall |
2967 |
assert_eq!( |
| 2968 |
files["big.bin"].content, |
| 2969 |
FileContent::Opaque { |
| 2970 |
blobs: vec![Repo::blob_ref(&big2)] |
| 2971 |
} |
| 2972 |
); |
| b7f102fc Andy McCall ↶ |
2973 |
|
| 2974 |
repo.checkout(&[h1], false, "checkout").unwrap(); |
| 2975 |
let (_, _, m) = repo.read_manifest().unwrap().unwrap(); |
| 2976 |
assert_eq!(m.entries["big.bin"].blob, Some(Repo::blob_ref(&big))); |
| 2977 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 2978 |
} |
| 2979 |
|
| 50a046e4 Andy McCall ↶ |
2980 |
#[test] |
| 2981 |
fn status_json_lists_conflicts_with_contributors() { |
| 2982 |
hermetic(); |
| 2983 |
let dir = tempfile::tempdir().unwrap(); |
| 2984 |
let root = dir.path(); |
| 2985 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 2986 |
write(root, "a.txt", b"one\n"); |
| 2987 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 2988 |
write(root, "a.txt", b"one\nleft\n"); |
| 2989 |
let ha = repo.commit("left", &[]).unwrap(); |
| 2990 |
repo.checkout(&[h1], true, "checkout").unwrap(); |
| 2991 |
write(root, "a.txt", b"one\nright\n"); |
| 2992 |
let hb = repo.commit("right", &[]).unwrap(); |
| 2993 |
repo.checkout(&[ha, hb], true, "checkout").unwrap(); |
| 2994 |
let snap = repo.snapshot("status").unwrap(); |
| 2995 |
assert!(snap.base_snapshot.is_conflicted()); |
| 2996 |
let json = repo.status_json(&snap).unwrap(); |
| 2997 |
let ida = repo.store().get_change(&ha).unwrap().change_id.to_hex(); |
| 2998 |
let idb = repo.store().get_change(&hb).unwrap().change_id.to_hex(); |
| 2999 |
assert!(json.contains("\"conflicted\":true"), "{json}"); |
| 75167672 Andy McCall |
3000 |
assert!( |
| 3001 |
json.contains("\"kind\":\"text\",\"path\":\"a.txt\""), |
| 3002 |
"{json}" |
| 3003 |
); |
| 50a046e4 Andy McCall ↶ |
3004 |
assert!(json.contains(&ida) && json.contains(&idb), "{json}"); |
| 3005 |
assert!(json.contains("\"resolved\":false"), "{json}"); |
| 3006 |
assert!(json.starts_with('{') && json.ends_with('}')); |
| 3007 |
|
| 3008 |
write(root, "a.txt", b"one\nleft\nright\n"); |
| 3009 |
let snap = repo.snapshot("status").unwrap(); |
| 3010 |
let json = repo.status_json(&snap).unwrap(); |
| 3011 |
assert!(json.contains("\"resolved\":true"), "{json}"); |
| 3012 |
repo.commit("resolve", &[]).unwrap(); |
| 3013 |
let snap = repo.snapshot("status").unwrap(); |
| 3014 |
let json = repo.status_json(&snap).unwrap(); |
| 75167672 Andy McCall |
3015 |
assert!( |
| 3016 |
json.contains("\"clean\":true,\"conflicted\":false,\"paths\":[],\"conflicts\":[]"), |
| 3017 |
"{json}" |
| 3018 |
); |
| 50a046e4 Andy McCall ↶ |
3019 |
} |
| 3020 |
|
| 9ac1fbe2 Andy McCall ↶ |
3021 |
#[test] |
| 3022 |
fn ignore_rules_do_not_untrack_files() { |
| 3023 |
hermetic(); |
| 3024 |
let dir = tempfile::tempdir().unwrap(); |
| 3025 |
let root = dir.path(); |
| 3026 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3027 |
write(root, "src.c", b"int x;\n"); |
| 3028 |
write(root, "build/out.o", b"\0obj"); |
| 3029 |
write(root, "sub/keep.log", b"keep\n"); |
| 3030 |
write(root, "sub/junk.log", b"junk\n"); |
| 3031 |
write(root, ".oxyignore", b"/build\n*.log\n!sub/keep.log\n"); |
| 3032 |
let snap = repo.snapshot("status").unwrap(); |
| 3033 |
let added: Vec<&str> = snap.statuses.iter().map(|s| s.path.as_str()).collect(); |
| 75167672 Andy McCall |
3034 |
assert_eq!( |
| 3035 |
added, |
| 3036 |
vec![".oxyignore", "src.c", "sub/keep.log"], |
| 3037 |
"anchored, glob, and negated rules" |
| 3038 |
); |
| 9ac1fbe2 Andy McCall ↶ |
3039 |
let h1 = repo.commit("base", &[]).unwrap(); |
| 3040 |
|
| 3041 |
write(root, ".oxyignore", b"/build\n*.log\n!sub/keep.log\nsrc.c\n"); |
| 3042 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3043 |
assert_eq!( |
| 3044 |
snap.statuses |
| 3045 |
.iter() |
| 3046 |
.map(|s| s.path.as_str()) |
| 3047 |
.collect::<Vec<_>>(), |
| 3048 |
vec![".oxyignore"] |
| 3049 |
); |
| 9ac1fbe2 Andy McCall ↶ |
3050 |
let h2 = repo.commit("ignore src.c", &[]).unwrap(); |
| 75167672 Andy McCall |
3051 |
assert!(repo |
| 3052 |
.load_state(&[h2]) |
| 3053 |
.unwrap() |
| 3054 |
.materialize() |
| 3055 |
.files |
| 3056 |
.contains_key("src.c")); |
| 9ac1fbe2 Andy McCall ↶ |
3057 |
|
| 3058 |
assert_eq!(repo.quick_dirty().unwrap(), Some(false)); |
| 3059 |
write(root, "src.c", b"int x, y;\n"); |
| 3060 |
assert_eq!(repo.quick_dirty().unwrap(), Some(true)); |
| 3061 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3062 |
assert_eq!( |
| 3063 |
snap.statuses |
| 3064 |
.iter() |
| 3065 |
.map(|s| (s.path.as_str(), s.status)) |
| 3066 |
.collect::<Vec<_>>(), |
| 3067 |
vec![("src.c", ChangeStatus::Modified)] |
| 3068 |
); |
| 9ac1fbe2 Andy McCall ↶ |
3069 |
let h3 = repo.commit("edit src.c", &[]).unwrap(); |
| 75167672 Andy McCall |
3070 |
assert!( |
| 3071 |
matches!(&repo.load_state(&[h3]).unwrap().materialize().files["src.c"].content, FileContent::Text { bytes, .. } if bytes == b"int x, y;\n") |
| 3072 |
); |
| 9ac1fbe2 Andy McCall ↶ |
3073 |
|
| 3074 |
fs::remove_file(root.join("src.c")).unwrap(); |
| 3075 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3076 |
assert_eq!( |
| 3077 |
snap.statuses |
| 3078 |
.iter() |
| 3079 |
.map(|s| (s.path.as_str(), s.status)) |
| 3080 |
.collect::<Vec<_>>(), |
| 3081 |
vec![("src.c", ChangeStatus::Removed)] |
| 3082 |
); |
| 9ac1fbe2 Andy McCall ↶ |
3083 |
repo.checkout(&[h3], true, "checkout").unwrap(); |
| 3084 |
assert_eq!(fs::read(root.join("src.c")).unwrap(), b"int x, y;\n"); |
| 3085 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3086 |
let _ = h1; |
| 3087 |
} |
| 3088 |
|
| b837d2b5 Andy McCall ↶ |
3089 |
#[test] |
| 3090 |
fn split_works_on_non_heads_by_node() { |
| 3091 |
hermetic(); |
| 3092 |
let dir = tempfile::tempdir().unwrap(); |
| 3093 |
let root = dir.path(); |
| 3094 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3095 |
write(root, "a.txt", b"one\n"); |
| 3096 |
write(root, "junk/x.lock", b"lock\n"); |
| 3097 |
let h1 = repo.commit("work plus junk", &[]).unwrap(); |
| 3098 |
write(root, "a.txt", b"one\ntwo\n"); |
| 3099 |
let h2 = repo.commit("more work", &[]).unwrap(); |
| 3100 |
assert_eq!(repo.store().get_change(&h2).unwrap().deps, vec![h1]); |
| 3101 |
let id1 = repo.store().get_change(&h1).unwrap().change_id; |
| 3102 |
|
| 3103 |
let (hj, h1b) = repo.split(&h1, &["junk".into()]).unwrap(); |
| 3104 |
assert_ne!(h1b, h1); |
| 75167672 Andy McCall |
3105 |
assert_eq!( |
| 3106 |
repo.store().get_change(&h1b).unwrap().change_id, |
| 3107 |
id1, |
| 3108 |
"the rest keeps the ChangeId" |
| 3109 |
); |
| b837d2b5 Andy McCall ↶ |
3110 |
let junk = repo.store().get_change(&hj).unwrap(); |
| 75167672 Andy McCall |
3111 |
assert!(junk.tree_ops.iter().all( |
| 3112 |
|op| matches!(op, TreeOp::AddNode { name, .. } if name == "junk" || name == "x.lock") |
| 3113 |
)); |
| b837d2b5 Andy McCall ↶ |
3114 |
let heads = repo.bookmark_heads().unwrap(); |
| 75167672 Andy McCall |
3115 |
assert!( |
| 3116 |
heads.contains(&hj), |
| 3117 |
"the split-off part is a head of its own" |
| 3118 |
); |
| b837d2b5 Andy McCall ↶ |
3119 |
let snap = repo.load_state(&heads).unwrap().materialize(); |
| 75167672 Andy McCall |
3120 |
assert!( |
| 3121 |
matches!(&snap.files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"one\ntwo\n"), |
| 3122 |
"the dependent was rewritten onto the new hash" |
| 3123 |
); |
| b837d2b5 Andy McCall ↶ |
3124 |
assert!(snap.files.contains_key("junk/x.lock")); |
| 75167672 Andy McCall |
3125 |
let h2b = heads |
| 3126 |
.iter() |
| 3127 |
.find(|h| repo.store().get_change(h).unwrap().message == "more work") |
| 3128 |
.copied() |
| 3129 |
.unwrap(); |
| b837d2b5 Andy McCall ↶ |
3130 |
assert_eq!(repo.store().get_change(&h2b).unwrap().deps, vec![h1b]); |
| 3131 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3132 |
|
| 3133 |
repo.abandon(&hj).unwrap(); |
| 3134 |
let heads = repo.bookmark_heads().unwrap(); |
| 3135 |
let snap = repo.load_state(&heads).unwrap().materialize(); |
| 3136 |
assert!(!snap.files.contains_key("junk/x.lock") && !snap.files.contains_key("junk")); |
| 3137 |
assert!(snap.files.contains_key("a.txt")); |
| 3138 |
|
| 3139 |
write(root, "junk/y.lock", b"y\n"); |
| 3140 |
let hb = repo.commit("junk base", &[]).unwrap(); |
| 3141 |
write(root, "junk/y.lock", b"yy\n"); |
| 3142 |
write(root, "b.txt", b"b\n"); |
| 3143 |
repo.commit("touches junk", &[]).unwrap(); |
| 3144 |
let err = repo.split(&hb, &["junk".into()]).unwrap_err().to_string(); |
| 3145 |
assert!(err.contains("depends on the split-off paths"), "{err}"); |
| 3146 |
} |
| 3147 |
|
| 7b9809e6 Andy McCall ↶ |
3148 |
#[test] |
| 3149 |
fn bookmarks_switch_merge_and_push() { |
| 3150 |
hermetic(); |
| 3151 |
let dir = tempfile::tempdir().unwrap(); |
| 3152 |
let root = dir.path(); |
| 3153 |
let origin = Repo::init(&root.join("origin")).unwrap(); |
| 3154 |
let url = root.join("origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
3155 |
let mut repo = Repo::clone(&url, &root.join("r")) |
| 3156 |
.unwrap() |
| 3157 |
.with_home(root.join("home")); |
| 7b9809e6 Andy McCall ↶ |
3158 |
let r = root.join("r"); |
| 3159 |
write(&r, "a.txt", b"a\n"); |
| 3160 |
let h_a = repo.commit("a", &[]).unwrap(); |
| 3161 |
assert!(repo.push("origin", false).unwrap()[0].ok); |
| 3162 |
|
| 3163 |
|
| 3164 |
assert!(repo.bookmark_create("bad name..", None).is_err()); |
| 3165 |
assert!(repo.bookmark_create("-x", None).is_err()); |
| 3166 |
assert_eq!(repo.bookmark_create("feature", None).unwrap(), vec![h_a]); |
| 3167 |
assert!(repo.bookmark_create("feature", None).is_err(), "exists"); |
| 3168 |
repo.bookmark_switch("feature", false).unwrap(); |
| 3169 |
assert_eq!(repo.config.bookmark, "feature"); |
| 3170 |
write(&r, "a.txt", b"a\nb\n"); |
| 3171 |
write(&r, "b.txt", b"b\n"); |
| 3172 |
let h_b = repo.commit("b on feature", &[]).unwrap(); |
| 75167672 Andy McCall |
3173 |
assert_eq!( |
| 3174 |
repo.bookmark_heads().unwrap(), |
| 3175 |
vec![h_b], |
| 3176 |
"b edits a.txt, so it depends on a" |
| 3177 |
); |
| 3178 |
assert_eq!( |
| 3179 |
repo.resolve_heads("main").unwrap(), |
| 3180 |
vec![h_a], |
| 3181 |
"main is untouched" |
| 3182 |
); |
| 3183 |
assert_eq!( |
| 3184 |
repo.resolve_change("feature").unwrap(), |
| 3185 |
h_b, |
| 3186 |
"a one-head bookmark names its change" |
| 3187 |
); |
| 7b9809e6 Andy McCall ↶ |
3188 |
|
| 3189 |
let reopened = Repo::open(&r).unwrap().with_home(root.join("home")); |
| 3190 |
assert_eq!(reopened.config.bookmark, "feature"); |
| 3191 |
|
| 3192 |
repo.bookmark_switch("main", false).unwrap(); |
| 3193 |
assert!(!r.join("b.txt").exists()); |
| 3194 |
assert_eq!(fs::read(r.join("a.txt")).unwrap(), b"a\n"); |
| 3195 |
write(&r, "c.txt", b"c\n"); |
| 3196 |
let h_c = repo.commit("c on main", &[]).unwrap(); |
| 3197 |
let list = repo.bookmarks().unwrap(); |
| 75167672 Andy McCall |
3198 |
assert_eq!( |
| 3199 |
list.iter() |
| 3200 |
.map(|b| (b.name.as_str(), b.current)) |
| 3201 |
.collect::<Vec<_>>(), |
| 3202 |
vec![("feature", false), ("main", true)] |
| 3203 |
); |
| 3204 |
assert_eq!( |
| 3205 |
list.iter().find(|b| b.name == "main").unwrap().ahead, |
| 3206 |
Some(1), |
| 3207 |
"c is not on origin yet" |
| 3208 |
); |
| 7b9809e6 Andy McCall ↶ |
3209 |
|
| 3210 |
let m = repo.merge("feature").unwrap(); |
| 3211 |
assert!(m.changed && m.checked_out); |
| 3212 |
let mut want = vec![h_b, h_c]; |
| 3213 |
want.sort(); |
| 3214 |
assert_eq!(m.heads, want); |
| 3215 |
assert_eq!(fs::read(r.join("b.txt")).unwrap(), b"b\n"); |
| 3216 |
assert!(!repo.merge("feature").unwrap().changed, "already merged"); |
| 3217 |
assert!(repo.merge("main").is_err()); |
| 3218 |
|
| 3219 |
assert!(repo.push_bookmark("origin", "feature", false).unwrap()[0].ok); |
| 3220 |
assert_eq!(origin.resolve_heads("feature").unwrap(), vec![h_b]); |
| 3221 |
assert!(repo.push("origin", false).unwrap()[0].ok); |
| 75167672 Andy McCall |
3222 |
assert_eq!( |
| 3223 |
repo.bookmarks() |
| 3224 |
.unwrap() |
| 3225 |
.iter() |
| 3226 |
.find(|b| b.name == "main") |
| 3227 |
.unwrap() |
| 3228 |
.ahead, |
| 3229 |
Some(0) |
| 3230 |
); |
| 7b9809e6 Andy McCall ↶ |
3231 |
|
| 3232 |
let ws = repo.workspace_add(&root.join("ws"), None).unwrap(); |
| 3233 |
assert_eq!(ws.config.bookmark, "main"); |
| 3234 |
let mut ws = ws; |
| 3235 |
ws.bookmark_switch("feature", false).unwrap(); |
| 75167672 Andy McCall |
3236 |
assert_eq!( |
| 3237 |
Repo::open(&r).unwrap().config.bookmark, |
| 3238 |
"main", |
| 3239 |
"the first workspace is unaffected" |
| 3240 |
); |
| 3241 |
assert!( |
| 3242 |
repo.bookmark_delete("feature").is_err(), |
| 3243 |
"current bookmark of another workspace" |
| 3244 |
); |
| 7b9809e6 Andy McCall ↶ |
3245 |
ws.bookmark_switch("main", false).unwrap(); |
| 3246 |
|
| 3247 |
assert_eq!(repo.bookmark_set("feature", &[h_a]).unwrap(), vec![h_a]); |
| 3248 |
assert!(repo.bookmark_delete("main").is_err(), "current here"); |
| 3249 |
repo.bookmark_delete("feature").unwrap(); |
| 3250 |
assert!(repo.resolve_heads("feature").is_err()); |
| 3251 |
assert!(repo.bookmark_set("feature", &[h_a]).is_err()); |
| 75167672 Andy McCall |
3252 |
assert!( |
| 3253 |
repo.checkout(&[h_a], false, "checkout").is_ok(), |
| 3254 |
"changes of a deleted bookmark remain" |
| 3255 |
); |
| 7b9809e6 Andy McCall ↶ |
3256 |
} |
| 3257 |
|
| 4dee277c Andy McCall ↶ |
3258 |
#[test] |
| 3259 |
fn oxygen_subrepos_pin_follow_and_update() { |
| 3260 |
hermetic(); |
| 3261 |
let dir = tempfile::tempdir().unwrap(); |
| 3262 |
let root = dir.path(); |
| 3263 |
|
| 3264 |
let lib_origin = Repo::init(&root.join("lib-origin")).unwrap(); |
| 3265 |
let lib_url = root.join("lib-origin").to_string_lossy().to_string(); |
| 75167672 Andy McCall |
3266 |
let lib = Repo::clone(&lib_url, &root.join("lib")) |
| 3267 |
.unwrap() |
| 3268 |
.with_home(root.join("home")); |
| 4dee277c Andy McCall ↶ |
3269 |
write(&root.join("lib"), "lib.txt", b"v1\n"); |
| 3270 |
let l1 = lib.commit("lib v1", &[]).unwrap(); |
| 3271 |
assert!(lib.push("origin", false).unwrap()[0].ok); |
| 3272 |
let _ = lib_origin; |
| 3273 |
|
| 3274 |
|
| 3275 |
let app_origin = Repo::init(&root.join("app-origin")).unwrap(); |
| 3276 |
let app_url = root.join("app-origin").to_string_lossy().to_string(); |
| 3277 |
let _ = app_origin; |
| 3278 |
let app_dir = root.join("app"); |
| 75167672 Andy McCall |
3279 |
let app = Repo::clone(&app_url, &app_dir) |
| 3280 |
.unwrap() |
| 3281 |
.with_home(root.join("home")); |
| 4dee277c Andy McCall ↶ |
3282 |
write(&app_dir, "main.txt", b"app\n"); |
| 3283 |
let pin = app.submodule_add(&lib_url, "libs/lib", None).unwrap(); |
| 3284 |
assert!(matches!(&pin, crate::subrepo::Pin::Oxy { heads, .. } if heads == &vec![l1])); |
| 3285 |
assert!(app_dir.join(".oxymodules").exists() && app_dir.join("libs/lib/.oxy").is_dir()); |
| 3286 |
let snap = app.snapshot("status").unwrap(); |
| 3287 |
let paths: Vec<&str> = snap.statuses.iter().map(|s| s.path.as_str()).collect(); |
| 75167672 Andy McCall |
3288 |
assert_eq!( |
| 3289 |
paths, |
| 3290 |
vec![".oxymodules", "libs/lib", "main.txt"], |
| 3291 |
"the nested files are not walked" |
| 3292 |
); |
| 4dee277c Andy McCall ↶ |
3293 |
let a1 = app.commit("app with lib", &[]).unwrap(); |
| 3294 |
let files = app.load_state(&[a1]).unwrap().materialize().files; |
| 3295 |
assert_eq!(files["libs/lib"].kind, NodeKind::Subrepo); |
| 3296 |
assert!(!files.contains_key("libs/lib/lib.txt")); |
| 3297 |
assert!(!app.snapshot("status").unwrap().changed); |
| 3298 |
let subs = app.subrepos().unwrap(); |
| 3299 |
assert_eq!(subs.len(), 1); |
| 3300 |
assert_eq!(subs[0].actual.as_ref(), Some(&subs[0].pin)); |
| 3301 |
|
| 3302 |
|
| 75167672 Andy McCall |
3303 |
let nested = Repo::open(&app_dir.join("libs/lib")) |
| 3304 |
.unwrap() |
| 3305 |
.with_home(root.join("home")); |
| 4dee277c Andy McCall ↶ |
3306 |
write(&app_dir.join("libs/lib"), "lib.txt", b"v2\n"); |
| 3307 |
let l2 = nested.commit("lib v2", &[]).unwrap(); |
| 3308 |
assert!(nested.push("origin", false).unwrap()[0].ok); |
| 3309 |
let snap = app.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3310 |
assert_eq!( |
| 3311 |
snap.statuses |
| 3312 |
.iter() |
| 3313 |
.map(|s| (s.path.as_str(), s.status)) |
| 3314 |
.collect::<Vec<_>>(), |
| 3315 |
vec![("libs/lib", ChangeStatus::Modified)] |
| 3316 |
); |
| 4dee277c Andy McCall ↶ |
3317 |
let a2 = app.commit("bump lib", &[]).unwrap(); |
| 3318 |
|
| 3319 |
let r = app.checkout(&[a1], false, "checkout").unwrap(); |
| 3320 |
assert!(r.notes.is_empty(), "{:?}", r.notes); |
| 3321 |
assert_eq!(fs::read(app_dir.join("libs/lib/lib.txt")).unwrap(), b"v1\n"); |
| 75167672 Andy McCall |
3322 |
assert_eq!( |
| 3323 |
Repo::open(&app_dir.join("libs/lib")) |
| 3324 |
.unwrap() |
| 3325 |
.bookmark_heads() |
| 3326 |
.unwrap(), |
| 3327 |
vec![l1] |
| 3328 |
); |
| 4dee277c Andy McCall ↶ |
3329 |
assert!(!app.snapshot("status").unwrap().changed); |
| 3330 |
|
| 3331 |
write(&app_dir.join("libs/lib"), "scratch.txt", b"wip\n"); |
| 3332 |
let r = app.checkout(&[a2], false, "checkout").unwrap(); |
| 75167672 Andy McCall |
3333 |
assert!( |
| 3334 |
r.notes.iter().any(|n| n.contains("uncommitted")), |
| 3335 |
"{:?}", |
| 3336 |
r.notes |
| 3337 |
); |
| 4dee277c Andy McCall ↶ |
3338 |
assert_eq!(fs::read(app_dir.join("libs/lib/lib.txt")).unwrap(), b"v1\n"); |
| 3339 |
fs::remove_file(app_dir.join("libs/lib/scratch.txt")).unwrap(); |
| 3340 |
let r = app.submodule_update(false, &[]).unwrap(); |
| 3341 |
assert_eq!(r, vec![("libs/lib".to_string(), SubrepoSync::Updated)]); |
| 3342 |
assert_eq!(fs::read(app_dir.join("libs/lib/lib.txt")).unwrap(), b"v2\n"); |
| 3343 |
let _ = l2; |
| 3344 |
|
| 3345 |
|
| 3346 |
assert!(app.push("origin", false).unwrap()[0].ok); |
| 3347 |
let clone_dir = root.join("app-clone"); |
| 75167672 Andy McCall |
3348 |
let clone = Repo::clone(&app_url, &clone_dir) |
| 3349 |
.unwrap() |
| 3350 |
.with_home(root.join("home")); |
| 4dee277c Andy McCall ↶ |
3351 |
assert!(!clone_dir.join("libs/lib/.oxy").exists()); |
| 75167672 Andy McCall |
3352 |
assert_eq!( |
| 3353 |
clone.submodule_update(false, &[]).unwrap(), |
| 3354 |
vec![("libs/lib".to_string(), SubrepoSync::Missing)] |
| 3355 |
); |
| 3356 |
assert_eq!( |
| 3357 |
clone.submodule_update(true, &[]).unwrap(), |
| 3358 |
vec![("libs/lib".to_string(), SubrepoSync::Updated)] |
| 3359 |
); |
| 3360 |
assert_eq!( |
| 3361 |
fs::read(clone_dir.join("libs/lib/lib.txt")).unwrap(), |
| 3362 |
b"v2\n" |
| 3363 |
); |
| 4dee277c Andy McCall ↶ |
3364 |
assert!(!clone.snapshot("status").unwrap().changed); |
| 3365 |
|
| 3366 |
clone.submodule_remove("libs/lib").unwrap(); |
| 3367 |
let snap = clone.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3368 |
assert!( |
| 3369 |
snap.statuses |
| 3370 |
.iter() |
| 3371 |
.any(|s| s.path == "libs/lib" && s.status == ChangeStatus::Removed), |
| 3372 |
"{:?}", |
| 3373 |
snap.statuses |
| 3374 |
); |
| 4dee277c Andy McCall ↶ |
3375 |
let c = clone.commit("drop lib", &[]).unwrap(); |
| 75167672 Andy McCall |
3376 |
assert!(!clone |
| 3377 |
.load_state(&[c]) |
| 3378 |
.unwrap() |
| 3379 |
.materialize() |
| 3380 |
.files |
| 3381 |
.contains_key("libs/lib")); |
| 4dee277c Andy McCall ↶ |
3382 |
assert!(clone_dir.join("libs/lib/lib.txt").exists()); |
| 75167672 Andy McCall |
3383 |
assert!( |
| 3384 |
!clone.snapshot("status").unwrap().changed, |
| 3385 |
"an unregistered nested repository is ignored" |
| 3386 |
); |
| 4dee277c Andy McCall ↶ |
3387 |
} |
| 3388 |
|
| 3389 |
#[test] |
| 3390 |
fn git_submodules_import_and_export_as_gitlinks() { |
| 3391 |
hermetic(); |
| 75167672 Andy McCall |
3392 |
if std::process::Command::new("git") |
| 3393 |
.arg("--version") |
| 3394 |
.output() |
| 3395 |
.is_err() |
| 3396 |
{ |
| 4dee277c Andy McCall ↶ |
3397 |
eprintln!("git not installed; skipping"); |
| 3398 |
return; |
| 3399 |
} |
| 3400 |
let dir = tempfile::tempdir().unwrap(); |
| 3401 |
let root = dir.path(); |
| 3402 |
let git_in = |d: &Path, args: &[&str]| -> String { |
| 3403 |
let out = std::process::Command::new("git") |
| 3404 |
.arg("-C") |
| 3405 |
.arg(d) |
| 75167672 Andy McCall |
3406 |
.args([ |
| 3407 |
"-c", |
| 3408 |
"user.name=Grace", |
| 3409 |
"-c", |
| 3410 |
"[email protected]", |
| 3411 |
"-c", |
| 3412 |
"commit.gpgsign=false", |
| 3413 |
"-c", |
| 3414 |
"protocol.file.allow=always", |
| 3415 |
]) |
| 4dee277c Andy McCall ↶ |
3416 |
.args(args) |
| 3417 |
.output() |
| 3418 |
.unwrap(); |
| 75167672 Andy McCall |
3419 |
assert!( |
| 3420 |
out.status.success(), |
| 3421 |
"git {args:?}: {}", |
| 3422 |
String::from_utf8_lossy(&out.stderr) |
| 3423 |
); |
| 4dee277c Andy McCall ↶ |
3424 |
String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 3425 |
}; |
| 3426 |
let sub = root.join("sub"); |
| 3427 |
fs::create_dir_all(&sub).unwrap(); |
| 3428 |
git_in(&sub, &["init", "-q", "-b", "main"]); |
| 3429 |
write(&sub, "s.txt", b"sub\n"); |
| 3430 |
git_in(&sub, &["add", "."]); |
| 3431 |
git_in(&sub, &["commit", "-q", "-m", "sub initial"]); |
| 3432 |
let sub_sha = git_in(&sub, &["rev-parse", "HEAD"]); |
| 3433 |
let g = root.join("main.git"); |
| 3434 |
fs::create_dir_all(&g).unwrap(); |
| 3435 |
git_in(&g, &["init", "-q", "-b", "main"]); |
| 3436 |
write(&g, "README.md", b"# app\n"); |
| 3437 |
git_in(&g, &["add", "."]); |
| 3438 |
git_in(&g, &["commit", "-q", "-m", "initial"]); |
| 75167672 Andy McCall |
3439 |
git_in( |
| 3440 |
&g, |
| 3441 |
&[ |
| 3442 |
"submodule", |
| 3443 |
"add", |
| 3444 |
"-q", |
| 3445 |
sub.to_str().unwrap(), |
| 3446 |
"vendor/sub", |
| 3447 |
], |
| 3448 |
); |
| 4dee277c Andy McCall ↶ |
3449 |
git_in(&g, &["commit", "-q", "-m", "add submodule"]); |
| 3450 |
|
| 3451 |
let dest = root.join("oxy"); |
| 3452 |
let repo = Repo::init(&dest).unwrap().with_home(root.join("home")); |
| 3453 |
let rep = repo.import_git(&g, "main").unwrap(); |
| 3454 |
assert_eq!((rep.commits, rep.submodules), (2, 1)); |
| 75167672 Andy McCall |
3455 |
let files = repo |
| 3456 |
.load_state(&repo.bookmark_heads().unwrap()) |
| 3457 |
.unwrap() |
| 3458 |
.materialize() |
| 3459 |
.files; |
| 4dee277c Andy McCall ↶ |
3460 |
assert_eq!(files["vendor/sub"].kind, NodeKind::Subrepo); |
| 75167672 Andy McCall |
3461 |
let FileContent::Opaque { blobs } = &files["vendor/sub"].content else { |
| 3462 |
panic!() |
| 3463 |
}; |
| 3464 |
assert_eq!( |
| 3465 |
crate::subrepo::Pin::decode(&repo.read_blob(&blobs[0]).unwrap()).unwrap(), |
| 3466 |
crate::subrepo::Pin::Git { |
| 3467 |
sha: sub_sha.clone(), |
| 3468 |
branch: "HEAD".into() |
| 3469 |
} |
| 3470 |
); |
| 3471 |
let modules = |
| 3472 |
crate::subrepo::Modules::parse(&fs::read_to_string(dest.join(".oxymodules")).unwrap()); |
| 4dee277c Andy McCall ↶ |
3473 |
assert_eq!(modules.entries["vendor/sub"].url, sub.to_string_lossy()); |
| 3474 |
assert!(modules.entries["vendor/sub"].git); |
| 75167672 Andy McCall |
3475 |
assert!( |
| 3476 |
files.contains_key(".gitmodules"), |
| 3477 |
"the Git file is kept for round trips" |
| 3478 |
); |
| 3479 |
assert!( |
| 3480 |
!dest.join("vendor/sub/.git").exists(), |
| 3481 |
"not cloned until update --init" |
| 3482 |
); |
| 4dee277c Andy McCall ↶ |
3483 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3484 |
|
| 75167672 Andy McCall |
3485 |
assert_eq!( |
| 3486 |
repo.submodule_update(true, &[]).unwrap(), |
| 3487 |
vec![("vendor/sub".to_string(), SubrepoSync::Updated)] |
| 3488 |
); |
| 3489 |
assert_eq!( |
| 3490 |
git_in(&dest.join("vendor/sub"), &["rev-parse", "HEAD"]), |
| 3491 |
sub_sha |
| 3492 |
); |
| 4dee277c Andy McCall ↶ |
3493 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3494 |
|
| 3495 |
let mirror = root.join("mirror.git"); |
| 3496 |
let rep = repo.git_export(&mirror).unwrap(); |
| 3497 |
assert_eq!(rep.skipped_subrepos, 0); |
| 3498 |
let ls = git_in(&mirror, &["ls-tree", "main", "vendor/sub"]); |
| 75167672 Andy McCall |
3499 |
assert!( |
| 3500 |
ls.starts_with("160000 commit ") && ls.contains(&sub_sha), |
| 3501 |
"{ls}" |
| 3502 |
); |
| 3503 |
assert_eq!( |
| 3504 |
git_in(&mirror, &["ls-tree", "--name-only", "main"]), |
| 3505 |
".gitmodules\nREADME.md\nvendor" |
| 3506 |
); |
| 4dee277c Andy McCall ↶ |
3507 |
let orig_tree = git_in(&g, &["rev-parse", "main^{tree}"]); |
| 75167672 Andy McCall |
3508 |
assert_eq!( |
| 3509 |
git_in(&mirror, &["rev-parse", "main^{tree}"]), |
| 3510 |
orig_tree, |
| 3511 |
"tree round trip" |
| 3512 |
); |
| 4dee277c Andy McCall ↶ |
3513 |
} |
| 3514 |
|
| 957fee30 Andy McCall ↶ |
3515 |
#[test] |
| 3516 |
fn partial_commits_by_path_and_by_hunk() { |
| 3517 |
hermetic(); |
| 3518 |
let dir = tempfile::tempdir().unwrap(); |
| 3519 |
let root = dir.path(); |
| 3520 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3521 |
write(root, "a.txt", b"1\n2\n3\n"); |
| 3522 |
write(root, "b.txt", b"b\n"); |
| 3523 |
let h0 = repo.commit("base", &[]).unwrap(); |
| 3524 |
write(root, "a.txt", b"0\n1\n2\n3\n4\n"); |
| 3525 |
write(root, "b.txt", b"b\nbb\n"); |
| 3526 |
write(root, "c.txt", b"c\n"); |
| 3527 |
|
| 75167672 Andy McCall |
3528 |
let h1 = repo |
| 3529 |
.commit_selection("a only", &[], &Selection::Paths(vec!["a.txt".into()])) |
| 3530 |
.unwrap(); |
| 957fee30 Andy McCall ↶ |
3531 |
let c1 = repo.store().get_change(&h1).unwrap(); |
| 75167672 Andy McCall |
3532 |
assert!( |
| 3533 |
c1.tree_ops.is_empty() && c1.edits.iter().all(|e| e.node() == c1.edits[0].node()), |
| 3534 |
"{:?}", |
| 3535 |
c1.tree_ops |
| 3536 |
); |
| 957fee30 Andy McCall ↶ |
3537 |
assert_eq!(c1.deps, vec![h0]); |
| 3538 |
let files = repo.load_state(&[h1]).unwrap().materialize().files; |
| 75167672 Andy McCall |
3539 |
assert!( |
| 3540 |
matches!(&files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"0\n1\n2\n3\n4\n") |
| 3541 |
); |
| 957fee30 Andy McCall ↶ |
3542 |
assert!(matches!(&files["b.txt"].content, FileContent::Text { bytes, .. } if bytes == b"b\n")); |
| 3543 |
assert!(!files.contains_key("c.txt")); |
| 3544 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3545 |
assert_eq!( |
| 3546 |
snap.statuses |
| 3547 |
.iter() |
| 3548 |
.map(|s| (s.path.as_str(), s.status)) |
| 3549 |
.collect::<Vec<_>>(), |
| 3550 |
vec![ |
| 3551 |
("b.txt", ChangeStatus::Modified), |
| 3552 |
("c.txt", ChangeStatus::Added) |
| 3553 |
] |
| 3554 |
); |
| 3555 |
assert_eq!( |
| 3556 |
fs::read(root.join("b.txt")).unwrap(), |
| 3557 |
b"b\nbb\n", |
| 3558 |
"disk untouched" |
| 3559 |
); |
| 957fee30 Andy McCall ↶ |
3560 |
|
| 3561 |
let (_, hunks) = repo.hunks().unwrap(); |
| 75167672 Andy McCall |
3562 |
let b_hunk = hunks |
| 3563 |
.iter() |
| 3564 |
.find(|h| h.path == "b.txt" && h.text.contains("+bb")) |
| 3565 |
.unwrap() |
| 3566 |
.index; |
| 3567 |
let h2 = repo |
| 3568 |
.commit_selection("b only", &[], &Selection::Hunks(vec![b_hunk])) |
| 3569 |
.unwrap(); |
| 957fee30 Andy McCall ↶ |
3570 |
let files = repo.load_state(&[h2]).unwrap().materialize().files; |
| 75167672 Andy McCall |
3571 |
assert!( |
| 3572 |
matches!(&files["b.txt"].content, FileContent::Text { bytes, .. } if bytes == b"b\nbb\n") |
| 3573 |
); |
| 957fee30 Andy McCall ↶ |
3574 |
assert!(!files.contains_key("c.txt")); |
| 3575 |
let snap = repo.snapshot("status").unwrap(); |
| 75167672 Andy McCall |
3576 |
assert_eq!( |
| 3577 |
snap.statuses |
| 3578 |
.iter() |
| 3579 |
.map(|s| s.path.as_str()) |
| 3580 |
.collect::<Vec<_>>(), |
| 3581 |
vec!["c.txt"] |
| 3582 |
); |
| 957fee30 Andy McCall ↶ |
3583 |
|
| 3584 |
|
| 3585 |
write(root, "d/e.txt", b"e\n"); |
| 3586 |
let (_, hunks) = repo.hunks().unwrap(); |
| 75167672 Andy McCall |
3587 |
let e_insert = hunks |
| 3588 |
.iter() |
| 3589 |
.find(|h| h.path == "d/e.txt" && h.text.contains("+e")) |
| 3590 |
.unwrap() |
| 3591 |
.index; |
| 3592 |
let h3 = repo |
| 3593 |
.commit_selection("e only", &[], &Selection::Hunks(vec![e_insert])) |
| 3594 |
.unwrap(); |
| 957fee30 Andy McCall ↶ |
3595 |
let c3 = repo.store().get_change(&h3).unwrap(); |
| 3596 |
assert_eq!(c3.tree_ops.len(), 2, "{:?}", c3.tree_ops); |
| 3597 |
let files = repo.load_state(&[h3]).unwrap().materialize().files; |
| 3598 |
assert!(files.contains_key("d/e.txt") && !files.contains_key("c.txt")); |
| d8c086ca Andy McCall ↶ |
3599 |
|
| 3600 |
write(root, "f/g.txt", b"g\n"); |
| 3601 |
let (_, hunks) = repo.hunks().unwrap(); |
| 75167672 Andy McCall |
3602 |
let g_add = hunks |
| 3603 |
.iter() |
| 3604 |
.find(|h| h.path == "f/g.txt" && h.text.starts_with("A ")) |
| 3605 |
.unwrap() |
| 3606 |
.index; |
| 3607 |
let h4 = repo |
| 3608 |
.commit_selection("g by addition", &[], &Selection::Hunks(vec![g_add])) |
| 3609 |
.unwrap(); |
| d8c086ca Andy McCall ↶ |
3610 |
let files = repo.load_state(&[h4]).unwrap().materialize().files; |
| 75167672 Andy McCall |
3611 |
assert!( |
| 3612 |
matches!(&files["f/g.txt"].content, FileContent::Text { bytes, .. } if bytes == b"g\n") |
| 3613 |
); |
| 3614 |
assert!(!repo |
| 3615 |
.snapshot("status") |
| 3616 |
.unwrap() |
| 3617 |
.statuses |
| 3618 |
.iter() |
| 3619 |
.any(|s| s.path == "f/g.txt")); |
| 957fee30 Andy McCall ↶ |
3620 |
|
| 3621 |
repo.commit("the rest", &[]).unwrap(); |
| 3622 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 75167672 Andy McCall |
3623 |
let files = repo |
| 3624 |
.load_state(&repo.bookmark_heads().unwrap()) |
| 3625 |
.unwrap() |
| 3626 |
.materialize() |
| 3627 |
.files; |
| 957fee30 Andy McCall ↶ |
3628 |
assert!(files.contains_key("c.txt")); |
| 3629 |
} |
| 3630 |
|
| 3631 |
#[test] |
| 3632 |
fn blame_attributes_lines_to_introducing_changes_across_renames() { |
| 3633 |
hermetic(); |
| 3634 |
let dir = tempfile::tempdir().unwrap(); |
| 3635 |
let root = dir.path(); |
| 3636 |
let mut repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 75167672 Andy McCall |
3637 |
repo.create_identity("Ada", "[email protected]", IdentityScope::User) |
| 3638 |
.unwrap(); |
| 957fee30 Andy McCall ↶ |
3639 |
write(root, "a.txt", b"one\n"); |
| 3640 |
let h1 = repo.commit("first", &[]).unwrap(); |
| 3641 |
write(root, "a.txt", b"one\ntwo\n"); |
| 3642 |
let h2 = repo.commit("second", &[]).unwrap(); |
| 3643 |
let b = repo.blame("a.txt").unwrap(); |
| 75167672 Andy McCall |
3644 |
assert_eq!( |
| 3645 |
b.iter() |
| 3646 |
.map(|l| (l.line, l.hash, l.text.as_slice())) |
| 3647 |
.collect::<Vec<_>>(), |
| 3648 |
vec![(1, h1, b"one\n".as_slice()), (2, h2, b"two\n".as_slice())] |
| 3649 |
); |
| 957fee30 Andy McCall ↶ |
3650 |
assert!(b.iter().all(|l| l.author == "Ada")); |
| 75167672 Andy McCall |
3651 |
assert_eq!( |
| 3652 |
b[0].change_id, |
| 3653 |
repo.store().get_change(&h1).unwrap().change_id |
| 3654 |
); |
| 957fee30 Andy McCall ↶ |
3655 |
fs::rename(root.join("a.txt"), root.join("b.txt")).unwrap(); |
| 3656 |
repo.commit("rename", &[]).unwrap(); |
| 3657 |
let b = repo.blame("b.txt").unwrap(); |
| 75167672 Andy McCall |
3658 |
assert_eq!( |
| 3659 |
b.iter().map(|l| l.hash).collect::<Vec<_>>(), |
| 3660 |
vec![h1, h2], |
| 3661 |
"the node survives the rename" |
| 3662 |
); |
| 957fee30 Andy McCall ↶ |
3663 |
assert!(repo.blame("a.txt").is_err()); |
| 3664 |
} |
| 3665 |
|
| 3666 |
#[test] |
| 3667 |
fn bisect_finds_the_first_bad_change() { |
| 3668 |
hermetic(); |
| 3669 |
let dir = tempfile::tempdir().unwrap(); |
| 3670 |
let root = dir.path(); |
| 3671 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3672 |
let mut content = String::new(); |
| 3673 |
let mut hashes = Vec::new(); |
| 3674 |
for i in 1..=8 { |
| 3675 |
content.push_str(&format!("line {i}{}\n", if i == 5 { " bug" } else { "" })); |
| 3676 |
write(root, "f.txt", content.as_bytes()); |
| 3677 |
hashes.push(repo.commit(&format!("step {i}"), &[]).unwrap()); |
| 3678 |
} |
| 3679 |
|
| 3680 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![hashes[7]]); |
| 3681 |
repo.bisect_start().unwrap(); |
| 75167672 Andy McCall |
3682 |
assert!(matches!( |
| 3683 |
repo.bisect_mark(Some(true), &[hashes[0]]).unwrap(), |
| 3684 |
BisectStep::NeedMarks { good: 1, bad: 0 } |
| 3685 |
)); |
| 957fee30 Andy McCall ↶ |
3686 |
let step = repo.bisect_mark(Some(false), &[hashes[7]]).unwrap(); |
| 75167672 Andy McCall |
3687 |
assert!( |
| 3688 |
matches!(step, BisectStep::Testing { remaining: 7, .. }), |
| 3689 |
"{step:?}" |
| 3690 |
); |
| 957fee30 Andy McCall ↶ |
3691 |
|
| 75167672 Andy McCall |
3692 |
let step = repo |
| 3693 |
.bisect_run(&["sh".into(), "-c".into(), "! grep -q bug f.txt".into()]) |
| 3694 |
.unwrap(); |
| 957fee30 Andy McCall ↶ |
3695 |
assert_eq!(step, BisectStep::Found { culprit: hashes[4] }); |
| 75167672 Andy McCall |
3696 |
assert_eq!( |
| 3697 |
repo.bisect_status().unwrap(), |
| 3698 |
Some(BisectStep::Found { culprit: hashes[4] }) |
| 3699 |
); |
| 957fee30 Andy McCall ↶ |
3700 |
repo.bisect_reset().unwrap(); |
| 3701 |
assert_eq!(fs::read_to_string(root.join("f.txt")).unwrap(), content); |
| 3702 |
assert!(repo.bisect_status().unwrap().is_none()); |
| 3703 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3704 |
|
| 3705 |
repo.bisect_start().unwrap(); |
| 3706 |
repo.bisect_mark(Some(true), &[hashes[0]]).unwrap(); |
| 3707 |
let mut step = repo.bisect_mark(Some(false), &[hashes[7]]).unwrap(); |
| 3708 |
let mut skipped_once = false; |
| 3709 |
let mut guard = 0; |
| 3710 |
while let BisectStep::Testing { .. } = step { |
| 3711 |
guard += 1; |
| 3712 |
assert!(guard < 20); |
| 3713 |
let text = fs::read_to_string(root.join("f.txt")).unwrap(); |
| 3714 |
if !skipped_once && text.contains("line 6") { |
| 3715 |
|
| 3716 |
skipped_once = true; |
| 3717 |
step = repo.bisect_mark(None, &[]).unwrap(); |
| 3718 |
continue; |
| 3719 |
} |
| 3720 |
step = repo.bisect_mark(Some(!text.contains("bug")), &[]).unwrap(); |
| 3721 |
} |
| 3722 |
assert!(skipped_once); |
| 3723 |
assert_eq!(step, BisectStep::Found { culprit: hashes[4] }); |
| 3724 |
repo.bisect_reset().unwrap(); |
| 3725 |
|
| 3726 |
repo.bisect_start().unwrap(); |
| 3727 |
repo.bisect_mark(Some(true), &[hashes[0]]).unwrap(); |
| 3728 |
let mut step = repo.bisect_mark(Some(false), &[hashes[7]]).unwrap(); |
| 3729 |
let mut guard = 0; |
| 3730 |
while let BisectStep::Testing { heads, .. } = &step { |
| 3731 |
guard += 1; |
| 3732 |
assert!(guard < 20); |
| 3733 |
let text = fs::read_to_string(root.join("f.txt")).unwrap(); |
| 75167672 Andy McCall |
3734 |
let is_culprit_state = |
| 3735 |
heads.contains(&hashes[4]) && !heads.iter().any(|h| hashes[5..].contains(h)); |
| 3736 |
step = if is_culprit_state { |
| 3737 |
repo.bisect_mark(None, &[]).unwrap() |
| 3738 |
} else { |
| 3739 |
repo.bisect_mark(Some(!text.contains("bug")), &[]).unwrap() |
| 3740 |
}; |
| 957fee30 Andy McCall ↶ |
3741 |
} |
| 3742 |
match step { |
| 75167672 Andy McCall |
3743 |
BisectStep::Inconclusive { candidates } => assert!( |
| 3744 |
candidates.contains(&hashes[4]) && candidates.len() <= 2, |
| 3745 |
"{candidates:?}" |
| 3746 |
), |
| 957fee30 Andy McCall ↶ |
3747 |
other => panic!("{other:?}"), |
| 3748 |
} |
| 3749 |
repo.bisect_reset().unwrap(); |
| 3750 |
} |
| 3751 |
|
| 728da0f9 Andy McCall ↶ |
3752 |
#[test] |
| 3753 |
fn squash_folds_a_change_into_its_dependency() { |
| 3754 |
hermetic(); |
| 3755 |
let dir = tempfile::tempdir().unwrap(); |
| 3756 |
let root = dir.path(); |
| 3757 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3758 |
write(root, "a.txt", b"1\n"); |
| 3759 |
let a = repo.commit("A", &[]).unwrap(); |
| 3760 |
write(root, "a.txt", b"1\n2\n"); |
| 3761 |
let c = repo.commit("C", &[]).unwrap(); |
| 3762 |
write(root, "a.txt", b"1\n2\n3\n"); |
| 3763 |
let e = repo.commit("E", &[]).unwrap(); |
| 3764 |
write(root, "b.txt", b"b\n"); |
| 3765 |
let b = repo.commit("B", &[]).unwrap(); |
| 3766 |
let id_a = repo.store().get_change(&a).unwrap().change_id; |
| 3767 |
let id_e = repo.store().get_change(&e).unwrap().change_id; |
| 3768 |
|
| 3769 |
let err = repo.squash(&e, &a).unwrap_err().to_string(); |
| 3770 |
assert!(err.contains(&c.short(12)), "{err}"); |
| 3771 |
|
| 3772 |
let a2 = repo.squash(&c, &a).unwrap(); |
| 3773 |
let a2c = repo.store().get_change(&a2).unwrap(); |
| 3774 |
assert_eq!(a2c.change_id, id_a); |
| 3775 |
assert!(a2c.deps.is_empty()); |
| 3776 |
let heads = repo.bookmark_heads().unwrap(); |
| 3777 |
let st = repo.load_state(&heads).unwrap(); |
| 3778 |
assert!(!st.contains(&c) && !st.contains(&a) && !st.contains(&e)); |
| 75167672 Andy McCall |
3779 |
let e2 = st |
| 3780 |
.changes() |
| 3781 |
.find(|(_, m)| m.change_id == id_e) |
| 3782 |
.map(|(h, _)| *h) |
| 3783 |
.unwrap(); |
| 3784 |
assert_eq!( |
| 3785 |
repo.store().get_change(&e2).unwrap().deps, |
| 3786 |
vec![a2], |
| 3787 |
"E now depends on A' alone" |
| 3788 |
); |
| 728da0f9 Andy McCall ↶ |
3789 |
let files = st.materialize().files; |
| 75167672 Andy McCall |
3790 |
assert!( |
| 3791 |
matches!(&files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"1\n2\n3\n") |
| 3792 |
); |
| 728da0f9 Andy McCall ↶ |
3793 |
assert!(files.contains_key("b.txt")); |
| 3794 |
assert!(st.contains(&b)); |
| 3795 |
assert!(!repo.snapshot("status").unwrap().changed); |
| 3796 |
assert_eq!(repo.log().unwrap().len(), 4, "working copy, B, E', A'"); |
| 3797 |
|
| 3798 |
repo.undo().unwrap(); |
| 3799 |
repo.undo().unwrap(); |
| 3800 |
let st = repo.load_state(&repo.bookmark_heads().unwrap()).unwrap(); |
| 3801 |
assert!(st.contains(&c) && st.contains(&a) && st.contains(&e)); |
| 3802 |
} |
| 3803 |
|
| 3804 |
#[test] |
| 3805 |
fn rebase_re_expresses_a_change_against_another_base() { |
| 3806 |
hermetic(); |
| 3807 |
let dir = tempfile::tempdir().unwrap(); |
| 3808 |
let root = dir.path(); |
| 3809 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3810 |
write(root, "a.txt", b"1\n2\n"); |
| 3811 |
let a = repo.commit("A", &[]).unwrap(); |
| 3812 |
write(root, "a.txt", b"1\n2\n3\n"); |
| 3813 |
let c = repo.commit("C: append 3", &[]).unwrap(); |
| 3814 |
write(root, "a.txt", b"1\n2\n3\n4\n"); |
| 3815 |
let e = repo.commit("E: append 4", &[]).unwrap(); |
| 3816 |
|
| 3817 |
repo.checkout(&[a], true, "checkout").unwrap(); |
| 3818 |
write(root, "a.txt", b"0\n1\n2\n"); |
| 3819 |
let d = repo.commit("D: prepend 0", &[]).unwrap(); |
| 3820 |
let id_c = repo.store().get_change(&c).unwrap().change_id; |
| 3821 |
let id_e = repo.store().get_change(&e).unwrap().change_id; |
| 3822 |
|
| 3823 |
let c2 = repo.rebase(&c, &[d]).unwrap(); |
| 3824 |
let c2c = repo.store().get_change(&c2).unwrap(); |
| 3825 |
assert_eq!(c2c.change_id, id_c); |
| 3826 |
assert!(c2c.deps.contains(&d), "{:?}", c2c.deps); |
| 3827 |
let heads = repo.bookmark_heads().unwrap(); |
| 3828 |
let st = repo.load_state(&heads).unwrap(); |
| 3829 |
assert!(!st.contains(&c) && st.contains(&d)); |
| 75167672 Andy McCall |
3830 |
let e2 = st |
| 3831 |
.changes() |
| 3832 |
.find(|(_, m)| m.change_id == id_e) |
| 3833 |
.map(|(h, _)| *h) |
| 3834 |
.unwrap(); |
| 728da0f9 Andy McCall ↶ |
3835 |
let e2deps = repo.store().get_change(&e2).unwrap().deps; |
| 75167672 Andy McCall |
3836 |
assert!( |
| 3837 |
e2deps.contains(&c2) && !e2deps.contains(&c) && e2deps.contains(&a), |
| 3838 |
"E follows C by content and still depends on A, which introduced the file: {e2deps:?}" |
| 3839 |
); |
| 728da0f9 Andy McCall ↶ |
3840 |
let files = st.materialize().files; |
| 75167672 Andy McCall |
3841 |
assert!( |
| 3842 |
matches!(&files["a.txt"].content, FileContent::Text { bytes, .. } if bytes == b"0\n1\n2\n3\n4\n"), |
| 3843 |
"{:?}", |
| 3844 |
files["a.txt"].content |
| 3845 |
); |
| 728da0f9 Andy McCall ↶ |
3846 |
assert!(!files["a.txt"].is_conflicted()); |
| 3847 |
|
| 3848 |
assert!(repo.rebase(&c2, &[c2]).is_err()); |
| 3849 |
let err = repo.rebase(&e2, &[d]).unwrap_err().to_string(); |
| 75167672 Andy McCall |
3850 |
assert!( |
| 3851 |
err.contains("lacks dependencies"), |
| 3852 |
"E needs C, which D's state does not have: {err}" |
| 3853 |
); |
| 728da0f9 Andy McCall ↶ |
3854 |
} |
| 3855 |
|
| c792033a Andy McCall ↶ |
3856 |
#[test] |
| 3857 |
fn unsigned_tags_keep_their_message() { |
| 3858 |
hermetic(); |
| 3859 |
let dir = tempfile::tempdir().unwrap(); |
| 3860 |
let root = dir.path(); |
| 3861 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 3862 |
write(root, "a.txt", b"a\n"); |
| 3863 |
repo.commit("a", &[]).unwrap(); |
| 3864 |
assert!(repo.secret_key().unwrap().is_none()); |
| 75167672 Andy McCall |
3865 |
repo.tag_create("v1", &repo.bookmark_heads().unwrap(), "first release") |
| 3866 |
.unwrap(); |
| c792033a Andy McCall ↶ |
3867 |
let tags = repo.tags().unwrap(); |
| 75167672 Andy McCall |
3868 |
assert_eq!( |
| 3869 |
( |
| 3870 |
tags[0].name.as_str(), |
| 3871 |
tags[0].message.as_str(), |
| 3872 |
tags[0].signer.as_deref() |
| 3873 |
), |
| 3874 |
("v1", "first release", None) |
| 3875 |
); |
| 3876 |
assert!( |
| 3877 |
repo.tag_refs().unwrap()[0].attestation.is_none(), |
| 3878 |
"an unsigned attestation is not advertised" |
| 3879 |
); |
| c792033a Andy McCall ↶ |
3880 |
} |
| 387335ff Andy McCall ↶ |
3881 |
|
| 3882 |
#[test] |
| 3883 |
fn fsck_passes_a_sound_repository_and_names_each_fault() { |
| 3884 |
hermetic(); |
| 3885 |
let dir = tempfile::tempdir().unwrap(); |
| 3886 |
let root = dir.path(); |
| 3887 |
let origin_path = root.join("origin"); |
| 3888 |
let origin = Repo::init(&origin_path).unwrap(); |
| 3889 |
let mut a = Repo::clone(&origin_path.to_string_lossy(), &root.join("a")) |
| 3890 |
.unwrap() |
| 3891 |
.with_home(root.join("home-a")); |
| 3892 |
a.create_identity("Ada", "[email protected]", IdentityScope::Repo) |
| 3893 |
.unwrap(); |
| 3894 |
write(&root.join("a"), "f.txt", b"one\n"); |
| 3895 |
write(&root.join("a"), "bin.dat", b"\0\x01\x02binary"); |
| 3896 |
let h1 = a.commit("first", &[]).unwrap(); |
| 3897 |
write(&root.join("a"), "f.txt", b"one\ntwo\n"); |
| 3898 |
let h2 = a.commit("second", &[]).unwrap(); |
| 3899 |
a.tag_create("v1", &[h2], "first release").unwrap(); |
| 3900 |
a.checkpoint().unwrap(); |
| 3901 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 3902 |
|
| 3903 |
|
| 3904 |
for repo in [&a, &origin] { |
| 3905 |
let rep = repo.fsck().unwrap(); |
| 3906 |
assert_eq!(rep.problems, vec![], "{rep:?}"); |
| 3907 |
|
| 3908 |
assert!(rep.changes >= 2 && rep.signed >= 2, "{rep:?}"); |
| 3909 |
assert!(rep.ops > 1 && rep.objects > 5); |
| 3910 |
} |
| 3911 |
assert!(origin.fsck().unwrap().attestations >= 1); |
| 3912 |
|
| 3913 |
|
| 3914 |
let c1 = a.store().path_for(&h1); |
| 3915 |
let pristine = fs::read(&c1).unwrap(); |
| 3916 |
let mut bytes = pristine.clone(); |
| 3917 |
*bytes.last_mut().unwrap() ^= 1; |
| 3918 |
fs::write(&c1, &bytes).unwrap(); |
| 3919 |
let rep = a.fsck().unwrap(); |
| 3920 |
assert!(rep.errors() >= 1); |
| 3921 |
assert!( |
| 3922 |
rep.problems |
| 3923 |
.iter() |
| 3924 |
.any(|p| p.object == Some(h1) && p.what.contains("unreadable")), |
| 3925 |
"{rep:?}" |
| 3926 |
); |
| 3927 |
fs::write(&c1, &pristine).unwrap(); |
| 3928 |
assert_eq!(a.fsck().unwrap().errors(), 0); |
| 3929 |
|
| 3930 |
|
| 3931 |
fs::remove_file(&c1).unwrap(); |
| 3932 |
let rep = a.fsck().unwrap(); |
| 3933 |
assert!( |
| 3934 |
rep.problems.iter().any(|p| p.severity == Severity::Error |
| 3935 |
&& p.object == Some(h1) |
| 3936 |
&& p.what.contains("change missing, needed by change")), |
| 3937 |
"{rep:?}" |
| 3938 |
); |
| 3939 |
fs::write(&c1, &pristine).unwrap(); |
| 3940 |
|
| 3941 |
|
| 3942 |
let c2 = a.store().get_change(&h1).unwrap(); |
| 3943 |
let blob = c2 |
| 3944 |
.edits |
| 3945 |
.iter() |
| 3946 |
.find_map(|e| match e { |
| 3947 |
Edit::Replace { new: Some(b), .. } => Some(*b), |
| 3948 |
_ => None, |
| 3949 |
}) |
| 3950 |
.expect("the binary file is a blob"); |
| 3951 |
let bp = a.store().path_for(&blob); |
| 3952 |
let blob_bytes = fs::read(&bp).unwrap(); |
| 3953 |
fs::remove_file(&bp).unwrap(); |
| 3954 |
let rep = a.fsck().unwrap(); |
| 3955 |
assert!( |
| 3956 |
rep.problems |
| 3957 |
.iter() |
| 3958 |
.any(|p| p.object == Some(blob) && p.what.contains("blob missing")), |
| 3959 |
"{rep:?}" |
| 3960 |
); |
| 3961 |
fs::write(&bp, &blob_bytes).unwrap(); |
| 3962 |
|
| 3963 |
|
| 3964 |
let head = root.join("a/.oxy/ops/head"); |
| 3965 |
let saved = fs::read(&head).unwrap(); |
| 3966 |
fs::write(&head, b"not a hash").unwrap(); |
| 3967 |
let rep = a.fsck().unwrap(); |
| 3968 |
assert!( |
| 3969 |
rep.problems.iter().any(|p| p.what.contains("ops/head")), |
| 3970 |
"{rep:?}" |
| 3971 |
); |
| 3972 |
fs::write(&head, saved).unwrap(); |
| 3973 |
assert_eq!(a.fsck().unwrap().problems, vec![]); |
| 3974 |
} |
| 4dcb8c09 Andy McCall ↶ |
3975 |
|
| 3976 |
|
| 3977 |
fn copy_tree(from: &Path, to: &Path) { |
| 3978 |
fs::create_dir_all(to).unwrap(); |
| 3979 |
for e in fs::read_dir(from).unwrap() { |
| 3980 |
let e = e.unwrap(); |
| 3981 |
let target = to.join(e.file_name()); |
| 3982 |
if e.file_type().unwrap().is_dir() { |
| 3983 |
copy_tree(&e.path(), &target); |
| 3984 |
} else { |
| 3985 |
fs::copy(e.path(), &target).unwrap(); |
| 3986 |
} |
| 3987 |
} |
| 3988 |
} |
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
#[test] |
| 3994 |
fn crash_at_every_step_of_commit_and_push_leaves_sound_repositories() { |
| 3995 |
use crate::durable::failpoint; |
| 3996 |
hermetic(); |
| 3997 |
let dir = tempfile::tempdir().unwrap(); |
| 3998 |
let pristine = dir.path().join("pristine"); |
| 3999 |
let origin_path = pristine.join("origin"); |
| 4000 |
Repo::init(&origin_path).unwrap(); |
| 4001 |
let mut a = Repo::clone(&origin_path.to_string_lossy(), &pristine.join("a")) |
| 4002 |
.unwrap() |
| 4003 |
.with_home(dir.path().join("home")); |
| 4004 |
a.create_identity("Ada", "[email protected]", IdentityScope::Repo) |
| 4005 |
.unwrap(); |
| 4006 |
write(&pristine.join("a"), "f.txt", b"one\n"); |
| 4007 |
a.commit("first", &[]).unwrap(); |
| 4008 |
assert!(a.push("origin", false).unwrap()[0].ok); |
| 4009 |
drop(a); |
| 4010 |
|
| 4011 |
type Op = fn(&Repo) -> Result<()>; |
| 4012 |
let edit_and_commit: Op = |r| { |
| 4013 |
write(r.root(), "f.txt", b"one\ntwo\n"); |
| 4014 |
write(r.root(), "big.bin", &vec![7u8; 300_000]); |
| 4015 |
r.commit("second", &[]).map(|_| ()) |
| 4016 |
}; |
| 4017 |
let push: Op = |r| { |
| 4018 |
let res = r.push("origin", false)?; |
| 4019 |
if res.iter().all(|x| x.ok) { |
| 4020 |
Ok(()) |
| 4021 |
} else { |
| 4022 |
Err(RepoError::Msg(format!("{res:?}"))) |
| 4023 |
} |
| 4024 |
}; |
| 4025 |
|
| 4026 |
for (name, prepare, op) in [ |
| 4027 |
("commit", None, edit_and_commit), |
| 4028 |
("push", Some(edit_and_commit), push), |
| 4029 |
] { |
| 4030 |
let mut steps = 0; |
| 4031 |
for n in 1.. { |
| 4032 |
let run = dir.path().join(format!("{name}-{n}")); |
| 4033 |
copy_tree(&pristine, &run); |
| 4034 |
|
| 4035 |
let open = |p: &Path| { |
| 4036 |
let mut r = Repo::open(p).unwrap().with_home(dir.path().join("home")); |
| 4037 |
r.config |
| 4038 |
.set("remote.origin.url", &run.join("origin").to_string_lossy()) |
| 4039 |
.unwrap(); |
| 4040 |
r.save_config().unwrap(); |
| 4041 |
r |
| 4042 |
}; |
| 4043 |
let a = open(&run.join("a")); |
| 4044 |
if let Some(prep) = prepare { |
| 4045 |
prep(&a).unwrap(); |
| 4046 |
} |
| 4047 |
failpoint::crash_after(n); |
| 4048 |
let outcome = op(&a); |
| 4049 |
let crashed = failpoint::disarm(); |
| 4050 |
drop(a); |
| 4051 |
if !crashed { |
| 4052 |
outcome.unwrap_or_else(|e| panic!("{name} failed without a crash: {e}")); |
| 4053 |
steps = n - 1; |
| 4054 |
break; |
| 4055 |
} |
| 4056 |
assert!( |
| 4057 |
outcome.is_err(), |
| 4058 |
"{name}: a crash at step {n} was swallowed" |
| 4059 |
); |
| 4060 |
for repo in ["a", "origin"] { |
| 4061 |
let r = Repo::open(&run.join(repo)).unwrap_or_else(|e| { |
| 4062 |
panic!("{name} crash at step {n}: {repo} no longer opens: {e}") |
| 4063 |
}); |
| 4064 |
let rep = r.fsck().unwrap(); |
| 4065 |
assert_eq!( |
| 4066 |
rep.errors(), |
| 4067 |
0, |
| 4068 |
"{name} crash at step {n}: {repo} fails fsck: {:?}", |
| 4069 |
rep.problems |
| 4070 |
); |
| 4071 |
} |
| 4072 |
|
| 4073 |
let a = open(&run.join("a")); |
| 4074 |
|
| 4075 |
let landed = a |
| 4076 |
.bookmark_heads() |
| 4077 |
.unwrap() |
| 4078 |
.iter() |
| 4079 |
.any(|h| a.store().get_change(h).unwrap().message == "second"); |
| 4080 |
let again = match (name, landed) { |
| 4081 |
("commit", true) => Ok(()), |
| 4082 |
("commit", false) => a.commit("second", &[]).map(|_| ()), |
| 4083 |
_ => op(&a), |
| 4084 |
}; |
| 4085 |
again.unwrap_or_else(|e| panic!("{name} crash at step {n}: retry failed: {e}")); |
| 4086 |
assert_eq!(a.fsck().unwrap().errors(), 0); |
| 4087 |
fs::remove_dir_all(&run).unwrap(); |
| 4088 |
} |
| 4089 |
assert!(steps >= 3, "{name} has only {steps} durable steps?"); |
| 4090 |
} |
| 4091 |
} |
| 19555025 Andy McCall ↶ |
4092 |
|
| 4093 |
|
| 4094 |
|
| 4095 |
|
| 4096 |
#[test] |
| 4097 |
fn concurrent_commits_in_two_workspaces_lose_nothing() { |
| 4098 |
hermetic(); |
| 4099 |
let dir = tempfile::tempdir().unwrap(); |
| 4100 |
let root = dir.path(); |
| 4101 |
let origin = root.join("origin"); |
| 4102 |
Repo::init_bare_unbound(&origin).unwrap(); |
| 4103 |
let main = root.join("main"); |
| 4104 |
let mut repo = Repo::init(&main).unwrap(); |
| 4105 |
repo.config |
| 4106 |
.set("remote.origin.url", &origin.to_string_lossy()) |
| 4107 |
.unwrap(); |
| 4108 |
repo.save_config().unwrap(); |
| 4109 |
write(&main, "seed.txt", b"seed\n"); |
| 4110 |
repo.commit("seed", &[]).unwrap(); |
| 4111 |
let second = root.join("second"); |
| 4112 |
repo.workspace_add(&second, None).unwrap(); |
| 4113 |
drop(repo); |
| 4114 |
|
| 4115 |
const EACH: usize = 15; |
| 4116 |
std::thread::scope(|s| { |
| 4117 |
for (tag, dir, push) in [("a", main.clone(), true), ("b", second.clone(), false)] { |
| 4118 |
s.spawn(move || { |
| 4119 |
let r = Repo::open(&dir).unwrap(); |
| 4120 |
for i in 0..EACH { |
| 4121 |
write( |
| 4122 |
&dir, |
| 4123 |
&format!("{tag}-{i}.txt"), |
| 4124 |
format!("{tag} {i}\n").as_bytes(), |
| 4125 |
); |
| 4126 |
r.commit(&format!("{tag} {i}"), &[]).unwrap(); |
| 4127 |
if push && i % 5 == 4 { |
| 4128 |
|
| 4129 |
let _ = r.push("origin", false); |
| 4130 |
} |
| 4131 |
} |
| 4132 |
}); |
| 4133 |
} |
| 4134 |
}); |
| 4135 |
|
| 4136 |
let repo = Repo::open(&main).unwrap(); |
| 4137 |
let heads = repo.bookmark_heads().unwrap(); |
| 4138 |
let state = repo.load_state(&heads).unwrap(); |
| 4139 |
let messages: BTreeSet<String> = state.changes().map(|(_, m)| m.message.clone()).collect(); |
| 4140 |
for tag in ["a", "b"] { |
| 4141 |
for i in 0..EACH { |
| 4142 |
assert!( |
| 4143 |
messages.contains(&format!("{tag} {i}")), |
| 4144 |
"lost commit {tag} {i}; have {messages:?}" |
| 4145 |
); |
| 4146 |
} |
| 4147 |
} |
| 4148 |
let rep = repo.fsck().unwrap(); |
| 4149 |
assert_eq!(rep.errors(), 0, "{:?}", rep.problems); |
| 4150 |
assert!(repo.push("origin", false).unwrap().iter().all(|r| r.ok)); |
| 4151 |
let hosted = Repo::open(&origin).unwrap(); |
| 4152 |
let hosted_heads = hosted.bookmark_heads().unwrap(); |
| 4153 |
assert_eq!( |
| 4154 |
hosted.load_state(&hosted_heads).unwrap().len(), |
| 4155 |
state.len(), |
| 4156 |
"the remote has every commit" |
| 4157 |
); |
| 4158 |
} |
| 4159 |
|
| 4160 |
|
| 4161 |
|
| 4162 |
|
| 4163 |
#[test] |
| 4164 |
fn operations_from_the_same_parent_are_merged() { |
| 4165 |
hermetic(); |
| 4166 |
let dir = tempfile::tempdir().unwrap(); |
| 4167 |
let root = dir.path(); |
| 4168 |
let repo = Repo::init(root).unwrap(); |
| 4169 |
write(root, "a.txt", b"a\n"); |
| 4170 |
let a = repo.commit("a", &[]).unwrap(); |
| 4171 |
write(root, "b.txt", b"b\n"); |
| 4172 |
let b = repo.commit("b", &[]).unwrap(); |
| 4173 |
let (x, op) = repo.head_op().unwrap(); |
| 4174 |
let state = |heads: Vec<ChangeHash>| repo.store().put_state(&StateObject::new(heads)).unwrap(); |
| 4175 |
|
| 4176 |
|
| 4177 |
let mut one = op.view.clone(); |
| 4178 |
one.bookmarks.insert("main".into(), state(vec![a])); |
| 4179 |
one.bookmarks.insert("topic".into(), state(vec![a])); |
| 4180 |
let mut two = op.view.clone(); |
| 4181 |
two.bookmarks.insert("main".into(), state(vec![b])); |
| 4182 |
repo.record_op(&[x], "one", one).unwrap(); |
| 4183 |
let merged = repo.record_op(&[x], "two", two).unwrap(); |
| 4184 |
|
| 4185 |
let (head, op) = repo.head_op().unwrap(); |
| 4186 |
assert_eq!(head, merged); |
| 4187 |
assert_eq!(op.parents.len(), 2, "a merge of the two operations"); |
| 4188 |
assert!(op.command.contains("merged with a concurrent operation")); |
| 4189 |
let mut main = repo |
| 4190 |
.store() |
| 4191 |
.get_state(&op.view.bookmarks["main"]) |
| 4192 |
.unwrap() |
| 4193 |
.heads; |
| 4194 |
main.sort(); |
| 4195 |
let mut want = vec![a, b]; |
| 4196 |
want.sort(); |
| 4197 |
assert_eq!(main, want, "both sides' heads are kept"); |
| 4198 |
assert!( |
| 4199 |
op.view.bookmarks.contains_key("topic"), |
| 4200 |
"a one-sided change survives" |
| 4201 |
); |
| 4202 |
assert_eq!(repo.fsck().unwrap().errors(), 0); |
| 4203 |
} |
| 3e5772cc Andy McCall ↶ |
4204 |
|
| 4205 |
#[test] |
| 4206 |
fn failed_requests_come_with_a_next_step() { |
| 4207 |
use crate::protocol::hint_for; |
| 4208 |
assert!(hint_for("https://h/x/oxy/v1/push: HTTP 401: bad token") |
| 4209 |
.unwrap() |
| 4210 |
.contains("token")); |
| 4211 |
assert!(hint_for("…: HTTP 403: forbidden") |
| 4212 |
.unwrap() |
| 4213 |
.contains("repo:write")); |
| 4214 |
assert!(hint_for("…: HTTP 404: no such repository") |
| 4215 |
.unwrap() |
| 4216 |
.contains("owner and repository name")); |
| 4217 |
assert!(hint_for("invalid peer certificate: UnknownIssuer") |
| 4218 |
.unwrap() |
| 4219 |
.contains("oxyhub-ca.pem")); |
| 4220 |
assert!(hint_for("Connect error: Connection refused (os error 111)") |
| 4221 |
.unwrap() |
| 4222 |
.contains("running")); |
| 4223 |
assert!(hint_for("Dns Failed: failed to lookup address information") |
| 4224 |
.unwrap() |
| 4225 |
.contains("resolve")); |
| 4226 |
assert_eq!(hint_for("something else entirely"), None); |
| 4227 |
} |
| 4228 |
|
| 4229 |
#[test] |
| 4230 |
fn doctor_names_problems_and_what_to_do() { |
| 4231 |
hermetic(); |
| 4232 |
let dir = tempfile::tempdir().unwrap(); |
| 4233 |
let root = dir.path(); |
| 4234 |
let origin = root.join("origin"); |
| 4235 |
Repo::init_bare_unbound(&origin).unwrap(); |
| 4236 |
let mut repo = Repo::init(&root.join("r")) |
| 4237 |
.unwrap() |
| 4238 |
.with_home(root.join("home")); |
| 4239 |
let find = |r: &DoctorReport, area: &str, h: Health| { |
| 4240 |
r.checks |
| 4241 |
.iter() |
| 4242 |
.find(|c| c.area == area && c.health == h) |
| 4243 |
.cloned() |
| 4244 |
}; |
| 4245 |
|
| 4246 |
let r = repo.doctor(true); |
| 4247 |
assert!(find(&r, "identity", Health::Warn) |
| 4248 |
.unwrap() |
| 4249 |
.hint |
| 4250 |
.unwrap() |
| 4251 |
.contains("oxy identity create")); |
| 4252 |
assert!(find(&r, "repository", Health::Ok).is_some()); |
| 4253 |
|
| 4254 |
repo.create_identity("Ada", "[email protected]", IdentityScope::Repo) |
| 4255 |
.unwrap(); |
| 4256 |
repo.config.set("author.name", "Ada").unwrap(); |
| 4257 |
repo.config.set("author.email", "[email protected]").unwrap(); |
| 4258 |
repo.config |
| 4259 |
.set("remote.origin.url", &origin.to_string_lossy()) |
| 4260 |
.unwrap(); |
| 4261 |
repo.config |
| 4262 |
.set("remote.down.url", "http://127.0.0.1:9/a/b") |
| 4263 |
.unwrap(); |
| 4264 |
repo.config.set("remote.down.token", "t").unwrap(); |
| 4265 |
repo.config |
| 4266 |
.set("remote.badca.url", "https://example.invalid/a/b") |
| 4267 |
.unwrap(); |
| 4268 |
repo.config |
| 4269 |
.set( |
| 4270 |
"remote.badca.ca", |
| 4271 |
&root.join("missing.pem").to_string_lossy(), |
| 4272 |
) |
| 4273 |
.unwrap(); |
| 4274 |
repo.save_config().unwrap(); |
| 4275 |
|
| 4276 |
let r = repo.doctor(true); |
| 4277 |
assert!(find(&r, "identity", Health::Ok).is_some(), "{r:?}"); |
| 4278 |
assert!(find(&r, "author", Health::Ok).is_some(), "{r:?}"); |
| 4279 |
assert!(find(&r, "remote origin", Health::Ok) |
| 4280 |
.unwrap() |
| 4281 |
.message |
| 4282 |
.contains("first push binds it")); |
| 4283 |
let down = find(&r, "remote down", Health::Fail).unwrap(); |
| 4284 |
assert!( |
| 4285 |
down.hint.as_deref().unwrap_or("").contains("running"), |
| 4286 |
"{down:?}" |
| 4287 |
); |
| 4288 |
let badca = find(&r, "remote badca", Health::Fail).unwrap(); |
| 4289 |
assert!( |
| 4290 |
badca.hint.unwrap().contains("remote.badca.ca"), |
| 4291 |
"the remote's own name is in the hint" |
| 4292 |
); |
| 4293 |
assert_eq!(r.worst(), Health::Fail); |
| 4294 |
|
| 4295 |
|
| 4296 |
let r = repo.doctor(false); |
| 4297 |
assert!(find(&r, "remote down", Health::Fail).is_none()); |
| 4298 |
assert!(find(&r, "remote down", Health::Ok) |
| 4299 |
.unwrap() |
| 4300 |
.message |
| 4301 |
.contains("not contacted")); |
| 4302 |
} |
| 62794ecc Andy McCall ↶ |
4303 |
|
| 4304 |
|
| 4305 |
|
| 4306 |
|
| 4307 |
#[test] |
| 4308 |
fn deleting_a_line_after_an_insertion_below_it_is_not_a_conflict() { |
| 4309 |
hermetic(); |
| 4310 |
let dir = tempfile::tempdir().unwrap(); |
| 4311 |
let root = dir.path(); |
| 4312 |
let repo = Repo::init(root).unwrap(); |
| 4313 |
write(root, "f", b"a\nb\nc\n"); |
| 4314 |
repo.commit("base", &[]).unwrap(); |
| 4315 |
write(root, "f", b"a\nb\nx\nc\n"); |
| 4316 |
let insert = repo.commit("insert x after b", &[]).unwrap(); |
| 4317 |
write(root, "f", b"a\nx\nc\n"); |
| 4318 |
let delete = repo.commit("delete b", &[]).unwrap(); |
| 4319 |
assert!( |
| 4320 |
repo.store() |
| 4321 |
.get_change(&delete) |
| 4322 |
.unwrap() |
| 4323 |
.deps |
| 4324 |
.contains(&insert), |
| 4325 |
"the deletion depends on the insertion below the deleted line" |
| 4326 |
); |
| 4327 |
let heads = repo.bookmark_heads().unwrap(); |
| 4328 |
assert_eq!(heads, vec![delete]); |
| 4329 |
let snap = repo.load_state(&heads).unwrap().materialize(); |
| 4330 |
assert!(!snap.is_conflicted()); |
| 4331 |
assert!( |
| 4332 |
matches!(&snap.files["f"].content, FileContent::Text { bytes, .. } if bytes == b"a\nx\nc\n") |
| 4333 |
); |
| 4334 |
} |
| 4335 |
|
| 4336 |
|
| 4337 |
|
| 4338 |
#[test] |
| 4339 |
fn a_binary_file_reverted_to_an_earlier_blob_keeps_it() { |
| 4340 |
hermetic(); |
| 4341 |
let dir = tempfile::tempdir().unwrap(); |
| 4342 |
let root = dir.path(); |
| 4343 |
let repo = Repo::init(root).unwrap(); |
| 4344 |
write(root, "bin", b"\0A"); |
| 4345 |
let v1 = repo.commit("v1", &[]).unwrap(); |
| 4346 |
write(root, "bin", b"\0B"); |
| 4347 |
let v2 = repo.commit("v2", &[]).unwrap(); |
| 4348 |
assert!(repo.store().get_change(&v2).unwrap().deps.contains(&v1)); |
| 4349 |
write(root, "bin", b"\0A"); |
| 4350 |
let v3 = repo.commit("back to A", &[]).unwrap(); |
| 4351 |
assert!(repo.store().get_change(&v3).unwrap().deps.contains(&v2)); |
| 4352 |
assert_eq!(repo.bookmark_heads().unwrap(), vec![v3]); |
| 4353 |
let state = repo.load_state(&[v3]).unwrap(); |
| 4354 |
let snap = state.materialize(); |
| 4355 |
let FileContent::Opaque { blobs } = &snap.files["bin"].content else { |
| 4356 |
panic!("opaque") |
| 4357 |
}; |
| 4358 |
assert_eq!(blobs.len(), 1, "one live blob, not none and not two"); |
| 4359 |
assert_eq!(repo.read_blob(&blobs[0]).unwrap(), b"\0A"); |
| 4360 |
} |
| 4361 |
|
| 4362 |
|
| 4363 |
|
| 4364 |
|
| 4365 |
#[test] |
| 4366 |
fn git_import_resolves_a_binary_merged_in_git_and_verifies() { |
| 4367 |
hermetic(); |
| 4368 |
if std::process::Command::new("git") |
| 4369 |
.arg("--version") |
| 4370 |
.output() |
| 4371 |
.is_err() |
| 4372 |
{ |
| 4373 |
eprintln!("git not installed; skipping"); |
| 4374 |
return; |
| 4375 |
} |
| 4376 |
let dir = tempfile::tempdir().unwrap(); |
| 4377 |
let root = dir.path(); |
| 4378 |
let g = root.join("src.git"); |
| 4379 |
fs::create_dir_all(&g).unwrap(); |
| 4380 |
let git = |args: &[&str]| -> String { |
| 4381 |
let out = std::process::Command::new("git") |
| 4382 |
.arg("-C") |
| 4383 |
.arg(&g) |
| 4384 |
.args([ |
| 4385 |
"-c", |
| 4386 |
"user.name=Grace", |
| 4387 |
"-c", |
| 4388 |
"[email protected]", |
| 4389 |
"-c", |
| 4390 |
"commit.gpgsign=false", |
| 4391 |
]) |
| 4392 |
.args(args) |
| 4393 |
.output() |
| 4394 |
.unwrap(); |
| 4395 |
assert!( |
| 4396 |
out.status.success(), |
| 4397 |
"git {args:?}: {}", |
| 4398 |
String::from_utf8_lossy(&out.stderr) |
| 4399 |
); |
| 4400 |
String::from_utf8_lossy(&out.stdout).trim().to_string() |
| 4401 |
}; |
| 4402 |
git(&["init", "-q", "-b", "main"]); |
| 4403 |
write(&g, "logo.bin", b"\0base"); |
| 4404 |
write(&g, "notes.txt", b"a\nb\nc\n"); |
| 4405 |
git(&["add", "."]); |
| 4406 |
git(&["commit", "-qm", "base"]); |
| 4407 |
git(&["checkout", "-qb", "side"]); |
| 4408 |
write(&g, "logo.bin", b"\0side"); |
| 4409 |
write(&g, "notes.txt", b"a\nb\nx\nc\n"); |
| 4410 |
git(&["commit", "-qam", "side"]); |
| 4411 |
git(&["checkout", "-q", "main"]); |
| 4412 |
write(&g, "logo.bin", b"\0main"); |
| 4413 |
git(&["commit", "-qam", "main"]); |
| 4414 |
|
| 4415 |
let _ = std::process::Command::new("git") |
| 4416 |
.arg("-C") |
| 4417 |
.arg(&g) |
| 4418 |
.args([ |
| 4419 |
"-c", |
| 4420 |
"user.name=G", |
| 4421 |
"-c", |
| 4422 |
"user.email=g@e", |
| 4423 |
"merge", |
| 4424 |
"-q", |
| 4425 |
"side", |
| 4426 |
]) |
| 4427 |
.output(); |
| 4428 |
write(&g, "logo.bin", b"\0main"); |
| 4429 |
git(&["add", "."]); |
| 4430 |
git(&["commit", "-qm", "merge side"]); |
| 4431 |
|
| 4432 |
write(&g, "logo.bin", b"\0base"); |
| 4433 |
git(&["commit", "-qam", "back to base"]); |
| 4434 |
|
| 4435 |
let repo = Repo::init(&root.join("o")).unwrap(); |
| 4436 |
let rep = repo.import_git(&g, "main").unwrap(); |
| 4437 |
assert_eq!(rep.merges, 1); |
| 4438 |
let v = repo.verify_git_import(&g, "main").unwrap(); |
| 4439 |
assert!(v.mismatches.is_empty(), "{:?}", v.mismatches); |
| 4440 |
assert_eq!(v.commits, 5); |
| 4441 |
let snap = repo |
| 4442 |
.load_state(&repo.bookmark_heads().unwrap()) |
| 4443 |
.unwrap() |
| 4444 |
.materialize(); |
| 4445 |
assert!(!snap.is_conflicted()); |
| 4446 |
let FileContent::Opaque { blobs } = &snap.files["logo.bin"].content else { |
| 4447 |
panic!("opaque") |
| 4448 |
}; |
| 4449 |
assert_eq!(repo.read_blob(&blobs[0]).unwrap(), b"\0base"); |
| 4450 |
} |
| c863c7a7 Andy McCall ↶ |
4451 |
|
| 4452 |
|
| 4453 |
|
| 4454 |
|
| 4455 |
#[cfg(unix)] |
| 4456 |
#[test] |
| 4457 |
fn windows_file_systems_keep_exec_bits_and_symlinks() { |
| 4458 |
use crate::portable::Platform; |
| 4459 |
use std::os::unix::fs::PermissionsExt; |
| 4460 |
hermetic(); |
| 4461 |
let dir = tempfile::tempdir().unwrap(); |
| 4462 |
let root = dir.path(); |
| 4463 |
let repo = Repo::init(root).unwrap(); |
| 4464 |
write(root, "run.sh", b"#!/bin/sh\n"); |
| 4465 |
fs::set_permissions(root.join("run.sh"), fs::Permissions::from_mode(0o755)).unwrap(); |
| 4466 |
std::os::unix::fs::symlink("run.sh", root.join("link")).unwrap(); |
| 4467 |
repo.commit("script and link", &[]).unwrap(); |
| 4468 |
drop(repo); |
| 4469 |
|
| 4470 |
|
| 4471 |
fs::set_permissions(root.join("run.sh"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 4472 |
fs::remove_file(root.join("link")).unwrap(); |
| 4473 |
write(root, "link", b"run.sh"); |
| 4474 |
let win = Repo::open(root).unwrap().with_platform(Platform::windows()); |
| 4475 |
let s = win.snapshot("oxy status").unwrap(); |
| 4476 |
assert!( |
| 4477 |
s.statuses.is_empty(), |
| 4478 |
"nothing changed on Windows: {:?}", |
| 4479 |
statuses(&s) |
| 4480 |
); |
| 4481 |
|
| 4482 |
write(root, "run.sh", b"#!/bin/sh\necho hi\n"); |
| 4483 |
write(root, "link", b"other"); |
| 4484 |
let h = win.commit("edit on Windows", &[]).unwrap(); |
| 4485 |
let snap = win.load_state(&[h]).unwrap().materialize(); |
| 4486 |
assert!(snap.files["run.sh"].mode.exec, "the exec bit survives"); |
| 4487 |
assert_eq!(snap.files["link"].kind, NodeKind::Symlink); |
| 4488 |
|
| 4489 |
let linux = Repo::open(root).unwrap(); |
| 4490 |
assert!(!linux.snapshot("oxy status").unwrap().statuses.is_empty()); |
| 4491 |
} |
| 4492 |
|
| 4493 |
|
| 4494 |
|
| 4495 |
#[test] |
| 4496 |
fn paths_other_platforms_cannot_hold_are_reported() { |
| 4497 |
use crate::portable::Platform; |
| 4498 |
hermetic(); |
| 4499 |
let dir = tempfile::tempdir().unwrap(); |
| 4500 |
let root = dir.path(); |
| 4501 |
let repo = Repo::init(root).unwrap(); |
| 4502 |
write(root, "README.md", b"a\n"); |
| 4503 |
write(root, "readme.md", b"b\n"); |
| 4504 |
write(root, "src/aux.c", b"int x;\n"); |
| 4505 |
let h = repo.commit("unportable", &[]).unwrap(); |
| 4506 |
let r = repo.doctor(false); |
| 4507 |
let port = r.checks.iter().find(|c| c.area == "portability").unwrap(); |
| 4508 |
assert_eq!(port.health, Health::Warn); |
| 4509 |
assert!( |
| 4510 |
port.message.contains("differ only in case") && port.message.contains("aux.c"), |
| 4511 |
"{}", |
| 4512 |
port.message |
| 4513 |
); |
| 4514 |
let mac = Repo::open(root).unwrap().with_platform(Platform::macos()); |
| 4515 |
let rep = mac.checkout(&[h], true, "oxy checkout").unwrap(); |
| 4516 |
assert!( |
| 4517 |
rep.notes |
| 4518 |
.iter() |
| 4519 |
.any(|n| n.contains("README.md and readme.md")), |
| 4520 |
"{:?}", |
| 4521 |
rep.notes |
| 4522 |
); |
| 4523 |
assert!( |
| 4524 |
!rep.notes.iter().any(|n| n.contains("aux.c")), |
| 4525 |
"macOS allows aux.c" |
| 4526 |
); |
| 4527 |
} |
| bc489b6c Andy McCall ↶ |
4528 |
|
| 4529 |
#[test] |
| 4530 |
fn history_and_blame_of_a_loaded_state_match_the_full_walk() { |
| 4531 |
hermetic(); |
| 4532 |
let dir = tempfile::tempdir().unwrap(); |
| 4533 |
let repo = Repo::init(dir.path()).unwrap(); |
| 4534 |
write(dir.path(), "a.txt", b"one\n"); |
| 4535 |
write(dir.path(), "other.txt", b"x\n"); |
| 4536 |
write(dir.path(), "bin.dat", &[0u8, 1, 2, 0, 255]); |
| 4537 |
repo.snapshot("t").unwrap(); |
| 4538 |
let c1 = repo.commit("first", &[]).unwrap(); |
| 4539 |
write(dir.path(), "other.txt", b"y\n"); |
| 4540 |
repo.snapshot("t").unwrap(); |
| 4541 |
repo.commit("other only", &[]).unwrap(); |
| 4542 |
write(dir.path(), "a.txt", b"one\ntwo\n"); |
| 4543 |
repo.snapshot("t").unwrap(); |
| 4544 |
let c3 = repo.commit("second line", &[]).unwrap(); |
| 4545 |
write(dir.path(), "other.txt", b"z\n"); |
| 4546 |
repo.snapshot("t").unwrap(); |
| 4547 |
repo.commit("other again", &[]).unwrap(); |
| 4548 |
|
| 4549 |
let heads = repo.bookmark_heads().unwrap(); |
| 4550 |
let state = repo.load_state(&heads).unwrap(); |
| 4551 |
let full: Vec<ChangeHash> = repo |
| 4552 |
.log_for_path_in("a.txt", &heads) |
| 4553 |
.unwrap() |
| 4554 |
.iter() |
| 4555 |
.map(|e| e.hash) |
| 4556 |
.collect(); |
| 4557 |
assert_eq!( |
| 4558 |
full, |
| 4559 |
vec![c3, c1], |
| 4560 |
"newest first, only changes touching the path" |
| 4561 |
); |
| 4562 |
let newest: Vec<ChangeHash> = repo |
| 4563 |
.log_for_path_of(&state, "a.txt", Some(1)) |
| 4564 |
.unwrap() |
| 4565 |
.iter() |
| 4566 |
.map(|e| e.hash) |
| 4567 |
.collect(); |
| 4568 |
assert_eq!(newest, vec![c3], "stops at the first"); |
| 4569 |
assert_eq!( |
| 4570 |
repo.log_for_path_of(&state, "a.txt", None).unwrap().len(), |
| 4571 |
2 |
| 4572 |
); |
| 4573 |
assert!(repo |
| 4574 |
.log_for_path_of(&state, "missing.txt", Some(1)) |
| 4575 |
.is_err()); |
| 4576 |
assert_eq!( |
| 4577 |
repo.log_state(&heads).unwrap().len(), |
| 4578 |
repo.log_of(&state).len() |
| 4579 |
); |
| 4580 |
|
| 4581 |
let tree = state.tree().view(); |
| 4582 |
let via_state = repo.blame_of(&state, &tree, "a.txt").unwrap(); |
| 4583 |
let via_heads = repo.blame_in("a.txt", &heads).unwrap(); |
| 4584 |
assert_eq!( |
| 4585 |
via_state |
| 4586 |
.iter() |
| 4587 |
.map(|l| (l.hash, l.text.clone())) |
| 4588 |
.collect::<Vec<_>>(), |
| 4589 |
via_heads |
| 4590 |
.iter() |
| 4591 |
.map(|l| (l.hash, l.text.clone())) |
| 4592 |
.collect::<Vec<_>>() |
| 4593 |
); |
| 4594 |
assert_eq!( |
| 4595 |
via_state.iter().map(|l| l.hash).collect::<Vec<_>>(), |
| 4596 |
vec![c1, c3] |
| 4597 |
); |
| 4598 |
assert!(repo.blame_of(&state, &tree, "missing.txt").is_err()); |
| 4599 |
} |
| 696bbf29 Andy McCall ↶ |
4600 |
|
| 4601 |
|
| 4602 |
|
| 4603 |
|
| 4604 |
#[cfg(unix)] |
| 4605 |
#[test] |
| 4606 |
fn stat_cache_takes_unchanged_text_from_the_base_and_misses_no_edit() { |
| 4607 |
use std::os::unix::fs::PermissionsExt; |
| 4608 |
hermetic(); |
| 4609 |
let dir = tempfile::tempdir().unwrap(); |
| 4610 |
let root = dir.path(); |
| 4611 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 4612 |
write(root, "a.txt", b"one\ntwo\n"); |
| 4613 |
write(root, "b.txt", b"bee\n"); |
| 4614 |
repo.commit("base", &[]).unwrap(); |
| 4615 |
let age = |p: &str| { |
| 4616 |
let old = std::time::SystemTime::now() - std::time::Duration::from_secs(30); |
| 4617 |
fs::File::options() |
| 4618 |
.write(true) |
| 4619 |
.open(root.join(p)) |
| 4620 |
.unwrap() |
| 4621 |
.set_modified(old) |
| 4622 |
.unwrap(); |
| 4623 |
}; |
| 4624 |
age("a.txt"); |
| 4625 |
age("b.txt"); |
| 4626 |
assert!( |
| 4627 |
!repo.snapshot("status").unwrap().changed, |
| 4628 |
"records the aged stat" |
| 4629 |
); |
| 4630 |
|
| 4631 |
|
| 4632 |
fs::set_permissions(root.join("a.txt"), fs::Permissions::from_mode(0o000)).unwrap(); |
| 4633 |
let snap = repo.snapshot("status").unwrap(); |
| 4634 |
assert!( |
| 4635 |
!snap.changed && snap.statuses.is_empty(), |
| 4636 |
"{:?}", |
| 4637 |
snap.statuses |
| 4638 |
); |
| 4639 |
fs::set_permissions(root.join("a.txt"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 4640 |
|
| 4641 |
|
| 4642 |
|
| 4643 |
write(root, "b.txt", b"bee edited\n"); |
| 4644 |
age("b.txt"); |
| 4645 |
let first = repo.snapshot("status").unwrap(); |
| 4646 |
assert_eq!( |
| 4647 |
statuses(&first), |
| 4648 |
vec![("b.txt".to_string(), ChangeStatus::Modified)] |
| 4649 |
); |
| 4650 |
let again = repo.snapshot("status").unwrap(); |
| 4651 |
assert_eq!( |
| 4652 |
statuses(&again), |
| 4653 |
vec![("b.txt".to_string(), ChangeStatus::Modified)], |
| 4654 |
"still seen once stat-clean" |
| 4655 |
); |
| 4656 |
assert!(!again.changed, "and the working copy hashes the same"); |
| 4657 |
fs::set_permissions(root.join("b.txt"), fs::Permissions::from_mode(0o000)).unwrap(); |
| 4658 |
assert!( |
| 4659 |
repo.snapshot("status").is_err(), |
| 4660 |
"an edited file must be read, never taken from the base" |
| 4661 |
); |
| 4662 |
fs::set_permissions(root.join("b.txt"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 4663 |
|
| 4664 |
|
| 4665 |
write(root, "a.txt", b"one\nTWO\n"); |
| 4666 |
assert_eq!( |
| 4667 |
statuses(&repo.snapshot("status").unwrap()), |
| 4668 |
vec![ |
| 4669 |
("a.txt".to_string(), ChangeStatus::Modified), |
| 4670 |
("b.txt".to_string(), ChangeStatus::Modified) |
| 4671 |
] |
| 4672 |
); |
| 4673 |
|
| 4674 |
repo.commit("edits", &[]).unwrap(); |
| 4675 |
age("a.txt"); |
| 4676 |
age("b.txt"); |
| 4677 |
assert!(repo.snapshot("status").unwrap().statuses.is_empty()); |
| 4678 |
fs::set_permissions(root.join("a.txt"), fs::Permissions::from_mode(0o000)).unwrap(); |
| 4679 |
fs::set_permissions(root.join("b.txt"), fs::Permissions::from_mode(0o000)).unwrap(); |
| 4680 |
let snap = repo.snapshot("status").unwrap(); |
| 4681 |
assert!(!snap.changed && snap.statuses.is_empty()); |
| 4682 |
fs::set_permissions(root.join("a.txt"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 4683 |
fs::set_permissions(root.join("b.txt"), fs::Permissions::from_mode(0o644)).unwrap(); |
| 4684 |
} |
| afaba5f1 Andy McCall ↶ |
4685 |
|
| 4686 |
|
| 4687 |
|
| 4688 |
|
| 4689 |
#[test] |
| 4690 |
fn checkpoints_keep_large_graphs_as_blobs_of_their_own() { |
| 4691 |
hermetic(); |
| 4692 |
let dir = tempfile::tempdir().unwrap(); |
| 4693 |
let root = dir.path(); |
| 4694 |
let repo = Repo::init(root).unwrap().with_home(root.join("home")); |
| 4695 |
let long: String = (0..300) |
| 4696 |
.map(|i| format!("line {i} of a long file\n")) |
| 4697 |
.collect(); |
| 4698 |
write(root, "long.txt", long.as_bytes()); |
| 4699 |
write(root, "short.txt", b"short\n"); |
| 4700 |
repo.commit("base", &[]).unwrap(); |
| 4701 |
let (r1, _, _) = repo.checkpoint().unwrap(); |
| 4702 |
let cp1 = crate::objects::Checkpoint::decode( |
| 4703 |
&repo.store().get_typed(&r1, ObjectType::Checkpoint).unwrap(), |
| 4704 |
) |
| 4705 |
.unwrap(); |
| 4706 |
let graphs1 = repo.checkpoint_graph_blobs(&cp1).unwrap(); |
| 4707 |
assert_eq!(graphs1.len(), 1, "only the long file's graph is a blob"); |
| 4708 |
|
| 4709 |
|
| 4710 |
repo.gc(true).unwrap(); |
| 4711 |
assert!(repo.store().has(&graphs1[0])); |
| 4712 |
let report = repo.fsck().unwrap(); |
| 4713 |
assert_eq!(report.errors(), 0, "{:?}", report.problems); |
| 4714 |
|
| 4715 |
|
| 4716 |
write(root, "short.txt", b"short, edited\n"); |
| 4717 |
repo.commit("edit the short file", &[]).unwrap(); |
| 4718 |
let heads = repo.bookmark_heads().unwrap(); |
| 4719 |
let fast = repo.load_state(&heads).unwrap(); |
| 4720 |
let full = repo.load_state_full(&heads).unwrap(); |
| 4721 |
assert_eq!(fast.checkpoint_payload(), full.checkpoint_payload()); |
| 4722 |
|
| 4723 |
|
| 4724 |
let (r2, _, _) = repo.checkpoint().unwrap(); |
| 4725 |
let cp2 = crate::objects::Checkpoint::decode( |
| 4726 |
&repo.store().get_typed(&r2, ObjectType::Checkpoint).unwrap(), |
| 4727 |
) |
| 4728 |
.unwrap(); |
| 4729 |
assert_eq!(repo.checkpoint_graph_blobs(&cp2).unwrap(), graphs1); |
| 4730 |
|
| 4731 |
|
| 4732 |
let bundle = repo.checkpoint_bundle_for(&heads).unwrap(); |
| 4733 |
assert!(bundle.objects.iter().any(|(t, body)| *t == ObjectType::Blob |
| 4734 |
&& crate::hash::hash_object(ObjectType::Blob, body) == graphs1[0])); |
| 4735 |
|
| 4736 |
|
| 4737 |
let state = repo.load_state_full(&heads).unwrap(); |
| 4738 |
let old = repo.store_blob(&state.checkpoint_payload()).unwrap(); |
| 4739 |
let cp_old = crate::objects::Checkpoint { |
| 4740 |
heads: cp2.heads.clone(), |
| 4741 |
count: cp2.count, |
| 4742 |
payload: old, |
| 4743 |
}; |
| 4744 |
let r_old = repo |
| 4745 |
.store() |
| 4746 |
.put(ObjectType::Checkpoint, &cp_old.encode()) |
| 4747 |
.unwrap(); |
| 4748 |
repo.install_checkpoint("main", &r_old).unwrap(); |
| 4749 |
assert_eq!( |
| 4750 |
repo.load_state(&heads).unwrap().checkpoint_payload(), |
| 4751 |
full.checkpoint_payload() |
| 4752 |
); |
| 4753 |
assert_eq!(repo.fsck().unwrap().errors(), 0); |
| 4754 |
|
| 4755 |
|
| 4756 |
|
| 4757 |
let dir2 = tempfile::tempdir().unwrap(); |
| 4758 |
let root2 = dir2.path(); |
| 4759 |
let repo2 = Repo::init(root2).unwrap().with_home(root2.join("home")); |
| 4760 |
write(root2, "long.txt", long.as_bytes()); |
| 4761 |
repo2.commit("base", &[]).unwrap(); |
| 4762 |
let (r, _, _) = repo2.checkpoint().unwrap(); |
| 4763 |
let cp = crate::objects::Checkpoint::decode( |
| 4764 |
&repo2.store().get_typed(&r, ObjectType::Checkpoint).unwrap(), |
| 4765 |
) |
| 4766 |
.unwrap(); |
| 4767 |
let graph = repo2.checkpoint_graph_blobs(&cp).unwrap()[0]; |
| 4768 |
fs::remove_file(repo2.store().path_for(&graph)).unwrap(); |
| 4769 |
assert!(repo2.load_state(&repo2.bookmark_heads().unwrap()).is_err()); |
| 4770 |
assert!(repo2.fsck().unwrap().errors() > 0, "fsck reports it"); |
| 4771 |
} |