use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct CherryPickCommand {
pub executor: CommandExecutor,
pub commits: Vec<String>,
pub no_commit: bool,
pub edit: bool,
pub signoff: bool,
pub reference: bool,
pub mainline: Option<u32>,
pub strategy: Option<String>,
pub abort: bool,
pub cont: bool,
pub skip: bool,
pub quit: bool,
pub allow_empty: bool,
pub keep_redundant: bool,
}
impl CherryPickCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn commit(&mut self, c: impl Into<String>) -> &mut Self {
self.commits.push(c.into());
self
}
pub fn no_commit(&mut self) -> &mut Self {
self.no_commit = true;
self
}
pub fn edit(&mut self) -> &mut Self {
self.edit = true;
self
}
pub fn signoff(&mut self) -> &mut Self {
self.signoff = true;
self
}
pub fn reference(&mut self) -> &mut Self {
self.reference = true;
self
}
pub fn mainline(&mut self, n: u32) -> &mut Self {
self.mainline = Some(n);
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 skip(&mut self) -> &mut Self {
self.skip = true;
self
}
pub fn quit(&mut self) -> &mut Self {
self.quit = true;
self
}
pub fn allow_empty(&mut self) -> &mut Self {
self.allow_empty = true;
self
}
pub fn keep_redundant(&mut self) -> &mut Self {
self.keep_redundant = true;
self
}
}
#[async_trait]
impl GitCommand for CherryPickCommand {
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!["cherry-pick".to_string()];
if self.abort {
args.push("--abort".into());
return args;
}
if self.cont {
args.push("--continue".into());
return args;
}
if self.skip {
args.push("--skip".into());
return args;
}
if self.quit {
args.push("--quit".into());
return args;
}
if self.no_commit {
args.push("--no-commit".into());
}
if self.edit {
args.push("--edit".into());
}
if self.signoff {
args.push("--signoff".into());
}
if self.reference {
args.push("-x".into());
}
if self.allow_empty {
args.push("--allow-empty".into());
}
if self.keep_redundant {
args.push("--keep-redundant-commits".into());
}
if let Some(m) = self.mainline {
args.push("--mainline".into());
args.push(m.to_string());
}
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
}
}