Skip to main content

Mountain/IPC/WindServiceHandlers/Git/
HandleCheckout.rs

1#![allow(non_snake_case)]
2
3//! `localGit:checkout(operationId, repoPath, treeish, detached?)`.
4//! `Detached=true` adds `--detach` so the caller can land on a
5//! commit hash without creating a tracking branch.
6
7use serde_json::Value;
8
9use crate::IPC::WindServiceHandlers::Git::Shared::RunGit;
10
11pub async fn HandleCheckout(Arguments:Vec<Value>) -> Result<Value, String> {
12	let OperationId = Arguments.first().and_then(Value::as_str).unwrap_or("").to_string();
13
14	let RepoPath = Arguments.get(1).and_then(Value::as_str).unwrap_or("").to_string();
15
16	let Treeish = Arguments.get(2).and_then(Value::as_str).unwrap_or("").to_string();
17
18	let Detached = Arguments.get(3).and_then(Value::as_bool).unwrap_or(false);
19
20	if RepoPath.is_empty() || Treeish.is_empty() {
21		return Err("git:checkout requires repoPath and treeish".to_string());
22	}
23
24	let Argv:Vec<String> = if Detached {
25		vec!["checkout".to_string(), "--detach".to_string(), Treeish]
26	} else {
27		vec!["checkout".to_string(), Treeish]
28	};
29
30	let (ExitCode, _, Stderr) = RunGit(&OperationId, &Argv, Some(&RepoPath)).await?;
31
32	if ExitCode != 0 {
33		return Err(format!("git checkout failed: {}", Stderr));
34	}
35
36	Ok(Value::Null)
37}