use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct PullCommand {
pub executor: CommandExecutor,
pub remote: Option<String>,
pub refspec: Option<String>,
pub rebase: Option<String>,
pub no_rebase: bool,
pub ff_only: bool,
pub no_ff: bool,
pub all: bool,
pub tags: bool,
pub autostash: bool,
pub quiet: bool,
}
impl PullCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn remote(&mut self, r: impl Into<String>) -> &mut Self {
self.remote = Some(r.into());
self
}
pub fn refspec(&mut self, r: impl Into<String>) -> &mut Self {
self.refspec = Some(r.into());
self
}
pub fn rebase(&mut self) -> &mut Self {
self.rebase = Some(String::new());
self
}
pub fn rebase_mode(&mut self, m: impl Into<String>) -> &mut Self {
self.rebase = Some(m.into());
self
}
pub fn no_rebase(&mut self) -> &mut Self {
self.no_rebase = true;
self
}
pub fn ff_only(&mut self) -> &mut Self {
self.ff_only = true;
self
}
pub fn no_ff(&mut self) -> &mut Self {
self.no_ff = true;
self
}
pub fn all(&mut self) -> &mut Self {
self.all = true;
self
}
pub fn tags(&mut self) -> &mut Self {
self.tags = true;
self
}
pub fn autostash(&mut self) -> &mut Self {
self.autostash = true;
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for PullCommand {
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!["pull".to_string()];
if let Some(r) = &self.rebase {
if r.is_empty() {
args.push("--rebase".into());
} else {
args.push(format!("--rebase={r}"));
}
}
if self.no_rebase {
args.push("--no-rebase".into());
}
if self.ff_only {
args.push("--ff-only".into());
}
if self.no_ff {
args.push("--no-ff".into());
}
if self.all {
args.push("--all".into());
}
if self.tags {
args.push("--tags".into());
}
if self.autostash {
args.push("--autostash".into());
}
if self.quiet {
args.push("--quiet".into());
}
if let Some(r) = &self.remote {
args.push(r.clone());
}
if let Some(r) = &self.refspec {
args.push(r.clone());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}