Skip to main content

git_proc/
fetch.rs

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