WIP: refactor: git: use thiserror and cleanup errors
All checks were successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-03 08:04:48 +01:00
parent 0b8e41a8ec
commit f461d42c99
5 changed files with 47 additions and 58 deletions

View file

@ -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 }

View file

@ -4,21 +4,23 @@ use git_next_config as config;
pub type Result<T> = core::result::Result<T, Error>;
#[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,

View file

@ -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<gix::remote::connect::Error> for Error {
fn from(gix_error: gix::remote::connect::Error) -> Self {
Self::Connect(gix_error.to_string())
}
}
}

View file

@ -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<gix::open::Error>),
Push,
#[error("gix open: {0}")]
Open(#[from] Box<gix::open::Error>),
#[error("io: {0}")]
Push(#[from] std::io::Error),
#[error("lock")]
Lock,
}
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;

View file

@ -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(