use std::marker::PhantomData;
use std::path::Path;
use crate::CommandError;
use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
#[must_use]
pub fn new() -> Reset<'static, NoTarget> {
Reset {
repo_path: None,
hard: false,
state: NoTarget,
_phantom: PhantomData,
}
}
#[derive(Debug)]
pub struct Reset<'a, S> {
repo_path: Option<&'a Path>,
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.repo_path = Some(path);
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 {
repo_path: self.repo_path,
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<(), CommandError> {
crate::Build::build(self).status().await
}
}
impl crate::Build for Reset<'_, NoTarget> {
fn build(self) -> cmd_proc::Command {
crate::base_command(self.repo_path)
.argument("reset")
.optional_flag(self.hard, "--hard")
}
}
impl<'a> crate::Build for Reset<'a, WithTarget<'a>> {
fn build(self) -> cmd_proc::Command {
crate::base_command(self.repo_path)
.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 {
repo_path: self.repo_path,
hard: self.hard,
state: NoTarget,
_phantom: PhantomData,
});
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 {
repo_path: self.repo_path,
hard: self.hard,
state: WithTarget {
target: self.state.target,
},
_phantom: PhantomData,
});
command.test_eq(other);
}
}