refactor: move repo_clone implementation to git crate
Some checks failed
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline failed
ci/woodpecker/push/tag-created Pipeline was successful
Rust / build (push) Successful in 2m14s

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

View file

@ -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]
@ -41,6 +35,7 @@ impl git::ForgeLike for ForgeJo {
branch: &config::BranchName,
file_path: &str,
) -> Result<String, git::file::Error> {
// TODO: get from local cloned repo
file::contents_get(&self.repo_details, &self.net, branch, file_path).await
}
@ -87,22 +82,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)]

View file

@ -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);

View file

@ -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 {

View file

@ -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<OpenRepository, git::repository::Error> {
todo!()
}
}

View file

@ -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");
}

View file

@ -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<git::OpenRepository, git::repository::Error>;
}

View file

@ -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<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 {
fn open(&self, gitdir: &GitDir) -> 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_next_commit: Option<git::Commit>,
last_dev_commit: Option<git::Commit>,
repository: Option<git::OpenRepository>,
repository: git::Repository,
open_repository: Option<git::OpenRepository>,
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<CloneRepo> 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<ValidateRepo> 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<StartMonitoring> 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<AdvanceMainTo> 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;
};