Elffuss BraidHub

utopia-ia/elffuss-braid public

71ae7b5a0cf6 · 4.9 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Helpers for the unit tests: an in-memory repository with short ways to
5//! write files, trees and changesets, and to read them back.
6
7use std::collections::BTreeMap;
8
9use braid_format::{
10 Change, ChangeOp, EntryKind, GitId, Kind, MemoryObjects, ObjectId, Objects, Signature,
11};
12
13use crate::access::{EMPTY_TREE, read_blob, read_changeset, read_tree};
14use crate::apply::apply;
15use crate::commit::commit;
16use crate::path::join;
17
18pub(crate) fn signature(name: &str) -> Signature {
19 Signature {
20 name: name.as_bytes().to_vec(),
21 email: format!("{name}@example.org").into_bytes(),
22 seconds: 1_700_000_000,
23 offset_minutes: 60,
24 }
25}
26
27pub(crate) fn delete(path: &str) -> Change {
28 Change {
29 path: path.as_bytes().to_vec(),
30 op: ChangeOp::Delete,
31 }
32}
33
34pub(crate) fn gitlink(path: &str, byte: u8) -> Change {
35 Change {
36 path: path.as_bytes().to_vec(),
37 op: ChangeOp::Gitlink {
38 commit: GitId::Sha1([byte; 20]),
39 },
40 }
41}
42
43pub(crate) struct Repo {
44 pub(crate) objects: MemoryObjects,
45}
46
47impl Repo {
48 pub(crate) fn new() -> Self {
49 Repo {
50 objects: MemoryObjects::new(),
51 }
52 }
53
54 pub(crate) fn blob(&self, content: &str) -> ObjectId {
55 self.objects.put(Kind::Blob, content.as_bytes()).unwrap()
56 }
57
58 pub(crate) fn file_kind(&self, content: &str) -> EntryKind {
59 EntryKind::File {
60 blob: self.blob(content),
61 size: content.len() as u64,
62 }
63 }
64
65 pub(crate) fn set(&self, path: &str, content: &str) -> Change {
66 Change {
67 path: path.as_bytes().to_vec(),
68 op: ChangeOp::File {
69 blob: self.blob(content),
70 size: content.len() as u64,
71 },
72 }
73 }
74
75 pub(crate) fn exec(&self, path: &str, content: &str) -> Change {
76 Change {
77 path: path.as_bytes().to_vec(),
78 op: ChangeOp::Executable {
79 blob: self.blob(content),
80 size: content.len() as u64,
81 },
82 }
83 }
84
85 pub(crate) fn symlink(&self, path: &str, target: &str) -> Change {
86 Change {
87 path: path.as_bytes().to_vec(),
88 op: ChangeOp::Symlink {
89 blob: self.blob(target),
90 size: target.len() as u64,
91 },
92 }
93 }
94
95 /// A tree with these regular files.
96 pub(crate) fn tree(&self, files: &[(&str, &str)]) -> ObjectId {
97 let changes: Vec<Change> = files
98 .iter()
99 .map(|(path, content)| self.set(path, content))
100 .collect();
101 apply(&self.objects, EMPTY_TREE, &changes).unwrap()
102 }
103
104 pub(crate) fn commit(&self, parents: &[ObjectId], changes: Vec<Change>) -> ObjectId {
105 commit(
106 &self.objects,
107 parents.to_vec(),
108 changes,
109 signature("author"),
110 signature("author"),
111 b"change\n".to_vec(),
112 Vec::new(),
113 )
114 .unwrap()
115 }
116
117 pub(crate) fn root(&self, changeset: ObjectId) -> ObjectId {
118 read_changeset(&self.objects, changeset).unwrap().root
119 }
120
121 /// Every file under `root`, as text: regular files by content,
122 /// executables as `exec:<content>`, symlinks as `link:<target>` and
123 /// gitlinks as `gitlink:<first byte>`.
124 pub(crate) fn files(&self, root: ObjectId) -> BTreeMap<String, String> {
125 let mut files = BTreeMap::new();
126 let mut pending = vec![(Vec::new(), root)];
127 while let Some((dir, tree)) = pending.pop() {
128 for entry in read_tree(&self.objects, tree).unwrap() {
129 let path = join(&dir, &entry.name);
130 let text = |blob: ObjectId| {
131 String::from_utf8(read_blob(&self.objects, blob).unwrap()).unwrap()
132 };
133 let value = match entry.kind {
134 EntryKind::Tree { tree } => {
135 pending.push((path, tree));
136 continue;
137 }
138 EntryKind::File { blob, .. } => text(blob),
139 EntryKind::Executable { blob, .. } => format!("exec:{}", text(blob)),
140 EntryKind::Symlink { blob, .. } => format!("link:{}", text(blob)),
141 EntryKind::Gitlink { commit } => format!("gitlink:{}", commit.as_bytes()[0]),
142 };
143 files.insert(String::from_utf8(path).unwrap(), value);
144 }
145 }
146 files
147 }
148}
149
150/// A path with `depth` names: `d/d/…/d/file`.
151pub(crate) fn deep_path(depth: usize) -> String {
152 let mut names = vec!["d"; depth - 1];
153 names.push("file");
154 names.join("/")
155}
156
157/// A map from path to text, for comparing with [`Repo::files`].
158pub(crate) fn text_map(files: &[(&str, &str)]) -> BTreeMap<String, String> {
159 files
160 .iter()
161 .map(|(path, value)| (path.to_string(), value.to_string()))
162 .collect()
163}