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    remote: Option<&'a Remote>,
20}
21
22impl<'a> Fetch<'a> {
23    #[must_use]
24    fn new() -> Self {
25        Self {
26            repo_path: None,
27            all: false,
28            remote: None,
29        }
30    }
31
32    /// Set the repository path (`-C <path>`).
33    #[must_use]
34    pub fn repo_path(mut self, path: &'a Path) -> Self {
35        self.repo_path = Some(path);
36        self
37    }
38
39    crate::flag_methods! {
40        /// Fetch all remotes.
41        ///
42        /// Corresponds to `--all`.
43        pub fn all / all_if, all, "Conditionally fetch all remotes."
44    }
45
46    /// Set the remote to fetch from.
47    #[must_use]
48    pub fn remote(mut self, remote: &'a Remote) -> Self {
49        self.remote = Some(remote);
50        self
51    }
52
53    /// Execute the command and return the exit status.
54    pub fn status(self) -> Result<(), CommandError> {
55        self.build().status()
56    }
57
58    fn build(self) -> cmd_proc::Command {
59        crate::base_command(self.repo_path)
60            .argument("fetch")
61            .optional_argument(self.all.then_some("--all"))
62            .optional_argument(self.remote)
63    }
64}
65
66impl Default for Fetch<'_> {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72#[cfg(feature = "test-utils")]
73impl Fetch<'_> {
74    /// Compare the built command with another command using debug representation.
75    pub fn test_eq(&self, other: &cmd_proc::Command) {
76        let command = Self {
77            repo_path: self.repo_path,
78            all: self.all,
79            remote: self.remote,
80        }
81        .build();
82        command.test_eq(other);
83    }
84}