From 22a0b401c000414ffbb55eda4b2fe48f8555eda0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 24 May 2024 08:47:34 +0100 Subject: [PATCH] WIP: refactor: get commit log from local repo Avoid using a forge-specific API to get a commit log when the information is already available locally in the cloned repo through a generic git command. --- crates/forge-forgejo/src/tests/mod.rs | 45 +++++++++++++++++++++++-- crates/git/src/repository/mock.rs | 19 +++++++++++ crates/git/src/repository/open/mod.rs | 42 +++++++++++++---------- crates/git/src/repository/open/oreal.rs | 8 +++++ 4 files changed, 94 insertions(+), 20 deletions(-) diff --git a/crates/forge-forgejo/src/tests/mod.rs b/crates/forge-forgejo/src/tests/mod.rs index 30223ca..90e52d5 100644 --- a/crates/forge-forgejo/src/tests/mod.rs +++ b/crates/forge-forgejo/src/tests/mod.rs @@ -20,7 +20,7 @@ mod branch { let mut net = MockNetwork::new(); //given given_forgejo_has_branches(&mut net, 1); - let repo_details = given_a_repo(fs.base(), 1); + let repo_details = given_repo_details(fs.base(), 1); let net = Network::from(net); let (repo, _reality) = git::repository::mock(); @@ -35,6 +35,47 @@ mod branch { assert_eq!(branches, vec![config::BranchName::new("string")]); } } + // mod validate_positions { + // + // use git::ForgeLike; + // + // use super::*; + // + // #[test] + // fn should_ok_all_branches_on_same_commit() { + // let_assert!(Ok(fs) = kxio::fs::temp()); + // let mut net = MockNetwork::new(); + // //given + // let repo_details = given_repo_details(fs.base(), 1); + // let net = Network::from(net); + // let (repo, _reality) = git::repository::mock(); + // let forge = given_a_forge(repo_details, &net, repo); + // let open_repository = given_an_open_repository(); + // let repo_config = given_a_repo_config(); + // + // let forge = forgejo::ForgeJo::new(repo_details, net.clone(), repo); + // + // let_assert!( + // Ok(positions) = forge + // .branches_validate_positions(open_repository, repo_config) + // .await + // ); + // } + // + // fn given_a_forge( + // repo_details: git::RepoDetails, + // net: &Network, + // repo: git::Repository, + // ) -> forgejo::ForgeJo { + // forgejo::ForgeJo::new(repo_details, net.clone(), repo) + // } + // fn given_an_open_repository() -> git::OpenRepository { + // todo!() + // } + // fn given_a_repo_config() -> config::RepoConfig { + // todo!() + // } + // } } fn given_forgejo_has_branches(net: &mut MockNetwork, i: u32) { @@ -48,7 +89,7 @@ fn given_forgejo_has_branches(net: &mut MockNetwork, i: u32) { net.add_get_response(&url, StatusCode::OK, body); } -fn given_a_repo(path: &Path, i: u32) -> git::RepoDetails { +fn given_repo_details(path: &Path, i: u32) -> git::RepoDetails { git::common::repo_details( i, git::Generation::new(), diff --git a/crates/git/src/repository/mock.rs b/crates/git/src/repository/mock.rs index 6975b3e..d6ac858 100644 --- a/crates/git/src/repository/mock.rs +++ b/crates/git/src/repository/mock.rs @@ -133,6 +133,17 @@ impl OpenRepositoryLike for MockOpenRepository { .map(|inner| inner.push(repo_details, branch_name, to_commit, force)) .unwrap() } + + fn commit_log( + &self, + branch_name: git_next_config::BranchName, + find_commits: Vec, + ) -> Vec { + self.inner + .lock() + .map(|inner| inner.commit_log(branch_name, find_commits)) + .unwrap() + } } impl OpenRepositoryLike for InnerMockOpenRepository { fn find_default_remote(&self, direction: Direction) -> Option { @@ -155,4 +166,12 @@ impl OpenRepositoryLike for InnerMockOpenRepository { ) -> std::prelude::v1::Result<(), crate::push::Error> { todo!() } + + fn commit_log( + &self, + branch_name: git_next_config::BranchName, + find_commits: Vec, + ) -> Vec { + todo!() + } } diff --git a/crates/git/src/repository/open/mod.rs b/crates/git/src/repository/open/mod.rs index 2a86c43..5c420e9 100644 --- a/crates/git/src/repository/open/mod.rs +++ b/crates/git/src/repository/open/mod.rs @@ -6,38 +6,44 @@ pub mod oreal; use std::sync::{Arc, Mutex}; -use git_next_config::BranchName; - -use crate::{ - fetch, push, - repository::{mock::MockOpenRepository, open::oreal::RealOpenRepository, Direction}, - GitRef, GitRemote, RepoDetails, -}; +use crate as git; +use git::repository::open::oreal::RealOpenRepository; +use git::repository::Direction; +use git_next_config as config; #[derive(Clone, Debug)] pub enum OpenRepository { - Real(oreal::RealOpenRepository), - Mock(MockOpenRepository), // TODO: (#38) contain a mock model of a repo + Real(RealOpenRepository), + Mock(git::repository::mock::MockOpenRepository), // TODO: (#38) contain a mock model of a repo } impl OpenRepository { pub fn real(gix_repo: gix::Repository) -> Self { - Self::Real(RealOpenRepository::new(Arc::new(Mutex::new(gix_repo)))) + Self::Real(oreal::RealOpenRepository::new(Arc::new(Mutex::new( + gix_repo, + )))) } #[cfg(not(tarpaulin_include))] // don't test mocks - pub const fn mock(mock: MockOpenRepository) -> Self { + pub const fn mock(mock: git::repository::mock::MockOpenRepository) -> Self { Self::Mock(mock) } } pub trait OpenRepositoryLike { - fn find_default_remote(&self, direction: Direction) -> Option; - fn fetch(&self) -> Result<(), fetch::Error>; + fn find_default_remote(&self, direction: Direction) -> Option; + fn fetch(&self) -> Result<(), git::fetch::Error>; fn push( &self, - repo_details: &RepoDetails, - branch_name: BranchName, - to_commit: GitRef, - force: push::Force, - ) -> Result<(), push::Error>; + repo_details: &git::RepoDetails, + branch_name: config::BranchName, + to_commit: git::GitRef, + force: git::push::Force, + ) -> Result<(), git::push::Error>; + + /// List of commits in a branch, optionally up-to any specified commit. + fn commit_log( + &self, + branch_name: config::BranchName, + find_commits: Vec, + ) -> Vec; } impl std::ops::Deref for OpenRepository { type Target = dyn OpenRepositoryLike; diff --git a/crates/git/src/repository/open/oreal.rs b/crates/git/src/repository/open/oreal.rs index b03f4b8..fff186c 100644 --- a/crates/git/src/repository/open/oreal.rs +++ b/crates/git/src/repository/open/oreal.rs @@ -100,6 +100,14 @@ impl super::OpenRepositoryLike for RealOpenRepository { } } } + + fn commit_log( + &self, + branch_name: git_next_config::BranchName, + find_commits: Vec, + ) -> Vec { + todo!() + } } impl From<&gix::Url> for GitRemote {