|
1 | 1 | import { execSync } from "child_process" |
| 2 | +import fs from "fs" |
| 3 | +import os from "os" |
| 4 | +import path from "path" |
2 | 5 | import { type NextRequest, NextResponse } from "next/server" |
3 | 6 |
|
4 | 7 | export async function POST(request: NextRequest) { |
5 | 8 | try { |
6 | | - const { repoPath, commitHash } = await request.json() |
| 9 | + const { repoPath, commitHash } = await request.json() |
7 | 10 |
|
8 | 11 | if (!repoPath || !commitHash) { |
9 | 12 | return NextResponse.json({ error: "Repository path and commit hash are required" }, { status: 400 }) |
10 | 13 | } |
11 | 14 |
|
12 | | - // Use git show to get the patch for a single commit. Disable color and pager for clean parsing. |
13 | | - const cmd = `cd "${repoPath}" && git show ${commitHash} --no-color --no-ext-diff` |
14 | | - const diffText = execSync(cmd, { encoding: "utf-8", env: { ...process.env, GIT_PAGER: "" } }) |
| 15 | + // Helper to run git commands that may exit with non-zero when differences exist (git diff returns 1) |
| 16 | + const runGit = (cmd: string): string => { |
| 17 | + try { |
| 18 | + return execSync(cmd, { encoding: "utf-8", env: { ...process.env, GIT_PAGER: "" } }) |
| 19 | + } catch (e: any) { |
| 20 | + const out: Buffer | string | undefined = e?.stdout |
| 21 | + if (out) { |
| 22 | + return Buffer.isBuffer(out) ? out.toString("utf-8") : String(out) |
| 23 | + } |
| 24 | + throw e |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // If the special working directory node is requested, return the working tree diff vs HEAD |
| 29 | + let diffText = "" |
| 30 | + if (commitHash === "WORKING_DIR") { |
| 31 | + // Base diff: tracked changes vs HEAD (staged + unstaged) |
| 32 | + const baseCmd = `cd "${repoPath}" && git diff HEAD --no-color --no-ext-diff` |
| 33 | + diffText = runGit(baseCmd) |
| 34 | + |
| 35 | + // Append diffs for untracked files using no-index against an empty temp file |
| 36 | + const listCmd = `cd "${repoPath}" && git ls-files --others --exclude-standard -z` |
| 37 | + let untrackedRaw = "" |
| 38 | + try { |
| 39 | + // Capture as Buffer to safely handle NULs |
| 40 | + const buf: Buffer = execSync(listCmd, { env: { ...process.env, GIT_PAGER: "" } }) as unknown as Buffer |
| 41 | + untrackedRaw = buf.toString("utf-8") |
| 42 | + } catch { |
| 43 | + untrackedRaw = "" |
| 44 | + } |
| 45 | + const files = untrackedRaw |
| 46 | + .split("\u0000") |
| 47 | + .map((s) => s.trim()) |
| 48 | + .filter((s) => s.length > 0) |
| 49 | + |
| 50 | + if (files.length > 0) { |
| 51 | + // Create a temporary empty file |
| 52 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "vcv-")) |
| 53 | + const emptyPath = path.join(tmpDir, "empty") |
| 54 | + fs.writeFileSync(emptyPath, "") |
| 55 | + try { |
| 56 | + for (const f of files) { |
| 57 | + const perFileCmd = `cd "${repoPath}" && git diff --no-index --no-color --no-ext-diff -- "${emptyPath}" "${f}"` |
| 58 | + const perFile = runGit(perFileCmd) |
| 59 | + if (perFile && perFile.trim().length > 0) { |
| 60 | + if (diffText && !diffText.endsWith("\n")) diffText += "\n" |
| 61 | + diffText += perFile |
| 62 | + } |
| 63 | + } |
| 64 | + } finally { |
| 65 | + // Cleanup temp file and dir |
| 66 | + try { fs.unlinkSync(emptyPath) } catch {} |
| 67 | + try { fs.rmdirSync(tmpDir) } catch {} |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + // Use git show to get the patch for a single commit. Disable color and pager for clean parsing. |
| 72 | + const cmd = `cd "${repoPath}" && git show ${commitHash} --no-color --no-ext-diff` |
| 73 | + diffText = runGit(cmd) |
| 74 | + } |
15 | 75 |
|
16 | 76 | return NextResponse.json({ diff: diffText }) |
17 | 77 | } catch (error: any) { |
|
0 commit comments