56e760f2bc
- Added new @oh-my-pi/pi-git-tool package with 22 git operations, safety guards, caching, and GitHub CLI integration. - Added /wt slash command for git worktree management with create, list, merge, remove, spawn, and parallel operations. - Added worktree library with collapse strategies (simple, merge-base, rebase) and session tracking for agent tasks. - Added structured git tool with TypeBox schema exposing gitTool and createGitTool from SDK. - Added safety policies blocking force push to protected branches and warning on destructive operations.
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
export interface GitRunResult {
|
|
stdout: string;
|
|
stderr: string;
|
|
exitCode: number;
|
|
}
|
|
|
|
export function runGit(args: string[], cwd: string): GitRunResult {
|
|
const result = Bun.spawnSync(["git", ...args], {
|
|
cwd,
|
|
stdin: "ignore",
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
});
|
|
return {
|
|
stdout: Buffer.from(result.stdout ?? []).toString(),
|
|
stderr: Buffer.from(result.stderr ?? []).toString(),
|
|
exitCode: result.exitCode ?? 0,
|
|
};
|
|
}
|
|
|
|
export function createTestRepo(): string {
|
|
const dir = mkdtempSync(join(tmpdir(), "git-tool-test-"));
|
|
let result = runGit(["init"], dir);
|
|
if (result.exitCode !== 0) {
|
|
throw new Error(result.stderr);
|
|
}
|
|
result = runGit(["config", "user.email", "test@example.com"], dir);
|
|
if (result.exitCode !== 0) throw new Error(result.stderr);
|
|
result = runGit(["config", "user.name", "Test User"], dir);
|
|
if (result.exitCode !== 0) throw new Error(result.stderr);
|
|
return dir;
|
|
}
|
|
|
|
export function writeFile(path: string, content: string): void {
|
|
writeFileSync(path, content, "utf-8");
|
|
}
|
|
|
|
export function cleanupRepo(dir: string): void {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|