| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Round trips of repositories built for one feature each, the import's |
| 5 | //! refusals, and the export of changesets made in braid. |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use std::collections::BTreeMap; |
| 10 | |
| 11 | use braid_format::changeset::decode_change_list; |
| 12 | use braid_format::{ |
| 13 | Change, ChangeOp, Changes, Changeset, ChangesetParts, Entry, EntryKind, GitId, Kind, |
| 14 | MemoryObjects, ObjectId, Objects, Signature, build_directory, read_directory, |
| 15 | }; |
| 16 | use braid_git::{ |
| 17 | Error, Exporter, GitKind, GitMap, GitOid, ImportOptions, RefTarget, RefUpdate, RoundTripReport, |
| 18 | export_commit, export_object, import, import_parallel, verify_round_trip, write_to_repo, |
| 19 | write_to_repo_recording, |
| 20 | }; |
| 21 | use common::{Repo, exec, file, link, raw_tree, submodule}; |
| 22 | |
| 23 | fn oid(text: &str) -> GitOid { |
| 24 | text.parse().unwrap() |
| 25 | } |
| 26 | |
| 27 | fn round_trip(repo: &Repo) -> RoundTripReport { |
| 28 | let tmp = tempfile::tempdir().unwrap(); |
| 29 | let report = verify_round_trip(repo.path(), tmp.path()).unwrap_or_else(|err| panic!("{err}")); |
| 30 | assert_eq!(report.commits, repo.count_commits()); |
| 31 | report |
| 32 | } |
| 33 | |
| 34 | fn import_all(repo: &Repo, objects: &MemoryObjects, map: &mut GitMap) -> braid_git::ImportReport { |
| 35 | import(repo.path(), objects, map, &ImportOptions::default()) |
| 36 | .unwrap_or_else(|err| panic!("{err}")) |
| 37 | } |
| 38 | |
| 39 | /// The thread counts of the parallel imports `check` compares. |
| 40 | const THREADS: [usize; 2] = [1, 4]; |
| 41 | |
| 42 | /// Every object held, with its kind and payload. |
| 43 | fn contents(objects: &MemoryObjects) -> BTreeMap<ObjectId, (Kind, Vec<u8>)> { |
| 44 | objects |
| 45 | .ids() |
| 46 | .into_iter() |
| 47 | .map(|id| (id, objects.get(&id).unwrap().unwrap())) |
| 48 | .collect() |
| 49 | } |
| 50 | |
| 51 | /// Imports into `objects` and `map` in parallel, and checks that the report, |
| 52 | /// the map and the objects are the one-thread import's. |
| 53 | fn assert_parallel_same( |
| 54 | repo: &Repo, |
| 55 | (objects, map): (&MemoryObjects, &mut GitMap), |
| 56 | threads: usize, |
| 57 | (expected_objects, expected_map, expected_report): ( |
| 58 | &MemoryObjects, |
| 59 | &GitMap, |
| 60 | &braid_git::ImportReport, |
| 61 | ), |
| 62 | ) { |
| 63 | let report = import_parallel( |
| 64 | repo.path(), |
| 65 | objects, |
| 66 | map, |
| 67 | &ImportOptions::default(), |
| 68 | threads, |
| 69 | ) |
| 70 | .unwrap_or_else(|err| panic!("{err}")); |
| 71 | assert_eq!(&report, expected_report, "{threads} threads"); |
| 72 | assert_eq!(map.to_bytes(), expected_map.to_bytes(), "{threads} threads"); |
| 73 | assert!( |
| 74 | contents(objects) == contents(expected_objects), |
| 75 | "{threads} threads: the objects differ" |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /// The checks every fixture goes through: the round trip holds; importing |
| 80 | /// imports every commit; importing again with the saved map imports nothing; |
| 81 | /// after `extend` adds commits, importing brings in exactly those; the round |
| 82 | /// trip still holds; and every imported changeset passes admission. The |
| 83 | /// parallel import, from scratch and then after `extend`, gives the same |
| 84 | /// reports, maps and objects as the one-thread import. |
| 85 | fn check(repo: &mut Repo, extend: impl FnOnce(&mut Repo) -> usize) -> (MemoryObjects, GitMap) { |
| 86 | round_trip(repo); |
| 87 | let objects = MemoryObjects::new(); |
| 88 | let mut map = GitMap::new(); |
| 89 | let first = import_all(repo, &objects, &mut map); |
| 90 | assert_eq!(first.commits, repo.count_commits()); |
| 91 | assert_eq!(map.counts().commits, first.commits); |
| 92 | let mut parallel: Vec<(MemoryObjects, GitMap)> = THREADS |
| 93 | .iter() |
| 94 | .map(|threads| { |
| 95 | let (many, mut many_map) = (MemoryObjects::new(), GitMap::new()); |
| 96 | assert_parallel_same( |
| 97 | repo, |
| 98 | (&many, &mut many_map), |
| 99 | *threads, |
| 100 | (&objects, &map, &first), |
| 101 | ); |
| 102 | (many, many_map) |
| 103 | }) |
| 104 | .collect(); |
| 105 | |
| 106 | let mut map = GitMap::from_bytes(&map.to_bytes()).unwrap(); |
| 107 | let again = import_all(repo, &objects, &mut map); |
| 108 | assert_eq!( |
| 109 | (again.commits, again.trees, again.blobs, again.tags), |
| 110 | (0, 0, 0, 0), |
| 111 | "nothing is new" |
| 112 | ); |
| 113 | assert_eq!(again.refs, first.refs); |
| 114 | |
| 115 | let added = extend(repo); |
| 116 | assert!(added > 0); |
| 117 | let next = import_all(repo, &objects, &mut map); |
| 118 | assert_eq!(next.commits, added, "only the new commits are imported"); |
| 119 | assert_eq!(map.counts().commits, repo.count_commits()); |
| 120 | for ((many, many_map), threads) in parallel.iter_mut().zip(THREADS) { |
| 121 | assert_parallel_same(repo, (many, many_map), threads, (&objects, &map, &next)); |
| 122 | } |
| 123 | round_trip(repo); |
| 124 | assert_admissible(&objects, &map); |
| 125 | (objects, map) |
| 126 | } |
| 127 | |
| 128 | fn decode(objects: &MemoryObjects, id: &ObjectId) -> Changeset { |
| 129 | let (kind, bytes) = objects.get(id).unwrap().unwrap(); |
| 130 | assert_eq!(kind, Kind::Changeset); |
| 131 | Changeset::decode(&bytes).unwrap() |
| 132 | } |
| 133 | |
| 134 | fn changeset(objects: &MemoryObjects, map: &GitMap, commit: &str) -> Changeset { |
| 135 | decode( |
| 136 | objects, |
| 137 | &map.changeset(&oid(commit)).expect("commit imported"), |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | fn change_list(objects: &MemoryObjects, changeset: &Changeset) -> Vec<Change> { |
| 142 | match &changeset.changes { |
| 143 | Changes::Inline(list) => list.clone(), |
| 144 | Changes::External { list, .. } => { |
| 145 | decode_change_list(&objects.get(list).unwrap().unwrap().1).unwrap() |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// A directory held in memory, to apply change lists to. |
| 151 | type Dir = BTreeMap<Vec<u8>, Node>; |
| 152 | |
| 153 | #[derive(Clone, Debug)] |
| 154 | enum Node { |
| 155 | Leaf(EntryKind), |
| 156 | Dir(Dir), |
| 157 | } |
| 158 | |
| 159 | /// Reads a braid directory and everything below it, and checks that no |
| 160 | /// subdirectory is empty: braid trees never hold one. |
| 161 | fn load(objects: &MemoryObjects, tree: ObjectId) -> Dir { |
| 162 | let entries = |
| 163 | read_directory(tree, |id| objects.get(id).unwrap().map(|(_, bytes)| bytes)).unwrap(); |
| 164 | entries |
| 165 | .into_iter() |
| 166 | .map(|entry| { |
| 167 | let node = match entry.kind { |
| 168 | EntryKind::Tree { tree } => { |
| 169 | let below = load(objects, tree); |
| 170 | assert!( |
| 171 | !below.is_empty(), |
| 172 | "{:?} is an empty subdirectory", |
| 173 | String::from_utf8_lossy(&entry.name) |
| 174 | ); |
| 175 | Node::Dir(below) |
| 176 | } |
| 177 | kind => Node::Leaf(kind), |
| 178 | }; |
| 179 | (entry.name, node) |
| 180 | }) |
| 181 | .collect() |
| 182 | } |
| 183 | |
| 184 | /// The braid id of a directory held in memory. |
| 185 | fn tree_id(dir: &Dir) -> ObjectId { |
| 186 | let entries = dir |
| 187 | .iter() |
| 188 | .map(|(name, node)| Entry { |
| 189 | name: name.clone(), |
| 190 | kind: match node { |
| 191 | Node::Leaf(kind) => kind.clone(), |
| 192 | Node::Dir(below) => EntryKind::Tree { |
| 193 | tree: tree_id(below), |
| 194 | }, |
| 195 | }, |
| 196 | }) |
| 197 | .collect(); |
| 198 | build_directory(entries).unwrap().root() |
| 199 | } |
| 200 | |
| 201 | /// Applies a change list as the store's admission check does. Each change |
| 202 | /// gives a file's final value, so deletes go first; a directory goes with its |
| 203 | /// last entry, and setting a file creates the directories above it. |
| 204 | fn apply(dir: &mut Dir, changes: &[Change]) { |
| 205 | let split = |path: &[u8]| -> Vec<Vec<u8>> { |
| 206 | path.split(|byte| *byte == b'/') |
| 207 | .map(<[u8]>::to_vec) |
| 208 | .collect() |
| 209 | }; |
| 210 | for change in changes.iter().filter(|c| c.op == ChangeOp::Delete) { |
| 211 | delete(dir, &split(&change.path)); |
| 212 | } |
| 213 | for change in changes.iter().filter(|c| c.op != ChangeOp::Delete) { |
| 214 | set(dir, &split(&change.path), change.op.to_entry().unwrap()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | fn delete(dir: &mut Dir, path: &[Vec<u8>]) { |
| 219 | match path { |
| 220 | [name] => assert!( |
| 221 | matches!(dir.remove(name), Some(Node::Leaf(_))), |
| 222 | "deleting a file that is not there" |
| 223 | ), |
| 224 | [name, rest @ ..] => { |
| 225 | let Some(Node::Dir(below)) = dir.get_mut(name) else { |
| 226 | panic!("deleting below a directory that is not there") |
| 227 | }; |
| 228 | delete(below, rest); |
| 229 | if below.is_empty() { |
| 230 | dir.remove(name); |
| 231 | } |
| 232 | } |
| 233 | [] => unreachable!("paths are never empty"), |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | fn set(dir: &mut Dir, path: &[Vec<u8>], kind: EntryKind) { |
| 238 | match path { |
| 239 | [name] => { |
| 240 | let previous = dir.insert(name.clone(), Node::Leaf(kind)); |
| 241 | assert!( |
| 242 | !matches!(previous, Some(Node::Dir(_))), |
| 243 | "a file over a directory that still has entries" |
| 244 | ); |
| 245 | } |
| 246 | [name, rest @ ..] => { |
| 247 | let node = dir |
| 248 | .entry(name.clone()) |
| 249 | .or_insert_with(|| Node::Dir(Dir::new())); |
| 250 | let Node::Dir(below) = node else { |
| 251 | panic!("a directory over a file that was not deleted") |
| 252 | }; |
| 253 | set(below, rest, kind); |
| 254 | } |
| 255 | [] => unreachable!("paths are never empty"), |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /// Every imported changeset passes admission: its change list, applied to |
| 260 | /// the root of its first parent (or to nothing), gives its own root, and no |
| 261 | /// tree it holds has an empty subdirectory. |
| 262 | fn assert_admissible(objects: &MemoryObjects, map: &GitMap) { |
| 263 | for (commit, id) in map.commits() { |
| 264 | let changeset = decode(objects, &id); |
| 265 | let mut dir = match changeset.parents.first() { |
| 266 | Some(parent) => load(objects, decode(objects, parent).root), |
| 267 | None => Dir::new(), |
| 268 | }; |
| 269 | apply(&mut dir, &change_list(objects, &changeset)); |
| 270 | assert_eq!(tree_id(&dir), changeset.root, "commit {commit}"); |
| 271 | load(objects, changeset.root); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /// The change list of an imported commit, as path and operation. |
| 276 | fn changes(objects: &MemoryObjects, map: &GitMap, commit: &str) -> Vec<(String, &'static str)> { |
| 277 | let list = change_list(objects, &changeset(objects, map, commit)); |
| 278 | list.into_iter() |
| 279 | .map(|change| { |
| 280 | let op = match change.op { |
| 281 | ChangeOp::Delete => "delete", |
| 282 | ChangeOp::File { .. } => "file", |
| 283 | ChangeOp::Executable { .. } => "executable", |
| 284 | ChangeOp::Symlink { .. } => "symlink", |
| 285 | ChangeOp::Gitlink { .. } => "gitlink", |
| 286 | }; |
| 287 | (String::from_utf8_lossy(&change.path).into_owned(), op) |
| 288 | }) |
| 289 | .collect() |
| 290 | } |
| 291 | |
| 292 | fn pairs(list: &[(&str, &'static str)]) -> Vec<(String, &'static str)> { |
| 293 | list.iter() |
| 294 | .map(|(path, op)| (path.to_string(), *op)) |
| 295 | .collect() |
| 296 | } |
| 297 | |
| 298 | #[test] |
| 299 | fn linear_history() { |
| 300 | let mut repo = Repo::new(); |
| 301 | let first = repo.snapshot( |
| 302 | "main", |
| 303 | &[ |
| 304 | file("README.md", "hello\n"), |
| 305 | file("src/lib.rs", "pub fn f() {}\n"), |
| 306 | file("src/util/mod.rs", ""), |
| 307 | ], |
| 308 | "first", |
| 309 | ); |
| 310 | let second = repo.snapshot( |
| 311 | "main", |
| 312 | &[ |
| 313 | file("README.md", "hello again\n"), |
| 314 | file("src/lib.rs", "pub fn f() {}\n"), |
| 315 | file("src/util/mod.rs", ""), |
| 316 | file("docs/guide.md", "guide\n"), |
| 317 | ], |
| 318 | "second", |
| 319 | ); |
| 320 | let third = repo.snapshot( |
| 321 | "main", |
| 322 | &[ |
| 323 | file("README.md", "hello again\n"), |
| 324 | file("docs/guide.md", "guide\n"), |
| 325 | ], |
| 326 | "third: drop src", |
| 327 | ); |
| 328 | let empty = repo.snapshot("main", &[], "everything deleted"); |
| 329 | let (objects, map) = check(&mut repo, |repo| { |
| 330 | repo.snapshot("main", &[file("README.md", "back\n")], "fourth"); |
| 331 | repo.snapshot("main", &[file("README.md", "and again\n")], "fifth"); |
| 332 | 2 |
| 333 | }); |
| 334 | assert_eq!( |
| 335 | changes(&objects, &map, &first), |
| 336 | pairs(&[ |
| 337 | ("README.md", "file"), |
| 338 | ("src/lib.rs", "file"), |
| 339 | ("src/util/mod.rs", "file") |
| 340 | ]) |
| 341 | ); |
| 342 | assert_eq!( |
| 343 | changes(&objects, &map, &second), |
| 344 | pairs(&[("README.md", "file"), ("docs/guide.md", "file")]) |
| 345 | ); |
| 346 | assert_eq!( |
| 347 | changes(&objects, &map, &third), |
| 348 | pairs(&[("src/lib.rs", "delete"), ("src/util/mod.rs", "delete")]) |
| 349 | ); |
| 350 | assert_eq!( |
| 351 | changes(&objects, &map, &empty), |
| 352 | pairs(&[("README.md", "delete"), ("docs/guide.md", "delete")]) |
| 353 | ); |
| 354 | let first_changeset = changeset(&objects, &map, &first); |
| 355 | assert!(first_changeset.parents.is_empty()); |
| 356 | assert_eq!(first_changeset.message, b"first\n"); |
| 357 | assert_eq!(first_changeset.author.name, b"Ada Lovelace"); |
| 358 | assert_eq!(first_changeset.author.email, b"ada@example.org"); |
| 359 | assert_eq!(first_changeset.author.offset_minutes, 60); |
| 360 | assert_eq!(first_changeset.committer.name, b"Charles Babbage"); |
| 361 | assert!(first_changeset.extras.is_empty()); |
| 362 | assert_eq!( |
| 363 | changeset(&objects, &map, &second).parents, |
| 364 | [map.changeset(&oid(&first)).unwrap()] |
| 365 | ); |
| 366 | } |
| 367 | |
| 368 | #[test] |
| 369 | fn branches_and_merges() { |
| 370 | let mut repo = Repo::new(); |
| 371 | let base = repo.snapshot("main", &[file("a", "1")], "base"); |
| 372 | for branch in ["left", "right", "third"] { |
| 373 | repo.set_ref(&format!("refs/heads/{branch}"), &base); |
| 374 | } |
| 375 | let left = repo.snapshot("left", &[file("a", "1"), file("l", "left")], "left"); |
| 376 | let right = repo.snapshot("right", &[file("a", "1"), file("r", "right")], "right"); |
| 377 | let third = repo.snapshot("third", &[file("a", "1"), file("t", "third")], "third"); |
| 378 | repo.snapshot("main", &[file("a", "2")], "main moves on"); |
| 379 | let merge = repo.merge( |
| 380 | "main", |
| 381 | &["left"], |
| 382 | &[file("a", "2"), file("l", "left")], |
| 383 | "merge left", |
| 384 | ); |
| 385 | let octopus = repo.merge( |
| 386 | "main", |
| 387 | &["right", "third"], |
| 388 | &[ |
| 389 | file("a", "2"), |
| 390 | file("l", "left"), |
| 391 | file("r", "right"), |
| 392 | file("t", "third"), |
| 393 | ], |
| 394 | "octopus", |
| 395 | ); |
| 396 | let (objects, map) = check(&mut repo, |repo| { |
| 397 | repo.snapshot("left", &[file("a", "1"), file("l", "more")], "left again"); |
| 398 | 1 |
| 399 | }); |
| 400 | assert_eq!(changes(&objects, &map, &merge), pairs(&[("l", "file")])); |
| 401 | assert_eq!( |
| 402 | changes(&objects, &map, &octopus), |
| 403 | pairs(&[("r", "file"), ("t", "file")]) |
| 404 | ); |
| 405 | let parents = changeset(&objects, &map, &octopus).parents; |
| 406 | assert_eq!(parents.len(), 3); |
| 407 | assert_eq!(parents[1], map.changeset(&oid(&right)).unwrap()); |
| 408 | assert_eq!(parents[2], map.changeset(&oid(&third)).unwrap()); |
| 409 | assert_eq!( |
| 410 | changeset(&objects, &map, &merge).parents[1], |
| 411 | map.changeset(&oid(&left)).unwrap() |
| 412 | ); |
| 413 | } |
| 414 | |
| 415 | #[test] |
| 416 | fn renames_and_a_directory_replaced_by_a_file() { |
| 417 | let mut repo = Repo::new(); |
| 418 | repo.snapshot( |
| 419 | "main", |
| 420 | &[ |
| 421 | file("old.txt", "content\n"), |
| 422 | file("dir/a", "a"), |
| 423 | file("dir/b/c", "c"), |
| 424 | ], |
| 425 | "start", |
| 426 | ); |
| 427 | let rename = repo.snapshot( |
| 428 | "main", |
| 429 | &[ |
| 430 | file("new.txt", "content\n"), |
| 431 | file("dir/a", "a"), |
| 432 | file("dir/b/c", "c"), |
| 433 | ], |
| 434 | "rename", |
| 435 | ); |
| 436 | let to_file = repo.snapshot( |
| 437 | "main", |
| 438 | &[file("new.txt", "content\n"), file("dir", "now a file\n")], |
| 439 | "directory to file", |
| 440 | ); |
| 441 | let to_dir = repo.snapshot( |
| 442 | "main", |
| 443 | &[file("new.txt", "content\n"), file("dir/x", "x")], |
| 444 | "file to directory", |
| 445 | ); |
| 446 | let (objects, map) = check(&mut repo, |repo| { |
| 447 | repo.snapshot("main", &[file("newer.txt", "content\n")], "rename again"); |
| 448 | 1 |
| 449 | }); |
| 450 | assert_eq!( |
| 451 | changes(&objects, &map, &rename), |
| 452 | pairs(&[("new.txt", "file"), ("old.txt", "delete")]) |
| 453 | ); |
| 454 | assert_eq!( |
| 455 | changes(&objects, &map, &to_file), |
| 456 | pairs(&[("dir", "file"), ("dir/a", "delete"), ("dir/b/c", "delete")]) |
| 457 | ); |
| 458 | assert_eq!( |
| 459 | changes(&objects, &map, &to_dir), |
| 460 | pairs(&[("dir", "delete"), ("dir/x", "file")]) |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn symlinks_and_executable_bits() { |
| 466 | let mut repo = Repo::new(); |
| 467 | let links = [ |
| 468 | link("latest", "run.sh"), |
| 469 | link("dangling", "nowhere/at/all"), |
| 470 | link("outside", "../../etc"), |
| 471 | link("dir/up", ".."), |
| 472 | ]; |
| 473 | let mut entries = vec![file("run.sh", "#!/bin/sh\necho hi\n")]; |
| 474 | entries.extend(links.iter().cloned()); |
| 475 | repo.snapshot("main", &entries, "links"); |
| 476 | entries[0] = exec("run.sh", "#!/bin/sh\necho hi\n"); |
| 477 | let chmod = repo.snapshot("main", &entries, "make it executable"); |
| 478 | entries[1] = file("latest", "run.sh"); |
| 479 | let unlink = repo.snapshot("main", &entries, "a link becomes a file"); |
| 480 | let (objects, map) = check(&mut repo, |repo| { |
| 481 | entries[0] = file("run.sh", "#!/bin/sh\necho hi\n"); |
| 482 | repo.snapshot("main", &entries, "not executable any more"); |
| 483 | 1 |
| 484 | }); |
| 485 | assert_eq!( |
| 486 | changes(&objects, &map, &chmod), |
| 487 | pairs(&[("run.sh", "executable")]) |
| 488 | ); |
| 489 | assert_eq!( |
| 490 | changes(&objects, &map, &unlink), |
| 491 | pairs(&[("latest", "file")]) |
| 492 | ); |
| 493 | } |
| 494 | |
| 495 | const SUBMODULE_A: &str = "1111111111111111111111111111111111111111"; |
| 496 | const SUBMODULE_B: &str = "2222222222222222222222222222222222222222"; |
| 497 | |
| 498 | #[test] |
| 499 | fn submodule_gitlinks() { |
| 500 | let mut repo = Repo::new(); |
| 501 | let text = "[submodule \"vendor\"]\n\tpath = vendor\n\turl = https://example.org/vendor.git\n"; |
| 502 | let modules = file(".gitmodules", text); |
| 503 | // The gitlink goes in through the index, as `git submodule add` does. |
| 504 | let modules_blob = repo.blob(text.as_bytes()); |
| 505 | let tree = repo.index_tree(&[ |
| 506 | format!("100644,{modules_blob},.gitmodules"), |
| 507 | format!("160000,{SUBMODULE_A},vendor"), |
| 508 | ]); |
| 509 | assert_eq!( |
| 510 | tree, |
| 511 | repo.tree(&[modules.clone(), submodule("vendor", SUBMODULE_A)]) |
| 512 | ); |
| 513 | let add = repo.commit(&tree, &[], "add a submodule"); |
| 514 | repo.set_ref("refs/heads/main", &add); |
| 515 | let bump = repo.snapshot( |
| 516 | "main", |
| 517 | &[modules.clone(), submodule("vendor", SUBMODULE_B)], |
| 518 | "bump it", |
| 519 | ); |
| 520 | let (objects, map) = check(&mut repo, |repo| { |
| 521 | repo.snapshot("main", &[file(".gitmodules", "")], "remove it"); |
| 522 | 1 |
| 523 | }); |
| 524 | assert_eq!( |
| 525 | changes(&objects, &map, &add), |
| 526 | pairs(&[(".gitmodules", "file"), ("vendor", "gitlink")]) |
| 527 | ); |
| 528 | assert_eq!( |
| 529 | changes(&objects, &map, &bump), |
| 530 | pairs(&[("vendor", "gitlink")]) |
| 531 | ); |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn awkward_names_and_contents() { |
| 536 | let mut repo = Repo::new(); |
| 537 | let mut entries: Vec<common::Entry> = vec![ |
| 538 | file(b"latin1-caf\xe9.txt", "a Latin-1 name\n"), |
| 539 | file(b"\xff\xfe\xfd", "not UTF-8 at all\n"), |
| 540 | file("café.txt", "precomposed\n"), |
| 541 | file("cafe\u{301}.txt", "decomposed\n"), |
| 542 | file("🦀.rs", "fn crab() {}\n"), |
| 543 | file("日本語/ファイル.txt", "日本語\n"), |
| 544 | file("עברית.txt", "right to left\n"), |
| 545 | file("zero\u{200d}width.txt", "joiner\n"), |
| 546 | file("with space/and tab\there", "x"), |
| 547 | file("-leading-dash", "x"), |
| 548 | file("#hash", "x"), |
| 549 | file(".hidden", "x"), |
| 550 | file("a:b", "a colon"), |
| 551 | file("back\\slash", "x"), |
| 552 | file("x".repeat(255), "a long name"), |
| 553 | file("empty", ""), |
| 554 | file("no-newline", "last line"), |
| 555 | file("crlf.txt", "a\r\nb\r\n"), |
| 556 | file("binary.bin", [0u8, 1, 2, 255, 0, 10, 13]), |
| 557 | ]; |
| 558 | let first = repo.snapshot("main", &entries, "awkward"); |
| 559 | let (objects, map) = check(&mut repo, |repo| { |
| 560 | entries[1] = file(b"\xff\xfe\xfd", "still not UTF-8\n"); |
| 561 | entries.push(file(b"dir-\xc0/inner", "under a non-UTF-8 directory")); |
| 562 | repo.snapshot("main", &entries, "more"); |
| 563 | 1 |
| 564 | }); |
| 565 | let listed = changes(&objects, &map, &first); |
| 566 | assert_eq!(listed.len(), 19); |
| 567 | let changeset = changeset(&objects, &map, &first); |
| 568 | let Changes::Inline(list) = changeset.changes else { |
| 569 | panic!("a short list is inline") |
| 570 | }; |
| 571 | assert!( |
| 572 | list.iter() |
| 573 | .any(|change| change.path == b"latin1-caf\xe9.txt") |
| 574 | ); |
| 575 | assert!( |
| 576 | list.iter() |
| 577 | .any(|change| change.path == "cafe\u{301}.txt".as_bytes()) |
| 578 | ); |
| 579 | } |
| 580 | |
| 581 | #[test] |
| 582 | fn crafted_commits_keep_their_bytes() { |
| 583 | let mut repo = Repo::new(); |
| 584 | let base = repo.snapshot("main", &[file("a", "1")], "base"); |
| 585 | let tree = repo.tree(&[file("a", "1")]); |
| 586 | let headers = |parent: &str, author: &str, committer: &str| { |
| 587 | format!("tree {tree}\nparent {parent}\nauthor {author}\ncommitter {committer}\n") |
| 588 | }; |
| 589 | let ada = "Ada Lovelace <ada@example.org> 1700000000 +0100"; |
| 590 | |
| 591 | let mut latin1 = headers(&base, ada, ada).into_bytes(); |
| 592 | latin1.extend_from_slice(b"encoding ISO-8859-1\n\nCaf\xe9 cr\xe8me\n"); |
| 593 | let latin1 = repo.literally("commit", &latin1); |
| 594 | |
| 595 | let extra = format!( |
| 596 | "{}x-custom value\nmulti first\n continued\n \n after a blank line\n\ |
| 597 | gpgsig -----BEGIN PGP SIGNATURE-----\n \n iQEzBAABCAAdFiEE\n -----END PGP SIGNATURE-----\n\ |
| 598 | \nlooks signed\n", |
| 599 | headers(&latin1, ada, ada) |
| 600 | ); |
| 601 | let extra = repo.literally("commit", extra.as_bytes()); |
| 602 | |
| 603 | let odd = format!( |
| 604 | "{}\nno trailing newline", |
| 605 | headers( |
| 606 | &extra, |
| 607 | "Odd<odd@example.org>1700000123 -0000", |
| 608 | " <> 0 +0000" |
| 609 | ) |
| 610 | ); |
| 611 | let odd = repo.literally("commit", odd.as_bytes()); |
| 612 | |
| 613 | let bare = headers(&odd, ada, "Strange Zone <z@example.org> 1700000000 +1560"); |
| 614 | let bare = repo.literally("commit", bare.as_bytes()); |
| 615 | |
| 616 | let uppercase = format!( |
| 617 | "tree {}\nparent {bare}\nauthor {ada}\ncommitter {ada}\n\nupper-case tree id\n", |
| 618 | tree.to_uppercase() |
| 619 | ); |
| 620 | let uppercase = repo.literally("commit", uppercase.as_bytes()); |
| 621 | repo.set_ref("refs/heads/crafted", &uppercase); |
| 622 | |
| 623 | let (objects, map) = check(&mut repo, |repo| { |
| 624 | repo.snapshot("crafted", &[file("a", "2")], "ordinary again"); |
| 625 | 1 |
| 626 | }); |
| 627 | |
| 628 | for commit in [&latin1, &extra, &odd, &bare, &uppercase] { |
| 629 | let changeset = changeset(&objects, &map, commit); |
| 630 | let origin = objects |
| 631 | .get(&changeset.git_origin.unwrap()) |
| 632 | .unwrap() |
| 633 | .unwrap() |
| 634 | .1; |
| 635 | assert_eq!(origin, repo.git_bytes(&["cat-file", "commit", commit])); |
| 636 | assert!(changeset.extras.is_empty()); |
| 637 | } |
| 638 | assert_eq!( |
| 639 | changeset(&objects, &map, &latin1).message, |
| 640 | b"Caf\xe9 cr\xe8me\n" |
| 641 | ); |
| 642 | assert_eq!(changeset(&objects, &map, &extra).message, b"looks signed\n"); |
| 643 | let odd = changeset(&objects, &map, &odd); |
| 644 | assert_eq!(odd.message, b"no trailing newline"); |
| 645 | assert_eq!( |
| 646 | (odd.author.name.as_slice(), odd.author.email.as_slice()), |
| 647 | (&b"Odd"[..], &b"odd@example.org"[..]) |
| 648 | ); |
| 649 | assert_eq!( |
| 650 | (odd.author.seconds, odd.author.offset_minutes), |
| 651 | (1_700_000_123, 0) |
| 652 | ); |
| 653 | assert_eq!(odd.committer.name, b""); |
| 654 | assert_eq!(odd.committer.seconds, 0); |
| 655 | let bare = changeset(&objects, &map, &bare); |
| 656 | assert_eq!(bare.message, b""); |
| 657 | assert_eq!(bare.committer.offset_minutes, 15 * 60 + 60); |
| 658 | assert_eq!(changes(&objects, &map, &uppercase), pairs(&[])); |
| 659 | } |
| 660 | |
| 661 | #[test] |
| 662 | fn non_canonical_trees_keep_their_raw_bytes() { |
| 663 | let mut repo = Repo::new(); |
| 664 | let x = repo.blob(b"x\n"); |
| 665 | let y = repo.blob(b"y\n"); |
| 666 | let inner = repo.tree(&[file("inner", "x\n")]); |
| 667 | let group_writable = repo.literally("tree", &raw_tree(&[("100664", b"group-writable", &x)])); |
| 668 | let padded = repo.literally("tree", &raw_tree(&[("040000", b"dir", &inner)])); |
| 669 | let padded_file = repo.literally("tree", &raw_tree(&[("0100644", b"f", &x)])); |
| 670 | let unsorted = repo.literally( |
| 671 | "tree", |
| 672 | &raw_tree(&[("100644", b"b", &x), ("100644", b"a", &y)]), |
| 673 | ); |
| 674 | let duplicate = repo.literally( |
| 675 | "tree", |
| 676 | &raw_tree(&[("100644", b"same", &x), ("100644", b"same", &y)]), |
| 677 | ); |
| 678 | let file_and_dir = repo.literally( |
| 679 | "tree", |
| 680 | &raw_tree(&[("100644", b"twice", &x), ("40000", b"twice", &inner)]), |
| 681 | ); |
| 682 | // Canonical on its own, but it names a tree that is not. |
| 683 | let holder = repo.mktree(&[("40000", "tree", &group_writable, b"nested")]); |
| 684 | let odd_trees = [ |
| 685 | ("100664", &group_writable), |
| 686 | ("padded", &padded), |
| 687 | ("padded-file", &padded_file), |
| 688 | ("unsorted", &unsorted), |
| 689 | ("duplicate", &duplicate), |
| 690 | ("file-and-dir", &file_and_dir), |
| 691 | ("holder", &holder), |
| 692 | ]; |
| 693 | let root_with = |repo: &Repo, plain: &str| { |
| 694 | let plain = repo.blob(plain.as_bytes()); |
| 695 | let mut entries: Vec<(&str, &str, &str, &[u8])> = odd_trees |
| 696 | .iter() |
| 697 | .map(|(name, id)| ("40000", "tree", id.as_str(), name.as_bytes())) |
| 698 | .collect(); |
| 699 | entries.push(("100644", "blob", &plain, b"plain")); |
| 700 | entries.push(("40000", "tree", &inner, b"canonical")); |
| 701 | repo.mktree(&entries) |
| 702 | }; |
| 703 | let first_root = root_with(&repo, "one\n"); |
| 704 | let first = repo.commit(&first_root, &[], "odd trees"); |
| 705 | let second_root = root_with(&repo, "two\n"); |
| 706 | let second = repo.commit(&second_root, &[&first], "edit beside them"); |
| 707 | repo.set_ref("refs/heads/main", &second); |
| 708 | |
| 709 | let (objects, map) = check(&mut repo, |repo| { |
| 710 | let third_root = root_with(repo, "three\n"); |
| 711 | let third = repo.commit(&third_root, &[&second], "again"); |
| 712 | repo.set_ref("refs/heads/main", &third); |
| 713 | 1 |
| 714 | }); |
| 715 | for (name, id) in odd_trees { |
| 716 | assert!(map.raw_tree(&oid(id)).is_some(), "{name} keeps its bytes"); |
| 717 | assert_eq!( |
| 718 | export_object(&objects, &map, &oid(id)).unwrap(), |
| 719 | (GitKind::Tree, repo.git_bytes(&["cat-file", "tree", id])) |
| 720 | ); |
| 721 | } |
| 722 | assert!( |
| 723 | map.raw_tree(&oid(&first_root)).is_some(), |
| 724 | "it holds odd trees" |
| 725 | ); |
| 726 | assert!(map.raw_tree(&oid(&inner)).is_none(), "canonical"); |
| 727 | assert_eq!(map.counts().raw_trees, odd_trees.len() + 3); |
| 728 | assert_eq!( |
| 729 | changes(&objects, &map, &second), |
| 730 | pairs(&[("plain", "file")]) |
| 731 | ); |
| 732 | |
| 733 | // Braid reads `100664` as a plain file, keeps the last of duplicate |
| 734 | // names as a git checkout does, and sorts by name bytes. |
| 735 | let entries = |tree: &str| { |
| 736 | let root = map.tree(&oid(tree)).unwrap(); |
| 737 | let (_, bytes) = objects.get(&root).unwrap().unwrap(); |
| 738 | match braid_format::Node::decode(&bytes).unwrap() { |
| 739 | braid_format::Node::Flat(entries) => entries, |
| 740 | other => panic!("unexpected node {other:?}"), |
| 741 | } |
| 742 | }; |
| 743 | assert!(matches!( |
| 744 | entries(&group_writable)[0].kind, |
| 745 | EntryKind::File { size: 2, .. } |
| 746 | )); |
| 747 | let names: Vec<Vec<u8>> = entries(&unsorted).into_iter().map(|e| e.name).collect(); |
| 748 | assert_eq!(names, [b"a".to_vec(), b"b".to_vec()]); |
| 749 | let same = entries(&duplicate); |
| 750 | assert_eq!(same.len(), 1); |
| 751 | assert!(matches!( |
| 752 | same[0].kind, |
| 753 | EntryKind::File { blob, .. } if blob == ObjectId::hash(Kind::Blob, b"y\n") |
| 754 | )); |
| 755 | assert!(matches!( |
| 756 | entries(&file_and_dir)[0].kind, |
| 757 | EntryKind::Tree { .. } |
| 758 | )); |
| 759 | |
| 760 | // Git agrees: reading the duplicates into an index keeps the last one. |
| 761 | let index = repo.scratch().join("duplicates-index"); |
| 762 | let mut read_tree = repo.command(); |
| 763 | read_tree.env("GIT_INDEX_FILE", &index); |
| 764 | read_tree.args(["read-tree", &duplicate]); |
| 765 | assert!(read_tree.status().unwrap().success()); |
| 766 | let mut list = repo.command(); |
| 767 | list.env("GIT_INDEX_FILE", &index); |
| 768 | list.args(["ls-files", "--stage"]); |
| 769 | let listed = String::from_utf8(list.output().unwrap().stdout).unwrap(); |
| 770 | assert_eq!(listed, format!("100644 {y} 0\tsame\n")); |
| 771 | } |
| 772 | |
| 773 | #[test] |
| 774 | fn annotated_tags() { |
| 775 | let mut repo = Repo::new(); |
| 776 | let first = repo.snapshot("main", &[file("a", "1")], "first"); |
| 777 | let v1 = repo.annotated_tag("v1", &first, "release 1"); |
| 778 | repo.git(&["tag", "lightweight", &first]); |
| 779 | let nested = repo.annotated_tag("v1-blessed", &v1, "a tag of a tag"); |
| 780 | let tree = repo.tree(&[file("only-in-a-tag", "t\n"), file("dir/deeper", "d\n")]); |
| 781 | let tree_tag = repo.annotated_tag("tree-tag", &tree, "points to a tree"); |
| 782 | let blob = repo.blob(b"a blob on its own\n"); |
| 783 | let blob_tag = repo.annotated_tag("blob-tag", &blob, "points to a blob"); |
| 784 | repo.git(&["tag", "lightweight-tree", &tree]); |
| 785 | let (objects, map) = check(&mut repo, |repo| { |
| 786 | let second = repo.snapshot("main", &[file("a", "2")], "second"); |
| 787 | repo.annotated_tag("v2", &second, "release 2"); |
| 788 | repo.git(&["tag", "--force", "moved", &v1]); |
| 789 | 1 |
| 790 | }); |
| 791 | // A name that pointed to an annotated tag and now is lightweight. |
| 792 | let mut map = map; |
| 793 | let moved = repo.git(&["rev-parse", "refs/tags/moved"]); |
| 794 | assert_eq!(map.tag_by_name(b"refs/tags/moved").unwrap().0, oid(&moved)); |
| 795 | repo.git(&["tag", "--force", "moved", &first]); |
| 796 | import_all(&repo, &objects, &mut map); |
| 797 | assert!(map.tag_by_name(b"refs/tags/moved").is_none()); |
| 798 | |
| 799 | let (tag, entry) = map.tag_by_name(b"refs/tags/v1").unwrap(); |
| 800 | assert_eq!(tag, oid(&v1)); |
| 801 | assert_eq!( |
| 802 | (entry.target, entry.target_kind), |
| 803 | (oid(&first), GitKind::Commit) |
| 804 | ); |
| 805 | assert_eq!( |
| 806 | objects.get(&entry.raw).unwrap().unwrap().1, |
| 807 | repo.git_bytes(&["cat-file", "tag", &v1]) |
| 808 | ); |
| 809 | let (_, entry) = map.tag_by_name(b"refs/tags/v1-blessed").unwrap(); |
| 810 | assert_eq!((entry.target, entry.target_kind), (oid(&v1), GitKind::Tag)); |
| 811 | let (_, entry) = map.tag_by_name(b"refs/tags/tree-tag").unwrap(); |
| 812 | assert_eq!( |
| 813 | (entry.target, entry.target_kind), |
| 814 | (oid(&tree), GitKind::Tree) |
| 815 | ); |
| 816 | let (_, entry) = map.tag_by_name(b"refs/tags/blob-tag").unwrap(); |
| 817 | assert_eq!( |
| 818 | (entry.target, entry.target_kind), |
| 819 | (oid(&blob), GitKind::Blob) |
| 820 | ); |
| 821 | assert!(map.tag_by_name(b"refs/tags/lightweight").is_none()); |
| 822 | assert!(map.tag_by_name(b"refs/tags/v2").is_some()); |
| 823 | assert_eq!(map.counts().tags, 5); |
| 824 | for tag in [&v1, &nested, &tree_tag, &blob_tag] { |
| 825 | assert_eq!( |
| 826 | map.git_tag(&map.tag(&oid(tag)).unwrap().raw), |
| 827 | Some(oid(tag)) |
| 828 | ); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | #[test] |
| 833 | fn a_directory_with_more_than_1024_files() { |
| 834 | let mut repo = Repo::new(); |
| 835 | let mut entries: Vec<common::Entry> = (0..1100) |
| 836 | .map(|i| file(format!("big/f{i:04}.txt"), format!("{i}\n"))) |
| 837 | .collect(); |
| 838 | entries.push(file("README", "a big directory\n")); |
| 839 | repo.snapshot("main", &entries, "many files"); |
| 840 | entries[500] = file("big/f0500.txt", "changed\n"); |
| 841 | entries.push(file("big/zz-new", "new\n")); |
| 842 | let edit = repo.snapshot("main", &entries, "edit one, add one"); |
| 843 | let (objects, map) = check(&mut repo, |repo| { |
| 844 | entries.retain(|(path, _)| path != b"big/f0007.txt"); |
| 845 | repo.snapshot("main", &entries, "delete one"); |
| 846 | 1 |
| 847 | }); |
| 848 | assert_eq!( |
| 849 | changes(&objects, &map, &edit), |
| 850 | pairs(&[("big/f0500.txt", "file"), ("big/zz-new", "file")]) |
| 851 | ); |
| 852 | // The braid side of `big` is split into several nodes. |
| 853 | let root = changeset(&objects, &map, &edit).root; |
| 854 | let (_, bytes) = objects.get(&root).unwrap().unwrap(); |
| 855 | let braid_format::Node::Flat(top) = braid_format::Node::decode(&bytes).unwrap() else { |
| 856 | panic!("the root is small") |
| 857 | }; |
| 858 | let big = top.iter().find(|e| e.name == b"big").unwrap(); |
| 859 | let EntryKind::Tree { tree } = big.kind else { |
| 860 | panic!("big is a directory") |
| 861 | }; |
| 862 | let (_, bytes) = objects.get(&tree).unwrap().unwrap(); |
| 863 | assert!(matches!( |
| 864 | braid_format::Node::decode(&bytes).unwrap(), |
| 865 | braid_format::Node::Internal { .. } |
| 866 | )); |
| 867 | } |
| 868 | |
| 869 | #[test] |
| 870 | fn replacement_objects_and_grafts_are_ignored() { |
| 871 | let mut repo = Repo::new(); |
| 872 | let first = repo.snapshot("main", &[file("a", "1")], "one"); |
| 873 | let second = repo.snapshot("main", &[file("a", "2")], "two"); |
| 874 | let fake_tree = repo.tree(&[file("a", "fake")]); |
| 875 | let fake = repo.commit(&fake_tree, &[], "a replacement"); |
| 876 | repo.git(&["replace", &second, &fake]); |
| 877 | std::fs::write(repo.path().join("info/grafts"), format!("{second}\n")).unwrap(); |
| 878 | let (objects, map) = check(&mut repo, |repo| { |
| 879 | repo.snapshot("main", &[file("a", "3")], "three"); |
| 880 | 1 |
| 881 | }); |
| 882 | assert_eq!( |
| 883 | changeset(&objects, &map, &second).parents, |
| 884 | [map.changeset(&oid(&first)).unwrap()] |
| 885 | ); |
| 886 | assert_eq!(changes(&objects, &map, &second), pairs(&[("a", "file")])); |
| 887 | assert!(map.changeset(&oid(&fake)).is_none()); |
| 888 | } |
| 889 | |
| 890 | #[test] |
| 891 | fn selected_refs_only() { |
| 892 | let mut repo = Repo::new(); |
| 893 | let base = repo.snapshot("main", &[file("a", "1")], "base"); |
| 894 | repo.set_ref("refs/heads/side", &base); |
| 895 | repo.snapshot("side", &[file("b", "2")], "side"); |
| 896 | let objects = MemoryObjects::new(); |
| 897 | let mut map = GitMap::new(); |
| 898 | let options = ImportOptions { |
| 899 | refs: vec![b"main".to_vec()], |
| 900 | }; |
| 901 | let report = import(repo.path(), &objects, &mut map, &options).unwrap(); |
| 902 | assert_eq!(report.commits, 1); |
| 903 | assert_eq!(report.refs.len(), 1); |
| 904 | assert_eq!(report.refs[0].name, b"refs/heads/main"); |
| 905 | let missing = ImportOptions { |
| 906 | refs: vec![b"refs/heads/nope".to_vec()], |
| 907 | }; |
| 908 | assert!(matches!( |
| 909 | import(repo.path(), &objects, &mut map, &missing), |
| 910 | Err(Error::MissingRef(_)) |
| 911 | )); |
| 912 | } |
| 913 | |
| 914 | #[test] |
| 915 | fn shallow_clones_are_refused() { |
| 916 | let mut repo = Repo::new(); |
| 917 | for i in 0..3 { |
| 918 | repo.snapshot("main", &[file("a", format!("{i}"))], "commit"); |
| 919 | } |
| 920 | let shallow = repo.scratch().join("shallow.git"); |
| 921 | let url = format!("file://{}", repo.path().display()); |
| 922 | let mut clone = repo.command(); |
| 923 | clone.args(["clone", "--quiet", "--bare", "--depth", "1", &url]); |
| 924 | clone.arg(&shallow); |
| 925 | assert!(clone.status().unwrap().success()); |
| 926 | let err = import( |
| 927 | &shallow, |
| 928 | &MemoryObjects::new(), |
| 929 | &mut GitMap::new(), |
| 930 | &ImportOptions::default(), |
| 931 | ) |
| 932 | .unwrap_err(); |
| 933 | assert!(matches!(err, Error::Shallow), "{err}"); |
| 934 | assert!(err.to_string().contains("shallow")); |
| 935 | } |
| 936 | |
| 937 | #[test] |
| 938 | fn sha256_repositories_are_refused() { |
| 939 | let mut repo = Repo::init(&["--object-format=sha256"]); |
| 940 | repo.snapshot("main", &[file("a", "1")], "a SHA-256 commit"); |
| 941 | let err = import( |
| 942 | repo.path(), |
| 943 | &MemoryObjects::new(), |
| 944 | &mut GitMap::new(), |
| 945 | &ImportOptions::default(), |
| 946 | ) |
| 947 | .unwrap_err(); |
| 948 | assert!(matches!(err, Error::Sha256), "{err}"); |
| 949 | assert!(err.to_string().contains("SHA-256")); |
| 950 | let tmp = tempfile::tempdir().unwrap(); |
| 951 | assert!(matches!( |
| 952 | verify_round_trip(repo.path(), tmp.path()), |
| 953 | Err(Error::Sha256) |
| 954 | )); |
| 955 | } |
| 956 | |
| 957 | /// Builds a commit whose tree holds `evil` (a path of names) as a directory |
| 958 | /// with a hook inside, and checks the import refuses it by commit and path. |
| 959 | fn refused(evil: &[&[u8]], expected_path: &[u8]) { |
| 960 | let mut repo = Repo::new(); |
| 961 | let hook = repo.tree(&[exec("hooks/post-checkout", "#!/bin/sh\necho owned\n")]); |
| 962 | let mut id = hook; |
| 963 | for name in evil.iter().rev() { |
| 964 | id = repo.literally("tree", &raw_tree(&[("40000", name, &id)])); |
| 965 | } |
| 966 | let commit = repo.commit(&id, &[], "evil"); |
| 967 | repo.set_ref("refs/heads/main", &commit); |
| 968 | let err = import( |
| 969 | repo.path(), |
| 970 | &MemoryObjects::new(), |
| 971 | &mut GitMap::new(), |
| 972 | &ImportOptions::default(), |
| 973 | ) |
| 974 | .unwrap_err(); |
| 975 | match &err { |
| 976 | Error::RefusedPath { |
| 977 | commit: c, path, .. |
| 978 | } => { |
| 979 | assert_eq!(c, &oid(&commit)); |
| 980 | assert_eq!(path, expected_path); |
| 981 | } |
| 982 | other => panic!("expected a refused path, got {other}"), |
| 983 | } |
| 984 | assert!(err.to_string().contains(&commit), "{err}"); |
| 985 | } |
| 986 | |
| 987 | #[test] |
| 988 | fn names_that_alias_dot_git_are_refused() { |
| 989 | refused(&[b".git"], b".git"); |
| 990 | refused(&[b".GIT"], b".GIT"); |
| 991 | refused(&[b".Git."], b".Git."); |
| 992 | refused(&[b"GIT~1"], b"GIT~1"); |
| 993 | refused(&[".g\u{200c}it".as_bytes()], ".g\u{200c}it".as_bytes()); |
| 994 | refused(&[b".braid"], b".braid"); |
| 995 | refused(&[b"sub", b"deeper", b".git"], b"sub/deeper/.git"); |
| 996 | refused(&[b".."], b".."); |
| 997 | refused(&[b"a/b"], b"a/b"); |
| 998 | } |
| 999 | |
| 1000 | #[test] |
| 1001 | fn symlinks_on_files_git_reads_are_refused() { |
| 1002 | let mut repo = Repo::new(); |
| 1003 | let target = repo.blob(b"/etc/passwd"); |
| 1004 | let tree = repo.literally("tree", &raw_tree(&[("120000", b".gitmodules", &target)])); |
| 1005 | let commit = repo.commit(&tree, &[], "evil link"); |
| 1006 | repo.set_ref("refs/heads/main", &commit); |
| 1007 | let err = import( |
| 1008 | repo.path(), |
| 1009 | &MemoryObjects::new(), |
| 1010 | &mut GitMap::new(), |
| 1011 | &ImportOptions::default(), |
| 1012 | ) |
| 1013 | .unwrap_err(); |
| 1014 | assert!( |
| 1015 | matches!(&err, Error::RefusedPath { path, .. } if path == b".gitmodules"), |
| 1016 | "{err}" |
| 1017 | ); |
| 1018 | } |
| 1019 | |
| 1020 | #[test] |
| 1021 | fn unknown_modes_are_refused() { |
| 1022 | let mut repo = Repo::new(); |
| 1023 | let x = repo.blob(b"x"); |
| 1024 | let tree = repo.literally("tree", &raw_tree(&[("20000", b"device", &x)])); |
| 1025 | let commit = repo.commit(&tree, &[], "odd mode"); |
| 1026 | repo.set_ref("refs/heads/main", &commit); |
| 1027 | let err = import( |
| 1028 | repo.path(), |
| 1029 | &MemoryObjects::new(), |
| 1030 | &mut GitMap::new(), |
| 1031 | &ImportOptions::default(), |
| 1032 | ) |
| 1033 | .unwrap_err(); |
| 1034 | assert!( |
| 1035 | matches!(&err, Error::UnsupportedMode { path, mode, .. } if path == b"device" && mode == b"20000"), |
| 1036 | "{err}" |
| 1037 | ); |
| 1038 | } |
| 1039 | |
| 1040 | fn store_tree(objects: &MemoryObjects, entries: Vec<Entry>) -> ObjectId { |
| 1041 | let directory = build_directory(entries).unwrap(); |
| 1042 | for (_, bytes) in &directory.nodes { |
| 1043 | objects.put(Kind::Tree, bytes).unwrap(); |
| 1044 | } |
| 1045 | directory.root() |
| 1046 | } |
| 1047 | |
| 1048 | fn store_blob(objects: &MemoryObjects, content: &[u8]) -> (ObjectId, u64) { |
| 1049 | ( |
| 1050 | objects.put(Kind::Blob, content).unwrap(), |
| 1051 | content.len() as u64, |
| 1052 | ) |
| 1053 | } |
| 1054 | |
| 1055 | #[test] |
| 1056 | fn changesets_made_in_braid_export_deterministically() { |
| 1057 | let mut repo = Repo::new(); |
| 1058 | let imported = repo.snapshot( |
| 1059 | "main", |
| 1060 | &[ |
| 1061 | file("README", "hi\n"), |
| 1062 | exec("run.sh", "#!/bin/sh\n"), |
| 1063 | link("latest", "README"), |
| 1064 | submodule("vendor", SUBMODULE_A), |
| 1065 | ], |
| 1066 | "imported", |
| 1067 | ); |
| 1068 | let objects = MemoryObjects::new(); |
| 1069 | let mut map = GitMap::new(); |
| 1070 | import_all(&repo, &objects, &mut map); |
| 1071 | |
| 1072 | // A change made in braid: edit README, add docs/a.md, drop run.sh. |
| 1073 | let (readme, readme_size) = store_blob(&objects, b"hello from braid\n"); |
| 1074 | let (doc, doc_size) = store_blob(&objects, b"# A\n"); |
| 1075 | let (latest, latest_size) = store_blob(&objects, b"README"); |
| 1076 | let docs = store_tree( |
| 1077 | &objects, |
| 1078 | vec![Entry { |
| 1079 | name: b"a.md".to_vec(), |
| 1080 | kind: EntryKind::File { |
| 1081 | blob: doc, |
| 1082 | size: doc_size, |
| 1083 | }, |
| 1084 | }], |
| 1085 | ); |
| 1086 | let root = store_tree( |
| 1087 | &objects, |
| 1088 | vec![ |
| 1089 | Entry { |
| 1090 | name: b"README".to_vec(), |
| 1091 | kind: EntryKind::File { |
| 1092 | blob: readme, |
| 1093 | size: readme_size, |
| 1094 | }, |
| 1095 | }, |
| 1096 | Entry { |
| 1097 | name: b"docs".to_vec(), |
| 1098 | kind: EntryKind::Tree { tree: docs }, |
| 1099 | }, |
| 1100 | Entry { |
| 1101 | name: b"latest".to_vec(), |
| 1102 | kind: EntryKind::Symlink { |
| 1103 | blob: latest, |
| 1104 | size: latest_size, |
| 1105 | }, |
| 1106 | }, |
| 1107 | Entry { |
| 1108 | name: b"vendor".to_vec(), |
| 1109 | kind: EntryKind::Gitlink { |
| 1110 | commit: GitId::Sha1([0x11; 20]), |
| 1111 | }, |
| 1112 | }, |
| 1113 | ], |
| 1114 | ); |
| 1115 | let grace = Signature { |
| 1116 | name: b"Grace Hopper".to_vec(), |
| 1117 | email: b"grace@example.org".to_vec(), |
| 1118 | seconds: 1_700_000_500, |
| 1119 | offset_minutes: -300, |
| 1120 | }; |
| 1121 | let assembled = ChangesetParts { |
| 1122 | parents: vec![map.changeset(&oid(&imported)).unwrap()], |
| 1123 | root, |
| 1124 | changes: vec![ |
| 1125 | braid_format::Change { |
| 1126 | path: b"README".to_vec(), |
| 1127 | op: ChangeOp::File { |
| 1128 | blob: readme, |
| 1129 | size: readme_size, |
| 1130 | }, |
| 1131 | }, |
| 1132 | braid_format::Change { |
| 1133 | path: b"docs/a.md".to_vec(), |
| 1134 | op: ChangeOp::File { |
| 1135 | blob: doc, |
| 1136 | size: doc_size, |
| 1137 | }, |
| 1138 | }, |
| 1139 | braid_format::Change { |
| 1140 | path: b"run.sh".to_vec(), |
| 1141 | op: ChangeOp::Delete, |
| 1142 | }, |
| 1143 | ], |
| 1144 | author: grace.clone(), |
| 1145 | committer: grace, |
| 1146 | message: b"Made in braid\n".to_vec(), |
| 1147 | extras: vec![(b"x-braid-note".to_vec(), b"two\nlines".to_vec())], |
| 1148 | git_origin: None, |
| 1149 | } |
| 1150 | .assemble() |
| 1151 | .unwrap(); |
| 1152 | let changeset = objects.put(Kind::Changeset, &assembled.encoded).unwrap(); |
| 1153 | |
| 1154 | let (commit, bytes) = export_commit(&objects, &map, &changeset).unwrap(); |
| 1155 | assert_eq!( |
| 1156 | export_commit(&objects, &map, &changeset).unwrap(), |
| 1157 | (commit, bytes.clone()) |
| 1158 | ); |
| 1159 | let text = String::from_utf8(bytes).unwrap(); |
| 1160 | assert!(text.contains(&format!("\nparent {imported}\n")), "{text}"); |
| 1161 | assert!(text.contains("author Grace Hopper <grace@example.org> 1700000500 -0500\n")); |
| 1162 | assert!(text.contains("x-braid-note two\n lines\n\nMade in braid\n")); |
| 1163 | |
| 1164 | let report = write_to_repo( |
| 1165 | repo.path(), |
| 1166 | &objects, |
| 1167 | &map, |
| 1168 | &[RefUpdate { |
| 1169 | name: b"refs/heads/braid".to_vec(), |
| 1170 | target: RefTarget::Changeset(changeset), |
| 1171 | }], |
| 1172 | ) |
| 1173 | .unwrap(); |
| 1174 | assert_eq!((report.commits, report.trees, report.blobs), (1, 2, 2)); |
| 1175 | assert_eq!( |
| 1176 | repo.git(&["rev-parse", "refs/heads/braid"]), |
| 1177 | commit.to_hex() |
| 1178 | ); |
| 1179 | repo.git(&["fsck", "--strict", "--no-dangling"]); |
| 1180 | |
| 1181 | // Git writes the same tree for the same content. |
| 1182 | let same = repo.tree(&[ |
| 1183 | file("README", "hello from braid\n"), |
| 1184 | file("docs/a.md", "# A\n"), |
| 1185 | link("latest", "README"), |
| 1186 | submodule("vendor", SUBMODULE_A), |
| 1187 | ]); |
| 1188 | assert_eq!( |
| 1189 | repo.git(&["rev-parse", &format!("{commit}^{{tree}}")]), |
| 1190 | same |
| 1191 | ); |
| 1192 | |
| 1193 | // Importing the exported commit gives a changeset with the same root. |
| 1194 | let again = import_all(&repo, &objects, &mut map); |
| 1195 | assert_eq!(again.commits, 1); |
| 1196 | let reimported = changeset_of(&objects, &map, &commit); |
| 1197 | assert_eq!(reimported.root, root); |
| 1198 | assert!(reimported.git_origin.is_some()); |
| 1199 | } |
| 1200 | |
| 1201 | fn changeset_of(objects: &MemoryObjects, map: &GitMap, commit: &GitOid) -> Changeset { |
| 1202 | changeset(objects, map, &commit.to_hex()) |
| 1203 | } |
| 1204 | |
| 1205 | /// Stores the directories holding exactly `files`, regular files given by |
| 1206 | /// path and content, and returns the root. |
| 1207 | fn store_files(objects: &MemoryObjects, files: &[(&str, &str)]) -> ObjectId { |
| 1208 | fn store_dir(objects: &MemoryObjects, dir: &Dir) -> ObjectId { |
| 1209 | let entries = dir |
| 1210 | .iter() |
| 1211 | .map(|(name, node)| Entry { |
| 1212 | name: name.clone(), |
| 1213 | kind: match node { |
| 1214 | Node::Leaf(kind) => kind.clone(), |
| 1215 | Node::Dir(below) => EntryKind::Tree { |
| 1216 | tree: store_dir(objects, below), |
| 1217 | }, |
| 1218 | }, |
| 1219 | }) |
| 1220 | .collect(); |
| 1221 | store_tree(objects, entries) |
| 1222 | } |
| 1223 | let mut dir = Dir::new(); |
| 1224 | let changes: Vec<Change> = files |
| 1225 | .iter() |
| 1226 | .map(|(path, content)| file_change(objects, path, content)) |
| 1227 | .collect(); |
| 1228 | apply(&mut dir, &changes); |
| 1229 | store_dir(objects, &dir) |
| 1230 | } |
| 1231 | |
| 1232 | fn file_change(objects: &MemoryObjects, path: &str, content: &str) -> Change { |
| 1233 | let (blob, size) = store_blob(objects, content.as_bytes()); |
| 1234 | Change { |
| 1235 | path: path.as_bytes().to_vec(), |
| 1236 | op: ChangeOp::File { blob, size }, |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /// A changeset made in braid on `parent`, whose tree holds exactly `files` |
| 1241 | /// and whose change list is `changes`. |
| 1242 | fn braid_changeset( |
| 1243 | objects: &MemoryObjects, |
| 1244 | parent: ObjectId, |
| 1245 | files: &[(&str, &str)], |
| 1246 | changes: Vec<Change>, |
| 1247 | message: &str, |
| 1248 | ) -> ObjectId { |
| 1249 | let grace = Signature { |
| 1250 | name: b"Grace Hopper".to_vec(), |
| 1251 | email: b"grace@example.org".to_vec(), |
| 1252 | seconds: 1_700_000_500, |
| 1253 | offset_minutes: -300, |
| 1254 | }; |
| 1255 | let assembled = ChangesetParts { |
| 1256 | parents: vec![parent], |
| 1257 | root: store_files(objects, files), |
| 1258 | changes, |
| 1259 | author: grace.clone(), |
| 1260 | committer: grace, |
| 1261 | message: message.as_bytes().to_vec(), |
| 1262 | extras: Vec::new(), |
| 1263 | git_origin: None, |
| 1264 | } |
| 1265 | .assemble() |
| 1266 | .unwrap(); |
| 1267 | objects.put(Kind::Changeset, &assembled.encoded).unwrap() |
| 1268 | } |
| 1269 | |
| 1270 | #[test] |
| 1271 | fn exported_commits_import_as_the_changesets_they_came_from() { |
| 1272 | let mut repo = Repo::new(); |
| 1273 | let imported = repo.snapshot( |
| 1274 | "main", |
| 1275 | &[file("README", "hi\n"), file("src/a.rs", "a\n")], |
| 1276 | "imported", |
| 1277 | ); |
| 1278 | let objects = MemoryObjects::new(); |
| 1279 | let mut map = GitMap::new(); |
| 1280 | import_all(&repo, &objects, &mut map); |
| 1281 | |
| 1282 | // Two changesets made in braid, one on top of the other. |
| 1283 | let base = map.changeset(&oid(&imported)).unwrap(); |
| 1284 | let first_files = [("README", "hello from braid\n"), ("src/a.rs", "a\n")]; |
| 1285 | let first = braid_changeset( |
| 1286 | &objects, |
| 1287 | base, |
| 1288 | &first_files, |
| 1289 | vec![file_change(&objects, "README", "hello from braid\n")], |
| 1290 | "one\n", |
| 1291 | ); |
| 1292 | let second_files = [ |
| 1293 | ("README", "hello from braid\n"), |
| 1294 | ("docs/b.md", "# B\n"), |
| 1295 | ("src/a.rs", "a\n"), |
| 1296 | ]; |
| 1297 | let second = braid_changeset( |
| 1298 | &objects, |
| 1299 | first, |
| 1300 | &second_files, |
| 1301 | vec![file_change(&objects, "docs/b.md", "# B\n")], |
| 1302 | "two\n", |
| 1303 | ); |
| 1304 | |
| 1305 | // Exporting them records their commits, trees and blobs in the map. |
| 1306 | let before = map.counts(); |
| 1307 | let report = write_to_repo_recording( |
| 1308 | repo.path(), |
| 1309 | &objects, |
| 1310 | &mut map, |
| 1311 | &[RefUpdate { |
| 1312 | name: b"refs/heads/main".to_vec(), |
| 1313 | target: RefTarget::Changeset(second), |
| 1314 | }], |
| 1315 | ) |
| 1316 | .unwrap(); |
| 1317 | assert_eq!((report.commits, report.trees, report.blobs), (2, 3, 2)); |
| 1318 | let tip = report.refs[0].1; |
| 1319 | assert_eq!(map.changeset(&tip), Some(second)); |
| 1320 | assert_eq!(map.commit(&second), Some(tip)); |
| 1321 | let first_commit = map.commit(&first).unwrap(); |
| 1322 | assert_eq!(repo.git(&["rev-parse", "main~1"]), first_commit.to_hex()); |
| 1323 | let after = map.counts(); |
| 1324 | assert_eq!(after.commits, before.commits + 2); |
| 1325 | assert_eq!(after.trees, before.trees + 3); |
| 1326 | assert_eq!(after.blobs, before.blobs + 2); |
| 1327 | // The plain export of the same changesets records nothing, and agrees. |
| 1328 | let plain = write_to_repo( |
| 1329 | repo.path(), |
| 1330 | &objects, |
| 1331 | &map, |
| 1332 | &[RefUpdate { |
| 1333 | name: b"refs/heads/again".to_vec(), |
| 1334 | target: RefTarget::Changeset(second), |
| 1335 | }], |
| 1336 | ) |
| 1337 | .unwrap(); |
| 1338 | assert_eq!(plain.refs[0].1, tip); |
| 1339 | assert_eq!((plain.commits, plain.trees, plain.blobs), (0, 0, 0)); |
| 1340 | |
| 1341 | // Importing the exported commits finds every object in the map: nothing |
| 1342 | // new, and the branch is the changeset made in braid. |
| 1343 | let again = import_all(&repo, &objects, &mut map); |
| 1344 | assert_eq!( |
| 1345 | (again.commits, again.trees, again.blobs, again.raw_trees), |
| 1346 | (0, 0, 0, 0) |
| 1347 | ); |
| 1348 | let main = again |
| 1349 | .refs |
| 1350 | .iter() |
| 1351 | .find(|r| r.name == b"refs/heads/main") |
| 1352 | .unwrap(); |
| 1353 | assert_eq!(main.braid, second); |
| 1354 | |
| 1355 | // A commit made in git on top of them builds on the braid changeset. |
| 1356 | let mut next_files: Vec<_> = second_files.iter().map(|(p, c)| file(p, c)).collect(); |
| 1357 | next_files.push(file("c", "from git\n")); |
| 1358 | let next = repo.snapshot("main", &next_files, "made in git"); |
| 1359 | let later = import_all(&repo, &objects, &mut map); |
| 1360 | assert_eq!(later.commits, 1); |
| 1361 | let next_changeset = changeset(&objects, &map, &next); |
| 1362 | assert_eq!(next_changeset.parents, [second]); |
| 1363 | assert_eq!(changes(&objects, &map, &next), pairs(&[("c", "file")])); |
| 1364 | |
| 1365 | // The map still encodes, and everything round-trips. |
| 1366 | assert_eq!(GitMap::from_bytes(&map.to_bytes()).unwrap(), map); |
| 1367 | assert_admissible(&objects, &map); |
| 1368 | round_trip(&repo); |
| 1369 | } |
| 1370 | |
| 1371 | #[test] |
| 1372 | fn a_changeset_that_disagrees_with_its_origin_is_not_exported() { |
| 1373 | let mut repo = Repo::new(); |
| 1374 | let first = repo.snapshot("main", &[file("a", "1")], "one"); |
| 1375 | let second = repo.snapshot("main", &[file("a", "2")], "two"); |
| 1376 | let objects = MemoryObjects::new(); |
| 1377 | let mut map = GitMap::new(); |
| 1378 | import_all(&repo, &objects, &mut map); |
| 1379 | let genuine = changeset(&objects, &map, &second); |
| 1380 | let first_changeset = changeset(&objects, &map, &first); |
| 1381 | |
| 1382 | let mut wrong_root = genuine.clone(); |
| 1383 | wrong_root.root = first_changeset.root; |
| 1384 | let mut wrong_parents = genuine.clone(); |
| 1385 | wrong_parents.parents = Vec::new(); |
| 1386 | for forged in [wrong_root, wrong_parents] { |
| 1387 | let id = objects.put(Kind::Changeset, &forged.encode()).unwrap(); |
| 1388 | let err = export_commit(&objects, &map, &id).unwrap_err(); |
| 1389 | assert!(matches!(err, Error::OriginMismatch { .. }), "{err}"); |
| 1390 | } |
| 1391 | let genuine_id = map.changeset(&oid(&second)).unwrap(); |
| 1392 | assert_eq!( |
| 1393 | export_commit(&objects, &map, &genuine_id).unwrap().0, |
| 1394 | oid(&second) |
| 1395 | ); |
| 1396 | } |
| 1397 | |
| 1398 | /// A history with a merge, an annotated tag, an executable, a symlink and a |
| 1399 | /// path that is not ASCII: every commit, oldest first. |
| 1400 | fn varied_history(repo: &mut Repo) -> Vec<String> { |
| 1401 | let base = vec![ |
| 1402 | file("README.md", "# demo\n"), |
| 1403 | file("src/ñandú.rs", "fn ñandú() {}\n"), |
| 1404 | exec("bin/run", "#!/bin/sh\necho run\n"), |
| 1405 | link("latest", "README.md"), |
| 1406 | ]; |
| 1407 | let first = repo.snapshot("main", &base, "first"); |
| 1408 | repo.set_ref("refs/heads/feature", &first); |
| 1409 | let mut on_feature = base.clone(); |
| 1410 | on_feature.push(file("docs/guía.md", "una guía\n")); |
| 1411 | let feature = repo.snapshot("feature", &on_feature, "a guide"); |
| 1412 | let mut on_main = base.clone(); |
| 1413 | on_main[0] = file("README.md", "# demo, again\n"); |
| 1414 | let second = repo.snapshot("main", &on_main, "main moves on"); |
| 1415 | let mut merged = on_main.clone(); |
| 1416 | merged.push(file("docs/guía.md", "una guía\n")); |
| 1417 | let merge = repo.merge("main", &["feature"], &merged, "merge the guide"); |
| 1418 | repo.annotated_tag("v1", &merge, "release 1"); |
| 1419 | merged[2] = file("bin/run", "#!/bin/sh\necho run, no longer executable\n"); |
| 1420 | let last = repo.snapshot("main", &merged, "after the tag"); |
| 1421 | vec![first, feature, second, merge, last] |
| 1422 | } |
| 1423 | |
| 1424 | #[test] |
| 1425 | fn imported_history_exports_to_its_commits_without_the_map() { |
| 1426 | let mut repo = Repo::new(); |
| 1427 | let commits = varied_history(&mut repo); |
| 1428 | let objects = MemoryObjects::new(); |
| 1429 | let mut map = GitMap::new(); |
| 1430 | import_all(&repo, &objects, &mut map); |
| 1431 | let changesets: Vec<ObjectId> = commits |
| 1432 | .iter() |
| 1433 | .map(|commit| map.changeset(&oid(commit)).unwrap()) |
| 1434 | .collect(); |
| 1435 | |
| 1436 | // What a server holds: the changesets and their git origins, and a map |
| 1437 | // that knows nothing of them. |
| 1438 | let empty = GitMap::new(); |
| 1439 | let head = *changesets.last().unwrap(); |
| 1440 | let mut exporter = Exporter::new(&objects, &empty); |
| 1441 | assert_eq!( |
| 1442 | exporter.commit_id(&head).unwrap(), |
| 1443 | oid(commits.last().unwrap()) |
| 1444 | ); |
| 1445 | for (commit, changeset) in commits.iter().zip(&changesets) { |
| 1446 | assert_eq!(exporter.commit_id(changeset).unwrap(), oid(commit)); |
| 1447 | assert_eq!( |
| 1448 | exporter.object(&oid(commit)).unwrap(), |
| 1449 | ( |
| 1450 | GitKind::Commit, |
| 1451 | repo.git_bytes(&["cat-file", "commit", commit]) |
| 1452 | ) |
| 1453 | ); |
| 1454 | // One at a time, through exporters of their own, too. |
| 1455 | assert_eq!( |
| 1456 | export_commit(&objects, &empty, changeset).unwrap().0, |
| 1457 | oid(commit) |
| 1458 | ); |
| 1459 | } |
| 1460 | // The trees, the blobs and the commits it computed are what the import |
| 1461 | // linked them to: recording them gives the importing map's pairings. |
| 1462 | let mut learned = GitMap::new(); |
| 1463 | learned.record(&exporter.pairings()); |
| 1464 | for (commit, changeset) in commits.iter().zip(&changesets) { |
| 1465 | assert_eq!(learned.changeset(&oid(commit)), Some(*changeset)); |
| 1466 | let root = decode(&objects, changeset).root; |
| 1467 | let tree = repo.git(&["rev-parse", &format!("{commit}^{{tree}}")]); |
| 1468 | assert_eq!(learned.tree(&oid(&tree)), Some(root)); |
| 1469 | } |
| 1470 | // A later export starts from what was recorded. |
| 1471 | let mut again = Exporter::new(&objects, &learned); |
| 1472 | assert_eq!( |
| 1473 | again.commit_id(&head).unwrap(), |
| 1474 | oid(commits.last().unwrap()) |
| 1475 | ); |
| 1476 | assert!(again.pairings().commits().is_empty()); |
| 1477 | } |
| 1478 | |
| 1479 | #[test] |
| 1480 | fn without_the_map_an_origin_must_name_the_root_and_the_parents() { |
| 1481 | let mut repo = Repo::new(); |
| 1482 | let first = repo.snapshot("main", &[file("a", "1")], "one"); |
| 1483 | let second = repo.snapshot("main", &[file("a", "2")], "two"); |
| 1484 | repo.set_ref("refs/heads/other", &first); |
| 1485 | let other = repo.snapshot("other", &[file("a", "1"), file("b", "b")], "elsewhere"); |
| 1486 | let objects = MemoryObjects::new(); |
| 1487 | let mut map = GitMap::new(); |
| 1488 | import_all(&repo, &objects, &mut map); |
| 1489 | let genuine = changeset(&objects, &map, &second); |
| 1490 | let first_changeset = changeset(&objects, &map, &first); |
| 1491 | let other_id = map.changeset(&oid(&other)).unwrap(); |
| 1492 | |
| 1493 | // Another tree than the commit's, another parent at its place, and a |
| 1494 | // parent too few. |
| 1495 | let mut wrong_root = genuine.clone(); |
| 1496 | wrong_root.root = first_changeset.root; |
| 1497 | let mut wrong_parent = genuine.clone(); |
| 1498 | wrong_parent.parents = vec![other_id]; |
| 1499 | let mut no_parent = genuine.clone(); |
| 1500 | no_parent.parents = Vec::new(); |
| 1501 | let empty = GitMap::new(); |
| 1502 | for (forged, what) in [ |
| 1503 | (wrong_root, "names tree"), |
| 1504 | (wrong_parent, "parent 0"), |
| 1505 | (no_parent, "parents"), |
| 1506 | ] { |
| 1507 | let id = objects.put(Kind::Changeset, &forged.encode()).unwrap(); |
| 1508 | let err = export_commit(&objects, &empty, &id).unwrap_err(); |
| 1509 | assert!( |
| 1510 | matches!(&err, Error::OriginMismatch { detail, .. } if detail.contains(what)), |
| 1511 | "{err}" |
| 1512 | ); |
| 1513 | // The map refuses them too. |
| 1514 | assert!(matches!( |
| 1515 | export_commit(&objects, &map, &id), |
| 1516 | Err(Error::OriginMismatch { .. }) |
| 1517 | )); |
| 1518 | } |
| 1519 | let genuine_id = map.changeset(&oid(&second)).unwrap(); |
| 1520 | assert_eq!( |
| 1521 | export_commit(&objects, &empty, &genuine_id).unwrap().0, |
| 1522 | oid(&second) |
| 1523 | ); |
| 1524 | } |
| 1525 | |
| 1526 | #[test] |
| 1527 | fn without_the_map_a_tree_that_is_not_canonical_cannot_be_checked() { |
| 1528 | let mut repo = Repo::new(); |
| 1529 | let x = repo.blob(b"x\n"); |
| 1530 | let odd = repo.literally("tree", &raw_tree(&[("100664", b"group-writable", &x)])); |
| 1531 | let root = repo.mktree(&[("40000", "tree", &odd, b"odd")]); |
| 1532 | let commit = repo.commit(&root, &[], "an odd tree"); |
| 1533 | repo.set_ref("refs/heads/main", &commit); |
| 1534 | let objects = MemoryObjects::new(); |
| 1535 | let mut map = GitMap::new(); |
| 1536 | import_all(&repo, &objects, &mut map); |
| 1537 | let id = map.changeset(&oid(&commit)).unwrap(); |
| 1538 | // With the map that imported it, the commit comes back; without it, its |
| 1539 | // tree is not the canonical one, and nothing tells the two apart. |
| 1540 | assert_eq!(export_commit(&objects, &map, &id).unwrap().0, oid(&commit)); |
| 1541 | let err = export_commit(&objects, &GitMap::new(), &id).unwrap_err(); |
| 1542 | assert!(matches!(err, Error::OriginMismatch { .. }), "{err}"); |
| 1543 | } |
| 1544 | |
| 1545 | /// The git id of the empty tree. |
| 1546 | const EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; |
| 1547 | |
| 1548 | #[test] |
| 1549 | fn the_empty_tree_need_not_be_stored() { |
| 1550 | // Git treats the empty tree as always present and does not store it when |
| 1551 | // a commit uses it; the import and the round trip must cope. |
| 1552 | let mut repo = Repo::new(); |
| 1553 | let empty = repo.commit(EMPTY_TREE, &[], "nothing yet"); |
| 1554 | repo.set_ref("refs/heads/main", &empty); |
| 1555 | let stored = repo |
| 1556 | .path() |
| 1557 | .join("objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904"); |
| 1558 | assert!(!stored.exists(), "git did not store the empty tree"); |
| 1559 | let (objects, map) = check(&mut repo, |repo| { |
| 1560 | repo.snapshot("main", &[file("a", "1")], "something"); |
| 1561 | 1 |
| 1562 | }); |
| 1563 | assert!(changes(&objects, &map, &empty).is_empty()); |
| 1564 | assert!(map.raw_tree(&oid(EMPTY_TREE)).is_none()); |
| 1565 | } |
| 1566 | |
| 1567 | #[test] |
| 1568 | fn empty_directories_are_left_out() { |
| 1569 | let mut repo = Repo::new(); |
| 1570 | let f = repo.blob(b"f\n"); |
| 1571 | let g = repo.blob(b"g\n"); |
| 1572 | // An empty directory beside a file. |
| 1573 | let single = repo.mktree(&[ |
| 1574 | ("100644", "blob", &f, b"f"), |
| 1575 | ("40000", "tree", EMPTY_TREE, b"empty"), |
| 1576 | ]); |
| 1577 | // A chain of them: a/b/c, where c is the empty tree. |
| 1578 | let b = repo.mktree(&[("40000", "tree", EMPTY_TREE, b"c")]); |
| 1579 | let a = repo.mktree(&[("40000", "tree", &b, b"b")]); |
| 1580 | // A directory with a file and an empty directory. |
| 1581 | let sub = repo.mktree(&[ |
| 1582 | ("100644", "blob", &g, b"g"), |
| 1583 | ("40000", "tree", EMPTY_TREE, b"hollow"), |
| 1584 | ]); |
| 1585 | let root = repo.mktree(&[ |
| 1586 | ("100644", "blob", &f, b"f"), |
| 1587 | ("40000", "tree", &single, b"single"), |
| 1588 | ("40000", "tree", &a, b"a"), |
| 1589 | ("40000", "tree", &sub, b"sub"), |
| 1590 | ]); |
| 1591 | let first = repo.commit(&root, &[], "empty directories"); |
| 1592 | // A root holding nothing but empty directories is the empty tree. |
| 1593 | let hollow_root = repo.mktree(&[ |
| 1594 | ("40000", "tree", &a, b"a"), |
| 1595 | ("40000", "tree", EMPTY_TREE, b"e"), |
| 1596 | ]); |
| 1597 | let hollow = repo.commit(&hollow_root, &[&first], "only empty directories"); |
| 1598 | repo.set_ref("refs/heads/main", &hollow); |
| 1599 | |
| 1600 | let (objects, map) = check(&mut repo, |repo| { |
| 1601 | let again = repo.mktree(&[("100644", "blob", &g, b"g"), ("40000", "tree", &b, b"b")]); |
| 1602 | let next = repo.commit(&again, &[&hollow], "a file again"); |
| 1603 | repo.set_ref("refs/heads/main", &next); |
| 1604 | 1 |
| 1605 | }); |
| 1606 | assert_eq!( |
| 1607 | changes(&objects, &map, &first), |
| 1608 | pairs(&[("f", "file"), ("single/f", "file"), ("sub/g", "file")]) |
| 1609 | ); |
| 1610 | assert_eq!( |
| 1611 | changes(&objects, &map, &hollow), |
| 1612 | pairs(&[("f", "delete"), ("single/f", "delete"), ("sub/g", "delete")]) |
| 1613 | ); |
| 1614 | let empty = map.tree(&oid(EMPTY_TREE)).unwrap(); |
| 1615 | assert_eq!(changeset(&objects, &map, &hollow).root, empty); |
| 1616 | assert_eq!(map.tree(&oid(&b)), Some(empty)); |
| 1617 | assert_eq!(map.tree(&oid(&a)), Some(empty)); |
| 1618 | // Every git tree that lost an entry keeps its bytes; the empty tree |
| 1619 | // itself is canonical. |
| 1620 | for tree in [&single, &b, &a, &sub, &root, &hollow_root] { |
| 1621 | assert!(map.raw_tree(&oid(tree)).is_some(), "{tree}"); |
| 1622 | } |
| 1623 | assert!(map.raw_tree(&oid(EMPTY_TREE)).is_none()); |
| 1624 | } |
| 1625 | |
| 1626 | #[test] |
| 1627 | fn a_commit_that_only_adds_or_removes_an_empty_directory() { |
| 1628 | let mut repo = Repo::new(); |
| 1629 | let f = repo.blob(b"f\n"); |
| 1630 | let plain = repo.mktree(&[("100644", "blob", &f, b"f")]); |
| 1631 | let base = repo.commit(&plain, &[], "a file"); |
| 1632 | let with_empty = repo.mktree(&[ |
| 1633 | ("100644", "blob", &f, b"f"), |
| 1634 | ("40000", "tree", EMPTY_TREE, b"empty"), |
| 1635 | ]); |
| 1636 | let add = repo.commit(&with_empty, &[&base], "add an empty directory"); |
| 1637 | let inner = repo.mktree(&[("40000", "tree", EMPTY_TREE, b"deeper")]); |
| 1638 | let nested = repo.mktree(&[ |
| 1639 | ("100644", "blob", &f, b"f"), |
| 1640 | ("40000", "tree", &inner, b"empty"), |
| 1641 | ]); |
| 1642 | let deepen = repo.commit(&nested, &[&add], "nest another one inside"); |
| 1643 | let remove = repo.commit(&plain, &[&deepen], "remove it"); |
| 1644 | repo.set_ref("refs/heads/main", &remove); |
| 1645 | |
| 1646 | let (objects, map) = check(&mut repo, |repo| { |
| 1647 | let again = repo.commit(&with_empty, &[&remove], "add it again"); |
| 1648 | repo.set_ref("refs/heads/main", &again); |
| 1649 | 1 |
| 1650 | }); |
| 1651 | let root = changeset(&objects, &map, &base).root; |
| 1652 | for (commit, parent) in [(&add, &base), (&deepen, &add), (&remove, &deepen)] { |
| 1653 | assert!(changes(&objects, &map, commit).is_empty(), "{commit}"); |
| 1654 | let changeset = changeset(&objects, &map, commit); |
| 1655 | assert_eq!(changeset.root, root, "{commit}"); |
| 1656 | assert_eq!(changeset.parents, [map.changeset(&oid(parent)).unwrap()]); |
| 1657 | // Still bit for bit. |
| 1658 | let (exported, bytes) = |
| 1659 | export_commit(&objects, &map, &map.changeset(&oid(commit)).unwrap()).unwrap(); |
| 1660 | assert_eq!(exported, oid(commit)); |
| 1661 | assert_eq!(bytes, repo.git_bytes(&["cat-file", "commit", commit])); |
| 1662 | } |
| 1663 | } |