Elffuss BraidHub

utopia-ia/elffuss-braid public

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