git_spawn/command/
reset.rs1use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone, Copy)]
9pub enum ResetMode {
10 Soft,
12 Mixed,
14 Hard,
16 Merge,
18 Keep,
20}
21
22#[derive(Debug, Clone, Default)]
24pub struct ResetCommand {
25 pub executor: CommandExecutor,
27 pub mode: Option<ResetMode>,
29 pub commit: Option<String>,
31 pub paths: Vec<String>,
33 pub quiet: bool,
35}
36
37impl ResetCommand {
38 #[must_use]
40 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn mode(&mut self, m: ResetMode) -> &mut Self {
46 self.mode = Some(m);
47 self
48 }
49
50 pub fn commit(&mut self, c: impl Into<String>) -> &mut Self {
52 self.commit = Some(c.into());
53 self
54 }
55
56 pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
58 self.paths.push(p.into());
59 self
60 }
61
62 pub fn quiet(&mut self) -> &mut Self {
64 self.quiet = true;
65 self
66 }
67}
68
69#[async_trait]
70impl GitCommand for ResetCommand {
71 type Output = CommandOutput;
72 fn get_executor(&self) -> &CommandExecutor {
73 &self.executor
74 }
75 fn get_executor_mut(&mut self) -> &mut CommandExecutor {
76 &mut self.executor
77 }
78 fn build_command_args(&self) -> Vec<String> {
79 let mut args = vec!["reset".to_string()];
80 match self.mode {
81 Some(ResetMode::Soft) => args.push("--soft".into()),
82 Some(ResetMode::Mixed) => args.push("--mixed".into()),
83 Some(ResetMode::Hard) => args.push("--hard".into()),
84 Some(ResetMode::Merge) => args.push("--merge".into()),
85 Some(ResetMode::Keep) => args.push("--keep".into()),
86 None => {}
87 }
88 if self.quiet {
89 args.push("--quiet".into());
90 }
91 if let Some(c) = &self.commit {
92 args.push(c.clone());
93 }
94 if !self.paths.is_empty() {
95 args.push("--".into());
96 args.extend(self.paths.iter().cloned());
97 }
98 args
99 }
100 async fn execute(&self) -> Result<CommandOutput> {
101 self.execute_raw().await
102 }
103}