diff --git a/scripts/check-comment-citations.mjs b/scripts/check-comment-citations.mjs index 73acf94315..9c8c45cc2d 100644 --- a/scripts/check-comment-citations.mjs +++ b/scripts/check-comment-citations.mjs @@ -885,8 +885,19 @@ export function checkComments(root, files, scope = { kind: "all" }) { return { failures: [...failures, ...blockFailures], warnings, examined }; } +function requireCheckAttrSource(root) { + const version = execFileSync("git", ["version"], { cwd: root, encoding: "utf8" }).trim(); + const [major, minor] = (version.match(/(\d+)\.(\d+)/) ?? []).slice(1).map(Number); + if (major < 2 || (major === 2 && minor < 40)) { + throw new Error( + `The comment check reads attributes at a ref with \`git check-attr --source\`, which needs git 2.40 or newer; found "${version}".`, + ); + } +} + export function generatedPaths(root, ref, paths) { if (paths.length === 0) return new Set(); + requireCheckAttrSource(root); const attributes = execFileSync( "git", ["check-attr", `--source=${ref}`, "-z", "--stdin", "linguist-generated"], diff --git a/scripts/check-comment-citations.test.mjs b/scripts/check-comment-citations.test.mjs index dd9695259a..a18c91edf0 100644 --- a/scripts/check-comment-citations.test.mjs +++ b/scripts/check-comment-citations.test.mjs @@ -9,7 +9,7 @@ import path from "node:path"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; -import { PROBE, checkComments } from "./check-comment-citations.mjs"; +import { PROBE, checkComments, generatedPaths } from "./check-comment-citations.mjs"; const CHECKER = fileURLToPath(new URL("./check-comment-citations.mjs", import.meta.url)); @@ -1013,3 +1013,31 @@ test("a TypeScript private field is code, not a standalone comment", (t) => { }); assert.deepEqual(warnings, []); }); + +test("a git too old for check-attr --source is named, not hidden behind EPIPE", (t) => { + const bin = mkdtempSync(path.join(tmpdir(), "old-git-")); + t.after(() => rmSync(bin, { recursive: true, force: true })); + const git = path.join(bin, "git"); + writeFileSync( + git, + [ + "#!/bin/sh", + 'if [ "$1" = version ]; then echo "git version 2.39.5"; exit 0; fi', + 'echo "error: unknown option \\`source=HEAD\'" >&2', + "exit 129", + "", + ].join("\n"), + { mode: 0o755 }, + ); + const savedPath = process.env.PATH; + process.env.PATH = `${bin}${path.delimiter}${savedPath}`; + t.after(() => { + process.env.PATH = savedPath; + }); + const paths = Array.from({ length: 20_000 }, (_, i) => `packages/core/src/file-${i}.ts`); + + assert.throws( + () => generatedPaths(bin, "HEAD", paths), + /needs git 2\.40 or newer; found "git version 2\.39\.5"/, + ); +});