Skip to main content

git_spawn/command/
pull.rs

1//! `git pull` — fetch from and integrate with another repository or a local branch.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git pull`.
8#[derive(Debug, Clone, Default)]
9pub struct PullCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Remote name.
13    pub remote: Option<String>,
14    /// Refspec.
15    pub refspec: Option<String>,
16    /// `--rebase` (optionally set to a merge strategy name like `true`/`merges`/`interactive`).
17    pub rebase: Option<String>,
18    /// `--no-rebase`.
19    pub no_rebase: bool,
20    /// `--ff-only`.
21    pub ff_only: bool,
22    /// `--no-ff`.
23    pub no_ff: bool,
24    /// `--all`.
25    pub all: bool,
26    /// `--tags`.
27    pub tags: bool,
28    /// `--autostash`.
29    pub autostash: bool,
30    /// `--quiet`.
31    pub quiet: bool,
32}
33
34impl PullCommand {
35    /// New `pull`.
36    #[must_use]
37    pub fn new() -> Self {
38        Self::default()
39    }
40    /// Remote.
41    pub fn remote(&mut self, r: impl Into<String>) -> &mut Self {
42        self.remote = Some(r.into());
43        self
44    }
45    /// Refspec.
46    pub fn refspec(&mut self, r: impl Into<String>) -> &mut Self {
47        self.refspec = Some(r.into());
48        self
49    }
50    /// Enable `--rebase`.
51    pub fn rebase(&mut self) -> &mut Self {
52        self.rebase = Some(String::new());
53        self
54    }
55    /// `--rebase=<mode>`.
56    pub fn rebase_mode(&mut self, m: impl Into<String>) -> &mut Self {
57        self.rebase = Some(m.into());
58        self
59    }
60    /// `--no-rebase`.
61    pub fn no_rebase(&mut self) -> &mut Self {
62        self.no_rebase = true;
63        self
64    }
65    /// `--ff-only`.
66    pub fn ff_only(&mut self) -> &mut Self {
67        self.ff_only = true;
68        self
69    }
70    /// `--no-ff`.
71    pub fn no_ff(&mut self) -> &mut Self {
72        self.no_ff = true;
73        self
74    }
75    /// `--all`.
76    pub fn all(&mut self) -> &mut Self {
77        self.all = true;
78        self
79    }
80    /// `--tags`.
81    pub fn tags(&mut self) -> &mut Self {
82        self.tags = true;
83        self
84    }
85    /// `--autostash`.
86    pub fn autostash(&mut self) -> &mut Self {
87        self.autostash = true;
88        self
89    }
90    /// `--quiet`.
91    pub fn quiet(&mut self) -> &mut Self {
92        self.quiet = true;
93        self
94    }
95}
96
97#[async_trait]
98impl GitCommand for PullCommand {
99    type Output = CommandOutput;
100    fn get_executor(&self) -> &CommandExecutor {
101        &self.executor
102    }
103    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
104        &mut self.executor
105    }
106    fn build_command_args(&self) -> Vec<String> {
107        let mut args = vec!["pull".to_string()];
108        if let Some(r) = &self.rebase {
109            if r.is_empty() {
110                args.push("--rebase".into());
111            } else {
112                args.push(format!("--rebase={r}"));
113            }
114        }
115        if self.no_rebase {
116            args.push("--no-rebase".into());
117        }
118        if self.ff_only {
119            args.push("--ff-only".into());
120        }
121        if self.no_ff {
122            args.push("--no-ff".into());
123        }
124        if self.all {
125            args.push("--all".into());
126        }
127        if self.tags {
128            args.push("--tags".into());
129        }
130        if self.autostash {
131            args.push("--autostash".into());
132        }
133        if self.quiet {
134            args.push("--quiet".into());
135        }
136        if let Some(r) = &self.remote {
137            args.push(r.clone());
138        }
139        if let Some(r) = &self.refspec {
140            args.push(r.clone());
141        }
142        args
143    }
144    async fn execute(&self) -> Result<CommandOutput> {
145        self.execute_raw().await
146    }
147}