2024-05-11 19:46:20 +01:00
|
|
|
use tracing::info;
|
|
|
|
|
2024-05-18 11:41:18 +01:00
|
|
|
use crate::repository::{open::OpenRepository, Direction};
|
|
|
|
|
|
|
|
use super::{GitRemote, RepoDetails};
|
2024-05-11 19:46:20 +01:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2024-05-18 11:41:18 +01:00
|
|
|
pub fn validate(repository: &OpenRepository, repo_details: &RepoDetails) -> Result<()> {
|
2024-05-11 19:46:20 +01:00
|
|
|
let git_remote = repo_details.git_remote();
|
2024-05-18 11:41:18 +01:00
|
|
|
let Some(push_remote) = repository.find_default_remote(Direction::Push) else {
|
|
|
|
return Err(Error::NoDefaultPushRemote);
|
|
|
|
};
|
|
|
|
let Some(fetch_remote) = repository.find_default_remote(Direction::Fetch) else {
|
|
|
|
return Err(Error::NoDefaultFetchRemote);
|
|
|
|
};
|
2024-05-11 19:46:20 +01:00
|
|
|
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>;
|
2024-05-14 16:28:17 +01:00
|
|
|
#[derive(Debug, derive_more::Display)]
|
2024-05-11 19:46:20 +01:00
|
|
|
pub enum Error {
|
|
|
|
NoDefaultPushRemote,
|
2024-05-18 11:41:18 +01:00
|
|
|
NoDefaultFetchRemote,
|
2024-05-11 19:46:20 +01:00
|
|
|
NoUrlForDefaultPushRemote,
|
|
|
|
NoHostnameForDefaultPushRemote,
|
|
|
|
UnableToOpenRepo(String),
|
|
|
|
Io(std::io::Error),
|
2024-05-14 16:28:17 +01:00
|
|
|
#[display("MismatchDefaultPushRemote(found: {found}, expected: {expected})")]
|
2024-05-11 19:46:20 +01:00
|
|
|
MismatchDefaultPushRemote {
|
|
|
|
found: GitRemote,
|
|
|
|
expected: GitRemote,
|
|
|
|
},
|
2024-05-14 16:28:17 +01:00
|
|
|
#[display("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")]
|
2024-05-11 19:46:20 +01:00
|
|
|
MismatchDefaultFetchRemote {
|
|
|
|
found: GitRemote,
|
|
|
|
expected: GitRemote,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
impl std::error::Error for Error {}
|