use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct LogCommand {
pub executor: CommandExecutor,
pub max_count: Option<u32>,
pub skip: Option<u32>,
pub oneline: bool,
pub graph: bool,
pub all: bool,
pub reverse: bool,
pub format: Option<String>,
pub since: Option<String>,
pub until: Option<String>,
pub author: Option<String>,
pub grep: Option<String>,
pub revisions: Vec<String>,
pub paths: Vec<String>,
}
impl LogCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn max_count(&mut self, n: u32) -> &mut Self {
self.max_count = Some(n);
self
}
pub fn skip(&mut self, n: u32) -> &mut Self {
self.skip = Some(n);
self
}
pub fn oneline(&mut self) -> &mut Self {
self.oneline = true;
self
}
pub fn graph(&mut self) -> &mut Self {
self.graph = true;
self
}
pub fn all(&mut self) -> &mut Self {
self.all = true;
self
}
pub fn reverse(&mut self) -> &mut Self {
self.reverse = true;
self
}
pub fn format(&mut self, fmt: impl Into<String>) -> &mut Self {
self.format = Some(fmt.into());
self
}
pub fn since(&mut self, s: impl Into<String>) -> &mut Self {
self.since = Some(s.into());
self
}
pub fn until(&mut self, s: impl Into<String>) -> &mut Self {
self.until = Some(s.into());
self
}
pub fn author(&mut self, s: impl Into<String>) -> &mut Self {
self.author = Some(s.into());
self
}
pub fn grep(&mut self, s: impl Into<String>) -> &mut Self {
self.grep = Some(s.into());
self
}
pub fn revision(&mut self, r: impl Into<String>) -> &mut Self {
self.revisions.push(r.into());
self
}
pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
self.paths.push(p.into());
self
}
}
#[async_trait]
impl GitCommand for LogCommand {
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!["log".to_string()];
if let Some(n) = self.max_count {
args.push(format!("-n{n}"));
}
if let Some(n) = self.skip {
args.push(format!("--skip={n}"));
}
if self.oneline {
args.push("--oneline".into());
}
if self.graph {
args.push("--graph".into());
}
if self.all {
args.push("--all".into());
}
if self.reverse {
args.push("--reverse".into());
}
if let Some(f) = &self.format {
args.push(format!("--format={f}"));
}
if let Some(s) = &self.since {
args.push(format!("--since={s}"));
}
if let Some(s) = &self.until {
args.push(format!("--until={s}"));
}
if let Some(s) = &self.author {
args.push(format!("--author={s}"));
}
if let Some(s) = &self.grep {
args.push(format!("--grep={s}"));
}
args.extend(self.revisions.iter().cloned());
if !self.paths.is_empty() {
args.push("--".into());
args.extend(self.paths.iter().cloned());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}