refactor: move repo_clone implementation to git crate
All checks were successful
Rust / build (push) Successful in 1m6s
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

This commit is contained in:
Paul Campbell 2024-05-26 09:44:56 +01:00
parent f10dc25aeb
commit d0638fdbc4
8 changed files with 36 additions and 60 deletions

View file

@ -4,26 +4,20 @@ mod file;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use git::validation::repo::validate_repo;
use git_next_config as config; use git_next_config as config;
use git_next_git as git; use git_next_git as git;
use kxio::network::{self, Network}; use kxio::network::{self, Network};
use tracing::{error, info, warn}; use tracing::{error, warn};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ForgeJo { pub struct ForgeJo {
repo_details: git::RepoDetails, repo_details: git::RepoDetails,
net: Network, net: Network,
repo: git::Repository,
} }
impl ForgeJo { impl ForgeJo {
pub const fn new(repo_details: git::RepoDetails, net: Network, repo: git::Repository) -> Self { pub const fn new(repo_details: git::RepoDetails, net: Network) -> Self {
Self { Self { repo_details, net }
repo_details,
net,
repo,
}
} }
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@ -87,22 +81,6 @@ impl git::ForgeLike for ForgeJo {
} }
} }
} }
fn repo_clone(
&self,
gitdir: config::GitDir,
) -> Result<git::OpenRepository, git::repository::Error> {
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)] #[derive(Debug, serde::Deserialize)]

View file

@ -23,8 +23,7 @@ mod branch {
let repo_details = given_repo_details(fs.base(), 1); let repo_details = given_repo_details(fs.base(), 1);
let net = Network::from(net); let net = Network::from(net);
let (repo, _reality) = git::repository::mock(); let forge = forgejo::ForgeJo::new(repo_details, net.clone());
let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo);
//when //when
let_assert!(Ok(branches) = forge.branches_get_all().await); let_assert!(Ok(branches) = forge.branches_get_all().await);

View file

@ -23,12 +23,8 @@ impl Forge {
Self::Mock(mock_forge::MockForgeEnv::new()) Self::Mock(mock_forge::MockForgeEnv::new())
} }
#[cfg(feature = "forgejo")] #[cfg(feature = "forgejo")]
pub const fn new_forgejo( pub const fn new_forgejo(repo_details: git::RepoDetails, net: Network) -> Self {
repo_details: git::RepoDetails, Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net))
net: Network,
repo: git::Repository,
) -> Self {
Self::ForgeJo(forgejo::ForgeJo::new(repo_details, net, repo))
} }
#[cfg(feature = "github")] #[cfg(feature = "github")]
pub const fn new_github(net: Network) -> Self { pub const fn new_github(net: Network) -> Self {

View file

@ -1,7 +1,6 @@
// //
#![cfg(not(tarpaulin_include))] #![cfg(not(tarpaulin_include))]
use git::OpenRepository;
use git_next_config as config; use git_next_config as config;
use git_next_git as git; 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 { async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
todo!() todo!()
} }
fn repo_clone(
&self,
_gitdir: config::GitDir,
) -> Result<OpenRepository, git::repository::Error> {
todo!()
}
} }

View file

@ -19,7 +19,6 @@ fn test_forgejo_name() {
panic!("fs") panic!("fs")
}; };
let net = Network::new_mock(); let net = Network::new_mock();
let (repo, _reality) = git::repository::mock();
let repo_details = git::common::repo_details( let repo_details = git::common::repo_details(
1, 1,
git::Generation::new(), git::Generation::new(),
@ -30,6 +29,6 @@ fn test_forgejo_name() {
)), )),
config::GitDir::new(fs.base()), 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"); assert_eq!(forge.name(), "forgejo");
} }

View file

@ -17,10 +17,4 @@ pub trait ForgeLike {
/// Checks the results of any (e.g. CI) status checks for the commit. /// Checks the results of any (e.g. CI) status checks for the commit.
async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status; async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
/// Clones a repo to disk.
fn repo_clone(
&self,
gitdir: config::GitDir,
) -> Result<git::OpenRepository, git::repository::Error>;
} }

View file

@ -6,11 +6,12 @@ mod real;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use git_next_config as config;
use git_next_config::GitDir; use git_next_config::GitDir;
pub use open::OpenRepository; pub use open::OpenRepository;
use crate::repository::mock::MockRepository; use crate::{repository::mock::MockRepository, validation::repo::validate_repo};
use super::RepoDetails; use super::RepoDetails;
@ -27,6 +28,23 @@ pub fn mock() -> (Repository, MockRepository) {
(Repository::Mock(mock_repository.clone()), mock_repository) (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<OpenRepository> {
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 { pub trait RepositoryLike {
fn open(&self, gitdir: &GitDir) -> Result<OpenRepository>; fn open(&self, gitdir: &GitDir) -> Result<OpenRepository>;
fn git_clone(&self, repo_details: &RepoDetails) -> Result<OpenRepository>; fn git_clone(&self, repo_details: &RepoDetails) -> Result<OpenRepository>;

View file

@ -31,7 +31,8 @@ pub struct RepoActor {
last_main_commit: Option<git::Commit>, last_main_commit: Option<git::Commit>,
last_next_commit: Option<git::Commit>, last_next_commit: Option<git::Commit>,
last_dev_commit: Option<git::Commit>, last_dev_commit: Option<git::Commit>,
repository: Option<git::OpenRepository>, repository: git::Repository,
open_repository: Option<git::OpenRepository>,
net: Network, net: Network,
forge: forge::Forge, forge: forge::Forge,
} }
@ -45,9 +46,7 @@ impl RepoActor {
) -> Self { ) -> Self {
let forge = match details.forge.forge_type() { let forge = match details.forge.forge_type() {
#[cfg(feature = "forgejo")] #[cfg(feature = "forgejo")]
config::ForgeType::ForgeJo => { config::ForgeType::ForgeJo => forge::Forge::new_forgejo(details.clone(), net.clone()),
forge::Forge::new_forgejo(details.clone(), net.clone(), repo)
}
config::ForgeType::MockForge => forge::Forge::new_mock(), config::ForgeType::MockForge => forge::Forge::new_mock(),
}; };
debug!(?forge, "new"); debug!(?forge, "new");
@ -61,7 +60,8 @@ impl RepoActor {
last_main_commit: None, last_main_commit: None,
last_next_commit: None, last_next_commit: None,
last_dev_commit: None, last_dev_commit: None,
repository: None, repository: repo,
open_repository: None,
net, net,
forge, forge,
} }
@ -96,9 +96,9 @@ impl Handler<CloneRepo> for RepoActor {
#[tracing::instrument(name = "RepoActor::CloneRepo", skip_all, fields(repo = %self.repo_details, gitdir = %self.repo_details.gitdir))] #[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 { fn handle(&mut self, _msg: CloneRepo, ctx: &mut Self::Context) -> Self::Result {
let gitdir = self.repo_details.gitdir.clone(); 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) => { Ok(repository) => {
self.repository.replace(repository); self.open_repository.replace(repository);
if self.repo_details.repo_config.is_none() { if self.repo_details.repo_config.is_none() {
ctx.address().do_send(LoadConfigFromRepo); ctx.address().do_send(LoadConfigFromRepo);
} else { } else {
@ -179,7 +179,7 @@ impl Handler<ValidateRepo> for RepoActor {
.wait(ctx); .wait(ctx);
} }
if let (Some(repository), Some(repo_config)) = ( if let (Some(repository), Some(repo_config)) = (
self.repository.clone(), self.open_repository.clone(),
self.repo_details.repo_config.clone(), self.repo_details.repo_config.clone(),
) { ) {
let repo_details = self.repo_details.clone(); let repo_details = self.repo_details.clone();
@ -241,7 +241,7 @@ impl Handler<StartMonitoring> for RepoActor {
.into_actor(self) .into_actor(self)
.wait(ctx); .wait(ctx);
} else if dev_ahead_of_next { } else if dev_ahead_of_next {
if let Some(repository) = self.repository.clone() { if let Some(repository) = self.open_repository.clone() {
branch::advance_next( branch::advance_next(
msg.next, msg.next,
msg.dev_commit_history, msg.dev_commit_history,
@ -282,7 +282,7 @@ impl Handler<AdvanceMainTo> for RepoActor {
warn!("No config loaded"); warn!("No config loaded");
return; return;
}; };
let Some(repository) = self.repository.clone() else { let Some(repository) = self.open_repository.clone() else {
warn!("No repository opened"); warn!("No repository opened");
return; return;
}; };