Only add version to .git-blame-ignore-revs when files changed (#16352)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-06-05 17:01:19 +08:00
1 parent ab65eb40cb
commit 1c439735fd
3 files changed
+68 -67

No files matched your search

@@ -0,0 +1,49 @@
#!/usr/bin/env node
/*
Clean up `.git-blame-ignore-revs`
Remove revs without actual file change
*/
import fs from "node:fs/promises";
import { execa } from "execa";
const FILE = new URL("../.git-blame-ignore-revs", import.meta.url);
const content = await fs.readFile(FILE, "utf8");
const lines = content.split("\n");
const revsToRemove = new Set();
const IGNORED_FILES = new Set(["package.json", "yarn.lock"]);
async function getChangedFiles(rev) {
const { stdout } = await execa("git", [
"show",
"--name-only",
"--pretty=",
rev,
]);
const files = stdout.split("\n");
return files;
}
for (const line of lines) {
if (!line || line.startsWith("#")) {
continue;
}
const rev = line;
const changedFiles = await getChangedFiles(rev);
if (changedFiles.every((file) => IGNORED_FILES.has(file))) {
revsToRemove.add(rev);
}
}
if (revsToRemove.size > 0) {
const updated = lines.filter((line) => !revsToRemove.has(line)).join("\n");
await fs.writeFile(FILE, updated);
}
+19 -7
View File
@@ -9,17 +9,29 @@ async function format() {
await runYarn(["lint:prettier", "--write"]);
}
// Only add commit to `.git-blame-ignore-revs` when files except `package.json` and `yarn.lock` changed
const IGNORED_FILES = new Set(["package.json", "yarn.lock"]);
async function shouldAddToGitBlameIgnoreRevsFile() {
const { stdout } = await runGit(["diff", "--name-only"]);
const changedFiles = stdout.split("\n");
return changedFiles.some((file) => !IGNORED_FILES.has(file));
}
async function commit({ version, repo }) {
const filesChanged = shouldAddToGitBlameIgnoreRevsFile();
await runGit(["commit", "-am", `Bump Prettier dependency to ${version}`]);
// Add rev to `.git-blame-ignore-revs` file
const file = ".git-blame-ignore-revs";
const mark = "# Prettier bump after release";
const { stdout: rev } = await runGit(["rev-parse", "HEAD"]);
let text = fs.readFileSync(file, "utf8");
text = text.replace(mark, `${mark}\n# ${version}\n${rev}`);
fs.writeFileSync(file, text);
await runGit(["commit", "-am", `Git blame ignore ${version}`]);
if (filesChanged) {
const file = ".git-blame-ignore-revs";
const mark = "# Prettier bump after release";
const { stdout: rev } = await runGit(["rev-parse", "HEAD"]);
let text = fs.readFileSync(file, "utf8");
text = text.replace(mark, `${mark}\n# ${version}\n${rev}`);
fs.writeFileSync(file, text);
await runGit(["commit", "-am", `Git blame ignore ${version}`]);
}
await runGit(["push", "--repo", repo]);
}