use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct PushCommand {
pub executor: CommandExecutor,
pub remote: Option<String>,
pub refspecs: Vec<String>,
pub all: bool,
pub tags: bool,
pub follow_tags: bool,
pub force: bool,
pub force_with_lease: bool,
pub delete: bool,
pub set_upstream: bool,
pub dry_run: bool,
pub atomic: bool,
pub quiet: bool,
}
impl PushCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn remote(&mut self, r: impl Into<String>) -> &mut Self {
self.remote = Some(r.into());
self
}
pub fn refspec(&mut self, r: impl Into<String>) -> &mut Self {
self.refspecs.push(r.into());
self
}
pub fn all(&mut self) -> &mut Self {
self.all = true;
self
}
pub fn tags(&mut self) -> &mut Self {
self.tags = true;
self
}
pub fn follow_tags(&mut self) -> &mut Self {
self.follow_tags = true;
self
}
pub fn force(&mut self) -> &mut Self {
self.force = true;
self
}
pub fn force_with_lease(&mut self) -> &mut Self {
self.force_with_lease = true;
self
}
pub fn delete(&mut self) -> &mut Self {
self.delete = true;
self
}
pub fn set_upstream(&mut self) -> &mut Self {
self.set_upstream = true;
self
}
pub fn dry_run(&mut self) -> &mut Self {
self.dry_run = true;
self
}
pub fn atomic(&mut self) -> &mut Self {
self.atomic = true;
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for PushCommand {
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!["push".to_string()];
if self.all {
args.push("--all".into());
}
if self.tags {
args.push("--tags".into());
}
if self.follow_tags {
args.push("--follow-tags".into());
}
if self.force {
args.push("--force".into());
}
if self.force_with_lease {
args.push("--force-with-lease".into());
}
if self.delete {
args.push("--delete".into());
}
if self.set_upstream {
args.push("--set-upstream".into());
}
if self.dry_run {
args.push("--dry-run".into());
}
if self.atomic {
args.push("--atomic".into());
}
if self.quiet {
args.push("--quiet".into());
}
if let Some(r) = &self.remote {
args.push(r.clone());
}
args.extend(self.refspecs.iter().cloned());
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}