git_spawn/command/
restore.rs1use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone, Default)]
9pub struct RestoreCommand {
10 pub executor: CommandExecutor,
12 pub paths: Vec<String>,
14 pub source: Option<String>,
16 pub staged: bool,
18 pub worktree: bool,
20 pub quiet: bool,
22 pub ours: bool,
24 pub theirs: bool,
26}
27
28impl RestoreCommand {
29 #[must_use]
31 pub fn new() -> Self {
32 Self::default()
33 }
34 pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
36 self.paths.push(p.into());
37 self
38 }
39 pub fn source(&mut self, s: impl Into<String>) -> &mut Self {
41 self.source = Some(s.into());
42 self
43 }
44 pub fn staged(&mut self) -> &mut Self {
46 self.staged = true;
47 self
48 }
49 pub fn worktree(&mut self) -> &mut Self {
51 self.worktree = true;
52 self
53 }
54 pub fn quiet(&mut self) -> &mut Self {
56 self.quiet = true;
57 self
58 }
59 pub fn ours(&mut self) -> &mut Self {
61 self.ours = true;
62 self
63 }
64 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}