fix(ci): 质量检查 - 处理强制推送后的基准提交缺失

This commit is contained in:
ddcat666
2026-09-26 17:30:17 +08:00
parent 331609e6ab
commit 8346865481
2 changed files with 23 additions and 4 deletions

View File

@@ -12,8 +12,19 @@ export async function checkChangedFormatting({ cwd = process.cwd(), baseSha = pr
return result.stdout;
};
const head = git(["rev-parse", "--verify", `${headSha}^{commit}`]).trim();
// 新分支/tag 的 before 为全零;首次提交没有父提交,改为检查整棵树。
const base = baseSha && !/^0+$/.test(baseSha) ? git(["rev-parse", "--verify", `${baseSha}^{commit}`]).trim() : git(["rev-list", "--parents", "-n", "1", head]).trim().split(" ")[1];
const parent = git(["rev-list", "--parents", "-n", "1", head]).trim().split(" ")[1] || null;
let base = parent;
if (baseSha && !/^0+$/.test(baseSha)) {
// 强制改写提交历史后,push 事件里的 before 可能已不在 checkout 中;
// 对合法但失效的 SHA 退回当前父提交,仍校验本次提交的完整改动。
if (!/^[0-9a-f]{40}$/i.test(baseSha)) throw new Error(`Invalid base SHA: ${baseSha}`);
const resolved = spawnSync("git", ["rev-parse", "--verify", `${baseSha}^{commit}`], { cwd, encoding: "utf8" });
if (resolved.status === 0) {
base = resolved.stdout.trim();
} else {
log(`Base SHA ${baseSha} is unavailable; checking against the head parent instead.`);
}
}
const files = (base ? git(["diff", "--relative", "--name-only", "--diff-filter=ACMR", "-z", base, head, "--", "."]) : git(["ls-tree", "-r", "--name-only", "-z", head, "--", "."]))
.split("\0")
.filter((file) => /\.(css|html|json|js|jsx|md|mdx|mjs|cjs|ts|tsx|yaml|yml)$/.test(file));

View File

@@ -71,9 +71,17 @@ describe("changed-file formatting gate", () => {
expect(repo.messages).toEqual([]);
});
test("invalid explicit base SHA fails closed", async () => {
test("unavailable explicit base SHA falls back to the head parent", async () => {
const repo = fixture();
await expect(repo.check({ baseSha: "f".repeat(40) })).rejects.toThrow("git rev-parse");
repo.write("changed.ts", "const value=1;\n");
repo.commit();
await expect(repo.check({ baseSha: "f".repeat(40) })).rejects.toThrow("changed.ts");
expect(repo.messages).toContain(`Base SHA ${"f".repeat(40)} is unavailable; checking against the head parent instead.`);
});
test("malformed explicit base SHA fails closed", async () => {
const repo = fixture();
await expect(repo.check({ baseSha: "not-a-sha" })).rejects.toThrow("Invalid base SHA");
});
test("zero before SHA compares the parent commit", async () => {