Skip to main content

Mountain/IPC/WindServiceHandlers/Git/
HandleExec.rs

1#![allow(non_snake_case)]
2
3//! `localGit:exec` - arbitrary `git` argv. Used by the Git
4//! extension for commands not on the curated `clone/pull/…`
5//! list. Accepts both the modern `{ Arguments, cwd?,
6//! operationId? }` shape and the legacy positional
7//! `(argv: string[], cwd?: string)`.
8
9use serde_json::{Value, json};
10
11use crate::IPC::WindServiceHandlers::Git::Shared::{AsStringArray, Generated, RunGit};
12
13pub async fn HandleExec(Arguments:Vec<Value>) -> Result<Value, String> {
14	let (Argv, Cwd, OperationId) = match Arguments.first() {
15		Some(First) if First.is_object() => {
16			let Obj = First.as_object().unwrap();
17
18			let Argv = Obj.get("Arguments").map(AsStringArray).unwrap_or_default();
19
20			let Cwd = Obj.get("cwd").and_then(Value::as_str).unwrap_or("").to_string();
21
22			let OperationId = Obj.get("operationId").and_then(Value::as_str).unwrap_or("").to_string();
23
24			(Argv, Cwd, OperationId)
25		},
26
27		Some(First) if First.is_array() => {
28			let Argv = AsStringArray(First);
29
30			let Cwd = Arguments.get(1).and_then(Value::as_str).unwrap_or("").to_string();
31
32			(Argv, Cwd, String::new())
33		},
34
35		_ => (Vec::new(), String::new(), String::new()),
36	};
37
38	if Argv.is_empty() {
39		return Err("git:exec requires non-empty Arguments".to_string());
40	}
41
42	let OperationIdRef = if OperationId.is_empty() { Generated() } else { OperationId };
43
44	let CwdOpt = if Cwd.is_empty() { None } else { Some(Cwd.as_str()) };
45
46	let (ExitCode, Stdout, Stderr) = RunGit(&OperationIdRef, &Argv, CwdOpt).await?;
47
48	Ok(json!({
49		"stdout": Stdout,
50		"stderr": Stderr,
51		"exitCode": ExitCode,
52	}))
53}