Skip to main content

mur_common/
project.rs

1//! Canonical project identity for skill `scope: Project` matching.
2//!
3//! A project == its git repo root. Harvest stamps a learned skill with the
4//! repo root it was learned in; injection computes the repo root of the current
5//! working dir; the two match iff you're in the same repo. Using the repo root
6//! (not the cwd) means a skill learned in a subdir of repo X still applies
7//! anywhere in X.
8
9use std::path::{Path, PathBuf};
10
11/// Walk up from `start` to the directory containing a `.git` entry (the repo
12/// root). `None` if `start` isn't inside a git repo.
13///
14/// Note: a `git worktree` has its own `.git` *file*, so it resolves to the
15/// worktree's own root — a worktree and its main checkout get distinct project
16/// ids (project-scoped skills don't cross between them). Acceptable for now.
17pub fn repo_root_of(start: &Path) -> Option<PathBuf> {
18    let mut dir = Some(start);
19    while let Some(d) = dir {
20        if d.join(".git").exists() {
21            return Some(d.to_path_buf());
22        }
23        dir = d.parent();
24    }
25    None
26}
27
28/// Canonical project id (repo-root path string) for scope matching, or `None`
29/// if not in a repo. Canonicalized so two entry points into the same repo
30/// (subdir, symlink, relative path) produce the same id.
31pub fn project_id(start: &Path) -> Option<String> {
32    let root = repo_root_of(start)?;
33    let canon = std::fs::canonicalize(&root).unwrap_or(root);
34    // Non-UTF-8 repo path → None (treated as "not in a project" → global) so a
35    // lossy id can never silently mismatch between stamp and inject time.
36    canon.to_str().map(str::to_owned)
37}
38
39/// The active project id for scope matching: `MUR_ACTIVE_PROJECT` env override,
40/// else the current working dir's repo root. `None` when neither resolves
41/// (→ only user/enterprise skills inject). Single source used by both the CLI
42/// injection hook and the agent runtime injector so they can't diverge.
43pub fn active_project_id() -> Option<String> {
44    if let Ok(v) = std::env::var("MUR_ACTIVE_PROJECT") {
45        let v = v.trim();
46        if !v.is_empty() {
47            return Some(v.to_string());
48        }
49    }
50    std::env::current_dir().ok().and_then(|d| project_id(&d))
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use std::fs;
57
58    #[test]
59    fn active_project_id_prefers_env_override() {
60        // nextest isolates each test in its own process → env is safe to set.
61        unsafe { std::env::set_var("MUR_ACTIVE_PROJECT", "/explicit") };
62        assert_eq!(active_project_id().as_deref(), Some("/explicit"));
63        unsafe { std::env::remove_var("MUR_ACTIVE_PROJECT") };
64    }
65
66    #[test]
67    fn repo_root_found_from_subdir_and_none_outside() {
68        let tmp = tempfile::tempdir().unwrap();
69        let root = tmp.path().join("myrepo");
70        let sub = root.join("a").join("b");
71        fs::create_dir_all(&sub).unwrap();
72        fs::create_dir_all(root.join(".git")).unwrap();
73
74        // from a nested subdir we find the repo root
75        assert_eq!(repo_root_of(&sub).as_deref(), Some(root.as_path()));
76        // project_id is the canonicalized repo root, stable across entry points
77        assert_eq!(project_id(&sub), project_id(&root));
78        // a dir with no .git ancestor → None
79        let bare = tmp.path().join("not-a-repo");
80        fs::create_dir_all(&bare).unwrap();
81        assert!(repo_root_of(&bare).is_none());
82        assert!(project_id(&bare).is_none());
83    }
84}