git-next/crates/git/src/validate.rs
Paul Campbell 341dc97a51
All checks were successful
Rust / build (push) Successful in 1m7s
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): add mock repository and tests
Repository is now behind an enum to allow selection of a mock Repsitory
for use in tests.
2024-05-21 19:47:01 +01:00

52 lines
1.7 KiB
Rust

use tracing::info;
use crate::repository::{Direction, OpenRepository};
use super::{GitRemote, RepoDetails};
#[tracing::instrument(skip_all)]
pub fn validate(repository: &OpenRepository, repo_details: &RepoDetails) -> Result<()> {
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);
};
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, derive_more::Display)]
pub enum Error {
NoDefaultPushRemote,
NoDefaultFetchRemote,
NoUrlForDefaultPushRemote,
NoHostnameForDefaultPushRemote,
UnableToOpenRepo(String),
Io(std::io::Error),
#[display("MismatchDefaultPushRemote(found: {found}, expected: {expected})")]
MismatchDefaultPushRemote {
found: GitRemote,
expected: GitRemote,
},
#[display("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")]
MismatchDefaultFetchRemote {
found: GitRemote,
expected: GitRemote,
},
}
impl std::error::Error for Error {}