git_spawn/command/
checkout.rs1use crate::command::{CommandExecutor, CommandOutput, GitCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone, Default)]
9pub struct CheckoutCommand {
10 pub executor: CommandExecutor,
12 pub target: Option<String>,
14 pub create: Option<String>,
16 pub create_or_reset: Option<String>,
18 pub force: bool,
20 pub track: bool,
22 pub no_track: bool,
24 pub orphan: Option<String>,
26 pub detach: bool,
28 pub paths: Vec<String>,
30 pub quiet: bool,
32}
33
34impl CheckoutCommand {
35 #[must_use]
37 pub fn new() -> Self {
38 Self::default()
39 }
40
41 pub fn target(&mut self, t: impl Into<String>) -> &mut Self {
43 self.target = Some(t.into());
44 self
45 }
46
47 pub fn create(&mut self, name: impl Into<String>) -> &mut Self {
49 self.create = Some(name.into());
50 self
51 }
52
53 pub fn create_or_reset(&mut self, name: impl Into<String>) -> &mut Self {
55 self.create_or_reset = Some(name.into());
56 self
57 }
58
59 pub fn force(&mut self) -> &mut Self {
61 self.force = true;
62 self
63 }
64
65 pub fn track(&mut self) -> &mut Self {
67 self.track = true;
68 self
69 }
70
71 pub fn no_track(&mut self) -> &mut Self {
73 self.no_track = true;
74 self
75 }
76
77 pub fn orphan(&mut self, name: impl Into<String>) -> &mut Self {
79 self.orphan = Some(name.into());
80 self
81 }
82
83 pub fn detach(&mut self) -> &mut Self {
85 self.detach = true;
86 self
87 }
88
89 pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
91 self.paths.push(p.into());
92 self
93 }
94
95 pub fn quiet(&mut self) -> &mut Self {
97 self.quiet = true;
98 self
99 }
100}
101
102#[async_trait]
103impl GitCommand for CheckoutCommand {
104 type Output = CommandOutput;
105 fn get_executor(&self) -> &CommandExecutor {
106 &self.executor
107 }
108 fn get_executor_mut(&mut self) -> &mut CommandExecutor {
109 &mut self.executor
110 }
111 fn build_command_args(&self) -> Vec<String> {
112 let mut args = vec!["checkout".to_string()];
113 if self.force {
114 args.push("--force".into());
115 }
116 if self.track {
117 args.push("--track".into());
118 }
119 if self.no_track {
120 args.push("--no-track".into());
121 }
122 if self.detach {
123 args.push("--detach".into());
124 }
125 if self.quiet {
126 args.push("--quiet".into());
127 }
128 if let Some(o) = &self.orphan {
129 args.push("--orphan".into());
130 args.push(o.clone());
131 }
132 if let Some(b) = &self.create {
133 args.push("-b".into());
134 args.push(b.clone());
135 }
136 if let Some(b) = &self.create_or_reset {
137 args.push("-B".into());
138 args.push(b.clone());
139 }
140 if let Some(t) = &self.target {
141 args.push(t.clone());
142 }
143 if !self.paths.is_empty() {
144 args.push("--".into());
145 args.extend(self.paths.iter().cloned());
146 }
147 args
148 }
149 async fn execute(&self) -> Result<CommandOutput> {
150 self.execute_raw().await
151 }
152}