git-next/crates/repo-actor/src/load.rs

54 lines
1.6 KiB
Rust

//
use derive_more::Display;
use std::path::PathBuf;
use tracing::info;
use git_next_config as config;
use git_next_git as git;
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
pub async fn config_from_repository(
repo_details: git::RepoDetails,
open_repository: &dyn git::repository::OpenRepositoryLike,
) -> Result<config::RepoConfig> {
info!("Loading .git-next.toml from repo");
let contents =
open_repository.read_file(&repo_details.branch, &PathBuf::from(".git-next.toml"))?;
let config = config::RepoConfig::parse(&contents)?;
let branches = open_repository.remote_branches()?;
required_branch(&config.branches().main(), &branches)?;
required_branch(&config.branches().next(), &branches)?;
required_branch(&config.branches().dev(), &branches)?;
Ok(config)
}
fn required_branch(
branch_name: &config::BranchName,
branches: &[config::BranchName],
) -> Result<()> {
branches
.iter()
.find(|branch| *branch == branch_name)
.ok_or_else(|| Error::BranchNotFound(branch_name.clone()))?;
Ok(())
}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, thiserror::Error, Display)]
pub enum Error {
#[display("file")]
File(#[from] git::file::Error),
#[display("config")]
Config(#[from] config::server::Error),
#[display("toml")]
Toml(#[from] toml::de::Error),
#[display("push")]
Push(#[from] git::push::Error),
#[display("branch not found: {}", 0)]
BranchNotFound(config::BranchName),
}