Skip to main content

git_spawn/command/
fetch.rs

1//! `git fetch` — download objects and refs from another repository.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git fetch`.
8#[derive(Debug, Clone, Default)]
9pub struct FetchCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Remote name.
13    pub remote: Option<String>,
14    /// Refspec.
15    pub refspec: Option<String>,
16    /// `--all`.
17    pub all: bool,
18    /// `--tags`.
19    pub tags: bool,
20    /// `--no-tags`.
21    pub no_tags: bool,
22    /// `--prune`.
23    pub prune: bool,
24    /// `--depth`.
25    pub depth: Option<u32>,
26    /// `--unshallow`.
27    pub unshallow: bool,
28    /// `--quiet`.
29    pub quiet: bool,
30}
31
32impl FetchCommand {
33    /// New `fetch`.
34    #[must_use]
35    pub fn new() -> Self {
36        Self::default()
37    }
38    /// Remote.
39    pub fn remote(&mut self, r: impl Into<String>) -> &mut Self {
40        self.remote = Some(r.into());
41        self
42    }
43    /// Refspec.
44    pub fn refspec(&mut self, r: impl Into<String>) -> &mut Self {
45        self.refspec = Some(r.into());
46        self
47    }
48    /// `--all`.
49    pub fn all(&mut self) -> &mut Self {
50        self.all = true;
51        self
52    }
53    /// `--tags`.
54    pub fn tags(&mut self) -> &mut Self {
55        self.tags = true;
56        self
57    }
58    /// `--no-tags`.
59    pub fn no_tags(&mut self) -> &mut Self {
60        self.no_tags = true;
61        self
62    }
63    /// `--prune`.
64    pub fn prune(&mut self) -> &mut Self {
65        self.prune = true;
66        self
67    }
68    /// `--depth`.
69    pub fn depth(&mut self, d: u32) -> &mut Self {
70        self.depth = Some(d);
71        self
72    }
73    /// `--unshallow`.
74    pub fn unshallow(&mut self) -> &mut Self {
75        self.unshallow = true;
76        self
77    }
78    /// `--quiet`.
79    pub fn quiet(&mut self) -> &mut Self {
80        self.quiet = true;
81        self
82    }
83}
84
85#[async_trait]
86impl GitCommand for FetchCommand {
87    type Output = CommandOutput;
88    fn get_executor(&self) -> &CommandExecutor {
89        &self.executor
90    }
91    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
92        &mut self.executor
93    }
94    fn build_command_args(&self) -> Vec<String> {
95        let mut args = vec!["fetch".to_string()];
96        if self.all {
97            args.push("--all".into());
98        }
99        if self.tags {
100            args.push("--tags".into());
101        }
102        if self.no_tags {
103            args.push("--no-tags".into());
104        }
105        if self.prune {
106            args.push("--prune".into());
107        }
108        if let Some(d) = self.depth {
109            args.push(format!("--depth={d}"));
110        }
111        if self.unshallow {
112            args.push("--unshallow".into());
113        }
114        if self.quiet {
115            args.push("--quiet".into());
116        }
117        if let Some(r) = &self.remote {
118            args.push(r.clone());
119        }
120        if let Some(r) = &self.refspec {
121            args.push(r.clone());
122        }
123        args
124    }
125    async fn execute(&self) -> Result<CommandOutput> {
126        self.execute_raw().await
127    }
128}