Skip to main content

git_spawn/command/
rm.rs

1//! `git rm` — remove files from the working tree and from the index.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git rm`.
8#[derive(Debug, Clone, Default)]
9pub struct RmCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Pathspecs.
13    pub paths: Vec<String>,
14    /// `--cached`.
15    pub cached: bool,
16    /// `-r`.
17    pub recursive: bool,
18    /// `--force`.
19    pub force: bool,
20    /// `--dry-run`.
21    pub dry_run: bool,
22    /// `--quiet`.
23    pub quiet: bool,
24}
25
26impl RmCommand {
27    /// New `rm`.
28    #[must_use]
29    pub fn new() -> Self {
30        Self::default()
31    }
32    /// Add a path.
33    pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
34        self.paths.push(p.into());
35        self
36    }
37    /// `--cached`.
38    pub fn cached(&mut self) -> &mut Self {
39        self.cached = true;
40        self
41    }
42    /// `-r`.
43    pub fn recursive(&mut self) -> &mut Self {
44        self.recursive = true;
45        self
46    }
47    /// `--force`.
48    pub fn force(&mut self) -> &mut Self {
49        self.force = true;
50        self
51    }
52    /// `--dry-run`.
53    pub fn dry_run(&mut self) -> &mut Self {
54        self.dry_run = true;
55        self
56    }
57    /// `--quiet`.
58    pub fn quiet(&mut self) -> &mut Self {
59        self.quiet = true;
60        self
61    }
62}
63
64#[async_trait]
65impl GitCommand for RmCommand {
66    type Output = CommandOutput;
67    fn get_executor(&self) -> &CommandExecutor {
68        &self.executor
69    }
70    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
71        &mut self.executor
72    }
73    fn build_command_args(&self) -> Vec<String> {
74        let mut args = vec!["rm".to_string()];
75        if self.cached {
76            args.push("--cached".into());
77        }
78        if self.recursive {
79            args.push("-r".into());
80        }
81        if self.force {
82            args.push("--force".into());
83        }
84        if self.dry_run {
85            args.push("--dry-run".into());
86        }
87        if self.quiet {
88            args.push("--quiet".into());
89        }
90        if !self.paths.is_empty() {
91            args.push("--".into());
92            args.extend(self.paths.iter().cloned());
93        }
94        args
95    }
96    async fn execute(&self) -> Result<CommandOutput> {
97        self.execute_raw().await
98    }
99}