| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! The hints an import gives the store: each new tree, blob and raw commit |
| 5 | //! is named similar to what the commit's first parent has in its place. |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use std::cell::RefCell; |
| 10 | use std::convert::Infallible; |
| 11 | |
| 12 | use braid_format::{ |
| 13 | Changeset, Entry, EntryKind, Kind, MemoryObjects, ObjectId, Objects, read_directory, |
| 14 | }; |
| 15 | use braid_git::{GitMap, GitOid, ImportOptions, import}; |
| 16 | use common::{Repo, file}; |
| 17 | |
| 18 | /// Objects in memory that remember the hints they get: the id stored and |
| 19 | /// the id it was said to be similar to. |
| 20 | #[derive(Default)] |
| 21 | struct Hinted { |
| 22 | objects: MemoryObjects, |
| 23 | hints: RefCell<Vec<(ObjectId, ObjectId)>>, |
| 24 | } |
| 25 | |
| 26 | impl Objects for Hinted { |
| 27 | type Error = Infallible; |
| 28 | |
| 29 | fn get(&self, id: &ObjectId) -> Result<Option<(Kind, Vec<u8>)>, Self::Error> { |
| 30 | self.objects.get(id) |
| 31 | } |
| 32 | |
| 33 | fn put(&self, kind: Kind, payload: &[u8]) -> Result<ObjectId, Self::Error> { |
| 34 | self.objects.put(kind, payload) |
| 35 | } |
| 36 | |
| 37 | fn put_similar( |
| 38 | &self, |
| 39 | kind: Kind, |
| 40 | payload: &[u8], |
| 41 | similar: &ObjectId, |
| 42 | ) -> Result<ObjectId, Self::Error> { |
| 43 | let id = self.objects.put(kind, payload)?; |
| 44 | self.hints.borrow_mut().push((id, *similar)); |
| 45 | Ok(id) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn text(version: u32) -> String { |
| 50 | (0..50) |
| 51 | .map(|n| { |
| 52 | format!( |
| 53 | "line {n} of version {}\n", |
| 54 | if n == 25 { version } else { 0 } |
| 55 | ) |
| 56 | }) |
| 57 | .collect() |
| 58 | } |
| 59 | |
| 60 | fn changeset(objects: &Hinted, map: &GitMap, commit: &str) -> Changeset { |
| 61 | let oid: GitOid = commit.parse().unwrap(); |
| 62 | let id = map.changeset(&oid).unwrap(); |
| 63 | let (_, bytes) = objects.get(&id).unwrap().unwrap(); |
| 64 | Changeset::decode(&bytes).unwrap() |
| 65 | } |
| 66 | |
| 67 | fn entries(objects: &Hinted, tree: ObjectId) -> Vec<Entry> { |
| 68 | read_directory(tree, |id| objects.get(id).unwrap().map(|(_, bytes)| bytes)).unwrap() |
| 69 | } |
| 70 | |
| 71 | fn entry(entries: &[Entry], name: &str) -> EntryKind { |
| 72 | entries |
| 73 | .iter() |
| 74 | .find(|entry| entry.name == name.as_bytes()) |
| 75 | .unwrap() |
| 76 | .kind |
| 77 | .clone() |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn the_import_names_what_the_first_parent_has_in_place() { |
| 82 | let mut git = Repo::new(); |
| 83 | let first = git.snapshot( |
| 84 | "main", |
| 85 | &[file("dir/a.txt", text(1)), file("b.txt", "same")], |
| 86 | "first", |
| 87 | ); |
| 88 | let second = git.snapshot( |
| 89 | "main", |
| 90 | &[file("dir/a.txt", text(2)), file("b.txt", "same")], |
| 91 | "second", |
| 92 | ); |
| 93 | let objects = Hinted::default(); |
| 94 | let mut map = GitMap::new(); |
| 95 | import(git.path(), &objects, &mut map, &ImportOptions::default()).unwrap(); |
| 96 | |
| 97 | let before = changeset(&objects, &map, &first); |
| 98 | let after = changeset(&objects, &map, &second); |
| 99 | let old_root = entries(&objects, before.root); |
| 100 | let new_root = entries(&objects, after.root); |
| 101 | let tree = |kind| match kind { |
| 102 | EntryKind::Tree { tree } => tree, |
| 103 | other => panic!("{other:?}"), |
| 104 | }; |
| 105 | let old_dir = tree(entry(&old_root, "dir")); |
| 106 | let new_dir = tree(entry(&new_root, "dir")); |
| 107 | let blob = |kind| match kind { |
| 108 | EntryKind::File { blob, .. } => blob, |
| 109 | other => panic!("{other:?}"), |
| 110 | }; |
| 111 | let old_a = blob(entry(&entries(&objects, old_dir), "a.txt")); |
| 112 | let new_a = blob(entry(&entries(&objects, new_dir), "a.txt")); |
| 113 | |
| 114 | let mut hints = objects.hints.take(); |
| 115 | hints.sort(); |
| 116 | let mut expected = vec![ |
| 117 | (after.root, before.root), |
| 118 | (new_dir, old_dir), |
| 119 | (new_a, old_a), |
| 120 | (after.git_origin.unwrap(), before.git_origin.unwrap()), |
| 121 | ]; |
| 122 | expected.sort(); |
| 123 | // The first commit has no parent to be like, and b.txt did not change. |
| 124 | assert_eq!(hints, expected); |
| 125 | } |