use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Copy)]
pub enum ResetMode {
Soft,
Mixed,
Hard,
Merge,
Keep,
}
#[derive(Debug, Clone, Default)]
pub struct ResetCommand {
pub executor: CommandExecutor,
pub mode: Option<ResetMode>,
pub commit: Option<String>,
pub paths: Vec<String>,
pub quiet: bool,
}
impl ResetCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn mode(&mut self, m: ResetMode) -> &mut Self {
self.mode = Some(m);
self
}
pub fn commit(&mut self, c: impl Into<String>) -> &mut Self {
self.commit = Some(c.into());
self
}
pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
self.paths.push(p.into());
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for ResetCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["reset".to_string()];
match self.mode {
Some(ResetMode::Soft) => args.push("--soft".into()),
Some(ResetMode::Mixed) => args.push("--mixed".into()),
Some(ResetMode::Hard) => args.push("--hard".into()),
Some(ResetMode::Merge) => args.push("--merge".into()),
Some(ResetMode::Keep) => args.push("--keep".into()),
None => {}
}
if self.quiet {
args.push("--quiet".into());
}
if let Some(c) = &self.commit {
args.push(c.clone());
}
if !self.paths.is_empty() {
args.push("--".into());
args.extend(self.paths.iter().cloned());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}