Skip to main content

git_proc/
clean.rs

1/// Create a new `git clean` command builder.
2#[must_use]
3pub fn new() -> Clean<'static> {
4    Clean::new()
5}
6
7/// Builder for `git clean` command.
8///
9/// See `git clean --help` for full documentation.
10#[derive(Debug)]
11pub struct Clean<'a> {
12    base: crate::RepoBase<'a>,
13    directories: bool,
14    force: bool,
15    include_ignored: bool,
16}
17
18crate::impl_repo_base!(Clean);
19
20impl<'a> Clean<'a> {
21    #[must_use]
22    fn new() -> Self {
23        Self {
24            base: crate::RepoBase::default(),
25            directories: false,
26            force: false,
27            include_ignored: false,
28        }
29    }
30
31    crate::flag_methods! {
32        /// Recurse into untracked directories.
33        ///
34        /// Corresponds to `-d` (no long form).
35        pub fn directories / directories_if, directories, "Conditionally recurse into untracked directories."
36    }
37
38    crate::flag_methods! {
39        /// Force removal even when `clean.requireForce` is set.
40        ///
41        /// Corresponds to `--force`.
42        pub fn force / force_if, force, "Conditionally force removal."
43    }
44
45    crate::flag_methods! {
46        /// Also remove files ignored by `.gitignore`.
47        ///
48        /// Corresponds to `-x` (no long form).
49        pub fn include_ignored / include_ignored_if, include_ignored, "Conditionally also remove ignored files."
50    }
51
52    /// Execute the command and return the exit status.
53    pub async fn status(self) -> Result<(), crate::Error> {
54        Ok(crate::Build::build(self)?.status().await?)
55    }
56}
57
58impl Default for Clean<'_> {
59    fn default() -> Self {
60        Self::new()
61    }
62}
63
64impl crate::Build for Clean<'_> {
65    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
66        Ok(self
67            .base
68            .command()?
69            .argument("clean")
70            .optional_flag(self.directories, "-d")
71            .optional_flag(self.force, "--force")
72            .optional_flag(self.include_ignored, "-x"))
73    }
74}
75
76#[cfg(feature = "test-utils")]
77impl Clean<'_> {
78    /// Compare the built command with another command using debug representation.
79    pub fn test_eq(&self, other: &cmd_proc::Command) {
80        let command = crate::Build::build(Self {
81            base: self.base,
82            directories: self.directories,
83            force: self.force,
84            include_ignored: self.include_ignored,
85        })
86        .unwrap();
87        command.test_eq(other);
88    }
89}