use crate::config::Config;
use std::collections::BTreeMap;
use std::path::Path;
use tokio::process::Command;
use tracing::trace;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BaseRef {
Branch(String),
DetachedSha(String),
WorktreeDir(String),
None,
}
impl BaseRef {
pub fn label(&self) -> &str {
match self {
BaseRef::Branch(b) => b.as_str(),
BaseRef::DetachedSha(s) => s.as_str(),
BaseRef::WorktreeDir(d) => d.as_str(),
BaseRef::None => "<not a git repo>",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Identity {
pub slug: String,
pub base_ref: BaseRef,
pub offset: u32,
}
impl Identity {
pub fn none() -> Self {
Self {
slug: String::new(),
base_ref: BaseRef::None,
offset: 0,
}
}
pub fn is_isolated(&self) -> bool {
!self.slug.is_empty()
}
pub async fn detect(project_root: &Path, config: &Config) -> Self {
let base_ref = detect_base_ref(project_root).await;
let slug = match &base_ref {
BaseRef::Branch(name) => slugify(name),
BaseRef::DetachedSha(sha) => format!("det-{sha}"),
BaseRef::WorktreeDir(dir) => slugify(dir),
BaseRef::None => String::new(),
};
let offset = offset_for(
&slug,
&config.worktrees.assign,
config.worktrees.modulus,
config.resolved_seed(),
);
trace!(slug = %slug, offset, ?base_ref, "worktree identity detected");
Self {
slug,
base_ref,
offset,
}
}
}
async fn detect_base_ref(project_root: &Path) -> BaseRef {
if let Some(branch) = git_output(project_root, &["rev-parse", "--abbrev-ref", "HEAD"])
.await
.filter(|b| b != "HEAD" && !b.is_empty())
{
return BaseRef::Branch(branch);
}
if let Some(git_dir) = git_output(project_root, &["rev-parse", "--git-dir"]).await
&& git_dir.contains("/worktrees/")
&& let Some(toplevel) = git_output(project_root, &["rev-parse", "--show-toplevel"]).await
&& let Some(name) = Path::new(&toplevel)
.file_name()
.and_then(|n| n.to_str())
.map(|s| s.to_string())
{
return BaseRef::WorktreeDir(name);
}
if let Some(sha) = git_output(project_root, &["rev-parse", "--short", "HEAD"])
.await
.filter(|s| !s.is_empty())
{
return BaseRef::DetachedSha(sha);
}
BaseRef::None
}
pub async fn detect_trunk(project_root: &Path, override_branch: Option<&str>) -> Option<String> {
if let Some(b) = override_branch
&& !b.is_empty()
{
return Some(b.to_string());
}
if let Some(out) = git_output(project_root, &["symbolic-ref", "refs/remotes/origin/HEAD"]).await
&& let Some(rest) = out.strip_prefix("refs/remotes/origin/")
{
return Some(rest.to_string());
}
for candidate in ["main", "master", "develop", "trunk"] {
if git_output(
project_root,
&["rev-parse", "--verify", "--quiet", candidate],
)
.await
.is_some()
{
return Some(candidate.to_string());
}
}
None
}
pub async fn merge_base(project_root: &Path, trunk: &str) -> Option<String> {
git_output(project_root, &["merge-base", trunk, "HEAD"]).await
}
pub async fn git_toplevel(project_root: &Path) -> Option<std::path::PathBuf> {
git_output(project_root, &["rev-parse", "--show-toplevel"])
.await
.map(std::path::PathBuf::from)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchEntry {
pub name: String,
pub remote_only: bool,
}
pub async fn list_branches(project_root: &Path) -> Vec<BranchEntry> {
use std::collections::HashSet;
let format = "--format=%(refname:short)\t%(committerdate:unix)";
let local_raw = git_output(
project_root,
&[
"for-each-ref",
"--sort=-committerdate",
format,
"refs/heads",
],
)
.await
.unwrap_or_default();
let remote_raw = git_output(
project_root,
&[
"for-each-ref",
"--sort=-committerdate",
format,
"refs/remotes",
],
)
.await
.unwrap_or_default();
let mut entries: Vec<(String, u64, bool)> = Vec::new();
let mut local_names: HashSet<String> = HashSet::new();
for line in local_raw.lines() {
let Some((name, date)) = line.split_once('\t') else {
continue;
};
if name.is_empty() {
continue;
}
let date: u64 = date.parse().unwrap_or(0);
local_names.insert(name.to_string());
entries.push((name.to_string(), date, false));
}
for line in remote_raw.lines() {
let Some((short, date)) = line.split_once('\t') else {
continue;
};
if short.ends_with("/HEAD") {
continue;
}
let Some(slash) = short.find('/') else {
continue;
};
let bare = &short[slash + 1..];
if bare.is_empty() || local_names.contains(bare) {
continue;
}
let date: u64 = date.parse().unwrap_or(0);
entries.push((bare.to_string(), date, true));
}
entries.sort_by_key(|b| std::cmp::Reverse(b.1));
entries
.into_iter()
.map(|(name, _, remote_only)| BranchEntry { name, remote_only })
.collect()
}
async fn git_output(project_root: &Path, args: &[&str]) -> Option<String> {
let output = Command::new("git")
.args(args)
.current_dir(project_root)
.output()
.await
.ok()?;
if !output.status.success() {
return None;
}
let s = String::from_utf8(output.stdout).ok()?;
Some(s.trim().to_string())
}
pub fn offset_for(slug: &str, assign: &BTreeMap<String, u32>, modulus: u32, seed: &str) -> u32 {
if slug.is_empty() {
return 0;
}
if let Some(&pinned) = assign.get(slug) {
return pinned;
}
let combined = format!("{seed}|{slug}");
fnv1a_32(&combined) % modulus.max(1)
}
pub fn fnv1a_32(input: &str) -> u32 {
let mut hash: u32 = 0x811c_9dc5;
for byte in input.bytes() {
hash ^= byte as u32;
hash = hash.wrapping_mul(0x0100_0193);
}
hash
}
pub fn slugify(input: &str) -> String {
let mut out = String::with_capacity(input.len());
let mut last_dash = true; for c in input.chars() {
let lc = c.to_ascii_lowercase();
if lc.is_ascii_alphanumeric() {
out.push(lc);
last_dash = false;
} else if !last_dash {
out.push('-');
last_dash = true;
}
}
if out.ends_with('-') {
out.pop();
}
out
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorktreeListEntry {
pub path: String,
pub branch: Option<String>,
pub detached: bool,
}
pub async fn list_worktrees(project_root: &Path) -> Vec<WorktreeListEntry> {
let Ok(output) = Command::new("git")
.args(["worktree", "list", "--porcelain"])
.current_dir(project_root)
.output()
.await
else {
return Vec::new();
};
if !output.status.success() {
return Vec::new();
}
let Ok(stdout) = String::from_utf8(output.stdout) else {
return Vec::new();
};
parse_worktree_porcelain(&stdout)
}
pub fn parse_worktree_porcelain(input: &str) -> Vec<WorktreeListEntry> {
let mut out = Vec::new();
let mut path: Option<String> = None;
let mut branch: Option<String> = None;
let mut detached = false;
for line in input.lines() {
if line.is_empty() {
if let Some(p) = path.take() {
out.push(WorktreeListEntry {
path: p,
branch: branch.take(),
detached: std::mem::take(&mut detached),
});
}
continue;
}
if let Some(p) = line.strip_prefix("worktree ") {
path = Some(p.to_string());
} else if let Some(b) = line.strip_prefix("branch ") {
branch = Some(b.trim_start_matches("refs/heads/").to_string());
} else if line == "detached" {
detached = true;
}
}
if let Some(p) = path {
out.push(WorktreeListEntry {
path: p,
branch,
detached,
});
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn parse_porcelain_minimal_two_worktrees() {
let input = "\
worktree /home/me/proj
HEAD abcd1234
branch refs/heads/main
worktree /home/me/proj-feature
HEAD ef567890
branch refs/heads/feature/x
";
let entries = parse_worktree_porcelain(input);
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].path, "/home/me/proj");
assert_eq!(entries[0].branch.as_deref(), Some("main"));
assert!(!entries[0].detached);
assert_eq!(entries[1].branch.as_deref(), Some("feature/x"));
}
#[test]
fn parse_porcelain_detached_entry() {
let input = "\
worktree /home/me/proj-detached
HEAD abcd1234
detached
";
let entries = parse_worktree_porcelain(input);
assert_eq!(entries.len(), 1);
assert!(entries[0].detached);
assert!(entries[0].branch.is_none());
}
#[test]
fn parse_porcelain_handles_trailing_no_blank_line() {
let input = "worktree /a\nHEAD aaa\nbranch refs/heads/main";
let entries = parse_worktree_porcelain(input);
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].path, "/a");
}
#[tokio::test]
async fn detect_trunk_override_wins_short_circuit() {
let path = std::path::Path::new("/nonexistent-croft-test-path");
let result = detect_trunk(path, Some("release/stable")).await;
assert_eq!(result.as_deref(), Some("release/stable"));
}
#[tokio::test]
async fn detect_trunk_empty_override_falls_through() {
let path = std::path::Path::new("/nonexistent-croft-test-path");
let result = detect_trunk(path, Some("")).await;
assert_eq!(result, None);
}
#[test]
fn slugify_lowercases_and_dashifies() {
assert_eq!(slugify("main"), "main");
assert_eq!(slugify("feature/x"), "feature-x");
assert_eq!(slugify("Refactor PR/2"), "refactor-pr-2");
assert_eq!(slugify("---multi---dashes---"), "multi-dashes");
assert_eq!(slugify(" spaces "), "spaces");
assert_eq!(slugify(""), "");
assert_eq!(slugify("Feature/X-1"), "feature-x-1");
}
#[test]
fn fnv1a_known_vectors() {
assert_eq!(fnv1a_32(""), 0x811c_9dc5);
assert_eq!(fnv1a_32("a"), 0xe40c_292c);
assert_eq!(fnv1a_32("foobar"), 0xbf9c_f968);
}
#[test]
fn fnv1a_is_deterministic() {
assert_eq!(fnv1a_32("croft|feature-x"), fnv1a_32("croft|feature-x"));
assert_ne!(fnv1a_32("croft|feature-x"), fnv1a_32("croft|feature-y"));
}
#[test]
fn empty_slug_offset_is_zero() {
let assign = BTreeMap::new();
assert_eq!(offset_for("", &assign, 1000, "seed"), 0);
}
#[test]
fn pinned_offset_wins_over_hash() {
let mut assign = BTreeMap::new();
assign.insert("main".to_string(), 0);
assign.insert("feature-x".to_string(), 7);
assert_eq!(offset_for("main", &assign, 1000, "seed"), 0);
assert_eq!(offset_for("feature-x", &assign, 1000, "seed"), 7);
}
#[test]
fn hashed_offset_is_within_modulus() {
let assign = BTreeMap::new();
for slug in ["a", "feature-x", "really-long-branch-name-here"] {
let offset = offset_for(slug, &assign, 100, "seed");
assert!(offset < 100, "offset {offset} not < 100 for slug {slug}");
}
}
#[test]
fn hashed_offset_diverges_with_seed() {
let assign = BTreeMap::new();
let a = offset_for("feature-x", &assign, 1000, "project-a");
let b = offset_for("feature-x", &assign, 1000, "project-b");
assert_ne!(a, b, "different seeds should produce different offsets");
}
#[test]
fn modulus_zero_does_not_panic() {
let assign = BTreeMap::new();
let off = offset_for("anything", &assign, 0, "seed");
assert_eq!(off, 0);
}
#[test]
fn identity_none_is_unisolated() {
let id = Identity::none();
assert!(!id.is_isolated());
assert_eq!(id.offset, 0);
assert!(id.slug.is_empty());
}
}