agent_hub_runtime/core/
git_catalog.rs1use crate::core::{git_fetcher, path_guard, scanner, skill_metadata};
2use anyhow::{bail, Context, Result};
3use std::path::{Path, PathBuf};
4
5#[derive(Debug, serde::Serialize)]
6pub struct GitSkillPreview {
7 pub rel_path: String,
9 pub repo_path: String,
11 pub name: String,
12 pub description: Option<String>,
13 pub version: Option<String>,
14}
15
16pub fn skill_rel_key(skill_dir: &Path, dir: &Path) -> String {
18 let rel = dir.strip_prefix(skill_dir).unwrap_or(dir);
19 if rel.as_os_str().is_empty() {
20 dir.file_name()
21 .map(|n| n.to_string_lossy().to_string())
22 .unwrap_or_default()
23 } else {
24 rel.to_string_lossy().replace('\\', "/")
25 }
26}
27
28pub fn preview_skill(repo_root: &Path, scan_root: &Path, dir: &Path) -> GitSkillPreview {
30 let meta = skill_metadata::parse_skill_md(dir);
31 let rel_path = skill_rel_key(scan_root, dir);
32 let fallback = dir
33 .file_name()
34 .map(|n| n.to_string_lossy().to_string())
35 .unwrap_or_else(|| rel_path.clone());
36 GitSkillPreview {
37 rel_path,
38 repo_path: git_fetcher::relative_subpath(repo_root, dir)
39 .unwrap_or_default()
40 .replace('\\', "/"),
41 name: meta
42 .name
43 .filter(|name| !name.trim().is_empty())
44 .unwrap_or(fallback),
45 description: meta.description,
46 version: meta.version,
47 }
48}
49
50#[derive(Debug, serde::Serialize)]
51pub struct LocalGitCatalog {
52 pub path: String,
53 pub skills: Vec<GitSkillPreview>,
54}
55
56fn local_repository_root(path: &Path) -> Result<PathBuf> {
57 let selected = path
58 .canonicalize()
59 .context("Local repository directory is unavailable")?;
60 let repo = git2::Repository::open(&selected).context("Select a Git working tree")?;
61 let root = repo
62 .workdir()
63 .context("Bare repositories have no Skills working tree")?
64 .canonicalize()?;
65 if selected != root {
66 bail!("Select the root directory of the Git working tree");
67 }
68 Ok(root)
69}
70
71pub fn scan_local_repository(path: &Path) -> Result<LocalGitCatalog> {
73 let root = local_repository_root(path)?;
74 let mut dirs = if skill_metadata::is_valid_skill_dir(&root) {
75 vec![root.clone()]
76 } else {
77 scanner::collect_skill_dirs(&root)
78 };
79 dirs.retain(|dir| path_guard::is_path_safe(&root, dir));
80 dirs = dirs
81 .into_iter()
82 .map(|dir| dir.canonicalize())
83 .collect::<std::io::Result<Vec<_>>>()?;
84 dirs.sort();
85 dirs.dedup();
86 Ok(LocalGitCatalog {
87 path: root.to_string_lossy().to_string(),
88 skills: dirs
89 .iter()
90 .map(|dir| preview_skill(&root, &root, dir))
91 .collect(),
92 })
93}
94
95pub fn resolve_local_skill(path: &Path, repo_path: &str) -> Result<PathBuf> {
97 let root = local_repository_root(path)?;
98 let candidate = root.join(repo_path);
99 if !path_guard::is_path_safe(&root, &candidate)
100 || !skill_metadata::is_valid_skill_dir(&candidate)
101 {
102 bail!("Skill directory is missing or outside the selected repository");
103 }
104 Ok(candidate.canonicalize()?)
105}
106
107#[cfg(test)]
108mod tests;