import { execFileSync } from "node:child_process";
import path from "node:path";
/**
 * Create a git worktree on a new branch from HEAD.
 * The worktree is placed as a sibling directory to the repo.
 */
export function createWorktree(repoPath, branchName) {
    const worktreePath = path.resolve(repoPath, "..", `${path.basename(repoPath)}-wt-${branchName}`);
    // Create branch and worktree in one step — uses execFileSync (no shell) to prevent injection
    execFileSync("git", ["worktree", "add", "-b", branchName, worktreePath, "HEAD"], {
        cwd: repoPath,
        stdio: "pipe",
    });
    return { worktreePath, branchName };
}
/**
 * Remove a git worktree and optionally delete the branch.
 */
export function cleanupWorktree(repoPath, worktreePath, branchName, deleteBranch = false) {
    try {
        execFileSync("git", ["worktree", "remove", worktreePath, "--force"], {
            cwd: repoPath,
            stdio: "pipe",
        });
    }
    catch {
        // If remove fails, try prune
        try {
            execFileSync("git", ["worktree", "prune"], { cwd: repoPath, stdio: "pipe" });
        }
        catch {
            // ignore
        }
    }
    if (deleteBranch && branchName) {
        try {
            execFileSync("git", ["branch", "-D", branchName], { cwd: repoPath, stdio: "pipe" });
        }
        catch {
            // branch may not exist
        }
    }
}
//# sourceMappingURL=git.js.map