Skip to main content

git_spawn/command/
push.rs

1//! `git push` — update remote refs along with associated objects.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git push`.
8#[derive(Debug, Clone, Default)]
9pub struct PushCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Remote name.
13    pub remote: Option<String>,
14    /// Refspecs.
15    pub refspecs: Vec<String>,
16    /// `--all`.
17    pub all: bool,
18    /// `--tags`.
19    pub tags: bool,
20    /// `--follow-tags`.
21    pub follow_tags: bool,
22    /// `--force` / `-f`.
23    pub force: bool,
24    /// `--force-with-lease`.
25    pub force_with_lease: bool,
26    /// `--delete`.
27    pub delete: bool,
28    /// `--set-upstream` / `-u`.
29    pub set_upstream: bool,
30    /// `--dry-run` / `-n`.
31    pub dry_run: bool,
32    /// `--atomic`.
33    pub atomic: bool,
34    /// `--quiet`.
35    pub quiet: bool,
36}
37
38impl PushCommand {
39    /// New `push`.
40    #[must_use]
41    pub fn new() -> Self {
42        Self::default()
43    }
44    /// Remote.
45    pub fn remote(&mut self, r: impl Into<String>) -> &mut Self {
46        self.remote = Some(r.into());
47        self
48    }
49    /// Add a refspec.
50    pub fn refspec(&mut self, r: impl Into<String>) -> &mut Self {
51        self.refspecs.push(r.into());
52        self
53    }
54    /// `--all`.
55    pub fn all(&mut self) -> &mut Self {
56        self.all = true;
57        self
58    }
59    /// `--tags`.
60    pub fn tags(&mut self) -> &mut Self {
61        self.tags = true;
62        self
63    }
64    /// `--follow-tags`.
65    pub fn follow_tags(&mut self) -> &mut Self {
66        self.follow_tags = true;
67        self
68    }
69    /// `--force`.
70    pub fn force(&mut self) -> &mut Self {
71        self.force = true;
72        self
73    }
74    /// `--force-with-lease`.
75    pub fn force_with_lease(&mut self) -> &mut Self {
76        self.force_with_lease = true;
77        self
78    }
79    /// `--delete`.
80    pub fn delete(&mut self) -> &mut Self {
81        self.delete = true;
82        self
83    }
84    /// `-u` / `--set-upstream`.
85    pub fn set_upstream(&mut self) -> &mut Self {
86        self.set_upstream = true;
87        self
88    }
89    /// `--dry-run`.
90    pub fn dry_run(&mut self) -> &mut Self {
91        self.dry_run = true;
92        self
93    }
94    /// `--atomic`.
95    pub fn atomic(&mut self) -> &mut Self {
96        self.atomic = true;
97        self
98    }
99    /// `--quiet`.
100    pub fn quiet(&mut self) -> &mut Self {
101        self.quiet = true;
102        self
103    }
104}
105
106#[async_trait]
107impl GitCommand for PushCommand {
108    type Output = CommandOutput;
109    fn get_executor(&self) -> &CommandExecutor {
110        &self.executor
111    }
112    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
113        &mut self.executor
114    }
115    fn build_command_args(&self) -> Vec<String> {
116        let mut args = vec!["push".to_string()];
117        if self.all {
118            args.push("--all".into());
119        }
120        if self.tags {
121            args.push("--tags".into());
122        }
123        if self.follow_tags {
124            args.push("--follow-tags".into());
125        }
126        if self.force {
127            args.push("--force".into());
128        }
129        if self.force_with_lease {
130            args.push("--force-with-lease".into());
131        }
132        if self.delete {
133            args.push("--delete".into());
134        }
135        if self.set_upstream {
136            args.push("--set-upstream".into());
137        }
138        if self.dry_run {
139            args.push("--dry-run".into());
140        }
141        if self.atomic {
142            args.push("--atomic".into());
143        }
144        if self.quiet {
145            args.push("--quiet".into());
146        }
147        if let Some(r) = &self.remote {
148            args.push(r.clone());
149        }
150        args.extend(self.refspecs.iter().cloned());
151        args
152    }
153    async fn execute(&self) -> Result<CommandOutput> {
154        self.execute_raw().await
155    }
156}