OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(targets): support unborn Git worktrees and ignore SVN metadata (#1552)
Michael D'Angelo23ae828305c4
diff --git a/plugins/codex-security/scripts/generate_in_scope_files.py b/plugins/codex-security/scripts/generate_in_scope_files.pyindex b3ebd959a..00e0b5c21 100644--- a/plugins/codex-security/scripts/generate_in_scope_files.py+++ b/plugins/codex-security/scripts/generate_in_scope_files.py@@ -12,7 +12,7 @@ from pathlib import Path# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))-from workbench_target import git_blob_samples, git_command+from workbench_target import SOURCE_METADATA_DIRECTORIES, git_blob_samples, git_commandclass InventoryError(ValueError):@@ -94,11 +94,11 @@ def generate_in_scope_files(repository: Path, scope: str, output: Path) -> int:"--hidden","--path-separator","/",- # Prune Git metadata and its contents even when the scope starts inside .git.+ # Prune metadata and its contents even when the scope starts inside it."--glob",- "!**/.git",+ "!**/{.git,.svn}","--glob",- "!**/.git/**",+ "!**/{.git,.svn}/**","--",scope,]@@ -146,7 +146,7 @@ def generate_in_scope_files(repository: Path, scope: str, output: Path) -> int:inventory.seek(0)rows: list[bytes] = []for path in inventory.read().split(b"\0"):- if not path:+ if not path or not SOURCE_METADATA_DIRECTORIES.isdisjoint(os.fsdecode(path).split("/")):continueif b"\n" in path or b"\r" in path:raise InventoryError(diff --git a/plugins/codex-security/scripts/generate_rank_input.py b/plugins/codex-security/scripts/generate_rank_input.pyindex 8d834cebf..ae7f90e5c 100644--- a/plugins/codex-security/scripts/generate_rank_input.py+++ b/plugins/codex-security/scripts/generate_rank_input.py@@ -31,7 +31,12 @@ from rank_preview import (preview_for,preview_for_bytes,)-from workbench_target import git_blob_samples, git_command, git_directory_snapshot_paths+from workbench_target import (+ SOURCE_METADATA_DIRECTORIES,+ git_blob_samples,+ git_command,+ git_directory_snapshot_paths,+)JsonRow = dict[str, object]@@ -155,11 +160,11 @@ def scope_candidates(repo: Path, scope_path: Path) -> Iterable[Path]:"--hidden","--no-require-git","--null",- # Also exclude descendants when the scope starts inside .git.+ # Also exclude descendants when the scope starts inside metadata."--glob",- "!**/.git",+ "!**/{.git,.svn}","--glob",- "!**/.git/**",+ "!**/{.git,.svn}/**","--",str(scope_path.relative_to(repo)),]@@ -207,7 +212,7 @@ def make_repo_scope_input(args: argparse.Namespace) -> None:relative = path.resolve(strict=True).relative_to(repo)except (OSError, ValueError):continue- if ".git" in relative.parts:+ if not SOURCE_METADATA_DIRECTORIES.isdisjoint(relative.parts):continuerows_by_path.setdefault(relative.as_posix(), {"path": relative.as_posix()})@@ -276,7 +281,8 @@ def git_changed_paths(repo: Path, base: str, head: str, mode: str) -> list[tuplereturn sorted((path, status)for path, status in combined.items()- if status == "D" or (not path.is_symlink() and path.is_file())+ if SOURCE_METADATA_DIRECTORIES.isdisjoint(path.relative_to(repo).parts)+ and (status == "D" or (not path.is_symlink() and path.is_file())))raise SystemExit(f"Unknown diff mode: {mode}")diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 30a567c8a..6a0793579 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -24,6 +24,7 @@ from rank_preview import DEFAULT_PREVIEW_READ_BYTES, is_binary_samplefrom windows_scan_local_files import copy_directory_junctionfrom workbench_constants import GIT_REPOSITORY_ENVIRONMENT+SOURCE_METADATA_DIRECTORIES = {".git", ".svn"}_WINDOWS = os.name == "nt"@@ -447,10 +448,16 @@ def worktree_content_digest_for_context() -> str:digest = hashlib.sha256()update_digest_field(digest, b"format", b"codex-security-snapshot/v1")+ source_pathspecs = [+ f":(literal){pathspec}",+ ":(top,glob,exclude)**/.svn",+ ":(top,glob,exclude)**/.svn/**",+ ]tracked_ok = _update_digest_field_from_git(digest,b"tracked-diff",repository,+ "--no-literal-pathspecs","diff","--binary","--full-index",@@ -459,18 +466,19 @@ def worktree_content_digest_for_context("--ignore-submodules=none","HEAD","--",- pathspec,+ *source_pathspecs,git_dir=git_dir,work_tree=work_tree,)untracked = git_bytes(repository,+ "--no-literal-pathspecs","ls-files","--others","--exclude-standard","-z","--",- pathspec,+ *source_pathspecs,git_dir=git_dir,work_tree=work_tree,)@@ -673,6 +681,8 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:nested_worktrees: set[tuple[int, int]] = set()for entry in (entry for entry in listed.split(b"\0") if entry):relative = Path(os.fsdecode(entry[2:]))+ if not SOURCE_METADATA_DIRECTORIES.isdisjoint(relative.parts):+ continueif scope_depth:if len(relative.parts) <= scope_depth:continue@@ -744,6 +754,8 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:while pending:children: list[Path] = []for path in pending:+ if not SOURCE_METADATA_DIRECTORIES.isdisjoint(path.relative_to(target).parts):+ continuetry:path = _directory_link_boundary(target, path, linked_prefixes)metadata = path.lstat()@@ -764,7 +776,7 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:continuecandidates: dict[bytes, Path] = {}for path in children:- if os.path.normcase(path.name) == ".git":+ if os.path.normcase(path.name) in SOURCE_METADATA_DIRECTORIES:continuetry:metadata = path.lstat()@@ -812,7 +824,7 @@ def source_directory_snapshot_paths(parent = Path(directory)for name in directories[:]:path = parent / name- if name == ".git" or path in excluded:+ if name in SOURCE_METADATA_DIRECTORIES or path in excluded:directories.remove(name)continuepaths.append(path)@@ -820,7 +832,9 @@ def source_directory_snapshot_paths(if getattr(path.lstat(), "st_reparse_tag", 0) & 0x20000000:directories.remove(name)paths.extend(- parent / name for name in files if name != ".git" and parent / name not in excluded+ parent / name+ for name in files+ if name not in SOURCE_METADATA_DIRECTORIES and parent / name not in excluded)return sorted(paths)@@ -843,7 +857,7 @@ def directory_content_digest(else git_directory_snapshot_paths(target))if paths is None:- paths = source_directory_snapshot_paths(target)+ paths = source_directory_snapshot_paths(target, excluded)digest = hashlib.sha256()update_digest_field(digest, b"format", b"codex-security-directory/v1")for path in paths:diff --git a/plugins/codex-security/tests/test_generate_in_scope_files.py b/plugins/codex-security/tests/test_generate_in_scope_files.pyindex 1c2d954d3..3ed4f46d3 100644--- a/plugins/codex-security/tests/test_generate_in_scope_files.py+++ b/plugins/codex-security/tests/test_generate_in_scope_files.py@@ -176,6 +176,26 @@ def test_inventory_keeps_ignored_tracked_files_without_ignored_untracked_files(assert "./app/ignored.skip" not in paths[email protected]("scope", [".", ".svn", "vendor/.svn/pristine/base"])+def test_inventory_excludes_svn_metadata_even_when_tracked(tmp_path: Path, scope: str) -> None:+ repository = make_repository(tmp_path)+ write_file(repository, ".svn/wc.db")+ write_file(repository, "vendor/.svn/pristine/base")+ write_file(repository, ".gitignore", b".svn/\n")+ git(repository, "add", "--force", ".svn", "vendor/.svn")+ output = tmp_path / "in_scope_files.txt"++ result = run_inventory(repository, scope, output)++ assert result.returncode == 0, result.stderr+ paths = output.read_text(encoding="utf-8").splitlines()+ assert all(".svn" not in PurePosixPath(path).parts for path in paths)+ if scope == ".":+ assert "./app/routes.py" in paths+ else:+ assert paths == []++@pytest.mark.parametrize(("scope", "absolute"),[diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.pyindex 54dd4e900..3353884f4 100644--- a/plugins/codex-security/tests/test_generate_rank_input.py+++ b/plugins/codex-security/tests/test_generate_rank_input.py@@ -291,15 +291,19 @@ def test_repo_input_preserves_every_requested_directory_file(tmp_path: Path) ->(repo / "src" / "tests").mkdir(parents=True)(repo / "src" / "examples").mkdir()(repo / "src" / "fixtures").mkdir()- (repo / "src" / ".git").mkdir()(repo / "src" / "runtime.py").write_text("runtime = True", encoding="utf-8")(repo / "src" / "tests" / "handler.py").write_text("handler = True", encoding="utf-8")(repo / "src" / "examples" / "demo.py").write_text("demo = True", encoding="utf-8")(repo / "src" / "fixtures" / "payload.txt").write_text("payload", encoding="utf-8")(repo / "src" / "Dockerfile").write_text("FROM scratch", encoding="utf-8")- (repo / "src" / ".git" / "config").write_text("private", encoding="utf-8")+ for metadata in (".git", ".svn"):+ (repo / "src" / metadata).mkdir()+ (repo / "src" / metadata / "config").write_text("metadata", encoding="utf-8")scopes = tmp_path / "target-paths.json"- scopes.write_text(json.dumps(["src", "src/runtime.py", "src/.git/config"]), encoding="utf-8")+ scopes.write_text(+ json.dumps(["src", "src/runtime.py", "src/.git/config", "src/.svn/config"]),+ encoding="utf-8",+ )output = tmp_path / "scoped-source-input.jsonl"run_repo_cli("make-repo-scope-input", repo, output, "--scopes-file", str(scopes))@@ -774,3 +778,62 @@ def test_revision_preview_uses_the_same_read_window_as_local_files(tmp_path: Patrows = read_jsonl(output)assert "first" in rows[0]["preview"]assert "outside_window" not in rows[0]["preview"]++[email protected]("mode", ["staged", "unstaged", "untracked", "deleted", "revisions"])[email protected]("command", ["make-diff-rank-input", "generate-in-scope-files"])+def test_local_diff_inventory_excludes_svn_but_revision_diff_retains_it(+ tmp_path: Path, mode: str, command: str+) -> None:+ repo = tmp_path / "repo"+ repo.mkdir()+ initialize_repo(repo)+ names = [".svn/wc.db", "vendor/.svn/entries", "source.py", "vendor/.svn-source.py"]+ (repo / "base.py").write_text("base\n", encoding="utf-8")+ if mode != "untracked":+ for name in names:+ source = repo / name+ source.parent.mkdir(parents=True, exist_ok=True)+ source.write_text("before\n", encoding="utf-8")+ git(repo, "add", ".")+ git(repo, "commit", "-qm", "base")+ base = git(repo, "rev-parse", "HEAD")+ for name in names:+ source = repo / name+ source.parent.mkdir(parents=True, exist_ok=True)+ if mode == "deleted":+ source.unlink()+ else:+ source.write_text("after\n", encoding="utf-8")+ if mode in {"staged", "revisions"}:+ git(repo, "add", ".")+ if mode == "revisions":+ git(repo, "commit", "-qm", "changed")+ diff_mode = "revisions" if mode == "revisions" else "local-patch"+ output = tmp_path / "inventory"+ if command == "make-diff-rank-input":+ run_repo_cli(command, repo, output, "--base", base, "--mode", diff_mode)+ paths = [row["path"] for row in read_jsonl(output)]+ else:+ subprocess.run(+ [+ sys.executable,+ str(PLUGIN_ROOT / "scripts" / "generate_in_scope_files.py"),+ "--repo",+ str(repo),+ "--scope",+ ".",+ "--out",+ str(output),+ "--diff-base",+ base,+ "--diff-mode",+ diff_mode,+ ],+ check=True,+ capture_output=True,+ text=True,+ )+ paths = output.read_text(encoding="utf-8").splitlines()+ expected = names if mode == "revisions" else names[2:]+ assert sorted(paths) == sorted(expected)diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.pyindex 69ab4410a..bd54c73a1 100644--- a/plugins/codex-security/tests/test_workbench_target.py+++ b/plugins/codex-security/tests/test_workbench_target.py@@ -670,6 +670,82 @@ def initialize_unborn_git_repository(target: Path) -> None:subprocess.run(["git", "init", "-q"], cwd=target, check=True)[email protected]("git", [False, True])+def test_directory_snapshots_exclude_svn_metadata(tmp_path: Path, git: bool) -> None:+ target = tmp_path / "target"+ if git:+ initialize_unborn_git_repository(target)+ else:+ target.mkdir()+ source = target / "app.py"+ source.write_text("source\n")+ metadata = [target / ".svn" / "wc.db", target / "nested" / ".svn" / "pristine" / "base"]+ for path in metadata:+ path.parent.mkdir(parents=True)+ path.write_text("metadata\n")+ if git:+ subprocess.run(["git", "add", "--force", "."], cwd=target, check=True)++ original = directory_content_digest(target)+ count_files = WORKBENCH_TARGET["directory_snapshot_regular_file_count"]+ assert count_files(target) == 1+ for path in metadata:+ path.write_text("metadata changed\n")+ assert directory_content_digest(target) == original+ assert count_files(target) == 1+ source.write_text("source changed\n")+ assert directory_content_digest(target) != original++[email protected]("tracked", [False, True])[email protected]("scope", [".", "nested[scope]"])+def test_worktree_snapshots_exclude_svn_metadata(tmp_path: Path, tracked: bool, scope: str) -> None:+ repository = tmp_path / "repository"+ initialize_git_repository(repository)+ target = repository / scope+ target.mkdir(exist_ok=True)+ source = target / ".svn-backup" / "app.py"+ source.parent.mkdir()+ source.write_text("source\n")+ metadata = [target / ".svn" / "wc.db", target / "nested" / ".svn" / "entries"]+ for path in metadata:+ path.parent.mkdir(parents=True)+ path.write_text("metadata\n")+ subprocess.run(["git", "add", str(source)], cwd=repository, check=True)+ if tracked:+ subprocess.run(["git", "add", "."], cwd=repository, check=True)+ subprocess.run(+ ["git", "commit", "-qm", "Synthetic source and metadata"], cwd=repository, check=True+ )++ original = worktree_content_digest(target)+ for path in metadata:+ path.write_text("updated metadata\n")+ assert worktree_content_digest(target) == original+ source.write_text("updated source\n")+ assert worktree_content_digest(target) != original++[email protected]("scope", [".svn", "nested/.svn"])+def test_git_directory_snapshots_exclude_svn_junctions(+ tmp_path: Path, junction_factory: Callable[[Path, Path], None], scope: str+) -> None:+ import workbench_target++ target = tmp_path / "target"+ initialize_unborn_git_repository(target)+ source = target / "app.py"+ source.write_text("source\n")+ metadata = target / scope+ metadata.mkdir(parents=True)+ outside = tmp_path / "outside"+ outside.mkdir()+ (outside / "state").write_text("metadata\n")+ junction_factory(metadata / "linked", outside)++ assert workbench_target.git_directory_snapshot_paths(target) == [source]++def test_windows_inventory_batches_wide_untracked_trees(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:@@ -1233,7 +1309,7 @@ def test_worktree_content_digest_streams_tracked_binary_patch(def require_streamed_diff(repository: Path, *args: str, **kwargs: object) -> subprocess.CompletedProcess[Any]:- if args and args[0] == "diff":+ if "diff" in args:assert kwargs.get("stdout_file") is not Nonereturn git_command(repository, *args, **kwargs)diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex fad14a251..eca87ee42 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -394,9 +394,12 @@ codex-security scan . --working-treecodex-security scan . --output-dir /path/outside/repository/results --dry-run```+Whole-codebase and path scans also accept ordinary directories, including SVN+working copies. Their source inventories exclude `.git` and `.svn` metadata.+`--diff` scans committed changes; `--working-tree` scans staged and unstaged-changes. Deep scans support whole repositories and path scopes. Working-tree-snapshots include untracked nested Git repositories. Initialized submodules must+changes. Both require Git. Deep scans support whole repositories and path scopes.+Working-tree snapshots include untracked nested Git repositories. Initialized submodules mustbe clean and at the commit recorded by the parent.Repeat `--knowledge-base PATH` for context: UTF-8 text files (including JSON and@@ -1808,6 +1811,9 @@ validation, and `patch` still fixes and verifies findings. Both commands acceptor literal text and work in the current directory. Saved finding/occurrence IDsselect their original repository for patching.+Basic patching also works in ordinary directories and Git worktrees before their+first commit. Directory change tracking excludes `.git` and `.svn` metadata.+```bashcodex-security validate "Possible SQL injection" --effort highcodex-security validate findings.json --knowledge-base ./context --context "Check the tenant boundary."diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex c9bc7a736..4017ce8d4 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1189,7 +1189,11 @@ interface CliDependencies {command: "git" | "gh" | "glab",args: readonly string[],repository: string,- options?: { trim?: boolean; environment?: NodeJS.ProcessEnv },+ options?: {+ trim?: boolean;+ environment?: NodeJS.ProcessEnv;+ input?: string;+ },): Promise<string>;assessPatchRisk?: (request: PatchRiskRequest) => Promise<PatchRiskReport>;bulkScan?: BulkScanDiscoveryDependencies;@@ -1209,6 +1213,32 @@ interface CliDependencies {checkForUpdate(signal: AbortSignal): Promise<UpdateNotice | undefined>;}+export const runRepositoryCommand: CliDependencies["runRepositoryCommand"] =+ async (command, args, repository, options) => {+ const executable = await resolveTrustedExecutable(+ command,+ process.env,+ repository,+ );+ if (executable === null) {+ throw new CodexSecurityError(+ `${command} is not available on a trusted PATH.`,+ );+ }+ const result = execFile(executable.executable, [...args], {+ cwd: repository,+ env: { ...executable.environment, ...options?.environment },+ windowsHide: true,+ maxBuffer: Infinity,+ });+ if (options?.input !== undefined) {+ result.child.stdin?.on("error", () => {});+ result.child.stdin?.end(options.input);+ }+ const { stdout } = await result;+ return options?.trim === false ? stdout : stdout.trim();+ };+const DEFAULT_DEPENDENCIES: CliDependencies = {createSecurity: (config) =>new CodexSecurity(config, undefined, { surface: "cli" }),@@ -1269,24 +1299,7 @@ const DEFAULT_DEPENDENCIES: CliDependencies = {environment,input,),- runRepositoryCommand: async (command, args, repository, options) => {- const executable = await resolveTrustedExecutable(- command,- process.env,- repository,- );- if (executable === null) {- throw new CodexSecurityError(- `${command} is not available on a trusted PATH.`,- );- }- const { stdout } = await execFile(executable.executable, [...args], {- cwd: repository,- env: { ...executable.environment, ...options?.environment },- windowsHide: true,- });- return options?.trim === false ? stdout : stdout.trim();- },+ runRepositoryCommand,exportFindings: runArtifactExport,runWorkbench: async (args, input, signal, pythonPath, protectedRoot) => {const environment = {@@ -7304,7 +7317,7 @@ async function snapshotPatchDirectory(for (const entry of await readdir(join(repository, directory), {withFileTypes: true,})) {- if (entry.name === ".git") continue;+ if (entry.name === ".git" || entry.name === ".svn") continue;const path = directory ? `${directory}/${entry.name}` : entry.name;const absolute = join(repository, path);if (entry.isDirectory()) {@@ -7477,14 +7490,58 @@ async function snapshotPatchTree(dependencies: CliDependencies,): Promise<string> {const root = await mkdtemp(join(tmpdir(), "codex-security-patch-tree-"));- const environment = { GIT_INDEX_FILE: join(root, "index") };+ const environment = {+ GIT_INDEX_FILE: join(root, "index"),+ GIT_LITERAL_PATHSPECS: "0",+ };+ const sourcePathspecs = [+ "--",+ ":/",+ ":(top,glob,exclude)**/.svn",+ ":(top,glob,exclude)**/.svn/**",+ ];const run = (args: string[]) =>dependencies.runRepositoryCommand("git", args, repository, {environment,});try {- await run(["read-tree", "HEAD"]);- await run(["--literal-pathspecs", "add", "--all"]);+ let hasHead = true;+ try {+ await run(["rev-parse", "--verify", "--quiet", "HEAD"]);+ } catch (error) {+ if (!isJsonObject(error) || error["code"] !== 1) throw error;+ // An unborn symbolic HEAD has no commit; malformed refs still fail.+ await run(["symbolic-ref", "--quiet", "HEAD"]);+ hasHead = false;+ }+ if (hasHead) {+ await run(["read-tree", "HEAD"]);+ } else {+ // Preserve tracked paths, including ignored and intent-to-add files, without+ // copying cached stat data or flags that can hide working-tree changes.+ // Git's quoted format preserves raw filename bytes through string transport.+ const entries = await dependencies.runRepositoryCommand(+ "git",+ [+ "-c",+ "core.quotePath=true",+ "ls-files",+ "--stage",+ "--full-name",+ ...sourcePathspecs,+ ],+ repository,+ { trim: false, environment: { GIT_LITERAL_PATHSPECS: "0" } },+ );+ await run(["read-tree", "--empty"]);+ await dependencies.runRepositoryCommand(+ "git",+ ["-c", "core.ignorestat=false", "update-index", "--index-info"],+ repository,+ { environment, input: entries },+ );+ }+ await run(["add", "--all", ...sourcePathspecs]);return await run(["write-tree"]);} finally {await rm(root, { recursive: true, force: true });diff --git a/sdk/typescript/tests-ts/cli-patch-results.test.ts b/sdk/typescript/tests-ts/cli-patch-results.test.tsindex 0be923c65..4e85b18d1 100644--- a/sdk/typescript/tests-ts/cli-patch-results.test.ts+++ b/sdk/typescript/tests-ts/cli-patch-results.test.ts@@ -1,30 +1,31 @@-import { execFile } from "node:child_process";-import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";+import {+ chmod,+ mkdir,+ mkdtemp,+ readFile,+ rm,+ utimes,+ writeFile,+} from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";-import { promisify } from "node:util";import { describe, expect, test } from "bun:test";-import { main, runCodexSkillCommand } from "../src/cli.js";+import {+ main,+ runCodexSkillCommand,+ runRepositoryCommand,+} from "../src/cli.js";import type { Finding } from "../src/models.js";import { dependencies } from "./cli-fixtures.js";import { createCliTest } from "./support/cli-run.js";type FixtureOptions = NonNullable<Parameters<typeof dependencies>[0]>;-const execFileAsync = promisify(execFile);-async function repositoryFixture({ initializeGit = true } = {}) {+async function repositoryFixture({+ initializeGit = true,+ initialCommit = true,+} = {}) {const directory = await mkdtemp(join(tmpdir(), "patch-results-"));- const runRepositoryCommand: NonNullable<- FixtureOptions["onRepositoryCommand"]- > = async (command, args, cwd, options) => {- const { stdout } = await execFileAsync(command, [...args], {- cwd,- encoding: "utf8",- env: { ...process.env, ...options?.environment },- windowsHide: true,- });- return options?.trim === false ? stdout : stdout.trim();- };const git = (...args: string[]) =>runRepositoryCommand("git", args, directory);try {@@ -34,7 +35,7 @@ async function repositoryFixture({ initializeGit = true } = {}) {await git("config", "user.name", "Synthetic User");await git("config", "user.email", "[email protected]");await git("add", ".");- await git("commit", "-m", "Synthetic fixture");+ if (initialCommit) await git("commit", "-m", "Synthetic fixture");}} catch (error) {await rm(directory, { recursive: true, force: true });@@ -176,12 +177,39 @@ lines.on("line", (line) => {expect(await fixture.git("write-tree")).toBe(index);});- test.each([false, true])(- "checks patch changes outside a Git repository: %j",- async (apply) => {- await using fixture = await repositoryFixture({ initializeGit: false });+ test.each([+ ["directory", false],+ ["directory", true],+ ["unborn Git", false],+ ["unborn Git", true],+ ["committed Git", false],+ ["committed Git", true],+ ] as const)(+ "checks patch changes in %s with source edits: %j",+ async (kind, apply) => {+ const initializeGit = kind !== "directory";+ await using fixture = await repositoryFixture({+ initializeGit,+ initialCommit: kind === "committed Git",+ });+ if (initializeGit) {+ await writeFile(join(fixture.directory, ".gitignore"), "dist/\n");+ await mkdir(join(fixture.directory, "dist"));+ await writeFile(join(fixture.directory, "staged.ts"), "staged\n");+ await fixture.git("add", ".gitignore", "staged.ts");+ }+ const indexPath = join(fixture.directory, ".git", "index");+ const index = initializeGit ? await readFile(indexPath) : null;+ const metadata = join(fixture.directory, initializeGit ? ".git" : ".svn");+ await mkdir(metadata, { recursive: true });const outcome = await fixture.patch(["Synthetic issue"], {onCodex: async () => {+ await writeFile(join(metadata, "synthetic-state"), "updated\n");+ if (initializeGit)+ await writeFile(+ join(fixture.directory, "dist", "test-output.log"),+ "test ran\n",+ );if (apply)await writeFile(join(fixture.directory, "app.ts"), "fixed\n");return 0;@@ -191,7 +219,9 @@ lines.on("line", (line) => {expect(outcome.result).toMatchObject({applied: apply,filesChanged: apply ? 1 : 0,+ files: apply ? ["app.ts"] : [],});+ if (index !== null) expect(await readFile(indexPath)).toEqual(index);if (!apply)expect(outcome.result.error).toMatchObject({code: "NO_PATCH_APPLIED",@@ -199,6 +229,349 @@ lines.on("line", (line) => {},);+ test.each([+ [false, false, false],+ [false, false, true],+ [false, true, false],+ [false, true, true],+ [true, false, false],+ [true, false, true],+ [true, true, false],+ [true, true, true],+ ])(+ "ignores SVN metadata in Git with commits: %j, tracked metadata: %j and source edits: %j",+ async (committed, tracked, apply) => {+ await using fixture = await repositoryFixture({+ initialCommit: committed,+ });+ const metadata = [".svn", "nested/.svn"];+ for (const directory of metadata) {+ await mkdir(join(fixture.directory, directory), { recursive: true });+ await writeFile(+ join(fixture.directory, directory, "wc.db"),+ "original metadata\n",+ );+ }+ const source = join(fixture.directory, "nested", ".svn-backup", "app.ts");+ await mkdir(join(fixture.directory, "nested", ".svn-backup"));+ await writeFile(source, "original source\n");+ if (tracked) await fixture.git("add", ".");+ if (committed) {+ await fixture.git("add", "nested/.svn-backup/app.ts");+ await fixture.git("commit", "-m", "Synthetic source and metadata");+ }+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ currentDirectory: join(fixture.directory, "nested"),+ onRepositoryCommand: (command, args, repository, options) =>+ runRepositoryCommand(command, args, repository, {+ ...options,+ environment: {+ GIT_LITERAL_PATHSPECS: "1",+ ...options?.environment,+ },+ }),+ onCodex: async () => {+ for (const directory of metadata)+ await writeFile(+ join(fixture.directory, directory, "wc.db"),+ "updated metadata\n",+ );+ if (apply) await writeFile(source, "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(apply ? 0 : 2);+ expect(outcome.result).toMatchObject({+ applied: apply,+ filesChanged: apply ? 1 : 0,+ files: apply ? ["nested/.svn-backup/app.ts"] : [],+ });+ if (!apply)+ expect(outcome.result.error).toMatchObject({+ code: "NO_PATCH_APPLIED",+ });+ expect(await readFile(indexPath)).toEqual(index);+ for (const directory of metadata)+ expect(+ await readFile(join(fixture.directory, directory, "wc.db"), "utf8"),+ ).toBe("updated metadata\n");+ },+ );++ test+ .skipIf(process.platform === "win32" || process.getuid?.() === 0)+ .each([false, true])(+ "patches readable source with unreadable SVN metadata, tracked: %j",+ async (tracked) => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ const metadata = [".svn", "nested/.svn"].map((directory) =>+ join(fixture.directory, directory, "wc.db"),+ );+ for (const directory of [".svn", "nested/.svn"])+ await mkdir(join(fixture.directory, directory), { recursive: true });+ for (const file of metadata) await writeFile(file, "metadata\n");+ if (tracked) await fixture.git("add", ".");+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);+ for (const file of metadata) await chmod(file, 0);+ try {+ for (const file of metadata)+ await expect(readFile(file)).rejects.toMatchObject({+ code: "EACCES",+ });+ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ await writeFile(join(fixture.directory, "app.ts"), "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(0);+ expect(outcome.result).toMatchObject({+ applied: true,+ filesChanged: 1,+ files: ["app.ts"],+ });+ expect(await readFile(indexPath)).toEqual(index);+ expect(await readFile(join(fixture.directory, "app.ts"), "utf8")).toBe(+ "fixed\n",+ );+ } finally {+ for (const file of metadata) await chmod(file, 0o600);+ }+ },+ );++ test.each([+ [false, undefined],+ [true, undefined],+ [true, "assume-unchanged"],+ [true, "skip-worktree"],+ ] as const)(+ "tracks force-staged ignored files in unborn Git with intent-to-add: %j and flag: %s",+ async (intentToAdd, flag) => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ await writeFile(+ join(fixture.directory, ".gitignore"),+ "ignored file.ts\n",+ );+ const source = join(fixture.directory, "ignored file.ts");+ await writeFile(source, "original\n");+ await fixture.git(+ "add",+ "--force",+ ...(intentToAdd ? ["--intent-to-add"] : []),+ "ignored file.ts",+ );+ if (flag)+ await fixture.git("update-index", `--${flag}`, "ignored file.ts");+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ await writeFile(source, "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(0);+ expect(outcome.result).toMatchObject({+ applied: true,+ filesChanged: 1,+ files: ["ignored file.ts"],+ });+ expect(await readFile(indexPath)).toEqual(index);+ },+ );++ test("tracks ignored files with an inherited literal-pathspec environment", async () => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ await writeFile(join(fixture.directory, ".gitignore"), "app.ts\n");+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ onRepositoryCommand: (command, args, repository, options) =>+ runRepositoryCommand(command, args, repository, {+ ...options,+ environment: {+ GIT_LITERAL_PATHSPECS: "1",+ ...options?.environment,+ },+ }),+ onCodex: async () => {+ await writeFile(join(fixture.directory, "app.ts"), "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(0);+ expect(outcome.result).toMatchObject({+ applied: true,+ filesChanged: 1,+ files: ["app.ts"],+ });+ expect(await readFile(indexPath)).toEqual(index);+ });++ test.skipIf(process.platform === "win32")(+ "tracks ignored filenames with raw bytes and Git quoting characters",+ async () => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ await fixture.git("config", "core.quotePath", "false");+ await writeFile(join(fixture.directory, ".gitignore"), "ignored*\n");+ const source = Buffer.concat([+ Buffer.from(join(fixture.directory, "ignored-")),+ process.platform === "darwin" ? Buffer.from("é") : Buffer.from([0xff]),+ Buffer.from('\n"\\\t\r.ts'),+ ]);+ await writeFile(source, "original\n");+ await fixture.git("add", "--force", ".");+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ await writeFile(source, "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(0);+ expect(outcome.result).toMatchObject({ applied: true, filesChanged: 1 });+ expect(await readFile(source, "utf8")).toBe("fixed\n");+ expect(await readFile(indexPath)).toEqual(index);+ },+ );++ test.each([+ ["assume-unchanged", "edit"],+ ["assume-unchanged", "delete"],+ ["assume-unchanged", "none"],+ ["skip-worktree", "edit"],+ ["skip-worktree", "delete"],+ ["skip-worktree", "none"],+ ["core.ignorestat", "edit"],+ ["core.ignorestat", "delete"],+ ["core.ignorestat", "none"],+ ] as const)(+ "checks unborn Git patches despite %s with change: %s",+ async (flag, change) => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ if (flag === "core.ignorestat") await fixture.git("config", flag, "true");+ else await fixture.git("update-index", `--${flag}`, "app.ts");+ const indexPath = join(fixture.directory, ".git", "index");+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ const source = join(fixture.directory, "app.ts");+ if (change === "delete") await rm(source);+ if (change === "edit") await writeFile(source, "fixed\n");+ return 0;+ },+ });++ const applied = change !== "none";+ expect(outcome.status, outcome.stderr).toBe(applied ? 0 : 2);+ expect(outcome.result).toMatchObject({+ applied,+ filesChanged: applied ? 1 : 0,+ files: applied ? ["app.ts"] : [],+ });+ expect(await readFile(indexPath)).toEqual(index);+ },+ );++ test("detects same-size changes with racy unborn Git index timestamps", async () => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ await fixture.git("config", "core.checkStat", "minimal");+ await fixture.git("config", "core.trustctime", "false");+ const source = join(fixture.directory, "app.ts");+ const indexPath = join(fixture.directory, ".git", "index");+ const timestamp = new Date("2000-01-01T00:00:00Z");+ await utimes(source, timestamp, timestamp);+ await fixture.git("add", "app.ts");+ await utimes(indexPath, timestamp, timestamp);+ await writeFile(source, "unsafe!!\n");+ await utimes(source, timestamp, timestamp);+ const index = await readFile(indexPath);++ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ await writeFile(source, "original\n");+ await utimes(source, timestamp, timestamp);+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(0);+ expect(outcome.result).toMatchObject({+ applied: true,+ filesChanged: 1,+ files: ["app.ts"],+ });+ expect(await readFile(indexPath)).toEqual(index);+ });++ test.each([false, true])(+ "checks unborn Git without an index with source edits: %j",+ async (apply) => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ const indexPath = join(fixture.directory, ".git", "index");+ await rm(indexPath);+ await writeFile(join(fixture.directory, ".gitignore"), "dist/\n");+ await mkdir(join(fixture.directory, "dist"));++ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: async () => {+ await writeFile(+ join(fixture.directory, "dist", "test-output.log"),+ "test ran\n",+ );+ if (apply)+ await writeFile(join(fixture.directory, "app.ts"), "fixed\n");+ return 0;+ },+ });++ expect(outcome.status, outcome.stderr).toBe(apply ? 0 : 2);+ expect(outcome.result).toMatchObject({+ applied: apply,+ filesChanged: apply ? 1 : 0,+ files: apply ? ["app.ts"] : [],+ });+ await expect(readFile(indexPath)).rejects.toMatchObject({+ code: "ENOENT",+ });+ },+ );++ test.each([+ ["missing commit", `${"1".repeat(40)}\n`],+ ["malformed ref", "broken\n"],+ ])("does not patch a Git checkout with a %s", async (_kind, ref) => {+ await using fixture = await repositoryFixture({ initialCommit: false });+ await writeFile(+ join(fixture.directory, ".git", "refs", "heads", "main"),+ ref,+ );+ let started = false;+ const outcome = await fixture.patch(["Synthetic issue"], {+ onCodex: () => {+ started = true;+ return 0;+ },+ });+ expect(outcome.status).toBe(2);+ expect(started).toBe(false);+ });+test("rejects a verified saved-finding result when no files change", async () => {await using fixture = await repositoryFixture();const finding = {