forked from kemitix/git-next
55 lines
1.6 KiB
Rust
55 lines
1.6 KiB
Rust
//
|
|
use git_next_core::{
|
|
git::{repository::open::OpenRepositoryLike, RepoDetails},
|
|
BranchName, RepoConfig,
|
|
};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use derive_more::Display;
|
|
use tracing::{info, instrument};
|
|
|
|
/// 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)
|
|
}
|
|
|
|
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),
|
|
}
|