From c6a1d2c21b3c4d48459678fc12bee505078a8885 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 8 Jun 2024 20:43:20 +0100 Subject: [PATCH] refactor: merge git::branch module into git::push --- crates/git/src/branch.rs | 37 ------------------------- crates/git/src/lib.rs | 1 - crates/git/src/push.rs | 35 +++++++++++++++++++---- crates/git/src/repository/mock.rs | 4 +-- crates/git/src/repository/open/mod.rs | 2 +- crates/git/src/repository/open/oreal.rs | 4 +-- crates/git/src/validation/positions.rs | 4 +-- crates/repo-actor/src/branch.rs | 4 +-- crates/repo-actor/src/load.rs | 2 +- 9 files changed, 40 insertions(+), 53 deletions(-) delete mode 100644 crates/git/src/branch.rs diff --git a/crates/git/src/branch.rs b/crates/git/src/branch.rs deleted file mode 100644 index cfa71003..00000000 --- a/crates/git/src/branch.rs +++ /dev/null @@ -1,37 +0,0 @@ -// -use crate as git; -use git_next_config as config; - -pub type Result = core::result::Result; - -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("network: {0}")] - Network(#[from] kxio::network::NetworkError), - - #[error("fetch: {0}")] - Fetch(#[from] git::fetch::Error), - - #[error("push: {0}")] - Push(#[from] git::push::Error), - - #[error("lock")] - Lock, - - #[error("gix iter: {0}")] - GixIter(#[from] gix::reference::iter::Error), - - #[error("gix iter init: {0}")] - GixIterInit(#[from] gix::reference::iter::init::Error), -} - -pub fn reset( - repository: &git::OpenRepository, - repo_details: &git::RepoDetails, - branch_name: config::BranchName, - to_commit: git::GitRef, - force: git::push::Force, -) -> Result<()> { - repository.fetch()?; - Ok(repository.push(repo_details, branch_name, to_commit, force)?) -} diff --git a/crates/git/src/lib.rs b/crates/git/src/lib.rs index 739a11ea..db926a52 100644 --- a/crates/git/src/lib.rs +++ b/crates/git/src/lib.rs @@ -1,5 +1,4 @@ // -pub mod branch; pub mod commit; pub mod common; pub mod fetch; diff --git a/crates/git/src/push.rs b/crates/git/src/push.rs index a4eb3edf..4490ff3e 100644 --- a/crates/git/src/push.rs +++ b/crates/git/src/push.rs @@ -1,4 +1,6 @@ use super::GitRef; +use crate as git; +use git_next_config as config; #[derive(Debug)] pub enum Force { @@ -14,16 +16,39 @@ impl std::fmt::Display for Force { } } +pub type Result = core::result::Result; + #[derive(Debug, thiserror::Error)] pub enum Error { - #[error("gix open: {0}")] - Open(#[from] Box), + #[error("io")] + Io(#[from] std::io::Error), - #[error("io: {0}")] - Push(#[from] std::io::Error), + #[error("network: {0}")] + Network(#[from] kxio::network::NetworkError), + + #[error("fetch: {0}")] + Fetch(#[from] git::fetch::Error), #[error("lock")] Lock, + + #[error("gix open: {0}")] + Open(#[from] Box), + + #[error("gix iter: {0}")] + GixIter(#[from] gix::reference::iter::Error), + + #[error("gix iter init: {0}")] + GixIterInit(#[from] gix::reference::iter::init::Error), } -pub type Result = core::result::Result; +pub fn reset( + repository: &git::OpenRepository, + repo_details: &git::RepoDetails, + branch_name: config::BranchName, + to_commit: git::GitRef, + force: git::push::Force, +) -> Result<()> { + repository.fetch()?; + repository.push(repo_details, branch_name, to_commit, force) +} diff --git a/crates/git/src/repository/mock.rs b/crates/git/src/repository/mock.rs index da53d191..53110d6e 100644 --- a/crates/git/src/repository/mock.rs +++ b/crates/git/src/repository/mock.rs @@ -109,7 +109,7 @@ impl InnerMockOpenRepository { #[allow(clippy::unwrap_used)] impl OpenRepositoryLike for MockOpenRepository { - fn remote_branches(&self) -> git::branch::Result> { + fn remote_branches(&self) -> git::push::Result> { self.inner .lock() .map(|inner| inner.remote_branches()) @@ -162,7 +162,7 @@ impl OpenRepositoryLike for MockOpenRepository { } } impl OpenRepositoryLike for InnerMockOpenRepository { - fn remote_branches(&self) -> git::branch::Result> { + fn remote_branches(&self) -> git::push::Result> { todo!(); } fn find_default_remote(&self, direction: Direction) -> Option { diff --git a/crates/git/src/repository/open/mod.rs b/crates/git/src/repository/open/mod.rs index 4d1c267f..c440b37f 100644 --- a/crates/git/src/repository/open/mod.rs +++ b/crates/git/src/repository/open/mod.rs @@ -28,7 +28,7 @@ impl OpenRepository { } } pub trait OpenRepositoryLike { - fn remote_branches(&self) -> git::branch::Result>; + fn remote_branches(&self) -> git::push::Result>; fn find_default_remote(&self, direction: Direction) -> Option; fn fetch(&self) -> Result<(), git::fetch::Error>; fn push( diff --git a/crates/git/src/repository/open/oreal.rs b/crates/git/src/repository/open/oreal.rs index 72069470..0dd8b42e 100644 --- a/crates/git/src/repository/open/oreal.rs +++ b/crates/git/src/repository/open/oreal.rs @@ -10,11 +10,11 @@ use tracing::{info, warn}; #[derive(Clone, Debug, derive_more::Constructor)] pub struct RealOpenRepository(Arc>); impl super::OpenRepositoryLike for RealOpenRepository { - fn remote_branches(&self) -> git::branch::Result> { + fn remote_branches(&self) -> git::push::Result> { let refs = self .0 .lock() - .map_err(|_| git::branch::Error::Lock) + .map_err(|_| git::push::Error::Lock) .and_then(|repo| { Ok(repo.references()?).and_then(|refs| { Ok(refs.remote_branches().map(|rb| { diff --git a/crates/git/src/validation/positions.rs b/crates/git/src/validation/positions.rs index 70610b08..af29a664 100644 --- a/crates/git/src/validation/positions.rs +++ b/crates/git/src/validation/positions.rs @@ -66,7 +66,7 @@ pub fn validate_positions( let next_is_ancestor_of_dev = commit_histories.dev.iter().any(|dev| dev == &next); if !next_is_ancestor_of_dev { info!("Next is not an ancestor of dev - resetting next to main"); - if let Err(err) = git::branch::reset( + if let Err(err) = git::push::reset( repository, repo_details, repo_config.branches().next(), @@ -96,7 +96,7 @@ pub fn validate_positions( repo_config.branches().main(), repo_config.branches().next() ); - if let Err(err) = git::branch::reset( + if let Err(err) = git::push::reset( repository, repo_details, repo_config.branches().next(), diff --git a/crates/repo-actor/src/branch.rs b/crates/repo-actor/src/branch.rs index 5ca7084d..9efbf731 100644 --- a/crates/repo-actor/src/branch.rs +++ b/crates/repo-actor/src/branch.rs @@ -30,7 +30,7 @@ pub async fn advance_next( return; } info!("Advancing next to commit '{}'", commit); - if let Err(err) = git::branch::reset( + if let Err(err) = git::push::reset( &repository, &repo_details, repo_config.branches().next(), @@ -84,7 +84,7 @@ pub async fn advance_main( repository: &git::OpenRepository, ) { info!("Advancing main to next"); - if let Err(err) = git::branch::reset( + if let Err(err) = git::push::reset( repository, repo_details, repo_config.branches().main(), diff --git a/crates/repo-actor/src/load.rs b/crates/repo-actor/src/load.rs index e24e14de..510db4ac 100644 --- a/crates/repo-actor/src/load.rs +++ b/crates/repo-actor/src/load.rs @@ -42,7 +42,7 @@ pub enum Error { File(git::file::Error), Config(config::server::Error), Toml(toml::de::Error), - Branch(git::branch::Error), + Branch(git::push::Error), BranchNotFound(config::BranchName), }