git_spawn/command/
show_ref.rs1use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone, Default)]
9pub struct ShowRefCommand {
10 pub executor: CommandExecutor,
12 pub patterns: Vec<String>,
14 pub heads: bool,
16 pub tags: bool,
18 pub verify: bool,
20 pub hash: Option<Option<u32>>,
22 pub dereference: bool,
24 pub head: bool,
26 pub exists: bool,
28 pub quiet: bool,
30}
31
32impl ShowRefCommand {
33 #[must_use]
35 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
41 self.patterns.push(p.into());
42 self
43 }
44
45 pub fn heads(&mut self) -> &mut Self {
47 self.heads = true;
48 self
49 }
50
51 pub fn tags(&mut self) -> &mut Self {
53 self.tags = true;
54 self
55 }
56
57 pub fn verify(&mut self) -> &mut Self {
59 self.verify = true;
60 self
61 }
62
63 pub fn hash(&mut self) -> &mut Self {
65 self.hash = Some(None);
66 self
67 }
68
69 pub fn hash_len(&mut self, n: u32) -> &mut Self {
71 self.hash = Some(Some(n));
72 self
73 }
74
75 pub fn dereference(&mut self) -> &mut Self {
77 self.dereference = true;
78 self
79 }
80
81 pub fn include_head(&mut self) -> &mut Self {
83 self.head = true;
84 self
85 }
86
87 pub fn exists(&mut self) -> &mut Self {
89 self.exists = true;
90 self
91 }
92
93 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}