use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct RebaseCommand {
pub executor: CommandExecutor,
pub upstream: Option<String>,
pub branch: Option<String>,
pub onto: Option<String>,
pub interactive: bool,
pub autosquash: bool,
pub autostash: bool,
pub abort: bool,
pub cont: bool,
pub skip: bool,
pub quit: bool,
pub root: bool,
pub strategy: Option<String>,
}
impl RebaseCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn upstream(&mut self, u: impl Into<String>) -> &mut Self {
self.upstream = Some(u.into());
self
}
pub fn branch(&mut self, b: impl Into<String>) -> &mut Self {
self.branch = Some(b.into());
self
}
pub fn onto(&mut self, o: impl Into<String>) -> &mut Self {
self.onto = Some(o.into());
self
}
pub fn interactive(&mut self) -> &mut Self {
self.interactive = true;
self
}
pub fn autosquash(&mut self) -> &mut Self {
self.autosquash = true;
self
}
pub fn autostash(&mut self) -> &mut Self {
self.autostash = true;
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 root(&mut self) -> &mut Self {
self.root = true;
self
}
pub fn strategy(&mut self, s: impl Into<String>) -> &mut Self {
self.strategy = Some(s.into());
self
}
}
#[async_trait]
impl GitCommand for RebaseCommand {
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!["rebase".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.interactive {
args.push("--interactive".into());
}
if self.autosquash {
args.push("--autosquash".into());
}
if self.autostash {
args.push("--autostash".into());
}
if self.root {
args.push("--root".into());
}
if let Some(o) = &self.onto {
args.push("--onto".into());
args.push(o.clone());
}
if let Some(s) = &self.strategy {
args.push(format!("--strategy={s}"));
}
if let Some(u) = &self.upstream {
args.push(u.clone());
}
if let Some(b) = &self.branch {
args.push(b.clone());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}