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	let RepoPath = Arguments.get(1).and_then(Value::as_str).unwrap_or("").to_string();
14	let Treeish = Arguments.get(2).and_then(Value::as_str).unwrap_or("").to_string();
15	let Detached = Arguments.get(3).and_then(Value::as_bool).unwrap_or(false);
16
17	if RepoPath.is_empty() || Treeish.is_empty() {
18		return Err("git:checkout requires repoPath and treeish".to_string());
19	}
20
21	let Argv:Vec<String> = if Detached {
22		vec!["checkout".to_string(), "--detach".to_string(), Treeish]
23	} else {
24		vec!["checkout".to_string(), Treeish]
25	};
26
27	let (ExitCode, _, Stderr) = RunGit(&OperationId, &Argv, Some(&RepoPath)).await?;
28	if ExitCode != 0 {
29		return Err(format!("git checkout failed: {}", Stderr));
30	}
31	Ok(Value::Null)
32}