2024-05-23 17:53:36 +01:00
|
|
|
//
|
2024-07-25 09:02:43 +01:00
|
|
|
use git_next_core::{server, BranchName, RepoConfig};
|
|
|
|
use git_next_git::{self as git, repository::open::OpenRepositoryLike};
|
|
|
|
|
2024-06-19 07:03:08 +01:00
|
|
|
use std::path::PathBuf;
|
2024-05-23 17:53:36 +01:00
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
use derive_more::Display;
|
|
|
|
use tracing::info;
|
2024-05-23 16:50:36 +01:00
|
|
|
|
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-07-25 09:02:43 +01:00
|
|
|
open_repository: &dyn OpenRepositoryLike,
|
|
|
|
) -> Result<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-07-25 09:02:43 +01:00
|
|
|
let 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-07-25 09:02:43 +01:00
|
|
|
fn required_branch(branch_name: &BranchName, branches: &[BranchName]) -> Result<()> {
|
2024-06-19 07:03:08 +01:00
|
|
|
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")]
|
2024-07-25 09:02:43 +01:00
|
|
|
Config(#[from] server::Error),
|
2024-06-19 07:03:08 +01:00
|
|
|
|
|
|
|
#[display("toml")]
|
|
|
|
Toml(#[from] toml::de::Error),
|
|
|
|
|
|
|
|
#[display("push")]
|
|
|
|
Push(#[from] git::push::Error),
|
|
|
|
|
|
|
|
#[display("branch not found: {}", 0)]
|
2024-07-25 09:02:43 +01:00
|
|
|
BranchNotFound(BranchName),
|
2024-04-09 14:52:12 +01:00
|
|
|
}
|