refactor: merge git::branch module into git::push

This commit is contained in:
Paul Campbell 2024-06-08 20:43:20 +01:00
parent 65e9ddf5db
commit c6a1d2c21b
9 changed files with 40 additions and 53 deletions

View file

@ -1,37 +0,0 @@
//
use crate as git;
use git_next_config as config;
pub type Result<T> = core::result::Result<T, Error>;
#[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)?)
}

View file

@ -1,5 +1,4 @@
// //
pub mod branch;
pub mod commit; pub mod commit;
pub mod common; pub mod common;
pub mod fetch; pub mod fetch;

View file

@ -1,4 +1,6 @@
use super::GitRef; use super::GitRef;
use crate as git;
use git_next_config as config;
#[derive(Debug)] #[derive(Debug)]
pub enum Force { pub enum Force {
@ -14,16 +16,39 @@ impl std::fmt::Display for Force {
} }
} }
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("gix open: {0}")] #[error("io")]
Open(#[from] Box<gix::open::Error>), Io(#[from] std::io::Error),
#[error("io: {0}")] #[error("network: {0}")]
Push(#[from] std::io::Error), Network(#[from] kxio::network::NetworkError),
#[error("fetch: {0}")]
Fetch(#[from] git::fetch::Error),
#[error("lock")] #[error("lock")]
Lock, Lock,
#[error("gix open: {0}")]
Open(#[from] Box<gix::open::Error>),
#[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<T> = core::result::Result<T, 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()?;
repository.push(repo_details, branch_name, to_commit, force)
}

View file

@ -109,7 +109,7 @@ impl InnerMockOpenRepository {
#[allow(clippy::unwrap_used)] #[allow(clippy::unwrap_used)]
impl OpenRepositoryLike for MockOpenRepository { impl OpenRepositoryLike for MockOpenRepository {
fn remote_branches(&self) -> git::branch::Result<Vec<config::BranchName>> { fn remote_branches(&self) -> git::push::Result<Vec<config::BranchName>> {
self.inner self.inner
.lock() .lock()
.map(|inner| inner.remote_branches()) .map(|inner| inner.remote_branches())
@ -162,7 +162,7 @@ impl OpenRepositoryLike for MockOpenRepository {
} }
} }
impl OpenRepositoryLike for InnerMockOpenRepository { impl OpenRepositoryLike for InnerMockOpenRepository {
fn remote_branches(&self) -> git::branch::Result<Vec<config::BranchName>> { fn remote_branches(&self) -> git::push::Result<Vec<config::BranchName>> {
todo!(); todo!();
} }
fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> { fn find_default_remote(&self, direction: Direction) -> Option<GitRemote> {

View file

@ -28,7 +28,7 @@ impl OpenRepository {
} }
} }
pub trait OpenRepositoryLike { pub trait OpenRepositoryLike {
fn remote_branches(&self) -> git::branch::Result<Vec<config::BranchName>>; fn remote_branches(&self) -> git::push::Result<Vec<config::BranchName>>;
fn find_default_remote(&self, direction: Direction) -> Option<git::GitRemote>; fn find_default_remote(&self, direction: Direction) -> Option<git::GitRemote>;
fn fetch(&self) -> Result<(), git::fetch::Error>; fn fetch(&self) -> Result<(), git::fetch::Error>;
fn push( fn push(

View file

@ -10,11 +10,11 @@ use tracing::{info, warn};
#[derive(Clone, Debug, derive_more::Constructor)] #[derive(Clone, Debug, derive_more::Constructor)]
pub struct RealOpenRepository(Arc<Mutex<gix::Repository>>); pub struct RealOpenRepository(Arc<Mutex<gix::Repository>>);
impl super::OpenRepositoryLike for RealOpenRepository { impl super::OpenRepositoryLike for RealOpenRepository {
fn remote_branches(&self) -> git::branch::Result<Vec<config::BranchName>> { fn remote_branches(&self) -> git::push::Result<Vec<config::BranchName>> {
let refs = self let refs = self
.0 .0
.lock() .lock()
.map_err(|_| git::branch::Error::Lock) .map_err(|_| git::push::Error::Lock)
.and_then(|repo| { .and_then(|repo| {
Ok(repo.references()?).and_then(|refs| { Ok(repo.references()?).and_then(|refs| {
Ok(refs.remote_branches().map(|rb| { Ok(refs.remote_branches().map(|rb| {

View file

@ -66,7 +66,7 @@ pub fn validate_positions(
let next_is_ancestor_of_dev = commit_histories.dev.iter().any(|dev| dev == &next); let next_is_ancestor_of_dev = commit_histories.dev.iter().any(|dev| dev == &next);
if !next_is_ancestor_of_dev { if !next_is_ancestor_of_dev {
info!("Next is not an ancestor of dev - resetting next to main"); 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, repository,
repo_details, repo_details,
repo_config.branches().next(), repo_config.branches().next(),
@ -96,7 +96,7 @@ pub fn validate_positions(
repo_config.branches().main(), repo_config.branches().main(),
repo_config.branches().next() repo_config.branches().next()
); );
if let Err(err) = git::branch::reset( if let Err(err) = git::push::reset(
repository, repository,
repo_details, repo_details,
repo_config.branches().next(), repo_config.branches().next(),

View file

@ -30,7 +30,7 @@ pub async fn advance_next(
return; return;
} }
info!("Advancing next to commit '{}'", commit); info!("Advancing next to commit '{}'", commit);
if let Err(err) = git::branch::reset( if let Err(err) = git::push::reset(
&repository, &repository,
&repo_details, &repo_details,
repo_config.branches().next(), repo_config.branches().next(),
@ -84,7 +84,7 @@ pub async fn advance_main(
repository: &git::OpenRepository, repository: &git::OpenRepository,
) { ) {
info!("Advancing main to next"); info!("Advancing main to next");
if let Err(err) = git::branch::reset( if let Err(err) = git::push::reset(
repository, repository,
repo_details, repo_details,
repo_config.branches().main(), repo_config.branches().main(),

View file

@ -42,7 +42,7 @@ pub enum Error {
File(git::file::Error), File(git::file::Error),
Config(config::server::Error), Config(config::server::Error),
Toml(toml::de::Error), Toml(toml::de::Error),
Branch(git::branch::Error), Branch(git::push::Error),
BranchNotFound(config::BranchName), BranchNotFound(config::BranchName),
} }