// use git_next_core::{server, BranchName, RepoConfig}; use git_next_git::{self as git, repository::open::OpenRepositoryLike}; use std::path::PathBuf; use derive_more::Display; use tracing::info; /// 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 OpenRepositoryLike, ) -> Result { info!("Loading .git-next.toml from repo"); let contents = open_repository.read_file(&repo_details.branch, &PathBuf::from(".git-next.toml"))?; let 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: &BranchName, branches: &[BranchName]) -> Result<()> { branches .iter() .find(|branch| *branch == branch_name) .ok_or_else(|| Error::BranchNotFound(branch_name.clone()))?; Ok(()) } pub type Result = core::result::Result; #[derive(Debug, thiserror::Error, Display)] pub enum Error { #[display("file")] File(#[from] git::file::Error), #[display("config")] Config(#[from] server::Error), #[display("toml")] Toml(#[from] toml::de::Error), #[display("push")] Push(#[from] git::push::Error), #[display("branch not found: {}", 0)] BranchNotFound(BranchName), }