git-next/crates/core/src/git/validation/remotes.rs
Paul Campbell 2ec5ae1d51
All checks were successful
Rust / build (push) Successful in 2m40s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
tests: restore unlinked test file
2024-07-26 19:18:12 +01:00

76 lines
2.2 KiB
Rust

//
use crate::{
git::{self, repository::open::OpenRepositoryLike},
RemoteUrl,
};
use tracing::info;
#[tracing::instrument(skip_all)]
pub fn validate_default_remotes(
open_repository: &dyn OpenRepositoryLike,
repo_details: &git::RepoDetails,
) -> Result<()> {
let push_remote = open_repository
.find_default_remote(git::repository::Direction::Push)
.ok_or_else(|| Error::NoDefaultPushRemote)?;
let fetch_remote = open_repository
.find_default_remote(git::repository::Direction::Fetch)
.ok_or_else(|| Error::NoDefaultFetchRemote)?;
let Some(remote_url) = repo_details.remote_url() else {
return Err(git::validation::remotes::Error::UnableToOpenRepo(
"Unable to build forge url".to_string(),
));
};
info!(config = %remote_url, push = %push_remote, fetch = %fetch_remote, "Check remotes match");
if !remote_url.matches(&push_remote) {
return Err(Error::MismatchDefaultPushRemote {
found: push_remote,
expected: remote_url,
});
}
if !remote_url.matches(&fetch_remote) {
return Err(Error::MismatchDefaultFetchRemote {
found: fetch_remote,
expected: remote_url,
});
}
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: RemoteUrl,
expected: RemoteUrl,
},
#[error("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")]
MismatchDefaultFetchRemote {
found: RemoteUrl,
expected: RemoteUrl,
},
#[error("Unable to open repo")]
GixOpen(#[from] gix::open::Error),
}