diff --git a/crates/git/Cargo.toml b/crates/git/Cargo.toml index f5757d6..c34de0b 100644 --- a/crates/git/Cargo.toml +++ b/crates/git/Cargo.toml @@ -41,6 +41,7 @@ secrecy = { workspace = true } # error handling derive_more = { workspace = true } derive-with = { workspace = true } +thiserror = { workspace = true } # # file watcher # inotify = { workspace = true } diff --git a/crates/git/src/branch.rs b/crates/git/src/branch.rs index f4533e4..3c7d492 100644 --- a/crates/git/src/branch.rs +++ b/crates/git/src/branch.rs @@ -4,21 +4,23 @@ use git_next_config as config; pub type Result = core::result::Result; -#[derive(Debug, derive_more::Display, derive_more::From)] +#[derive(Debug, thiserror::Error)] pub enum Error { - Network(kxio::network::NetworkError), + #[error("network: {0}")] + Network(#[from] kxio::network::NetworkError), - Fetch(git::fetch::Error), + #[error("fetch: {0}")] + Fetch(#[from] git::fetch::Error), - Push(git::push::Error), + #[error("push: {0}")] + Push(#[from] git::push::Error), + #[error("lock")] Lock, - - Generic(String), - I1(gix::reference::iter::Error), - I2(gix::reference::iter::init::Error), + // Generic(String), + // I1(gix::reference::iter::Error), + // I2(gix::reference::iter::init::Error), } -impl std::error::Error for Error {} pub fn reset( repository: &git::OpenRepository, diff --git a/crates/git/src/fetch.rs b/crates/git/src/fetch.rs index 29de9e0..67e0133 100644 --- a/crates/git/src/fetch.rs +++ b/crates/git/src/fetch.rs @@ -1,20 +1,20 @@ -#[derive(Debug, derive_more::Display)] +#[derive(Debug, thiserror::Error)] pub enum Error { + #[error("unable to open repo: {0}")] UnableToOpenRepo(String), + + #[error("no remote found")] NoFetchRemoteFound, + + #[error("remote connect: {0}")] Connect(String), - Fetch(String), + + #[error("prepare: {0}")] + Prepare(String), + + #[error("receive: {0}")] + Receive(String), + + #[error("lock")] Lock, } -impl std::error::Error for Error {} - -mod gix_error { - #![cfg(not(tarpaulin_include))] // third-party library errors - use super::Error; - - impl From for Error { - fn from(gix_error: gix::remote::connect::Error) -> Self { - Self::Connect(gix_error.to_string()) - } - } -} diff --git a/crates/git/src/push.rs b/crates/git/src/push.rs index 080d52c..a4eb3ed 100644 --- a/crates/git/src/push.rs +++ b/crates/git/src/push.rs @@ -14,12 +14,16 @@ impl std::fmt::Display for Force { } } -#[derive(Debug, derive_more::From, derive_more::Display)] +#[derive(Debug, thiserror::Error)] pub enum Error { - Open(Box), - Push, + #[error("gix open: {0}")] + Open(#[from] Box), + + #[error("io: {0}")] + Push(#[from] std::io::Error), + + #[error("lock")] Lock, } -impl std::error::Error for Error {} pub type Result = core::result::Result; diff --git a/crates/git/src/repository/open/oreal.rs b/crates/git/src/repository/open/oreal.rs index c9113b4..37f3faf 100644 --- a/crates/git/src/repository/open/oreal.rs +++ b/crates/git/src/repository/open/oreal.rs @@ -3,7 +3,7 @@ use crate as git; use config::BranchName; use git_next_config as config; -use gix::bstr::BStr; +use gix::{bstr::BStr, clone::PrepareFetch}; use std::sync::{Arc, Mutex}; use tracing::{info, warn}; @@ -56,17 +56,13 @@ impl super::OpenRepositoryLike for RealOpenRepository { #[cfg(not(tarpaulin_include))] // test is on local repo - should always have remotes return Err(git::fetch::Error::NoFetchRemoteFound); }; - let prepared_fetch = remote - .connect(gix::remote::Direction::Fetch)? - .prepare_fetch(gix::progress::Discard, Default::default()); - match prepared_fetch { - Ok(fetch) => { - fetch - .receive(gix::progress::Discard, &Default::default()) - .map_err(|e| git::fetch::Error::Fetch(e.to_string()))?; - } - Err(e) => Err(git::fetch::Error::Fetch(e.to_string()))?, - } + remote + .connect(gix::remote::Direction::Fetch) + .map_err(|gix| git::fetch::Error::Connect(gix.to_string()))? + .prepare_fetch(gix::progress::Discard, Default::default()) + .map_err(|gix| git::fetch::Error::Prepare(gix.to_string()))? + .receive(gix::progress::Discard, &Default::default()) + .map_err(|gix| git::fetch::Error::Receive(gix.to_string()))?; info!("Fetch okay"); Ok(()) } @@ -101,31 +97,17 @@ impl super::OpenRepositoryLike for RealOpenRepository { .map_err(|_| git::push::Error::Lock) .map(|r| r.git_dir().to_path_buf())?; let ctx = gix::diff::command::Context { - git_dir: Some(git_dir.clone()), + git_dir: Some(git_dir), ..Default::default() }; - match gix::command::prepare(command.expose_secret()) + gix::command::prepare(command.expose_secret()) .with_context(ctx) .with_shell_allow_argument_splitting() .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) - .spawn() - { - Ok(mut child) => match child.wait() { - Ok(_) => { - info!("Push okay"); - Ok(()) - } - Err(err) => { - warn!(?err, ?git_dir, "Failed (wait)"); - Err(git::push::Error::Push) - } - }, - Err(err) => { - warn!(?err, ?git_dir, "Failed (spawn)"); - Err(git::push::Error::Push) - } - } + .spawn()? + .wait()?; + Ok(()) } fn commit_log(