fn find_closest_directory_with_build_file()

in focus/internals/src/lib/model/repo.rs [756:801]


fn find_closest_directory_with_build_file(
    git_repo: &git2::Repository,
    commit_id: git2::Oid,
    path: impl AsRef<Path>,
    // ceiling: impl AsRef<Path>,
) -> Result<Option<PathBuf>> {
    let path = path.as_ref();
    let tree = git_repo
        .find_commit(commit_id)
        .context("Resolving commit")?
        .tree()
        .context("Resolving tree")?;

    let mut path = path.to_owned();
    loop {
        if let Ok(tree_entry) = tree.get_path(&path) {
            // If the entry is a tree, get it.
            if tree_entry.kind() == Some(ObjectType::Tree) {
                let tree_object = tree_entry
                    .to_object(git_repo)
                    .with_context(|| format!("Resolving tree {}", path.display()))?;
                let current_tree = tree_object.as_tree().unwrap();
                // Iterate through the tree to see if there is a build file.
                for entry in current_tree.iter() {
                    if entry.kind() == Some(ObjectType::Blob) {
                        if let Some(name) = entry.name() {
                            let candidate_path = PathBuf::from_str(name)?;
                            if is_build_definition(candidate_path) {
                                info!(?name, ?path, "Found build definition");

                                return Ok(Some(path));
                            }
                        }
                    }
                }
            }
        }

        if !path.pop() {
            // We have reached the root with no match.
            break;
        }
    }

    Ok(None)
}