Skip to main content

git_proc/
clean.rs

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