Skip to main content

git_spawn/command/
for_each_ref.rs

1//! `git for-each-ref` — output information on each ref matching a pattern.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git for-each-ref`.
8#[derive(Debug, Clone, Default)]
9pub struct ForEachRefCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Patterns to match (e.g. `"refs/heads/*"`).
13    pub patterns: Vec<String>,
14    /// `--format=<fmt>`.
15    pub format: Option<String>,
16    /// `--count=<n>`.
17    pub count: Option<u32>,
18    /// `--sort=<key>`.
19    pub sort: Option<String>,
20    /// `--contains`.
21    pub contains: Option<String>,
22    /// `--merged`.
23    pub merged: Option<String>,
24    /// `--no-merged`.
25    pub no_merged: Option<String>,
26    /// `--points-at`.
27    pub points_at: Option<String>,
28}
29
30impl ForEachRefCommand {
31    /// New command.
32    #[must_use]
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    /// Add a pattern.
38    pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
39        self.patterns.push(p.into());
40        self
41    }
42
43    /// Custom `--format`.
44    pub fn format(&mut self, fmt: impl Into<String>) -> &mut Self {
45        self.format = Some(fmt.into());
46        self
47    }
48
49    /// `--count`.
50    pub fn count(&mut self, n: u32) -> &mut Self {
51        self.count = Some(n);
52        self
53    }
54
55    /// `--sort`.
56    pub fn sort(&mut self, key: impl Into<String>) -> &mut Self {
57        self.sort = Some(key.into());
58        self
59    }
60
61    /// `--contains`.
62    pub fn contains(&mut self, c: impl Into<String>) -> &mut Self {
63        self.contains = Some(c.into());
64        self
65    }
66
67    /// `--merged`.
68    pub fn merged(&mut self, c: impl Into<String>) -> &mut Self {
69        self.merged = Some(c.into());
70        self
71    }
72
73    /// `--no-merged`.
74    pub fn no_merged(&mut self, c: impl Into<String>) -> &mut Self {
75        self.no_merged = Some(c.into());
76        self
77    }
78
79    /// `--points-at`.
80    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}