## Summary Implement the `git_worktree_list` tool that parses `git worktree list` output into structured data with path, branch, commit hash, and current directory marker. ## Changes - Implemented `parseWorktreeList()` helper to parse raw `git worktree list` output - Handles standard branches (e.g., `[main]`) - Handles detached HEAD (marked as `(detached)`) - Marks the current working directory with `isCurrent: true` - Wired parser into `git_worktree_list` tool execute function - Returns both formatted display text and structured `details.worktrees` array - Added error handling for non-git-repo directories - Restructured tool definitions inside extension factory to capture `pi` via closure ## Testing - [x] Parsing logic verified against real `git worktree list` output (3 worktrees) - [ ] Manual verification in pi TUI pending extension load test ## Checklist - [x] Self-reviewed the diff - [x] Code follows project conventions (AGENTS.md, DESIGN.md, DOMAIN.md) - [x] Descriptive naming (no single-character variables) - [x] Error handling with clear messages and suggestions Co-authored-by: David Kong <davkon@gmail.com> Reviewed-on: #16
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
/**
|
|
* Git worktree list parser — pure functions, no pi SDK dependencies.
|
|
*
|
|
* Parses raw `git worktree list` output into structured entries.
|
|
*/
|
|
|
|
import path from "path";
|
|
|
|
// ─── Type Definitions ──────────────────────────────────────────────
|
|
|
|
/** A single worktree entry parsed from `git worktree list`. */
|
|
export interface WorktreeEntry {
|
|
/** Full path to the worktree directory. */
|
|
path: string;
|
|
/** Short commit hash checked out in this worktree. */
|
|
commit: string;
|
|
/** Branch name, or "(detached)" for detached HEAD. */
|
|
branch: string;
|
|
/** Whether this worktree is the current working directory. */
|
|
isCurrent: boolean;
|
|
}
|
|
|
|
// ─── Parser ────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Parse raw `git worktree list` output into structured entries.
|
|
*
|
|
* Expected format (whitespace-separated):
|
|
* /path/to/repo abc1234 [main]
|
|
* /tmp/feature-x def5678 [feature-x]
|
|
* /tmp/detached fedcba HEAD
|
|
*
|
|
* @param rawOutput - Raw stdout from `git worktree list`
|
|
* @param currentDir - Current working directory for comparison
|
|
* @returns Array of parsed worktree entries
|
|
*/
|
|
export function parseWorktreeList(rawOutput: string, currentDir: string): WorktreeEntry[] {
|
|
const lines = rawOutput.trim().split("\n").filter((line) => line.trim());
|
|
|
|
return lines.map((line) => {
|
|
// Split on whitespace: path, commit_hash, [branch] or HEAD
|
|
const parts = line.trim().split(/\s+/);
|
|
|
|
// Guard against malformed lines — need at least 3 tokens (path, commit, branch/HEAD)
|
|
if (parts.length < 3) {
|
|
return null;
|
|
}
|
|
|
|
const worktreePath = parts[0];
|
|
const commitHash = parts[1];
|
|
const branchRaw = parts.slice(2).join(" ");
|
|
|
|
// Validate: branch must be in brackets [name] or HEAD for detached
|
|
if (!branchRaw.match(/^\[.+\]$/) && !branchRaw.match(/^HEAD$/)) {
|
|
return null;
|
|
}
|
|
|
|
// Extract branch name from brackets, or mark as detached
|
|
const bracketMatch = branchRaw.match(/^\[(.+)\]$/);
|
|
const branchName = bracketMatch ? bracketMatch[1] : "(detached)";
|
|
|
|
return {
|
|
path: worktreePath,
|
|
commit: commitHash,
|
|
branch: branchName,
|
|
isCurrent: path.resolve(worktreePath) === path.resolve(currentDir),
|
|
};
|
|
}).filter((entry): entry is WorktreeEntry => entry !== null);
|
|
}
|