From c3c4c41c732db52758d8ea42c0e4b6bad8a78ae1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 19 May 2024 15:05:24 +0100 Subject: [PATCH] refactor(git): only expose OpenRepository from repository::open --- crates/git/src/lib.rs | 2 +- crates/git/src/repository/mock.rs | 16 ++++--- crates/git/src/repository/mod.rs | 16 +++---- crates/git/src/repository/open/mod.rs | 44 +++++++++++++++++++ .../src/repository/{open.rs => open/oreal.rs} | 34 +++----------- crates/git/src/repository/real.rs | 35 ++++++++------- crates/git/src/validate.rs | 2 +- 7 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 crates/git/src/repository/open/mod.rs rename crates/git/src/repository/{open.rs => open/oreal.rs} (81%) diff --git a/crates/git/src/lib.rs b/crates/git/src/lib.rs index f734ba2..e8f61fa 100644 --- a/crates/git/src/lib.rs +++ b/crates/git/src/lib.rs @@ -17,6 +17,6 @@ pub use generation::Generation; pub use git_ref::GitRef; pub use git_remote::GitRemote; pub use repo_details::RepoDetails; -pub use repository::open::OpenRepository; +pub use repository::OpenRepository; pub use repository::Repository; pub use validate::validate; diff --git a/crates/git/src/repository/mock.rs b/crates/git/src/repository/mock.rs index 0617744..c074826 100644 --- a/crates/git/src/repository/mock.rs +++ b/crates/git/src/repository/mock.rs @@ -1,11 +1,17 @@ -use super::*; +use git_next_config::GitDir; + +use crate::{ + repository::{open::OpenRepository, Error, RepositoryLike}, + RepoDetails, +}; + pub struct MockRepository; impl RepositoryLike for MockRepository { - fn open(&self, _gitdir: &GitDir) -> Result { - Ok(open::OpenRepository::Mock) + fn open(&self, _gitdir: &GitDir) -> Result { + Ok(OpenRepository::Mock) } - fn git_clone(&self, _repo_details: &RepoDetails) -> Result { - Ok(open::OpenRepository::Mock) + fn git_clone(&self, _repo_details: &RepoDetails) -> Result { + Ok(OpenRepository::Mock) } } diff --git a/crates/git/src/repository/mod.rs b/crates/git/src/repository/mod.rs index 4659d0d..5e858eb 100644 --- a/crates/git/src/repository/mod.rs +++ b/crates/git/src/repository/mod.rs @@ -2,18 +2,12 @@ #![cfg(not(tarpaulin_include))] mod mock; -pub mod open; +mod open; mod real; -use std::{ - ops::Deref as _, - sync::{atomic::AtomicBool, Arc, Mutex}, -}; +use git_next_config::GitDir; -use git_next_config::{BranchName, GitDir, Hostname, RepoPath}; -use tracing::{info, warn}; - -use crate::{fetch, push, GitRef, GitRemote}; +pub use open::OpenRepository; use super::RepoDetails; @@ -30,8 +24,8 @@ pub const fn mock() -> Repository { } pub trait RepositoryLike { - fn open(&self, gitdir: &GitDir) -> Result; - fn git_clone(&self, repo_details: &RepoDetails) -> Result; + fn open(&self, gitdir: &GitDir) -> Result; + fn git_clone(&self, repo_details: &RepoDetails) -> Result; } impl std::ops::Deref for Repository { type Target = dyn RepositoryLike; diff --git a/crates/git/src/repository/open/mod.rs b/crates/git/src/repository/open/mod.rs new file mode 100644 index 0000000..2922f9b --- /dev/null +++ b/crates/git/src/repository/open/mod.rs @@ -0,0 +1,44 @@ +// +pub mod oreal; + +use std::sync::{Arc, Mutex}; + +use git_next_config::BranchName; + +use crate::{ + fetch, push, + repository::{open::oreal::RealOpenRepository, Direction}, + GitRef, GitRemote, RepoDetails, +}; + +#[derive(Clone, Debug)] +pub enum OpenRepository { + Real(oreal::RealOpenRepository), + Mock, // TODO: (#38) contain a mock model of a repo +} +impl OpenRepository { + pub fn new(gix_repo: gix::Repository) -> Self { + Self::Real(RealOpenRepository::new(Arc::new(Mutex::new(gix_repo)))) + } +} +pub trait OpenRepositoryLike { + fn find_default_remote(&self, direction: Direction) -> Option; + fn fetch(&self) -> Result<(), fetch::Error>; + fn push( + &self, + repo_details: &RepoDetails, + branch_name: BranchName, + to_commit: GitRef, + force: push::Force, + ) -> Result<(), push::Error>; +} +impl std::ops::Deref for OpenRepository { + type Target = dyn OpenRepositoryLike; + + fn deref(&self) -> &Self::Target { + match self { + Self::Real(real) => real, + Self::Mock => todo!(), + } + } +} diff --git a/crates/git/src/repository/open.rs b/crates/git/src/repository/open/oreal.rs similarity index 81% rename from crates/git/src/repository/open.rs rename to crates/git/src/repository/open/oreal.rs index 6184e7f..f103557 100644 --- a/crates/git/src/repository/open.rs +++ b/crates/git/src/repository/open/oreal.rs @@ -1,34 +1,14 @@ -use super::*; -#[derive(Clone, Debug)] -pub enum OpenRepository { - Real(RealOpenRepository), - Mock, // TODO: (#38) contain a mock model of a repo -} -pub trait OpenRepositoryLike { - fn find_default_remote(&self, direction: Direction) -> Option; - fn fetch(&self) -> Result<(), fetch::Error>; - fn push( - &self, - repo_details: &RepoDetails, - branch_name: BranchName, - to_commit: GitRef, - force: push::Force, - ) -> Result<(), push::Error>; -} -impl std::ops::Deref for OpenRepository { - type Target = dyn OpenRepositoryLike; +// +use std::sync::{Arc, Mutex}; - fn deref(&self) -> &Self::Target { - match self { - Self::Real(real) => real, - Self::Mock => todo!(), - } - } -} +use git_next_config::{BranchName, Hostname, RepoPath}; +use tracing::{info, warn}; + +use crate::{fetch, push, repository::Direction, GitRef, GitRemote, RepoDetails}; #[derive(Clone, Debug, derive_more::Constructor)] pub struct RealOpenRepository(Arc>); -impl OpenRepositoryLike for RealOpenRepository { +impl super::OpenRepositoryLike for RealOpenRepository { fn find_default_remote(&self, direction: Direction) -> Option { let Ok(repository) = self.0.lock() else { return None; diff --git a/crates/git/src/repository/real.rs b/crates/git/src/repository/real.rs index 96df79c..4d04fd7 100644 --- a/crates/git/src/repository/real.rs +++ b/crates/git/src/repository/real.rs @@ -1,23 +1,28 @@ -use super::*; +use std::sync::atomic::AtomicBool; + +use derive_more::Deref as _; +use git_next_config::GitDir; + +use crate::{ + repository::{open::OpenRepository, Error, RepositoryLike}, + RepoDetails, +}; + pub struct RealRepository; impl RepositoryLike for RealRepository { - fn open(&self, gitdir: &GitDir) -> Result { - Ok(open::OpenRepository::Real(open::RealOpenRepository::new( - Arc::new(Mutex::new( - gix::ThreadSafeRepository::open(gitdir.to_path_buf())?.to_thread_local(), - )), - ))) + fn open(&self, gitdir: &GitDir) -> Result { + let gix_repo = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?.to_thread_local(); + Ok(OpenRepository::new(gix_repo)) } - fn git_clone(&self, repo_details: &RepoDetails) -> Result { + fn git_clone(&self, repo_details: &RepoDetails) -> Result { use secrecy::ExposeSecret; - let origin = repo_details.origin(); - let (repository, _outcome) = - gix::prepare_clone_bare(origin.expose_secret().as_str(), repo_details.gitdir.deref())? - .fetch_only(gix::progress::Discard, &AtomicBool::new(false))?; + let (gix_repo, _outcome) = gix::prepare_clone_bare( + repo_details.origin().expose_secret().as_str(), + repo_details.gitdir.deref(), + )? + .fetch_only(gix::progress::Discard, &AtomicBool::new(false))?; - Ok(open::OpenRepository::Real(open::RealOpenRepository::new( - Arc::new(Mutex::new(repository)), - ))) + Ok(OpenRepository::new(gix_repo)) } } diff --git a/crates/git/src/validate.rs b/crates/git/src/validate.rs index 23fa7ad..31e306d 100644 --- a/crates/git/src/validate.rs +++ b/crates/git/src/validate.rs @@ -1,6 +1,6 @@ use tracing::info; -use crate::repository::{open::OpenRepository, Direction}; +use crate::repository::{Direction, OpenRepository}; use super::{GitRemote, RepoDetails};