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    /// Set the repository path (`-C <path>`).
32    #[must_use]
33    pub fn repo_path(mut self, path: &'a Path) -> Self {
34        self.repo_path = Some(path);
35        self
36    }
37
38    crate::flag_methods! {
39        /// Add all changes (new, modified, deleted).
40        ///
41        /// Corresponds to `--all` or `-A`.
42        pub fn all / all_if, all, "Conditionally add all changes."
43    }
44
45    /// Add a pathspec to stage.
46    #[must_use]
47    pub fn pathspec(mut self, pathspec: &'a str) -> Self {
48        self.pathspecs.push(pathspec);
49        self
50    }
51
52    /// Execute the command and return the exit status.
53    pub fn status(self) -> Result<(), CommandError> {
54        self.build().status()
55    }
56
57    fn build(self) -> cmd_proc::Command {
58        crate::base_command(self.repo_path)
59            .argument("add")
60            .optional_argument(self.all.then_some("--all"))
61            .arguments(self.pathspecs)
62    }
63}
64
65impl Default for Add<'_> {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71#[cfg(feature = "test-utils")]
72impl Add<'_> {
73    /// Compare the built command with another command using debug representation.
74    pub fn test_eq(&self, other: &cmd_proc::Command) {
75        let command = Self {
76            repo_path: self.repo_path,
77            all: self.all,
78            pathspecs: self.pathspecs.clone(),
79        }
80        .build();
81        command.test_eq(other);
82    }
83}