import type { WorkspaceProbeWorktree } from "agent-relay-sdk"; export function shortBranch(ref: string | undefined): string | undefined { if (!ref || ref === "HEAD") return undefined; return ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref; } export function parseWorktrees(output: string): WorkspaceProbeWorktree[] { const blocks = output.split(/\n\s*\n/).map((block) => block.trim()).filter(Boolean); return blocks.map((block) => { const worktree: WorkspaceProbeWorktree = { path: "" }; for (const line of block.split("\n")) { if (line.startsWith("worktree ")) worktree.path = line.slice("worktree ".length); else if (line.startsWith("HEAD ")) worktree.headSha = line.slice("HEAD ".length); else if (line.startsWith("branch ")) worktree.branch = shortBranch(line.slice("branch ".length)); else if (line === "bare") worktree.bare = true; else if (line === "detached") worktree.detached = true; else if (line.startsWith("prunable")) { worktree.prunable = true; const reason = line.slice("prunable".length).trim(); if (reason) worktree.reason = reason; } else if (line.startsWith("locked")) { worktree.locked = true; const reason = line.slice("locked".length).trim(); if (reason) worktree.reason = reason; } } return worktree; }).filter((worktree) => worktree.path); }