1use std::path::Path;
2
3use crate::CommandError;
4use crate::url::Remote;
5
6#[must_use]
8pub fn new() -> Fetch<'static> {
9 Fetch::new()
10}
11
12#[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 #[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 pub fn all / all_if, all, "Conditionally fetch all remotes."
44 }
45
46 #[must_use]
48 pub fn remote(mut self, remote: &'a Remote) -> Self {
49 self.remote = Some(remote);
50 self
51 }
52
53 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 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}