From daa40e762196d276df271a219de47a0c0931752c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 9 May 2024 22:23:57 +0100 Subject: [PATCH] refactor(server): extract Repository::open and clone --- src/server/config/mod.rs | 5 +++++ src/server/config/tests.rs | 12 ++++++------ src/server/gitforge/forgejo/mod.rs | 5 ++--- src/server/gitforge/forgejo/repo/clone.rs | 20 -------------------- src/server/gitforge/forgejo/repo/mod.rs | 5 ----- src/server/gitforge/forgejo/repo/open.rs | 8 -------- src/server/gitforge/types.rs | 21 ++++++++++++++++++++- 7 files changed, 33 insertions(+), 43 deletions(-) delete mode 100644 src/server/gitforge/forgejo/repo/clone.rs delete mode 100644 src/server/gitforge/forgejo/repo/mod.rs delete mode 100644 src/server/gitforge/forgejo/repo/open.rs diff --git a/src/server/config/mod.rs b/src/server/config/mod.rs index 7555c2f..efbf899 100644 --- a/src/server/config/mod.rs +++ b/src/server/config/mod.rs @@ -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 for GitDir { fn from(value: PathBuf) -> Self { Self(value) diff --git a/src/server/config/tests.rs b/src/server/config/tests.rs index 5877e9a..c449a61 100644 --- a/src/server/config/tests.rs +++ b/src/server/config/tests.rs @@ -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(()) } diff --git a/src/server/gitforge/forgejo/mod.rs b/src/server/gitforge/forgejo/mod.rs index bbe6b20..66ada1f 100644 --- a/src/server/gitforge/forgejo/mod.rs +++ b/src/server/gitforge/forgejo/mod.rs @@ -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 { 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 diff --git a/src/server/gitforge/forgejo/repo/clone.rs b/src/server/gitforge/forgejo/repo/clone.rs deleted file mode 100644 index 7ea1dc2..0000000 --- a/src/server/gitforge/forgejo/repo/clone.rs +++ /dev/null @@ -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 { - 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()) -} diff --git a/src/server/gitforge/forgejo/repo/mod.rs b/src/server/gitforge/forgejo/repo/mod.rs deleted file mode 100644 index df614ba..0000000 --- a/src/server/gitforge/forgejo/repo/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod clone; -mod open; - -pub use clone::clone; -pub use open::open; diff --git a/src/server/gitforge/forgejo/repo/open.rs b/src/server/gitforge/forgejo/repo/open.rs deleted file mode 100644 index c2235bd..0000000 --- a/src/server/gitforge/forgejo/repo/open.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::server::{ - config::GitDir, - gitforge::{RepoCloneError, Repository}, -}; - -pub fn open(gitdir: GitDir) -> Result { - Ok(gix::open(gitdir)?.into_sync().into()) -} diff --git a/src/server/gitforge/types.rs b/src/server/gitforge/types.rs index 1570bbb..0b91e53 100644 --- a/src/server/gitforge/types.rs +++ b/src/server/gitforge/types.rs @@ -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) -> Result { + Ok(Self(gix::ThreadSafeRepository::open(gitdir.into())?)) + } + pub fn clone(repo_details: &RepoDetails) -> Result { + 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;