Skip to main content

parse_tree

Function parse_tree 

Source
pub fn parse_tree(data: &[u8]) -> Result<Vec<TreeEntry>>
Expand description

Parse the raw data of a tree object into its entries.

§Format

Each entry is "<mode> <name>\0<20-byte-sha1>" concatenated with no separator between entries.

§Errors

Returns Error::CorruptObject if the data is malformed.

Examples found in repository?
examples/walk_tree.rs (line 18)
12fn walk_tree(
13    repo: &grit_lib::repo::Repository,
14    tree_oid: grit_lib::objects::ObjectId,
15    prefix: &str,
16) -> grit_lib::error::Result<()> {
17    let obj = repo.odb.read(&tree_oid)?;
18    let entries = parse_tree(&obj.data)?;
19    for e in entries {
20        let name = String::from_utf8_lossy(&e.name);
21        let path = if prefix.is_empty() {
22            name.into_owned()
23        } else {
24            format!("{prefix}/{name}")
25        };
26        if e.mode == MODE_TREE {
27            println!("{path}/ (tree {})", e.oid);
28            walk_tree(repo, e.oid, &path)?;
29        } else {
30            println!("{path} -> {}", e.oid);
31        }
32    }
33    Ok(())
34}