Skip to main content

git_proc/
push.rs

1use crate::repository::Remote;
2
3/// Create a new `git push` command builder.
4#[must_use]
5pub fn new() -> Push<'static> {
6    Push::new()
7}
8
9/// Builder for `git push` command.
10///
11/// See `git push --help` for full documentation.
12#[derive(Debug)]
13pub struct Push<'a> {
14    base: crate::RepoBase<'a>,
15    force: bool,
16    porcelain: bool,
17    remote: Option<&'a Remote>,
18    refspec: Option<&'a str>,
19}
20
21crate::impl_repo_base!(Push);
22crate::impl_porcelain!(Push);
23
24impl<'a> Push<'a> {
25    #[must_use]
26    fn new() -> Self {
27        Self {
28            base: crate::RepoBase::default(),
29            force: false,
30            porcelain: false,
31            remote: None,
32            refspec: None,
33        }
34    }
35
36    crate::flag_methods! {
37        /// Force push (overwrite remote refs).
38        ///
39        /// Corresponds to `--force`.
40        pub fn force / force_if, force, "Conditionally force push."
41    }
42
43    /// Set the remote to push to.
44    #[must_use]
45    pub fn remote(mut self, remote: &'a Remote) -> Self {
46        self.remote = Some(remote);
47        self
48    }
49
50    /// Set the refspec to push.
51    #[must_use]
52    pub fn refspec(mut self, refspec: &'a str) -> Self {
53        self.refspec = Some(refspec);
54        self
55    }
56
57    /// Execute the command and return the exit status.
58    pub async fn status(self) -> Result<(), crate::Error> {
59        Ok(crate::Build::build(self)?.status().await?)
60    }
61}
62
63impl Default for Push<'_> {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69impl crate::Build for Push<'_> {
70    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
71        Ok(self
72            .base
73            .command()?
74            .argument("push")
75            .optional_flag(self.force, "--force")
76            .optional_flag(self.porcelain, "--porcelain")
77            .optional_argument(self.remote)
78            .optional_argument(self.refspec))
79    }
80}
81
82#[cfg(feature = "test-utils")]
83impl Push<'_> {
84    /// Compare the built command with another command using debug representation.
85    pub fn test_eq(&self, other: &cmd_proc::Command) {
86        let command = crate::Build::build(Self {
87            base: self.base,
88            force: self.force,
89            porcelain: self.porcelain,
90            remote: self.remote,
91            refspec: self.refspec,
92        })
93        .unwrap();
94        command.test_eq(other);
95    }
96}