git-next/crates/git/src/validation/repo.rs
Paul Campbell 621e599b31
All checks were successful
Rust / build (push) Successful in 1m31s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
refactor: git: use thiserror and cleanup errors
2024-06-03 20:31:39 +01:00

66 lines
1.9 KiB
Rust

use tracing::info;
use crate as git;
#[tracing::instrument(skip_all)]
pub fn validate_repo(
repository: &git::OpenRepository,
repo_details: &git::RepoDetails,
) -> Result<()> {
let Some(push_remote) = repository.find_default_remote(git::repository::Direction::Push) else {
return Err(Error::NoDefaultPushRemote);
};
let Some(fetch_remote) = repository.find_default_remote(git::repository::Direction::Fetch)
else {
return Err(Error::NoDefaultFetchRemote);
};
let git_remote = repo_details.git_remote();
info!(config = %git_remote, push = %push_remote, fetch = %fetch_remote, "Check remotes match");
if git_remote != push_remote {
return Err(Error::MismatchDefaultPushRemote {
found: push_remote,
expected: git_remote,
});
}
if git_remote != fetch_remote {
return Err(Error::MismatchDefaultFetchRemote {
found: fetch_remote,
expected: git_remote,
});
}
Ok(())
}
type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("no default push remote")]
NoDefaultPushRemote,
#[error("no default fetch remote")]
NoDefaultFetchRemote,
#[error("no url for default push remote")]
NoUrlForDefaultPushRemote,
#[error("no hostname for default push remote")]
NoHostnameForDefaultPushRemote,
#[error("unable to open repo: {0}")]
UnableToOpenRepo(String),
#[error("io")]
Io(#[from] std::io::Error),
#[error("MismatchDefaultPushRemote(found: {found}, expected: {expected})")]
MismatchDefaultPushRemote {
found: git::GitRemote,
expected: git::GitRemote,
},
#[error("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")]
MismatchDefaultFetchRemote {
found: git::GitRemote,
expected: git::GitRemote,
},
}