diff --git a/crates/forge-forgejo/src/lib.rs b/crates/forge-forgejo/src/lib.rs index d00e5efc..f6c7c9eb 100644 --- a/crates/forge-forgejo/src/lib.rs +++ b/crates/forge-forgejo/src/lib.rs @@ -4,26 +4,20 @@ mod file; #[cfg(test)] mod tests; -use git::validation::repo::validate_repo; use git_next_config as config; use git_next_git as git; use kxio::network::{self, Network}; -use tracing::{error, info, warn}; +use tracing::{error, warn}; #[derive(Clone, Debug)] pub struct ForgeJo { repo_details: git::RepoDetails, net: Network, - repo: git::Repository, } impl ForgeJo { - pub const fn new(repo_details: git::RepoDetails, net: Network, repo: git::Repository) -> Self { - Self { - repo_details, - net, - repo, - } + pub const fn new(repo_details: git::RepoDetails, net: Network) -> Self { + Self { repo_details, net } } } #[async_trait::async_trait] @@ -87,22 +81,6 @@ impl git::ForgeLike for ForgeJo { } } } - - fn repo_clone( - &self, - gitdir: config::GitDir, - ) -> Result { - let repository = if !gitdir.exists() { - info!("Local copy not found - cloning..."); - self.repo.git_clone(&self.repo_details)? - } else { - self.repo.open(&gitdir)? - }; - info!("Validating..."); - validate_repo(&repository, &self.repo_details) - .map_err(|e| git::repository::Error::Validation(e.to_string()))?; - Ok(repository) - } } #[derive(Debug, serde::Deserialize)] diff --git a/crates/forge-forgejo/src/tests/mod.rs b/crates/forge-forgejo/src/tests/mod.rs index 90e52d51..b5b02d71 100644 --- a/crates/forge-forgejo/src/tests/mod.rs +++ b/crates/forge-forgejo/src/tests/mod.rs @@ -23,8 +23,7 @@ mod branch { let repo_details = given_repo_details(fs.base(), 1); let net = Network::from(net); - let (repo, _reality) = git::repository::mock(); - let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo); + let forge = forgejo::ForgeJo::new(repo_details, net.clone()); //when let_assert!(Ok(branches) = forge.branches_get_all().await); diff --git a/crates/forge/src/lib.rs b/crates/forge/src/lib.rs index e4dddf8d..12c47ab0 100644 --- a/crates/forge/src/lib.rs +++ b/crates/forge/src/lib.rs @@ -23,12 +23,8 @@ impl Forge { Self::Mock(mock_forge::MockForgeEnv::new()) } #[cfg(feature = "forgejo")] - pub const fn new_forgejo( - repo_details: git::RepoDetails, - net: Network, - repo: git::Repository, - ) -> Self { - Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net, repo)) + pub const fn new_forgejo(repo_details: git::RepoDetails, net: Network) -> Self { + Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net)) } #[cfg(feature = "github")] pub const fn new_github(net: Network) -> Self { diff --git a/crates/forge/src/mock_forge.rs b/crates/forge/src/mock_forge.rs index 921dceaf..6063dd3b 100644 --- a/crates/forge/src/mock_forge.rs +++ b/crates/forge/src/mock_forge.rs @@ -1,7 +1,6 @@ // #![cfg(not(tarpaulin_include))] -use git::OpenRepository; use git_next_config as config; use git_next_git as git; @@ -34,11 +33,4 @@ impl git::ForgeLike for MockForgeEnv { async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status { todo!() } - - fn repo_clone( - &self, - _gitdir: config::GitDir, - ) -> Result { - todo!() - } } diff --git a/crates/forge/src/tests/mod.rs b/crates/forge/src/tests/mod.rs index 39be0a6b..636f4fb4 100644 --- a/crates/forge/src/tests/mod.rs +++ b/crates/forge/src/tests/mod.rs @@ -19,7 +19,6 @@ fn test_forgejo_name() { panic!("fs") }; let net = Network::new_mock(); - let (repo, _reality) = git::repository::mock(); let repo_details = git::common::repo_details( 1, git::Generation::new(), @@ -30,6 +29,6 @@ fn test_forgejo_name() { )), config::GitDir::new(fs.base()), ); - let forge = Forge::new_forgejo(repo_details, net, repo); + let forge = Forge::new_forgejo(repo_details, net); assert_eq!(forge.name(), "forgejo"); } diff --git a/crates/git/src/forge_like.rs b/crates/git/src/forge_like.rs index e4a4a1ca..062da652 100644 --- a/crates/git/src/forge_like.rs +++ b/crates/git/src/forge_like.rs @@ -17,10 +17,4 @@ pub trait ForgeLike { /// Checks the results of any (e.g. CI) status checks for the commit. async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status; - - /// Clones a repo to disk. - fn repo_clone( - &self, - gitdir: config::GitDir, - ) -> Result; } diff --git a/crates/git/src/repository/mod.rs b/crates/git/src/repository/mod.rs index c58677ca..1a14a21e 100644 --- a/crates/git/src/repository/mod.rs +++ b/crates/git/src/repository/mod.rs @@ -6,11 +6,12 @@ mod real; #[cfg(test)] mod tests; +use git_next_config as config; use git_next_config::GitDir; pub use open::OpenRepository; -use crate::repository::mock::MockRepository; +use crate::{repository::mock::MockRepository, validation::repo::validate_repo}; use super::RepoDetails; @@ -27,6 +28,23 @@ pub fn mock() -> (Repository, MockRepository) { (Repository::Mock(mock_repository.clone()), mock_repository) } +/// Opens a repository, cloning if necessary +pub fn open( + repository: &Repository, + repo_details: &RepoDetails, + gitdir: config::GitDir, +) -> Result { + let repository = if !gitdir.exists() { + // info!("Local copy not found - cloning..."); + repository.git_clone(repo_details)? + } else { + repository.open(&gitdir)? + }; + // info!("Validating..."); + validate_repo(&repository, repo_details).map_err(|e| Error::Validation(e.to_string()))?; + Ok(repository) +} + pub trait RepositoryLike { fn open(&self, gitdir: &GitDir) -> Result; fn git_clone(&self, repo_details: &RepoDetails) -> Result; diff --git a/crates/repo-actor/src/lib.rs b/crates/repo-actor/src/lib.rs index 679cc007..db6ab095 100644 --- a/crates/repo-actor/src/lib.rs +++ b/crates/repo-actor/src/lib.rs @@ -31,7 +31,8 @@ pub struct RepoActor { last_main_commit: Option, last_next_commit: Option, last_dev_commit: Option, - repository: Option, + repository: git::Repository, + open_repository: Option, net: Network, forge: forge::Forge, } @@ -45,9 +46,7 @@ impl RepoActor { ) -> Self { let forge = match details.forge.forge_type() { #[cfg(feature = "forgejo")] - config::ForgeType::ForgeJo => { - forge::Forge::new_forgejo(details.clone(), net.clone(), repo) - } + config::ForgeType::ForgeJo => forge::Forge::new_forgejo(details.clone(), net.clone()), config::ForgeType::MockForge => forge::Forge::new_mock(), }; debug!(?forge, "new"); @@ -61,7 +60,8 @@ impl RepoActor { last_main_commit: None, last_next_commit: None, last_dev_commit: None, - repository: None, + repository: repo, + open_repository: None, net, forge, } @@ -96,9 +96,9 @@ impl Handler for RepoActor { #[tracing::instrument(name = "RepoActor::CloneRepo", skip_all, fields(repo = %self.repo_details, gitdir = %self.repo_details.gitdir))] fn handle(&mut self, _msg: CloneRepo, ctx: &mut Self::Context) -> Self::Result { let gitdir = self.repo_details.gitdir.clone(); - match self.forge.repo_clone(gitdir) { + match git::repository::open(&self.repository, &self.repo_details, gitdir) { Ok(repository) => { - self.repository.replace(repository); + self.open_repository.replace(repository); if self.repo_details.repo_config.is_none() { ctx.address().do_send(LoadConfigFromRepo); } else { @@ -179,7 +179,7 @@ impl Handler for RepoActor { .wait(ctx); } if let (Some(repository), Some(repo_config)) = ( - self.repository.clone(), + self.open_repository.clone(), self.repo_details.repo_config.clone(), ) { let repo_details = self.repo_details.clone(); @@ -241,7 +241,7 @@ impl Handler for RepoActor { .into_actor(self) .wait(ctx); } else if dev_ahead_of_next { - if let Some(repository) = self.repository.clone() { + if let Some(repository) = self.open_repository.clone() { branch::advance_next( msg.next, msg.dev_commit_history, @@ -282,7 +282,7 @@ impl Handler for RepoActor { warn!("No config loaded"); return; }; - let Some(repository) = self.repository.clone() else { + let Some(repository) = self.open_repository.clone() else { warn!("No repository opened"); return; };