use crate::{
    bare::GitBare,
    cmd::{oid::ObjectId, tree::TreeInfo},
    errors::{GitError, GitResult},
};

impl GitBare {
    pub fn resolve_tree(&self, oid: ObjectId) -> GitResult<TreeInfo> {
        let repo = self.gix_repo()?;
        let gix_id: gix::hash::ObjectId = (&oid).try_into()?;
        let header = repo.find_header(gix_id)?;
        if header.kind() != gix::object::Kind::Tree {
            return Err(GitError::ParseError(format!(
                "object {} is not a tree (type: {})",
                oid,
                header.kind()
            )));
        }

        let gix_tree = repo.find_tree(gix_id)?;
        let entry_count = gix_tree.iter().count();

        Ok(TreeInfo {
            oid,
            entry_count,
            is_empty: entry_count == 0,
        })
    }
}
