git_spawn/command/
for_each_ref.rs1use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone, Default)]
9pub struct ForEachRefCommand {
10 pub executor: CommandExecutor,
12 pub patterns: Vec<String>,
14 pub format: Option<String>,
16 pub count: Option<u32>,
18 pub sort: Option<String>,
20 pub contains: Option<String>,
22 pub merged: Option<String>,
24 pub no_merged: Option<String>,
26 pub points_at: Option<String>,
28}
29
30impl ForEachRefCommand {
31 #[must_use]
33 pub fn new() -> Self {
34 Self::default()
35 }
36
37 pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
39 self.patterns.push(p.into());
40 self
41 }
42
43 pub fn format(&mut self, fmt: impl Into<String>) -> &mut Self {
45 self.format = Some(fmt.into());
46 self
47 }
48
49 pub fn count(&mut self, n: u32) -> &mut Self {
51 self.count = Some(n);
52 self
53 }
54
55 pub fn sort(&mut self, key: impl Into<String>) -> &mut Self {
57 self.sort = Some(key.into());
58 self
59 }
60
61 pub fn contains(&mut self, c: impl Into<String>) -> &mut Self {
63 self.contains = Some(c.into());
64 self
65 }
66
67 pub fn merged(&mut self, c: impl Into<String>) -> &mut Self {
69 self.merged = Some(c.into());
70 self
71 }
72
73 pub fn no_merged(&mut self, c: impl Into<String>) -> &mut Self {
75 self.no_merged = Some(c.into());
76 self
77 }
78
79 pub fn points_at(&mut self, c: impl Into<String>) -> &mut Self {
81 self.points_at = Some(c.into());
82 self
83 }
84}
85
86#[async_trait]
87impl GitCommand for ForEachRefCommand {
88 type Output = CommandOutput;
89 fn get_executor(&self) -> &CommandExecutor {
90 &self.executor
91 }
92 fn get_executor_mut(&mut self) -> &mut CommandExecutor {
93 &mut self.executor
94 }
95 fn build_command_args(&self) -> Vec<String> {
96 let mut args = vec!["for-each-ref".to_string()];
97 if let Some(f) = &self.format {
98 args.push(format!("--format={f}"));
99 }
100 if let Some(n) = self.count {
101 args.push(format!("--count={n}"));
102 }
103 if let Some(s) = &self.sort {
104 args.push(format!("--sort={s}"));
105 }
106 if let Some(c) = &self.contains {
107 args.push(format!("--contains={c}"));
108 }
109 if let Some(c) = &self.merged {
110 args.push(format!("--merged={c}"));
111 }
112 if let Some(c) = &self.no_merged {
113 args.push(format!("--no-merged={c}"));
114 }
115 if let Some(c) = &self.points_at {
116 args.push(format!("--points-at={c}"));
117 }
118 args.extend(self.patterns.iter().cloned());
119 args
120 }
121 async fn execute(&self) -> Result<CommandOutput> {
122 self.execute_raw().await
123 }
124}