Lightweight coding agent that runs in your terminal
Rust
fix: diff command for filenames with special characters (#954)
hanson-openai7edfbae062ed
diff --git a/codex-cli/src/utils/get-diff.ts b/codex-cli/src/utils/get-diff.tsindex 9ac7844d2a..cd3d2040f1 100644--- a/codex-cli/src/utils/get-diff.ts+++ b/codex-cli/src/utils/get-diff.ts@@ -1,4 +1,4 @@-import { execSync } from "node:child_process";+import { execSync, execFileSync } from "node:child_process";// The objects thrown by `child_process.execSync()` are `Error` instances that// include additional, undocumented properties such as `status` (exit code) and@@ -89,12 +89,18 @@ export function getGitDiff(): {//// `git diff --color --no-index /dev/null <file>` exits with status 1// when differences are found, so we capture stdout from the thrown- // error object instead of letting it propagate.- execSync(`git diff --color --no-index -- "${nullDevice}" "${file}"`, {- encoding: "utf8",- stdio: ["ignore", "pipe", "ignore"],- maxBuffer: 10 * 1024 * 1024,- });+ // error object instead of letting it propagate. Using `execFileSync`+ // avoids shell interpolation issues with special characters in the+ // path.+ execFileSync(+ "git",+ ["diff", "--color", "--no-index", "--", nullDevice, file],+ {+ encoding: "utf8",+ stdio: ["ignore", "pipe", "ignore"],+ maxBuffer: 10 * 1024 * 1024,+ },+ );} catch (err) {if (isExecSyncError(err) &&diff --git a/codex-cli/tests/get-diff-special-chars.test.ts b/codex-cli/tests/get-diff-special-chars.test.tsnew file mode 100644index 0000000000..e701fddfe2--- /dev/null+++ b/codex-cli/tests/get-diff-special-chars.test.ts@@ -0,0 +1,28 @@+import { mkdtempSync, writeFileSync, rmSync } from "fs";+import { tmpdir } from "os";+import { join } from "path";+import { execSync } from "child_process";+import { describe, it, expect } from "vitest";++import { getGitDiff } from "../src/utils/get-diff.js";++describe("getGitDiff", () => {+ it("handles untracked files with special characters", () => {+ const repoDir = mkdtempSync(join(tmpdir(), "git-diff-test-"));+ const prevCwd = process.cwd();+ try {+ process.chdir(repoDir);+ execSync("git init", { stdio: "ignore" });++ const fileName = "a$b.txt";+ writeFileSync(join(repoDir, fileName), "hello\n");++ const { isGitRepo, diff } = getGitDiff();+ expect(isGitRepo).toBe(true);+ expect(diff).toContain(fileName);+ } finally {+ process.chdir(prevCwd);+ rmSync(repoDir, { recursive: true, force: true });+ }+ });+});