Skip to main content

git_proc/
push.rs

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