2024-05-11 19:46:20 +01:00
|
|
|
use tracing::info;
|
|
|
|
|
2024-05-26 08:56:01 +01:00
|
|
|
use crate as git;
|
2024-05-11 19:46:20 +01:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2024-06-19 07:03:08 +01:00
|
|
|
pub fn validate_default_remotes(
|
2024-06-19 16:40:10 +01:00
|
|
|
open_repository: &dyn git::repository::OpenRepositoryLike,
|
2024-05-26 08:56:01 +01:00
|
|
|
repo_details: &git::RepoDetails,
|
|
|
|
) -> Result<()> {
|
2024-06-19 16:40:10 +01:00
|
|
|
let push_remote = open_repository
|
2024-06-09 10:21:09 +01:00
|
|
|
.find_default_remote(git::repository::Direction::Push)
|
|
|
|
.ok_or_else(|| Error::NoDefaultPushRemote)?;
|
2024-06-19 16:40:10 +01:00
|
|
|
let fetch_remote = open_repository
|
2024-06-09 10:21:09 +01:00
|
|
|
.find_default_remote(git::repository::Direction::Fetch)
|
|
|
|
.ok_or_else(|| Error::NoDefaultFetchRemote)?;
|
2024-05-19 20:02:06 +01:00
|
|
|
let git_remote = repo_details.git_remote();
|
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-06-03 08:04:48 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2024-05-11 19:46:20 +01:00
|
|
|
pub enum Error {
|
2024-06-03 08:04:48 +01:00
|
|
|
#[error("no default push remote")]
|
2024-05-11 19:46:20 +01:00
|
|
|
NoDefaultPushRemote,
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("no default fetch remote")]
|
2024-05-18 11:41:18 +01:00
|
|
|
NoDefaultFetchRemote,
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("no url for default push remote")]
|
2024-05-11 19:46:20 +01:00
|
|
|
NoUrlForDefaultPushRemote,
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("no hostname for default push remote")]
|
2024-05-11 19:46:20 +01:00
|
|
|
NoHostnameForDefaultPushRemote,
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("unable to open repo: {0}")]
|
2024-05-11 19:46:20 +01:00
|
|
|
UnableToOpenRepo(String),
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("io")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
|
|
|
#[error("MismatchDefaultPushRemote(found: {found}, expected: {expected})")]
|
2024-05-11 19:46:20 +01:00
|
|
|
MismatchDefaultPushRemote {
|
2024-05-26 08:56:01 +01:00
|
|
|
found: git::GitRemote,
|
|
|
|
expected: git::GitRemote,
|
2024-05-11 19:46:20 +01:00
|
|
|
},
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")]
|
2024-05-11 19:46:20 +01:00
|
|
|
MismatchDefaultFetchRemote {
|
2024-05-26 08:56:01 +01:00
|
|
|
found: git::GitRemote,
|
|
|
|
expected: git::GitRemote,
|
2024-05-11 19:46:20 +01:00
|
|
|
},
|
|
|
|
}
|