Skip to main content

git_spawn/command/
show_ref.rs

1//! `git show-ref` — list references in a local repository.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git show-ref`.
8#[derive(Debug, Clone, Default)]
9pub struct ShowRefCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Patterns to match.
13    pub patterns: Vec<String>,
14    /// `--heads`.
15    pub heads: bool,
16    /// `--tags`.
17    pub tags: bool,
18    /// `--verify`: error if a pattern doesn't match.
19    pub verify: bool,
20    /// `--hash[=N]`.
21    pub hash: Option<Option<u32>>,
22    /// `--dereference`.
23    pub dereference: bool,
24    /// `--head`.
25    pub head: bool,
26    /// `--exists`: test existence of a specific ref.
27    pub exists: bool,
28    /// `-q` quiet.
29    pub quiet: bool,
30}
31
32impl ShowRefCommand {
33    /// New command.
34    #[must_use]
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Add a ref pattern.
40    pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
41        self.patterns.push(p.into());
42        self
43    }
44
45    /// Limit to branch refs.
46    pub fn heads(&mut self) -> &mut Self {
47        self.heads = true;
48        self
49    }
50
51    /// Limit to tag refs.
52    pub fn tags(&mut self) -> &mut Self {
53        self.tags = true;
54        self
55    }
56
57    /// `--verify`.
58    pub fn verify(&mut self) -> &mut Self {
59        self.verify = true;
60        self
61    }
62
63    /// `--hash`.
64    pub fn hash(&mut self) -> &mut Self {
65        self.hash = Some(None);
66        self
67    }
68
69    /// `--hash=N`.
70    pub fn hash_len(&mut self, n: u32) -> &mut Self {
71        self.hash = Some(Some(n));
72        self
73    }
74
75    /// `--dereference`.
76    pub fn dereference(&mut self) -> &mut Self {
77        self.dereference = true;
78        self
79    }
80
81    /// Include HEAD in output (`--head`).
82    pub fn include_head(&mut self) -> &mut Self {
83        self.head = true;
84        self
85    }
86
87    /// `--exists`.
88    pub fn exists(&mut self) -> &mut Self {
89        self.exists = true;
90        self
91    }
92
93    /// `-q`.
94    pub fn quiet(&mut self) -> &mut Self {
95        self.quiet = true;
96        self
97    }
98}
99
100#[async_trait]
101impl GitCommand for ShowRefCommand {
102    type Output = CommandOutput;
103    fn get_executor(&self) -> &CommandExecutor {
104        &self.executor
105    }
106    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
107        &mut self.executor
108    }
109    fn build_command_args(&self) -> Vec<String> {
110        let mut args = vec!["show-ref".to_string()];
111        if self.heads {
112            args.push("--heads".into());
113        }
114        if self.tags {
115            args.push("--tags".into());
116        }
117        if self.head {
118            args.push("--head".into());
119        }
120        if self.dereference {
121            args.push("--dereference".into());
122        }
123        if self.verify {
124            args.push("--verify".into());
125        }
126        if self.exists {
127            args.push("--exists".into());
128        }
129        if self.quiet {
130            args.push("-q".into());
131        }
132        match self.hash {
133            Some(None) => args.push("--hash".into()),
134            Some(Some(n)) => args.push(format!("--hash={n}")),
135            None => {}
136        }
137        args.extend(self.patterns.iter().cloned());
138        args
139    }
140    async fn execute(&self) -> Result<CommandOutput> {
141        self.execute_raw().await
142    }
143}