refactor(server): extract Repository::open and clone

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()) Self(value.into())
} }
} }
impl From<&GitDir> for PathBuf {
fn from(value: &GitDir) -> Self {
value.to_path_buf()
}
}
impl From<PathBuf> for GitDir { impl From<PathBuf> for GitDir {
fn from(value: PathBuf) -> Self { fn from(value: PathBuf) -> Self {
Self(value) 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.forge.hostname = Hostname("git.kemitix.net".to_string());
repo_details.repo_path = RepoPath("kemitix/git-next".to_string()); repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
let gitdir = &repo_details.gitdir; let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?; let repository = Repository::open(gitdir)?;
let found_git_remote = gitdir.find_default_remote(&repository.into(), Direction::Push)?; let found_git_remote = gitdir.find_default_remote(&repository, Direction::Push)?;
let config_git_remote = repo_details.git_remote(); let config_git_remote = repo_details.git_remote();
assert_eq!( 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.forge.hostname = Hostname("git.kemitix.net".to_string());
repo_details.repo_path = RepoPath("kemitix/git-next".to_string()); repo_details.repo_path = RepoPath("kemitix/git-next".to_string());
let gitdir = &repo_details.gitdir; let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?; let repository = Repository::open(gitdir)?;
gitdir.validate(&repository.into(), &repo_details)?; gitdir.validate(&repository, &repo_details)?;
Ok(()) 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.forge.hostname = Hostname("localhost".to_string());
repo_details.repo_path = RepoPath("hello/world".to_string()); repo_details.repo_path = RepoPath("hello/world".to_string());
let gitdir = &repo_details.gitdir; let gitdir = &repo_details.gitdir;
let repository = gix::ThreadSafeRepository::open(gitdir.to_path_buf())?; let repository = Repository::open(gitdir)?;
let_assert!(Err(_) = gitdir.validate(&repository.into(), &repo_details)); let_assert!(Err(_) = gitdir.validate(&repository, &repo_details));
Ok(()) Ok(())
} }

View file

@ -1,6 +1,5 @@
pub mod branch; pub mod branch;
mod file; mod file;
mod repo;
use std::time::Duration; use std::time::Duration;
@ -139,9 +138,9 @@ impl super::ForgeLike for ForgeJoEnv {
fn repo_clone(&self, gitdir: GitDir) -> Result<Repository, RepoCloneError> { fn repo_clone(&self, gitdir: GitDir) -> Result<Repository, RepoCloneError> {
let repository = if !gitdir.exists() { let repository = if !gitdir.exists() {
info!("Local copy not found - cloning..."); info!("Local copy not found - cloning...");
repo::clone(&self.repo_details, gitdir.clone())? Repository::clone(&self.repo_details)?
} else { } else {
repo::open(gitdir.clone())? Repository::open(gitdir.clone())?
}; };
info!("Validating..."); info!("Validating...");
gitdir 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)] #[derive(Debug, PartialEq, Eq)]
pub struct Branch(pub BranchName); pub struct Branch(pub BranchName);
@ -99,6 +104,20 @@ impl std::fmt::Display for Message {
#[derive(Debug, Clone, derive_more::From)] #[derive(Debug, Clone, derive_more::From)]
pub struct Repository(gix::ThreadSafeRepository); 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 { impl std::ops::Deref for Repository {
type Target = gix::ThreadSafeRepository; type Target = gix::ThreadSafeRepository;