Elffuss BraidHub

utopia-ia/elffuss-braid public

38b3e04f7d4a · 3.2 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Trees as a workspace sees them: every file by path.
5
6use std::collections::{BTreeMap, BTreeSet};
7
8use braid_engine::{EMPTY_TREE, read_changeset, read_tree};
9use braid_format::{ChangeOp, EntryKind, ObjectId, Objects};
10
11use crate::error::Error;
12use crate::paths::join;
13
14/// Every file of a tree, by path, and every directory.
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
16pub(crate) struct Flat {
17 /// Regular files, executables, symlinks and gitlinks.
18 pub files: BTreeMap<Vec<u8>, ChangeOp>,
19 /// Paths of directories, the root excepted.
20 pub dirs: BTreeSet<Vec<u8>>,
21}
22
23/// Reads the whole tree `root`. The walk keeps its own stack.
24pub(crate) fn flatten<O: Objects>(objects: &O, root: ObjectId) -> Result<Flat, Error> {
25 let mut flat = Flat::default();
26 let mut pending = vec![(Vec::new(), root)];
27 while let Some((dir, tree)) = pending.pop() {
28 for entry in read_tree(objects, tree)? {
29 let path = join(&dir, &entry.name);
30 match &entry.kind {
31 EntryKind::Tree { tree } => {
32 flat.dirs.insert(path.clone());
33 pending.push((path, *tree));
34 }
35 kind => {
36 if let Some(op) = ChangeOp::from_entry(kind) {
37 flat.files.insert(path, op);
38 }
39 }
40 }
41 }
42 }
43 Ok(flat)
44}
45
46/// The root tree of a changeset, or the empty tree for none.
47pub(crate) fn root_of<O: Objects>(
48 objects: &O,
49 changeset: Option<ObjectId>,
50) -> Result<ObjectId, Error> {
51 match changeset {
52 Some(id) => Ok(read_changeset(objects, id)?.root),
53 None => Ok(EMPTY_TREE),
54 }
55}
56
57/// Whether two entries are the same kind of thing: regular files (with or
58/// without the executable bit), symlinks, or gitlinks.
59pub(crate) fn same_class(a: &ChangeOp, b: &ChangeOp) -> bool {
60 let class = |op: &ChangeOp| match op {
61 ChangeOp::File { .. } | ChangeOp::Executable { .. } => 0,
62 ChangeOp::Symlink { .. } => 1,
63 ChangeOp::Gitlink { .. } => 2,
64 ChangeOp::Delete => 3,
65 };
66 class(a) == class(b)
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72 use braid_format::{Change, Kind, MemoryObjects};
73
74 #[test]
75 fn flattening() {
76 let objects = MemoryObjects::new();
77 let blob = objects.put(Kind::Blob, b"x").unwrap();
78 let file = ChangeOp::File { blob, size: 1 };
79 let changes: Vec<Change> = ["a/b/c", "a/d", "e"]
80 .iter()
81 .map(|path| Change {
82 path: path.as_bytes().to_vec(),
83 op: file.clone(),
84 })
85 .collect();
86 let root = braid_engine::apply(&objects, EMPTY_TREE, &changes).unwrap();
87 let flat = flatten(&objects, root).unwrap();
88 assert_eq!(
89 flat.files.keys().cloned().collect::<Vec<_>>(),
90 [b"a/b/c".to_vec(), b"a/d".to_vec(), b"e".to_vec()]
91 );
92 assert_eq!(
93 flat.dirs.iter().cloned().collect::<Vec<_>>(),
94 [b"a".to_vec(), b"a/b".to_vec()]
95 );
96 assert_eq!(flatten(&objects, EMPTY_TREE).unwrap(), Flat::default());
97 }
98}