git_proc/
status.rs

1use std::path::Path;
2
3/// Create a new `git status` command builder.
4#[must_use]
5pub fn new() -> Status<'static> {
6    Status::new()
7}
8
9/// Builder for `git status` command.
10///
11/// See `git status --help` for full documentation.
12#[derive(Debug)]
13pub struct Status<'a> {
14    repo_path: Option<&'a Path>,
15    porcelain: bool,
16}
17
18impl<'a> Status<'a> {
19    #[must_use]
20    fn new() -> Self {
21        Self {
22            repo_path: None,
23            porcelain: false,
24        }
25    }
26
27    /// Set the repository path (`-C <path>`).
28    #[must_use]
29    pub fn repo_path(mut self, path: &'a Path) -> Self {
30        self.repo_path = Some(path);
31        self
32    }
33
34    crate::flag_methods! {
35        /// Give output in machine-parseable format.
36        ///
37        /// Corresponds to `--porcelain`.
38        pub fn porcelain / porcelain_if, porcelain, "Conditionally enable porcelain output."
39    }
40
41    /// Capture stdout from this command.
42    #[must_use]
43    pub fn stdout(self) -> cmd_proc::Capture {
44        self.build().stdout()
45    }
46
47    fn build(self) -> cmd_proc::Command {
48        crate::base_command(self.repo_path)
49            .argument("status")
50            .optional_argument(self.porcelain.then_some("--porcelain"))
51    }
52}
53
54impl Default for Status<'_> {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60#[cfg(feature = "test-utils")]
61impl Status<'_> {
62    /// Compare the built command with another command using debug representation.
63    pub fn test_eq(&self, other: &cmd_proc::Command) {
64        let command = Self {
65            repo_path: self.repo_path,
66            porcelain: self.porcelain,
67        }
68        .build();
69        command.test_eq(other);
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_status() {
79        let output = Status::new().stdout().string().unwrap();
80        // Just verify it runs without error
81        let _ = output;
82    }
83
84    #[test]
85    fn test_status_porcelain() {
86        let output = Status::new().porcelain().stdout().string().unwrap();
87        // Porcelain output is empty if repo is clean
88        let _ = output;
89    }
90}