Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions plugins/codex/scripts/lib/git.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,21 @@ function formatSection(title, body) {

function formatUntrackedFile(cwd, relativePath) {
const absolutePath = path.join(cwd, relativePath);
const stat = fs.statSync(absolutePath);
if (stat.size > MAX_UNTRACKED_BYTES) {
return `### ${relativePath}\n(skipped: ${stat.size} bytes exceeds ${MAX_UNTRACKED_BYTES} byte limit)`;
}
try {
const stat = fs.statSync(absolutePath);
if (stat.size > MAX_UNTRACKED_BYTES) {
return `### ${relativePath}\n(skipped: ${stat.size} bytes exceeds ${MAX_UNTRACKED_BYTES} byte limit)`;
}

const buffer = fs.readFileSync(absolutePath);
if (!isProbablyText(buffer)) {
return `### ${relativePath}\n(skipped: binary file)`;
}
const buffer = fs.readFileSync(absolutePath);
if (!isProbablyText(buffer)) {
return `### ${relativePath}\n(skipped: binary file)`;
}

return [`### ${relativePath}`, "```", buffer.toString("utf8").trimEnd(), "```"].join("\n");
return [`### ${relativePath}`, "```", buffer.toString("utf8").trimEnd(), "```"].join("\n");
} catch {
return `### ${relativePath}\n(skipped: broken symlink or unreadable file)`;
}
}

function collectWorkingTreeContext(cwd, state) {
Expand Down
16 changes: 16 additions & 0 deletions tests/git.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ test("resolveReviewTarget requires an explicit base when no default branch can b
/Unable to detect the repository default branch\. Pass --base <ref> or use --scope working-tree\./
);
});

test("collectReviewContext skips broken untracked symlinks instead of crashing", () => {
const cwd = makeTempDir();
initGitRepo(cwd);
fs.writeFileSync(path.join(cwd, "app.js"), "console.log('v1');\n");
run("git", ["add", "app.js"], { cwd });
run("git", ["commit", "-m", "init"], { cwd });
fs.symlinkSync("missing-target", path.join(cwd, "broken-link"));

const target = resolveReviewTarget(cwd, {});
const context = collectReviewContext(cwd, target);

assert.equal(target.mode, "working-tree");
assert.match(context.content, /### broken-link/);
assert.match(context.content, /skipped: broken symlink or unreadable file/i);
});