use std::marker::PhantomData;
use std::path::Path;
use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
#[must_use]
pub fn new() -> Reset<'static, NoTarget> {
Reset {
base: crate::RepoBase::default(),
hard: false,
state: NoTarget,
_phantom: PhantomData,
}
}
#[derive(Debug)]
pub struct Reset<'a, S> {
base: crate::RepoBase<'a>,
hard: bool,
state: S,
_phantom: PhantomData<&'a ()>,
}
impl<'a, S> Reset<'a, S> {
#[must_use]
pub fn repo_path(mut self, path: &'a Path) -> Self {
self.base.repo_path = Some(path);
self
}
#[must_use]
pub fn env_policy(mut self, policy: crate::EnvPolicy) -> Self {
self.base.env_policy = policy;
self
}
crate::flag_methods! {
pub fn hard / hard_if, hard, "Conditionally reset the index and working tree."
}
}
impl<'a, S> crate::RepoPath<'a> for Reset<'a, S> {
fn repo_path(self, path: &'a Path) -> Self {
self.repo_path(path)
}
}
impl<'a> Reset<'a, NoTarget> {
#[must_use]
pub fn commit_ish(self, commit_ish: impl Into<CommitIsh<'a>>) -> Reset<'a, WithTarget<'a>> {
Reset {
base: self.base,
hard: self.hard,
state: WithTarget {
target: commit_ish.into(),
},
_phantom: PhantomData,
}
}
}
impl<'a, S> Reset<'a, S>
where
Self: crate::Build,
{
pub async fn status(self) -> Result<(), crate::Error> {
Ok(crate::Build::build(self)?.status().await?)
}
}
impl crate::Build for Reset<'_, NoTarget> {
fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
Ok(self
.base
.command()?
.argument("reset")
.optional_flag(self.hard, "--hard"))
}
}
impl<'a> crate::Build for Reset<'a, WithTarget<'a>> {
fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
Ok(self
.base
.command()?
.argument("reset")
.optional_flag(self.hard, "--hard")
.argument(self.state.target))
}
}
#[cfg(feature = "test-utils")]
impl<'a> Reset<'a, NoTarget> {
pub fn test_eq(&self, other: &cmd_proc::Command) {
let command = crate::Build::build(Self {
base: self.base,
hard: self.hard,
state: NoTarget,
_phantom: PhantomData,
})
.unwrap();
command.test_eq(other);
}
}
#[cfg(feature = "test-utils")]
impl<'a> Reset<'a, WithTarget<'a>> {
pub fn test_eq(&self, other: &cmd_proc::Command) {
let command = crate::Build::build(Self {
base: self.base,
hard: self.hard,
state: WithTarget {
target: self.state.target,
},
_phantom: PhantomData,
})
.unwrap();
command.test_eq(other);
}
}