Skip to main content

git_worktree_manager/
repo_config.rs

1//! Repo-local `.cwconfig.json` loader.
2//!
3//! Walks up from `start` looking for the first `.cwconfig.json`. Returns the
4//! parsed JSON value alongside the path it was found at. Used by Phase 7.2's
5//! layered config resolution; not yet wired into config callers.
6
7use std::path::{Path, PathBuf};
8
9use crate::error::Result;
10
11pub struct RepoConfig {
12    pub path: PathBuf,
13    pub value: serde_json::Value,
14}
15
16pub fn find_repo_config(start: &Path) -> Option<RepoConfig> {
17    let mut cur = start.to_path_buf();
18    loop {
19        let candidate = cur.join(".cwconfig.json");
20        if candidate.exists() {
21            if let Ok(content) = std::fs::read_to_string(&candidate) {
22                if let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) {
23                    return Some(RepoConfig {
24                        path: candidate,
25                        value,
26                    });
27                }
28            }
29        }
30        if !cur.pop() {
31            return None;
32        }
33    }
34}
35
36pub fn load_repo_config(start: &Path) -> Result<Option<serde_json::Value>> {
37    Ok(find_repo_config(start).map(|c| c.value))
38}