git_proc/
show_ref.rs

1use std::path::Path;
2
3/// Create a new `git show-ref` command builder.
4#[must_use]
5pub fn new() -> ShowRef<'static> {
6    ShowRef::new()
7}
8
9/// Builder for `git show-ref` command.
10///
11/// See `git show-ref --help` for full documentation.
12#[derive(Debug)]
13pub struct ShowRef<'a> {
14    repo_path: Option<&'a Path>,
15    verify: bool,
16    pattern: Option<&'a str>,
17}
18
19impl<'a> ShowRef<'a> {
20    #[must_use]
21    fn new() -> Self {
22        Self {
23            repo_path: None,
24            verify: false,
25            pattern: None,
26        }
27    }
28
29    /// Set the repository path (`-C <path>`).
30    #[must_use]
31    pub fn repo_path(mut self, path: &'a Path) -> Self {
32        self.repo_path = Some(path);
33        self
34    }
35
36    crate::flag_methods! {
37        /// Enable strict reference checking.
38        ///
39        /// Corresponds to `--verify`. When used, requires an exact ref path.
40        pub fn verify / verify_if, verify, "Conditionally enable strict reference checking."
41    }
42
43    /// Set the pattern to match references against.
44    #[must_use]
45    pub fn pattern(mut self, pattern: &'a str) -> Self {
46        self.pattern = Some(pattern);
47        self
48    }
49
50    /// Capture stdout from this command.
51    ///
52    /// Returns error if the ref does not exist (with `--verify`).
53    #[must_use]
54    pub fn stdout(self) -> cmd_proc::Capture {
55        self.build().stdout()
56    }
57
58    fn build(self) -> cmd_proc::Command {
59        crate::base_command(self.repo_path)
60            .argument("show-ref")
61            .optional_argument(self.verify.then_some("--verify"))
62            .optional_argument(self.pattern)
63    }
64}
65
66impl Default for ShowRef<'_> {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72#[cfg(feature = "test-utils")]
73impl ShowRef<'_> {
74    /// Compare the built command with another command using debug representation.
75    pub fn test_eq(&self, other: &cmd_proc::Command) {
76        let command = Self {
77            repo_path: self.repo_path,
78            verify: self.verify,
79            pattern: self.pattern,
80        }
81        .build();
82        command.test_eq(other);
83    }
84}