use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct MergeCommand {
pub executor: CommandExecutor,
pub commits: Vec<String>,
pub no_ff: bool,
pub ff_only: bool,
pub squash: bool,
pub commit: bool,
pub no_commit: bool,
pub message: Option<String>,
pub strategy: Option<String>,
pub abort: bool,
pub cont: bool,
pub quiet: bool,
}
impl MergeCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn commit_ref(&mut self, c: impl Into<String>) -> &mut Self {
self.commits.push(c.into());
self
}
pub fn no_ff(&mut self) -> &mut Self {
self.no_ff = true;
self
}
pub fn ff_only(&mut self) -> &mut Self {
self.ff_only = true;
self
}
pub fn squash(&mut self) -> &mut Self {
self.squash = true;
self
}
pub fn commit(&mut self) -> &mut Self {
self.commit = true;
self
}
pub fn no_commit(&mut self) -> &mut Self {
self.no_commit = true;
self
}
pub fn message(&mut self, m: impl Into<String>) -> &mut Self {
self.message = Some(m.into());
self
}
pub fn strategy(&mut self, s: impl Into<String>) -> &mut Self {
self.strategy = Some(s.into());
self
}
pub fn abort(&mut self) -> &mut Self {
self.abort = true;
self
}
pub fn cont(&mut self) -> &mut Self {
self.cont = true;
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for MergeCommand {
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!["merge".to_string()];
if self.abort {
args.push("--abort".into());
return args;
}
if self.cont {
args.push("--continue".into());
return args;
}
if self.no_ff {
args.push("--no-ff".into());
}
if self.ff_only {
args.push("--ff-only".into());
}
if self.squash {
args.push("--squash".into());
}
if self.commit {
args.push("--commit".into());
}
if self.no_commit {
args.push("--no-commit".into());
}
if self.quiet {
args.push("--quiet".into());
}
if let Some(m) = &self.message {
args.push("-m".into());
args.push(m.clone());
}
if let Some(s) = &self.strategy {
args.push(format!("--strategy={s}"));
}
args.extend(self.commits.iter().cloned());
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}