import { execFile } from "node:child_process"; import { createHash, randomBytes } from "node:crypto"; import { copyFileSync, existsSync, mkdirSync, readdirSync, realpathSync, rmSync, statSync } from "node:fs"; import { basename, join, relative, normalize } from "node:path"; import { promisify } from "node:util"; import { RpcError, type GitInfo } from "@linkshell/wire"; // Git worktrees for sessions: a session that shouldn't touch the project's // working directory gets its own checkout on its own branch, made from the // project's last commit and kept under the host's home, outside the project. const run = promisify(execFile); async function git(cwd: string, args: string[], timeout = 60_000): Promise { // windowsHide: the host has no console, so each git would otherwise flash one open on Windows. const { stdout } = await run("git", ["-C", cwd, ...args], { timeout, maxBuffer: 16 * 1024 * 1024, windowsHide: true }); return stdout.trim(); } /** What git says about a directory; undefined when it isn't in a repository (or git is missing). */ export async function gitInfo(path: string): Promise { try { const root = normalize(await git(path, ["rev-parse", "--show-toplevel"])); const [branch, head, status] = await Promise.all([ git(path, ["symbolic-ref", "--short", "-q", "HEAD"]).catch(() => ""), git(path, ["rev-parse", "--short", "HEAD"]).catch(() => ""), git(path, ["status", "--porcelain"]), ]); return { root, branch: branch || undefined, head: head || undefined, dirty: status.length > 0 }; } catch { return undefined; } } /** * The branch checked out in a directory (the abbreviated commit on a detached * HEAD); undefined outside a repository. Doesn't look at the working tree, so * it stays quick in a large repository. */ export async function gitBranch(path: string): Promise { try { const branch = await git(path, ["symbolic-ref", "--short", "-q", "HEAD"], 3000).catch(() => ""); return branch || (await git(path, ["rev-parse", "--short", "HEAD"], 3000)) || undefined; } catch { return undefined; } } export interface CreatedWorktree { /** The worktree's top directory. */ path: string; /** Where a session started from `cwd` works: the same place inside the worktree. */ cwd: string; branch: string; /** The repository it was made from. */ source: string; /** The commit it started at. */ base: string; } /** Letters, digits and dashes from a title, for a directory and branch name. */ export function slug(label: string): string { const trimmed = label.trim(); if (!trimmed) return "session"; const cleaned = trimmed .normalize("NFKD") .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 24) .replace(/-+$/, ""); const hasNonAscii = [...trimmed].some((ch) => ch.charCodeAt(0) > 0x7f); if (cleaned.length >= 8 || (!hasNonAscii && cleaned.length > 0)) { return cleaned; } if (!hasNonAscii) return "session"; // Non-ASCII / CJK titles strip down to little or no ASCII; append a stable // content hash so distinct CJK sessions get distinct readable branch slugs. const digest = createHash("sha256").update(trimmed).digest("hex").slice(0, 8); return cleaned ? `${cleaned}-${digest}` : `s-${digest}`; } /** * Copies local `.env*` files from `sourceDir` into `targetDir` when git didn't * check them out, so agents in a new worktree don't fail on missing env vars. */ function copyEnvFiles(sourceDir: string, targetDir: string): void { try { for (const entry of readdirSync(sourceDir, { withFileTypes: true })) { if (!entry.isFile() || !/^\.env(?:\..+)?$/.test(entry.name)) continue; const src = join(sourceDir, entry.name); const dst = join(targetDir, entry.name); if (existsSync(dst)) continue; if (statSync(src).size <= 1024 * 1024) { copyFileSync(src, dst); } } } catch { // Best-effort env bootstrap. } } /** * Makes a worktree of the repository `cwd` is in, on a new branch at its * current commit. Uncommitted changes stay where they are. */ export async function createWorktree(cwd: string, home: string, label: string): Promise { const info = await gitInfo(cwd); if (!info) throw RpcError.app("not_a_repo", "这个目录不在 git 仓库里,不能创建 worktree"); const base = await git(info.root, ["rev-parse", "HEAD"]).catch(() => { throw RpcError.app("not_a_repo", "这个仓库还没有提交,不能创建 worktree"); }); const name = `${slug(label)}-${randomBytes(3).toString("hex")}`; const parent = join(home, "worktrees", basename(info.root)); mkdirSync(parent, { recursive: true }); const path = join(parent, name); const branch = `linkshell/${name}`; try { await git(info.root, ["worktree", "add", "-b", branch, path, base]); } catch (error) { throw RpcError.app("git_failed", `创建 worktree 失败:${error instanceof Error ? error.message.split("\n").slice(-2).join(" ").trim() : String(error)}`); } // Git resolves symlinks in its root (e.g. /var -> /private/var on macOS). // Compare physical paths so an alias cannot turn the suffix into ../ segments. const subCwd = join(path, relative(realpathSync(info.root), realpathSync(cwd))); copyEnvFiles(info.root, path); if (subCwd !== path) copyEnvFiles(cwd, subCwd); return { path, cwd: subCwd, branch, source: info.root, base }; } /** Whether removing the worktree would lose work: uncommitted changes, and commits since it was made. */ export async function worktreeState(path: string, base: string): Promise<{ dirty: boolean; ahead: number }> { if (!existsSync(path)) return { dirty: false, ahead: 0 }; const status = await git(path, ["status", "--porcelain"]).catch(() => ""); const ahead = Number(await git(path, ["rev-list", "--count", `${base}..HEAD`]).catch(() => "0")); return { dirty: status.length > 0, ahead: Number.isFinite(ahead) ? ahead : 0 }; } /** Removes a worktree and the branch made for it. */ export async function removeWorktree(worktree: { path: string; branch: string; source: string }): Promise { if (existsSync(worktree.path)) { await git(worktree.source, ["worktree", "remove", "--force", worktree.path]).catch(() => { // Not (or no longer) known to git: the directory is all that is left. rmSync(worktree.path, { recursive: true, force: true }); }); } await git(worktree.source, ["worktree", "prune"]).catch(() => {}); await git(worktree.source, ["branch", "-D", worktree.branch]).catch(() => {}); }