2024-05-23 17:53:36 +01:00
|
|
|
//
|
2024-06-19 07:03:08 +01:00
|
|
|
use derive_more::Display;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use tracing::info;
|
2024-05-23 17:53:36 +01:00
|
|
|
|
2024-05-23 16:50:36 +01:00
|
|
|
use git_next_config as config;
|
|
|
|
use git_next_git as git;
|
|
|
|
|
2024-05-23 17:53:36 +01:00
|
|
|
/// Loads the [RepoConfig] from the `.git-next.toml` file in the repository
|
|
|
|
#[tracing::instrument(skip_all, fields(branch = %repo_details.branch))]
|
2024-06-19 07:03:08 +01:00
|
|
|
pub async fn config_from_repository(
|
2024-05-26 16:51:51 +01:00
|
|
|
repo_details: git::RepoDetails,
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository: &dyn git::repository::OpenRepositoryLike,
|
2024-06-19 07:03:08 +01:00
|
|
|
) -> Result<config::RepoConfig> {
|
2024-05-23 17:53:36 +01:00
|
|
|
info!("Loading .git-next.toml from repo");
|
2024-06-19 07:03:08 +01:00
|
|
|
let contents =
|
|
|
|
open_repository.read_file(&repo_details.branch, &PathBuf::from(".git-next.toml"))?;
|
2024-06-19 07:02:18 +01:00
|
|
|
let config = config::RepoConfig::parse(&contents)?;
|
2024-06-19 07:03:08 +01:00
|
|
|
let branches = open_repository.remote_branches()?;
|
|
|
|
required_branch(&config.branches().main(), &branches)?;
|
|
|
|
required_branch(&config.branches().next(), &branches)?;
|
|
|
|
required_branch(&config.branches().dev(), &branches)?;
|
2024-04-09 14:52:12 +01:00
|
|
|
Ok(config)
|
2024-04-09 10:44:01 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 07:03:08 +01:00
|
|
|
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(())
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
2024-04-09 14:52:12 +01:00
|
|
|
|
2024-06-19 07:03:08 +01:00
|
|
|
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),
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|