summaryrefslogtreecommitdiff
path: root/crates/vcs_data/src/current.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-10-06 04:13:32 +0800
committerGitHub <noreply@github.com>2025-10-06 04:13:32 +0800
commit4a32781c096f30cb39e16c745076e6b7537929cd (patch)
tree304075598071f55cb9dc15dc7ac8d9b8740e511e /crates/vcs_data/src/current.rs
parent57959d26c68dc1d403f527f1f8b407abe8059a28 (diff)
parent85f7c35d6c573b715c166fe7501225ecab6731ea (diff)
Merge pull request #17 from JustEnoughVCS/jvcs_dev
Jvcs dev
Diffstat (limited to 'crates/vcs_data/src/current.rs')
-rw-r--r--crates/vcs_data/src/current.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/crates/vcs_data/src/current.rs b/crates/vcs_data/src/current.rs
new file mode 100644
index 0000000..97b5058
--- /dev/null
+++ b/crates/vcs_data/src/current.rs
@@ -0,0 +1,78 @@
+use crate::constants::*;
+use std::io::{self, Error};
+use std::{env::set_current_dir, path::PathBuf};
+
+/// Find the nearest vault or local workspace and correct the `current_dir` to it
+pub fn correct_current_dir() -> Result<(), io::Error> {
+ if let Some(local_workspace) = current_local_path() {
+ set_current_dir(local_workspace)?;
+ return Ok(());
+ }
+ if let Some(vault) = current_vault_path() {
+ set_current_dir(vault)?;
+ return Ok(());
+ }
+ Err(Error::new(
+ io::ErrorKind::NotFound,
+ "Could not find any vault or local workspace!",
+ ))
+}
+
+/// Get the nearest Vault directory from `current_dir`
+pub fn current_vault_path() -> Option<PathBuf> {
+ let current_dir = std::env::current_dir().ok()?;
+ find_vault_path(current_dir)
+}
+
+/// Get the nearest local workspace from `current_dir`
+pub fn current_local_path() -> Option<PathBuf> {
+ let current_dir = std::env::current_dir().ok()?;
+ find_local_path(current_dir)
+}
+
+/// Get the nearest Vault directory from the specified path
+pub fn find_vault_path(path: impl Into<PathBuf>) -> Option<PathBuf> {
+ let mut current_path = path.into();
+ let vault_file = SERVER_FILE_VAULT;
+
+ loop {
+ let vault_toml_path = current_path.join(vault_file);
+ if vault_toml_path.exists() {
+ return Some(current_path);
+ }
+
+ if let Some(parent) = current_path.parent() {
+ current_path = parent.to_path_buf();
+ } else {
+ break;
+ }
+ }
+
+ None
+}
+
+/// Get the nearest local workspace from the specified path
+pub fn find_local_path(path: impl Into<PathBuf>) -> Option<PathBuf> {
+ let mut current_path = path.into();
+ let workspace_dir = CLIENT_PATH_WORKSPACE_ROOT;
+
+ loop {
+ let jvc_path = current_path.join(workspace_dir);
+ if jvc_path.exists() {
+ return Some(current_path);
+ }
+
+ if let Some(parent) = current_path.parent() {
+ current_path = parent.to_path_buf();
+ } else {
+ break;
+ }
+ }
+
+ None
+}
+
+/// Get the system's document directory and join with .just_enough_vcs
+pub fn current_doc_dir() -> Option<PathBuf> {
+ dirs::document_dir().map(|path| path.join(".just_enough_vcs"))
+}