git_proc/
show.rs

1use std::path::Path;
2
3use crate::CommandError;
4
5/// Create a new `git show` command builder.
6///
7/// The object can be a commit, tree, blob, or tag reference.
8/// For file contents at a specific revision, use format: `revision:path`
9#[must_use]
10pub fn new(object: &str) -> Show<'_> {
11    Show::new(object)
12}
13
14/// Builder for `git show` command.
15///
16/// See `git show --help` for full documentation.
17#[derive(Debug)]
18pub struct Show<'a> {
19    repo_path: Option<&'a Path>,
20    object: &'a str,
21}
22
23impl<'a> Show<'a> {
24    #[must_use]
25    fn new(object: &'a str) -> Self {
26        Self {
27            repo_path: None,
28            object,
29        }
30    }
31
32    /// Set the repository path (`-C <path>`).
33    #[must_use]
34    pub fn repo_path(mut self, path: &'a Path) -> Self {
35        self.repo_path = Some(path);
36        self
37    }
38
39    /// Capture stdout from this command.
40    #[must_use]
41    pub fn stdout(self) -> cmd_proc::Capture {
42        self.build().stdout()
43    }
44
45    /// Execute and return full output regardless of exit status.
46    ///
47    /// Use this when you need to inspect stderr on failure.
48    pub fn output(self) -> Result<cmd_proc::Output, CommandError> {
49        self.build().output()
50    }
51
52    fn build(self) -> cmd_proc::Command {
53        crate::base_command(self.repo_path)
54            .argument("show")
55            .argument(self.object)
56    }
57}
58
59#[cfg(feature = "test-utils")]
60impl Show<'_> {
61    /// Compare the built command with another command using debug representation.
62    pub fn test_eq(&self, other: &cmd_proc::Command) {
63        let command = Self {
64            repo_path: self.repo_path,
65            object: self.object,
66        }
67        .build();
68        command.test_eq(other);
69    }
70}