use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct BranchCommand {
pub executor: CommandExecutor,
pub list: bool,
pub all: bool,
pub remotes: bool,
pub verbose: bool,
pub name: Option<String>,
pub start_point: Option<String>,
pub delete: Option<String>,
pub force_delete: bool,
pub rename_from: Option<String>,
pub rename_to: Option<String>,
pub track: bool,
pub no_track: bool,
pub set_upstream_to: Option<String>,
pub unset_upstream: bool,
pub show_current: bool,
pub contains: Option<String>,
pub merged: Option<String>,
}
impl BranchCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn list(&mut self) -> &mut Self {
self.list = true;
self
}
pub fn all(&mut self) -> &mut Self {
self.all = true;
self
}
pub fn remotes(&mut self) -> &mut Self {
self.remotes = true;
self
}
pub fn verbose(&mut self) -> &mut Self {
self.verbose = true;
self
}
pub fn create(&mut self, name: impl Into<String>) -> &mut Self {
self.name = Some(name.into());
self
}
pub fn start_point(&mut self, sp: impl Into<String>) -> &mut Self {
self.start_point = Some(sp.into());
self
}
pub fn delete(&mut self, name: impl Into<String>) -> &mut Self {
self.delete = Some(name.into());
self
}
pub fn force_delete(&mut self) -> &mut Self {
self.force_delete = true;
self
}
pub fn rename(&mut self, from: impl Into<String>, to: impl Into<String>) -> &mut Self {
self.rename_from = Some(from.into());
self.rename_to = Some(to.into());
self
}
pub fn set_upstream_to(&mut self, s: impl Into<String>) -> &mut Self {
self.set_upstream_to = Some(s.into());
self
}
pub fn unset_upstream(&mut self) -> &mut Self {
self.unset_upstream = true;
self
}
pub fn track(&mut self) -> &mut Self {
self.track = true;
self
}
pub fn no_track(&mut self) -> &mut Self {
self.no_track = true;
self
}
pub fn show_current(&mut self) -> &mut Self {
self.show_current = true;
self
}
pub fn contains(&mut self, c: impl Into<String>) -> &mut Self {
self.contains = Some(c.into());
self
}
pub fn merged(&mut self, c: impl Into<String>) -> &mut Self {
self.merged = Some(c.into());
self
}
}
#[async_trait]
impl GitCommand for BranchCommand {
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!["branch".to_string()];
if self.list {
args.push("--list".into());
}
if self.all {
args.push("--all".into());
}
if self.remotes {
args.push("--remotes".into());
}
if self.verbose {
args.push("--verbose".into());
}
if self.force_delete {
args.push("-D".into());
}
if self.track {
args.push("--track".into());
}
if self.no_track {
args.push("--no-track".into());
}
if self.unset_upstream {
args.push("--unset-upstream".into());
}
if self.show_current {
args.push("--show-current".into());
}
if let Some(u) = &self.set_upstream_to {
args.push(format!("--set-upstream-to={u}"));
}
if let Some(c) = &self.contains {
args.push(format!("--contains={c}"));
}
if let Some(m) = &self.merged {
args.push(format!("--merged={m}"));
}
if let Some(d) = &self.delete {
args.push("-d".into());
args.push(d.clone());
} else if let (Some(from), Some(to)) = (&self.rename_from, &self.rename_to) {
args.push("-m".into());
args.push(from.clone());
args.push(to.clone());
} else if let Some(name) = &self.name {
args.push(name.clone());
if let Some(sp) = &self.start_point {
args.push(sp.clone());
}
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}