use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct CheckoutCommand {
pub executor: CommandExecutor,
pub target: Option<String>,
pub create: Option<String>,
pub create_or_reset: Option<String>,
pub force: bool,
pub track: bool,
pub no_track: bool,
pub orphan: Option<String>,
pub detach: bool,
pub paths: Vec<String>,
pub quiet: bool,
}
impl CheckoutCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn target(&mut self, t: impl Into<String>) -> &mut Self {
self.target = Some(t.into());
self
}
pub fn create(&mut self, name: impl Into<String>) -> &mut Self {
self.create = Some(name.into());
self
}
pub fn create_or_reset(&mut self, name: impl Into<String>) -> &mut Self {
self.create_or_reset = Some(name.into());
self
}
pub fn force(&mut self) -> &mut Self {
self.force = 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 orphan(&mut self, name: impl Into<String>) -> &mut Self {
self.orphan = Some(name.into());
self
}
pub fn detach(&mut self) -> &mut Self {
self.detach = true;
self
}
pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
self.paths.push(p.into());
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for CheckoutCommand {
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!["checkout".to_string()];
if self.force {
args.push("--force".into());
}
if self.track {
args.push("--track".into());
}
if self.no_track {
args.push("--no-track".into());
}
if self.detach {
args.push("--detach".into());
}
if self.quiet {
args.push("--quiet".into());
}
if let Some(o) = &self.orphan {
args.push("--orphan".into());
args.push(o.clone());
}
if let Some(b) = &self.create {
args.push("-b".into());
args.push(b.clone());
}
if let Some(b) = &self.create_or_reset {
args.push("-B".into());
args.push(b.clone());
}
if let Some(t) = &self.target {
args.push(t.clone());
}
if !self.paths.is_empty() {
args.push("--".into());
args.extend(self.paths.iter().cloned());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}