1use std::path::Path;
2
3use crate::CommandError;
4
5#[must_use]
7pub fn new() -> Add<'static> {
8 Add::new()
9}
10
11#[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 #[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 pub fn all / all_if, all, "Conditionally add all changes."
43 }
44
45 #[must_use]
47 pub fn pathspec(mut self, pathspec: &'a str) -> Self {
48 self.pathspecs.push(pathspec);
49 self
50 }
51
52 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 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}