Skip to main content

git_spawn/command/
show.rs

1//! `git show` — show various types of objects.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git show`.
8#[derive(Debug, Clone, Default)]
9pub struct ShowCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Objects to show.
13    pub objects: Vec<String>,
14    /// `--format=...`.
15    pub format: Option<String>,
16    /// `--stat`.
17    pub stat: bool,
18    /// `--name-only`.
19    pub name_only: bool,
20    /// `--no-patch`.
21    pub no_patch: bool,
22}
23
24impl ShowCommand {
25    /// New `show` command.
26    #[must_use]
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Show a specific object (commit, tag, tree, blob).
32    pub fn object(&mut self, o: impl Into<String>) -> &mut Self {
33        self.objects.push(o.into());
34        self
35    }
36
37    /// Pretty format.
38    pub fn format(&mut self, fmt: impl Into<String>) -> &mut Self {
39        self.format = Some(fmt.into());
40        self
41    }
42
43    /// Include `--stat`.
44    pub fn stat(&mut self) -> &mut Self {
45        self.stat = true;
46        self
47    }
48
49    /// `--name-only`.
50    pub fn name_only(&mut self) -> &mut Self {
51        self.name_only = true;
52        self
53    }
54
55    /// Suppress patch output.
56    pub fn no_patch(&mut self) -> &mut Self {
57        self.no_patch = true;
58        self
59    }
60}
61
62#[async_trait]
63impl GitCommand for ShowCommand {
64    type Output = CommandOutput;
65    fn get_executor(&self) -> &CommandExecutor {
66        &self.executor
67    }
68    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
69        &mut self.executor
70    }
71    fn build_command_args(&self) -> Vec<String> {
72        let mut args = vec!["show".to_string()];
73        if let Some(f) = &self.format {
74            args.push(format!("--format={f}"));
75        }
76        if self.stat {
77            args.push("--stat".into());
78        }
79        if self.name_only {
80            args.push("--name-only".into());
81        }
82        if self.no_patch {
83            args.push("--no-patch".into());
84        }
85        args.extend(self.objects.iter().cloned());
86        args
87    }
88    async fn execute(&self) -> Result<CommandOutput> {
89        self.execute_raw().await
90    }
91}