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 let _env = crate::test_env::EnvGuard::set([("MUR_ACTIVE_PROJECT", "/explicit")]);
61 assert_eq!(active_project_id().as_deref(), Some("/explicit"));
62 }
63
64 #[test]
65 fn repo_root_found_from_subdir_and_none_outside() {
66 let tmp = tempfile::tempdir().unwrap();
67 let root = tmp.path().join("myrepo");
68 let sub = root.join("a").join("b");
69 fs::create_dir_all(&sub).unwrap();
70 fs::create_dir_all(root.join(".git")).unwrap();
71
72 // from a nested subdir we find the repo root
73 assert_eq!(repo_root_of(&sub).as_deref(), Some(root.as_path()));
74 // project_id is the canonicalized repo root, stable across entry points
75 assert_eq!(project_id(&sub), project_id(&root));
76 // a dir with no .git ancestor → None
77 let bare = tmp.path().join("not-a-repo");
78 fs::create_dir_all(&bare).unwrap();
79 assert!(repo_root_of(&bare).is_none());
80 assert!(project_id(&bare).is_none());
81 }
82}