use std::path::{Path, PathBuf};
use vitrify_core::CoreError;
use vitrify_core::model::{Instance, Meta, Shape};
use vitrify_core::trace::hash12;
use crate::git::Git;
#[derive(Debug, Clone)]
pub struct Layout {
pub primary: PathBuf,
pub scratch: PathBuf,
pub wt_root: PathBuf,
}
impl Layout {
pub fn resolve(
primary: &Path,
home: Option<&Path>,
worktree_dir: &str,
) -> Result<Layout, CoreError> {
let wt_root = if worktree_dir.trim().is_empty() {
let Some(home) = home else {
return Err(CoreError::refusal(
1,
"error: cannot resolve the vitrify home (set VITRIFY_HOME or HOME)",
));
};
let name = primary
.file_name()
.map(|n| ref_part(&n.to_string_lossy()))
.unwrap_or_else(|| "repo".to_string());
let key = hash12(&primary.to_string_lossy());
home.join("worktrees").join(format!("{name}-{}", &key[..8]))
} else {
let p = PathBuf::from(worktree_dir.trim());
if p.is_absolute() { p } else { primary.join(p) }
};
Ok(Layout::at(primary, &wt_root))
}
pub fn at(primary: &Path, wt_root: &Path) -> Layout {
Layout {
primary: primary.to_path_buf(),
scratch: primary.join(".vitrify"),
wt_root: normalize(wt_root),
}
}
pub fn mainline(&self, slug: &str) -> PathBuf {
self.wt_root.join(format!("mainline-{slug}"))
}
pub fn mainline_lock(&self, slug: &str) -> PathBuf {
self.wt_root.join(format!("mainline-{slug}.lock"))
}
pub fn base_checkout(&self, slug: &str) -> PathBuf {
self.wt_root.join(format!("base-{slug}"))
}
pub fn head_checkout(&self, slug: &str) -> PathBuf {
self.scratch
.join(format!("tx-{}-{slug}-head", std::process::id()))
}
pub fn job_logs(&self, job: &str) -> PathBuf {
self.scratch.join("logs").join(job)
}
pub fn lane_logs(&self, job: &str, slug: &str) -> PathBuf {
self.job_logs(job).join(slug)
}
pub fn unit_worktree(&self, slug: &str, unit: &str, seq: i64) -> PathBuf {
self.wt_root.join(format!("{slug}-{unit}-c{seq}"))
}
pub fn in_tree(&self) -> Option<PathBuf> {
self.wt_root
.strip_prefix(&self.primary)
.ok()
.map(Path::to_path_buf)
}
pub fn ensure_scratch(&self) -> Result<(), CoreError> {
ensure_scratch(&self.primary).map(|_| ())
}
pub fn ensure_worktree_root(&self, primary: &Git) -> Result<(), CoreError> {
std::fs::create_dir_all(&self.wt_root).map_err(|source| CoreError::Io {
path: self.wt_root.clone(),
source,
})?;
let Some(rel) = self.in_tree() else {
return Ok(());
};
let rel = rel.to_string_lossy().replace('\\', "/");
if rel.is_empty() {
return Err(CoreError::refusal(
2,
"error: run.worktree_dir is the repository root itself — name a directory",
));
}
primary
.exclude_locally(&format!("/{rel}/"))
.map_err(CoreError::from)?;
if let Some(source) = primary
.ignored_by(&format!("{rel}/probe"))
.map_err(CoreError::from)?
&& !source.contains("info/exclude")
{
return Err(CoreError::refusal(
2,
format!(
"error: the worktree directory {rel}/ is ignored by {source} — tools that honour parent ignore files would check nothing in vitrify's worktrees; remove that pattern, or point run.worktree_dir elsewhere"
),
));
}
Ok(())
}
}
fn normalize(path: &Path) -> PathBuf {
use std::path::Component;
let mut out = PathBuf::new();
for part in path.components() {
match part {
Component::CurDir => {}
Component::ParentDir => {
if !out.pop() {
out.push("..");
}
}
other => out.push(other.as_os_str()),
}
}
out
}
pub fn ensure_scratch(primary: &Path) -> Result<PathBuf, CoreError> {
let scratch = primary.join(".vitrify");
std::fs::create_dir_all(&scratch).map_err(|source| CoreError::Io {
path: scratch.clone(),
source,
})?;
let _ = std::fs::write(scratch.join(".gitignore"), "*\n");
Ok(scratch)
}
pub fn ref_part(s: &str) -> String {
let mut out: String = s
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-') {
c
} else {
'-'
}
})
.collect();
while out.contains("..") {
out = out.replace("..", ".");
}
let mut out = out
.trim_start_matches(['.', '-'])
.trim_end_matches('.')
.to_string();
if out.ends_with(".lock") {
out.push('-');
}
if out.is_empty() {
out.push_str("sdd");
}
out
}
pub fn slug_of(meta: &Meta, root: &Path, shape: Shape) -> String {
let named = meta.get_str("sdd").map(str::trim).unwrap_or("");
if !named.is_empty() {
return ref_part(named);
}
let stem = match shape {
Shape::Bundle => root.file_name().map(|n| n.to_string_lossy().to_string()),
Shape::Single => root.file_name().map(|n| {
let n = n.to_string_lossy();
n.strip_suffix(".sdd.md")
.or_else(|| n.strip_suffix(".md"))
.unwrap_or(&n)
.to_string()
}),
};
ref_part(&stem.unwrap_or_default())
}
pub fn sdd_slug(inst: &Instance) -> String {
slug_of(&inst.meta, &inst.root, inst.shape)
}
pub fn branch_prefix(slug: &str, unit: &str) -> String {
format!("sdd/{slug}/{}/", ref_part(unit))
}
pub fn unit_branch(slug: &str, unit: &str, seq: i64) -> String {
format!("{}c{seq}", branch_prefix(slug, unit))
}
pub fn branch_seq(branch: &str) -> Option<i64> {
branch.rsplit('/').next()?.strip_prefix('c')?.parse().ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_carry_the_slug_and_the_token() {
assert_eq!(
unit_branch("shop-cart", "U-001", 3),
"sdd/shop-cart/U-001/c3"
);
assert_eq!(branch_seq("sdd/shop-cart/U-001/c3"), Some(3));
assert_eq!(branch_seq("sdd/shop-cart/U-001/x3"), None);
let l = Layout::at(Path::new("/r"), Path::new("/w"));
assert_eq!(
l.unit_worktree("shop-cart", "U-001", 3),
PathBuf::from("/w/shop-cart-U-001-c3")
);
assert_eq!(
l.mainline("shop-cart"),
PathBuf::from("/w/mainline-shop-cart")
);
assert_eq!(
l.mainline_lock("shop-cart"),
PathBuf::from("/w/mainline-shop-cart.lock")
);
assert_eq!(
l.lane_logs("run-x", "shop-cart"),
PathBuf::from("/r/.vitrify/logs/run-x/shop-cart")
);
assert_eq!(
l.head_checkout("shop-cart"),
PathBuf::from(format!(
"/r/.vitrify/tx-{}-shop-cart-head",
std::process::id()
))
);
assert!(l.in_tree().is_none());
assert_eq!(
Layout::at(Path::new("/r"), Path::new("/r/wt")).in_tree(),
Some(PathBuf::from("wt"))
);
}
#[test]
fn ref_part_is_always_a_legal_component() {
assert_eq!(ref_part("shop-cart"), "shop-cart");
assert_eq!(ref_part("my sdd/v2"), "my-sdd-v2");
assert_eq!(ref_part("..hidden"), "hidden");
assert_eq!(ref_part("a..b"), "a.b");
assert_eq!(ref_part("x.lock"), "x.lock-");
assert_eq!(ref_part("~^:"), "sdd");
}
#[test]
fn default_root_is_outside_the_working_tree_and_keyed_by_repository() {
let a = Layout::resolve(Path::new("/src/app"), Some(Path::new("/h")), "").unwrap();
let b = Layout::resolve(Path::new("/other/app"), Some(Path::new("/h")), "").unwrap();
assert!(a.wt_root.starts_with("/h/worktrees"));
assert!(a.in_tree().is_none());
assert_ne!(a.wt_root, b.wt_root, "same name, different repository");
let c = Layout::resolve(Path::new("/src/app"), None, "/big/disk").unwrap();
assert_eq!(c.wt_root, PathBuf::from("/big/disk"));
let d = Layout::resolve(Path::new("/src/app"), None, "vitrify-wt").unwrap();
assert_eq!(d.in_tree(), Some(PathBuf::from("vitrify-wt")));
let e = Layout::resolve(Path::new("/src/app"), None, "../app-wt").unwrap();
assert_eq!(e.wt_root, PathBuf::from("/src/app-wt"));
assert!(e.in_tree().is_none());
let f = Layout::resolve(Path::new("/src/app"), None, "./a/../wt").unwrap();
assert_eq!(f.in_tree(), Some(PathBuf::from("wt")));
assert!(Layout::resolve(Path::new("/src/app"), None, "").is_err());
}
}