Skip to main content

git_spawn/command/
restore.rs

1//! `git restore` — restore working tree files.
2
3use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Builder for `git restore`.
8#[derive(Debug, Clone, Default)]
9pub struct RestoreCommand {
10    /// Shared executor.
11    pub executor: CommandExecutor,
12    /// Pathspecs.
13    pub paths: Vec<String>,
14    /// `--source`.
15    pub source: Option<String>,
16    /// `--staged`.
17    pub staged: bool,
18    /// `--worktree`.
19    pub worktree: bool,
20    /// `--quiet`.
21    pub quiet: bool,
22    /// `--ours`.
23    pub ours: bool,
24    /// `--theirs`.
25    pub theirs: bool,
26}
27
28impl RestoreCommand {
29    /// New `restore`.
30    #[must_use]
31    pub fn new() -> Self {
32        Self::default()
33    }
34    /// Add a path.
35    pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
36        self.paths.push(p.into());
37        self
38    }
39    /// Source tree-ish.
40    pub fn source(&mut self, s: impl Into<String>) -> &mut Self {
41        self.source = Some(s.into());
42        self
43    }
44    /// Restore the staged copy.
45    pub fn staged(&mut self) -> &mut Self {
46        self.staged = true;
47        self
48    }
49    /// Restore the working tree copy.
50    pub fn worktree(&mut self) -> &mut Self {
51        self.worktree = true;
52        self
53    }
54    /// `--quiet`.
55    pub fn quiet(&mut self) -> &mut Self {
56        self.quiet = true;
57        self
58    }
59    /// `--ours`.
60    pub fn ours(&mut self) -> &mut Self {
61        self.ours = true;
62        self
63    }
64    /// `--theirs`.
65    pub fn theirs(&mut self) -> &mut Self {
66        self.theirs = true;
67        self
68    }
69}
70
71#[async_trait]
72impl GitCommand for RestoreCommand {
73    type Output = CommandOutput;
74    fn get_executor(&self) -> &CommandExecutor {
75        &self.executor
76    }
77    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
78        &mut self.executor
79    }
80    fn build_command_args(&self) -> Vec<String> {
81        let mut args = vec!["restore".to_string()];
82        if let Some(s) = &self.source {
83            args.push(format!("--source={s}"));
84        }
85        if self.staged {
86            args.push("--staged".into());
87        }
88        if self.worktree {
89            args.push("--worktree".into());
90        }
91        if self.quiet {
92            args.push("--quiet".into());
93        }
94        if self.ours {
95            args.push("--ours".into());
96        }
97        if self.theirs {
98            args.push("--theirs".into());
99        }
100        if !self.paths.is_empty() {
101            args.push("--".into());
102            args.extend(self.paths.iter().cloned());
103        }
104        args
105    }
106    async fn execute(&self) -> Result<CommandOutput> {
107        self.execute_raw().await
108    }
109}