use crate::command::{CommandExecutor, GitCommand};
use crate::error::{Error, Result};
use async_trait::async_trait;
use std::path::PathBuf;
#[derive(Debug, Clone, Default)]
pub struct HashObjectCommand {
pub executor: CommandExecutor,
pub paths: Vec<PathBuf>,
pub write: bool,
pub stdin: bool,
pub object_type: Option<String>,
}
impl HashObjectCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn path(&mut self, p: impl Into<PathBuf>) -> &mut Self {
self.paths.push(p.into());
self
}
pub fn write(&mut self) -> &mut Self {
self.write = true;
self
}
pub fn object_type(&mut self, t: impl Into<String>) -> &mut Self {
self.object_type = Some(t.into());
self
}
}
#[async_trait]
impl GitCommand for HashObjectCommand {
type Output = String;
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!["hash-object".to_string()];
if self.write {
args.push("-w".into());
}
if let Some(t) = &self.object_type {
args.push("-t".into());
args.push(t.clone());
}
if self.stdin {
args.push("--stdin".into());
}
args.extend(self.paths.iter().map(|p| p.display().to_string()));
args
}
async fn execute(&self) -> Result<String> {
if self.paths.is_empty() && !self.stdin {
return Err(Error::invalid_config(
"hash-object requires at least one path",
));
}
let out = self.execute_raw().await?;
Ok(out.stdout_trimmed().to_string())
}
}