refactor(server): extract Repository::open and clone
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful

This commit is contained in:
Paul Campbell 2024-05-09 22:23:57 +01:00
parent 62bee38c85
commit daa40e7621
7 changed files with 33 additions and 43 deletions

View file

@ -579,6 +579,11 @@ impl From<&str> for GitDir {
Self(value.into())
}
}
impl From<&GitDir> for PathBuf {
fn from(value: &GitDir) -> Self {
value.to_path_buf()
}
}
impl From<PathBuf> for GitDir {
fn from(value: PathBuf) -> Self {
Self(value)

View file

@ -166,8 +166,8 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
repo_details.forge.hostname = Hostname("git.kemitix.net".to_string());
repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?;
let found_git_remote = gitdir.find_default_remote(&repository.into(), Direction::Push)?;
let repository = Repository::open(gitdir)?;
let found_git_remote = gitdir.find_default_remote(&repository, Direction::Push)?;
let config_git_remote = repo_details.git_remote();
assert_eq!(
@ -191,8 +191,8 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
repo_details.forge.hostname = Hostname("git.kemitix.net".to_string());
repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?;
gitdir.validate(&repository.into(), &repo_details)?;
let repository = Repository::open(gitdir)?;
gitdir.validate(&repository, &repo_details)?;
Ok(())
}
@ -210,8 +210,8 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> {
repo_details.forge.hostname = Hostname("localhost".to_string());
repo_details.repo_path = RepoPath("hello/world".to_string());
let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?;
let_assert!(Err(_) = gitdir.validate(&repository.into(), &repo_details));
let repository = Repository::open(gitdir)?;
let_assert!(Err(_) = gitdir.validate(&repository, &repo_details));
Ok(())
}

View file

@ -1,6 +1,5 @@
pub mod branch;
mod file;
mod repo;
use std::time::Duration;
@ -139,9 +138,9 @@ impl super::ForgeLike for ForgeJoEnv {
fn repo_clone(&self, gitdir: GitDir) -> Result<Repository, RepoCloneError> {
let repository = if !gitdir.exists() {
info!("Local copy not found - cloning...");
repo::clone(&self.repo_details, gitdir.clone())?
Repository::clone(&self.repo_details)?
} else {
repo::open(gitdir.clone())?
Repository::open(gitdir.clone())?
};
info!("Validating...");
gitdir

View file

@ -1,20 +0,0 @@
use std::{ops::Deref, sync::atomic::AtomicBool};
use tracing::info;
use crate::server::{
config::{GitDir, RepoDetails},
gitforge::{RepoCloneError, Repository},
};
#[tracing::instrument(skip_all)]
pub fn clone(repo_details: &RepoDetails, gitdir: GitDir) -> Result<Repository, RepoCloneError> {
use secrecy::ExposeSecret;
let origin = repo_details.origin();
let (repository, _outcome) =
gix::prepare_clone_bare(origin.expose_secret().as_str(), gitdir.deref())?
.fetch_only(gix::progress::Discard, &AtomicBool::new(false))?;
info!("Cloned - OK");
Ok(repository.into_sync().into())
}

View file

@ -1,5 +0,0 @@
mod clone;
mod open;
pub use clone::clone;
pub use open::open;

View file

@ -1,8 +0,0 @@
use crate::server::{
config::GitDir,
gitforge::{RepoCloneError, Repository},
};
pub fn open(gitdir: GitDir) -> Result<Repository, RepoCloneError> {
Ok(gix::open(gitdir)?.into_sync().into())
}

View file

@ -1,4 +1,9 @@
use crate::server::config::BranchName;
use std::{ops::Deref as _, path::PathBuf, sync::atomic::AtomicBool};
use crate::server::{
config::{BranchName, RepoDetails},
gitforge::RepoCloneError,
};
#[derive(Debug, PartialEq, Eq)]
pub struct Branch(pub BranchName);
@ -99,6 +104,20 @@ impl std::fmt::Display for Message {
#[derive(Debug, Clone, derive_more::From)]
pub struct Repository(gix::ThreadSafeRepository);
impl Repository {
pub fn open(gitdir: impl Into<PathBuf>) -> Result<Self, RepoCloneError> {
Ok(Self(gix::ThreadSafeRepository::open(gitdir.into())?))
}
pub fn clone(repo_details: &RepoDetails) -> Result<Self, RepoCloneError> {
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))?;
Ok(repository.into_sync().into())
}
}
impl std::ops::Deref for Repository {
type Target = gix::ThreadSafeRepository;