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