Skip to main content

Module worktrees

Module worktrees 

Source
Expand description

Read-side introspection for git worktrees.

Claude Code’s --worktree [name] flag (and its duplex equivalent crate::duplex::DuplexOptions::worktree) creates fresh git worktrees so an agent’s writes can land somewhere other than the current working tree. Hosts that orchestrate worktree-isolated chats need a way to enumerate the worktrees that exist for a given repo, see what branches they’re on, and notice when one is locked or prunable.

This module is a thin Rust API over git worktree list --porcelain. It is read-only on purpose; mutations (pruning, removing worktrees) are tracked separately so consumers that only want to introspect don’t opt into write semantics.

§Why shell out

Reading .git/worktrees/ directly is brittle: git tracks locked, prunable, detached-HEAD, and bare-repo state in ways that have evolved across releases. Asking git itself is the cheap, correct option, and git is already a transitive dependency of any worktree-using workflow.

§Example

use claude_wrapper::worktrees::WorktreeRoot;

let root = WorktreeRoot::for_repo(".");
for wt in root.list()? {
    println!(
        "{}  {}  {}{}",
        wt.path.display(),
        wt.branch.as_deref().unwrap_or("(detached)"),
        wt.head.as_deref().unwrap_or(""),
        if wt.is_locked { "  [locked]" } else { "" },
    );
}

Structs§

Worktree
One git worktree as reported by git worktree list --porcelain.
WorktreeRoot
Entry point for listing git worktrees rooted at a repository. Construct with Self::for_repo pointing at any path inside the repo (typically the repo root); git resolves the actual .git from there.