use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct ForEachRefCommand {
pub executor: CommandExecutor,
pub patterns: Vec<String>,
pub format: Option<String>,
pub count: Option<u32>,
pub sort: Option<String>,
pub contains: Option<String>,
pub merged: Option<String>,
pub no_merged: Option<String>,
pub points_at: Option<String>,
}
impl ForEachRefCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
self.patterns.push(p.into());
self
}
pub fn format(&mut self, fmt: impl Into<String>) -> &mut Self {
self.format = Some(fmt.into());
self
}
pub fn count(&mut self, n: u32) -> &mut Self {
self.count = Some(n);
self
}
pub fn sort(&mut self, key: impl Into<String>) -> &mut Self {
self.sort = Some(key.into());
self
}
pub fn contains(&mut self, c: impl Into<String>) -> &mut Self {
self.contains = Some(c.into());
self
}
pub fn merged(&mut self, c: impl Into<String>) -> &mut Self {
self.merged = Some(c.into());
self
}
pub fn no_merged(&mut self, c: impl Into<String>) -> &mut Self {
self.no_merged = Some(c.into());
self
}
pub fn points_at(&mut self, c: impl Into<String>) -> &mut Self {
self.points_at = Some(c.into());
self
}
}
#[async_trait]
impl GitCommand for ForEachRefCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["for-each-ref".to_string()];
if let Some(f) = &self.format {
args.push(format!("--format={f}"));
}
if let Some(n) = self.count {
args.push(format!("--count={n}"));
}
if let Some(s) = &self.sort {
args.push(format!("--sort={s}"));
}
if let Some(c) = &self.contains {
args.push(format!("--contains={c}"));
}
if let Some(c) = &self.merged {
args.push(format!("--merged={c}"));
}
if let Some(c) = &self.no_merged {
args.push(format!("--no-merged={c}"));
}
if let Some(c) = &self.points_at {
args.push(format!("--points-at={c}"));
}
args.extend(self.patterns.iter().cloned());
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}