use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Clone, PartialEq)]
pub struct ProjectRoot {
pub root: PathBuf,
pub source: ProjectRootSource,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ProjectRootSource {
Git,
AncestorWalk,
Fallback,
}
pub fn resolve_project_root(start: &Path) -> ProjectRoot {
if let Some(primary_root) = git_common_dir_root(start) {
return ProjectRoot {
root: primary_root,
source: ProjectRootSource::Git,
};
}
for ancestor in start.ancestors() {
if ancestor.join(".tracevault").is_dir() {
return ProjectRoot {
root: ancestor.to_path_buf(),
source: ProjectRootSource::AncestorWalk,
};
}
}
ProjectRoot {
root: start.to_path_buf(),
source: ProjectRootSource::Fallback,
}
}
pub fn worktree_toplevel(from: &Path) -> String {
Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(from)
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| {
let p = PathBuf::from(s.trim());
p.canonicalize().unwrap_or(p).to_string_lossy().into_owned()
})
.unwrap_or_else(|| {
from.canonicalize()
.unwrap_or_else(|_| from.to_path_buf())
.to_string_lossy()
.into_owned()
})
}
fn git_common_dir_root(start_dir: &Path) -> Option<PathBuf> {
let output = Command::new("git")
.args([
"-C",
&start_dir.to_string_lossy(),
"rev-parse",
"--git-common-dir",
])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let stdout = std::str::from_utf8(&output.stdout).ok()?;
let raw = stdout.lines().next()?.trim();
let p = PathBuf::from(raw);
let git_common_dir = if p.is_absolute() {
p.canonicalize().ok()?
} else {
start_dir.join(&p).canonicalize().ok()?
};
git_common_dir.parent().map(PathBuf::from)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::{add_worktree, init_git_repo};
#[test]
fn primary_checkout_resolves_to_repo_root() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
let result = resolve_project_root(&repo);
assert_eq!(result.source, ProjectRootSource::Git);
assert_eq!(
result.root.canonicalize().unwrap(),
repo.canonicalize().unwrap(),
"primary checkout must resolve to repo root"
);
}
#[test]
fn nested_worktree_resolves_to_primary_root() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
let wt = repo.join("nested-wt");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
add_worktree(&repo, &wt);
let result = resolve_project_root(&wt);
assert_eq!(result.source, ProjectRootSource::Git);
assert_eq!(
result.root.canonicalize().unwrap(),
repo.canonicalize().unwrap(),
"nested worktree must resolve to primary repo root"
);
}
#[test]
fn sibling_worktree_resolves_to_primary_root() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
let wt = tmp.path().join("sibling-wt");
std::fs::create_dir_all(&repo).unwrap();
init_git_repo(&repo);
add_worktree(&repo, &wt);
let result = resolve_project_root(&wt);
assert_eq!(result.source, ProjectRootSource::Git);
assert_eq!(
result.root.canonicalize().unwrap(),
repo.canonicalize().unwrap(),
"sibling worktree must resolve to primary repo root (not the worktree dir)"
);
}
#[test]
fn non_git_with_tracevault_ancestor_uses_ancestor_walk() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".tracevault")).unwrap();
let subdir = tmp.path().join("a").join("b").join("c");
std::fs::create_dir_all(&subdir).unwrap();
let result = resolve_project_root(&subdir);
assert_eq!(result.source, ProjectRootSource::AncestorWalk);
assert_eq!(
result.root.canonicalize().unwrap(),
tmp.path().canonicalize().unwrap(),
"ancestor walk must find the directory that contains .tracevault/"
);
}
#[test]
fn non_git_no_tracevault_falls_back_to_start() {
let tmp = tempfile::tempdir().unwrap();
let result = resolve_project_root(tmp.path());
assert_eq!(result.source, ProjectRootSource::Fallback);
assert_eq!(
result.root,
tmp.path(),
"with no git and no .tracevault/, root must be start itself"
);
}
}