rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Git workspace identity for session paths: which checkout and which
//! repository a directory belongs to. Shared by session search scopes and the
//! `/sessions` hub grouping.

use std::path::{Path, PathBuf};

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Workspace {
    /// Checkout root: the directory holding `.git`, or the directory itself
    /// outside Git.
    pub(crate) worktree: PathBuf,
    /// Shared Git directory, identical for every worktree of one repository.
    /// Equals `worktree` outside Git.
    pub(crate) repo: PathBuf,
}

impl Workspace {
    /// Whether the directory sits inside a Git checkout.
    pub(crate) fn is_git(&self) -> bool {
        self.repo != self.worktree
    }

    /// Resolve Git administrative paths without running project configuration,
    /// hooks or subprocesses. A non-Git directory is its own local scope. A
    /// `.git` file naming no Git directory there is passed over for the
    /// checkout above it. `rho/fs/look` names a folder's project from this
    /// too, so it groups folders as sessions are grouped.
    pub(crate) fn resolve(cwd: &Path) -> Self {
        let cwd = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
        for ancestor in cwd.ancestors() {
            let Some(git_dir) = crate::git::git_dir_at(ancestor) else {
                continue;
            };
            // A linked worktree's Git directory names the one its
            // repository's worktrees share, relative to itself.
            let common = crate::git::read_small(&git_dir.join("commondir"))
                .map(|relative| git_dir.join(relative.trim()))
                .unwrap_or(git_dir);
            if let Ok(repo) = common.canonicalize() {
                return Self {
                    worktree: ancestor.to_path_buf(),
                    repo,
                };
            }
        }
        Self {
            worktree: cwd.clone(),
            repo: cwd,
        }
    }
}