1use crate::{Component, Error, query};
4use std::{ffi::OsString, path::Path};
5
6pub fn source_changes(
11 binary: &Path,
12 root: &Path,
13 empty: bool,
14 files: usize,
15) -> Result<usize, Error> {
16 let mut options = vec![OsString::from("--files"), OsString::from(files.to_string())];
17 if empty {
18 options.push(OsString::from("--empty-scaffold"));
19 }
20 query(Component::Vcs, binary, "source_changes", root, &options)?
21 .get("local_changes")
22 .and_then(serde_json::Value::as_u64)
23 .and_then(|count| usize::try_from(count).ok())
24 .ok_or(Error::InvalidResponse)
25}
26
27pub fn exists(binary: &Path, root: &Path) -> Result<bool, Error> {
32 query(Component::Vcs, binary, "repository_exists", root, &[])?
33 .get("exists")
34 .and_then(serde_json::Value::as_bool)
35 .ok_or(Error::InvalidResponse)
36}
37
38pub fn initialize_new(binary: &Path, root: &Path) -> Result<(), Error> {
43 initialized(binary, root, "initialize")
44}
45
46pub fn initialize_reserved(binary: &Path, git: &Path) -> Result<(), Error> {
51 initialized(binary, git, "initialize_reserved")
52}
53
54fn initialized(binary: &Path, root: &Path, operation: &str) -> Result<(), Error> {
56 let data = query(Component::Vcs, binary, operation, root, &[])?;
57 if data.get("initialized").and_then(serde_json::Value::as_bool) != Some(true) {
58 return Err(Error::InvalidResponse);
59 }
60 Ok(())
61}