use crate::core::skill_store::{ProjectRecord, SkillStore};
use anyhow::{bail, Result};
use std::path::Path;
pub fn resolve_project(store: &SkillStore, directory: &Path) -> Result<String> {
let directory = directory.canonicalize()?;
if !directory.is_dir() {
bail!("Workspace must be a directory");
}
let directory = directory.as_path();
let projects: Vec<_> = store
.get_all_projects()?
.into_iter()
.filter(|p| p.workspace_type == "project")
.collect();
if let Some(project) = projects
.iter()
.find(|p| Path::new(&p.path).canonicalize().ok().as_deref() == Some(directory))
{
return Ok(project.id.clone());
}
let is_worktree = |path: &Path| {
git2::Repository::open_ext(path, git2::RepositoryOpenFlags::NO_SEARCH, &[] as &[&Path])
.is_ok_and(|repository| repository.is_worktree())
};
let candidate_is_worktree = is_worktree(directory);
for project in projects {
if (candidate_is_worktree || is_worktree(Path::new(&project.path)))
&& crate::core::project_worktrees::directory(
store,
&project.id,
Some(&directory.to_string_lossy()),
)
.ok()
.as_deref()
== Some(directory)
{
return Ok(project.id);
}
}
let now = chrono::Utc::now().timestamp_millis();
let project = ProjectRecord {
id: uuid::Uuid::new_v4().to_string(),
name: directory
.file_name()
.and_then(|p| p.to_str())
.unwrap_or("workspace")
.into(),
path: directory.to_string_lossy().into(),
workspace_type: "project".into(),
linked_agent_key: None,
linked_agent_name: None,
disabled_path: None,
sort_order: 0,
created_at: now,
updated_at: now,
visited_at: Some(now),
};
if let Err(error) = store.insert_project(&project) {
if let Some(existing) = store
.get_all_projects()?
.into_iter()
.find(|item| item.path == project.path && item.workspace_type == "project")
{
return Ok(existing.id);
}
return Err(error);
}
Ok(project.id)
}
#[cfg(test)]
#[path = "workspace_registration/tests.rs"]
mod tests;