2024-07-25 09:02:43 +01:00
|
|
|
//
|
2024-07-26 06:49:09 +01:00
|
|
|
use crate::{
|
|
|
|
git::{self, repository::open::OpenRepositoryLike},
|
|
|
|
RemoteUrl,
|
|
|
|
};
|
2024-05-11 19:46:20 +01:00
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
use tracing::info;
|
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-07-25 09:02:43 +01:00
|
|
|
open_repository: &dyn 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-07-06 19:55:39 +01:00
|
|
|
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");
|
2024-07-26 08:24:35 +01:00
|
|
|
if !remote_url.matches(&push_remote) {
|
2024-05-11 19:46:20 +01:00
|
|
|
return Err(Error::MismatchDefaultPushRemote {
|
|
|
|
found: push_remote,
|
2024-07-06 19:55:39 +01:00
|
|
|
expected: remote_url,
|
2024-05-11 19:46:20 +01:00
|
|
|
});
|
|
|
|
}
|
2024-07-26 08:24:35 +01:00
|
|
|
if !remote_url.matches(&fetch_remote) {
|
2024-05-11 19:46:20 +01:00
|
|
|
return Err(Error::MismatchDefaultFetchRemote {
|
|
|
|
found: fetch_remote,
|
2024-07-06 19:55:39 +01:00
|
|
|
expected: remote_url,
|
2024-05-11 19:46:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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-07-06 19:55:39 +01:00
|
|
|
found: RemoteUrl,
|
|
|
|
expected: RemoteUrl,
|
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-07-06 19:55:39 +01:00
|
|
|
found: RemoteUrl,
|
|
|
|
expected: RemoteUrl,
|
2024-05-11 19:46:20 +01:00
|
|
|
},
|
2024-07-06 19:55:39 +01:00
|
|
|
|
|
|
|
#[error("Unable to open repo")]
|
|
|
|
GixOpen(#[from] gix::open::Error),
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|