Skip to main content

git_proc/
add.rs

1use std::path::Path;
2
3use crate::CommandError;
4
5/// Create a new `git add` command builder.
6#[must_use]
7pub fn new() -> Add<'static> {
8    Add::new()
9}
10
11/// Builder for `git add` command.
12///
13/// See `git add --help` for full documentation.
14#[derive(Debug)]
15pub struct Add<'a> {
16    repo_path: Option<&'a Path>,
17    all: bool,
18    pathspecs: Vec<&'a str>,
19}
20
21impl<'a> Add<'a> {
22    #[must_use]
23    fn new() -> Self {
24        Self {
25            repo_path: None,
26            all: false,
27            pathspecs: Vec::new(),
28        }
29    }
30
31    crate::flag_methods! {
32        /// Add all changes (new, modified, deleted).
33        ///
34        /// Corresponds to `--all` or `-A`.
35        pub fn all / all_if, all, "Conditionally add all changes."
36    }
37
38    /// Add a pathspec to stage.
39    #[must_use]
40    pub fn pathspec(mut self, pathspec: &'a str) -> Self {
41        self.pathspecs.push(pathspec);
42        self
43    }
44
45    /// Execute the command and return the exit status.
46    pub async fn status(self) -> Result<(), CommandError> {
47        crate::Build::build(self).status().await
48    }
49}
50
51crate::impl_repo_path!(Add);
52
53impl Default for Add<'_> {
54    fn default() -> Self {
55        Self::new()
56    }
57}
58
59impl crate::Build for Add<'_> {
60    fn build(self) -> cmd_proc::Command {
61        crate::base_command(self.repo_path)
62            .argument("add")
63            .optional_flag(self.all, "--all")
64            .arguments(self.pathspecs)
65    }
66}
67
68#[cfg(feature = "test-utils")]
69impl Add<'_> {
70    /// Compare the built command with another command using debug representation.
71    pub fn test_eq(&self, other: &cmd_proc::Command) {
72        let command = crate::Build::build(Self {
73            repo_path: self.repo_path,
74            all: self.all,
75            pathspecs: self.pathspecs.clone(),
76        });
77        command.test_eq(other);
78    }
79}