2024-07-25 09:02:43 +01:00
|
|
|
//
|
2024-07-26 06:49:09 +01:00
|
|
|
use crate::{git, git::repository::open::OpenRepositoryLike, BranchName};
|
2024-05-11 19:46:20 +01:00
|
|
|
|
2024-08-12 21:25:24 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2024-05-11 19:46:20 +01:00
|
|
|
pub enum Force {
|
|
|
|
No,
|
2024-07-25 09:02:43 +01:00
|
|
|
From(git::GitRef),
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|
|
|
|
impl std::fmt::Display for Force {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-05-19 09:44:59 +01:00
|
|
|
Self::No => write!(f, "fast-forward"),
|
2024-08-06 07:43:28 +01:00
|
|
|
Self::From(from) => write!(f, "force-if-from:{from}"),
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 22:21:55 +01:00
|
|
|
|
2024-06-08 20:43:20 +01:00
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
2024-06-03 08:04:48 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2024-05-14 16:28:17 +01:00
|
|
|
pub enum Error {
|
2024-06-08 20:43:20 +01:00
|
|
|
#[error("io")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
|
|
|
#[error("network: {0}")]
|
|
|
|
Network(#[from] kxio::network::NetworkError),
|
2024-06-03 08:04:48 +01:00
|
|
|
|
2024-06-08 20:43:20 +01:00
|
|
|
#[error("fetch: {0}")]
|
|
|
|
Fetch(#[from] git::fetch::Error),
|
2024-06-03 08:04:48 +01:00
|
|
|
|
|
|
|
#[error("lock")]
|
2024-05-18 11:41:18 +01:00
|
|
|
Lock,
|
2024-06-08 20:43:20 +01:00
|
|
|
|
|
|
|
#[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),
|
2024-06-09 10:21:09 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[error("test")]
|
|
|
|
TestResult(#[from] Box<dyn std::error::Error>),
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|
|
|
|
|
2024-08-06 07:43:28 +01:00
|
|
|
/// Resets the position of a branch in the remote repo
|
|
|
|
///
|
|
|
|
/// Performs a 'git fetch' first to ensure we have up-to-date branch positions before
|
|
|
|
/// performing `git push`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will return an `Err` if their is no remote fetch defined in .git/config, or
|
|
|
|
/// if there are any network connectivity issues with the remote server.
|
2024-06-08 20:43:20 +01:00
|
|
|
pub fn reset(
|
2024-07-25 09:02:43 +01:00
|
|
|
open_repository: &dyn OpenRepositoryLike,
|
2024-06-08 20:43:20 +01:00
|
|
|
repo_details: &git::RepoDetails,
|
2024-07-25 09:02:43 +01:00
|
|
|
branch_name: &BranchName,
|
2024-06-08 21:13:31 +01:00
|
|
|
to_commit: &git::GitRef,
|
|
|
|
force: &git::push::Force,
|
2024-06-08 20:43:20 +01:00
|
|
|
) -> Result<()> {
|
2024-06-19 07:03:08 +01:00
|
|
|
open_repository.fetch()?;
|
|
|
|
open_repository.push(repo_details, branch_name, to_commit, force)
|
2024-06-08 20:43:20 +01:00
|
|
|
}
|