use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct ShowRefCommand {
pub executor: CommandExecutor,
pub patterns: Vec<String>,
pub heads: bool,
pub tags: bool,
pub verify: bool,
pub hash: Option<Option<u32>>,
pub dereference: bool,
pub head: bool,
pub exists: bool,
pub quiet: bool,
}
impl ShowRefCommand {
#[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 heads(&mut self) -> &mut Self {
self.heads = true;
self
}
pub fn tags(&mut self) -> &mut Self {
self.tags = true;
self
}
pub fn verify(&mut self) -> &mut Self {
self.verify = true;
self
}
pub fn hash(&mut self) -> &mut Self {
self.hash = Some(None);
self
}
pub fn hash_len(&mut self, n: u32) -> &mut Self {
self.hash = Some(Some(n));
self
}
pub fn dereference(&mut self) -> &mut Self {
self.dereference = true;
self
}
pub fn include_head(&mut self) -> &mut Self {
self.head = true;
self
}
pub fn exists(&mut self) -> &mut Self {
self.exists = true;
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for ShowRefCommand {
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!["show-ref".to_string()];
if self.heads {
args.push("--heads".into());
}
if self.tags {
args.push("--tags".into());
}
if self.head {
args.push("--head".into());
}
if self.dereference {
args.push("--dereference".into());
}
if self.verify {
args.push("--verify".into());
}
if self.exists {
args.push("--exists".into());
}
if self.quiet {
args.push("-q".into());
}
match self.hash {
Some(None) => args.push("--hash".into()),
Some(Some(n)) => args.push(format!("--hash={n}")),
None => {}
}
args.extend(self.patterns.iter().cloned());
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}