Skip to main content

git_proc/
add.rs

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