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

56 lines
1.6 KiB
Rust
Raw Normal View History

//
use git_next_core::{
git::{repository::open::OpenRepositoryLike, RepoDetails},
BranchName, RepoConfig,
};
use std::path::PathBuf;
use derive_more::Display;
use tracing::{info, instrument};
2024-05-23 16:50:36 +01:00
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
#[instrument(skip_all, fields(branch = %repo_details.branch))]
pub async fn config_from_repository(
repo_details: RepoDetails,
open_repository: &dyn OpenRepositoryLike,
) -> Result<RepoConfig> {
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)
2024-04-09 10:44:01 +01:00
}
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<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error, Display)]
pub enum Error {
#[display("file")]
File(#[from] crate::git::file::Error),
#[display("config")]
Config(#[from] git_next_core::server::Error),
#[display("toml")]
Toml(#[from] toml::de::Error),
#[display("push")]
Push(#[from] crate::git::push::Error),
#[display("branch not found: {}", 0)]
BranchNotFound(BranchName),
}