Skip to content

Commit f28bc80

Browse files
feat(scripts): fail a branch that deletes files main still ships (#3150)
* feat(scripts): fail a branch that deletes files main still ships Written after a scare that turned out to be a measurement error, and the error is the reason it exists. Comparing tip to tip on a branch a month behind reports every file main has added since the merge base as a deletion: 1,284 of them, an entire skills tree among them, none of it real. A merge keeps mains side and a pull request shows the three-dot diff, which reported zero. So the gate uses the three-dot form and reports renames separately, because in a name-only diff a rename is indistinguishable from a deletion and treating them alike would either mask real loss or block every legitimate move. * ci: enforce the no-deletions guard
1 parent 79dff20 commit f28bc80

4 files changed

Lines changed: 139 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ jobs:
4444
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
4545
with:
4646
fetch-depth: 0
47+
- name: Reject accidental file deletions
48+
if: github.event_name == 'pull_request'
49+
run: node scripts/check-no-main-deletions.mjs --base origin/main
4750
- uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4
4851
id: filter
4952
with:

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"player:perf": "bun run --filter @hyperframes/player perf",
4949
"format:check": "oxfmt --check .",
5050
"knip": "knip",
51-
"test:scripts": "node --import tsx --test scripts/check-tracked-artifacts.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/publish-workflow.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/lint-skills.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs",
51+
"test:scripts": "node --import tsx --test scripts/check-tracked-artifacts.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/publish-workflow.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/lint-skills.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs",
5252
"typecheck:scripts": "tsc --noEmit -p scripts/tsconfig.json",
5353
"test:skills": "node --test 'skills/**/*.test.mjs'",
5454
"generate:previews": "tsx scripts/generate-template-previews.ts",
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Fail if this branch would delete files that exist on the base.
4+
*
5+
* Written after a scare that turned out to be a measurement error, which is
6+
* the reason it uses the three-dot form. Comparing tip to tip (two dots) on a
7+
* branch that is a month behind reports every file main added since the merge
8+
* base as a deletion: 1,284 of them, including an entire skills tree. None of
9+
* that is real. A merge keeps main's side, and a pull request shows the
10+
* three-dot diff, which reported zero.
11+
*
12+
* So this exists to catch deletions a branch genuinely proposes, and to make
13+
* the distinction hard to get wrong again. If it ever disagrees with a manual
14+
* git diff, check which form the manual one used before believing it.
15+
*
16+
* Renames are reported separately. In a name-only diff a rename is
17+
* indistinguishable from a deletion, so treating them alike would either mask
18+
* real loss or block every legitimate move.
19+
*
20+
* node scripts/check-no-main-deletions.mjs [--base origin/main]
21+
*/
22+
23+
import { execFileSync } from "node:child_process";
24+
25+
const BASE_FLAG = "--base";
26+
27+
export function parseBase(argv, fallback = "origin/main") {
28+
const index = argv.indexOf(BASE_FLAG);
29+
if (index === -1) return fallback;
30+
const value = argv[index + 1];
31+
if (!value || value.startsWith("-")) {
32+
throw new Error(`${BASE_FLAG} needs a ref, for example ${BASE_FLAG} origin/main`);
33+
}
34+
return value;
35+
}
36+
37+
/**
38+
* Split a `--name-status` diff into deletions and renames.
39+
*
40+
* Git reports a rename as `R<score>\told\tnew`. Reading only the first column
41+
* would file that under "deleted", which is the false alarm this guards
42+
* against being noisy enough to ignore.
43+
*/
44+
// one branch per git status code
45+
// fallow-ignore-next-line complexity
46+
export function classify(nameStatus) {
47+
const deleted = [];
48+
const renamed = [];
49+
for (const line of nameStatus.split("\n")) {
50+
if (!line.trim()) continue;
51+
const [status, ...paths] = line.split("\t");
52+
if (status.startsWith("R")) renamed.push({ from: paths[0], to: paths[1] });
53+
else if (status === "D") deleted.push(paths[0]);
54+
}
55+
return { deleted, renamed };
56+
}
57+
58+
// a script entry point
59+
// fallow-ignore-next-line complexity
60+
function main() {
61+
const base = parseBase(process.argv.slice(2));
62+
let diff;
63+
try {
64+
diff = execFileSync("git", ["diff", "--name-status", "-M", `${base}...HEAD`], {
65+
encoding: "utf8",
66+
});
67+
} catch (error) {
68+
// An unreachable base is not a pass. Reporting "no deletions" because the
69+
// ref was misspelled is the exact failure this exists to prevent.
70+
console.error(`cannot diff against ${base}: ${error.message.trim()}`);
71+
process.exit(2);
72+
}
73+
74+
const { deleted, renamed } = classify(diff);
75+
if (renamed.length > 0) {
76+
console.log(`${renamed.length} renamed (allowed):`);
77+
for (const { from, to } of renamed.slice(0, 10)) console.log(` ${from} -> ${to}`);
78+
if (renamed.length > 10) console.log(` … and ${renamed.length - 10} more`);
79+
}
80+
81+
if (deleted.length === 0) {
82+
console.log(`No files from ${base} are deleted by this branch.`);
83+
return;
84+
}
85+
86+
console.error(`This branch deletes ${deleted.length} files that exist on ${base}:`);
87+
for (const path of deleted.slice(0, 25)) console.error(` ${path}`);
88+
if (deleted.length > 25) console.error(` … and ${deleted.length - 25} more`);
89+
console.error("\nIf a deletion is intended, remove this check for that path deliberately.");
90+
process.exit(1);
91+
}
92+
93+
if (import.meta.url === `file://${process.argv[1]}`) main();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { strict as assert } from "node:assert";
2+
import { test } from "node:test";
3+
4+
import { classify, parseBase } from "./check-no-main-deletions.mjs";
5+
6+
test("a branch that only adds reports nothing", () => {
7+
const { deleted, renamed } = classify("A\tpackages/cli/src/new.ts\nM\tpackages/cli/src/old.ts\n");
8+
assert.deepEqual(deleted, []);
9+
assert.deepEqual(renamed, []);
10+
});
11+
12+
test("a deleted file is named", () => {
13+
const { deleted } = classify("D\t.agents/skills/README.md\nA\tsrc/new.ts\n");
14+
assert.deepEqual(deleted, [".agents/skills/README.md"]);
15+
});
16+
17+
test("a rename is not reported as a deletion", () => {
18+
// The false alarm worth avoiding: in a name-only diff a rename looks exactly
19+
// like loss, and a check that cried wolf on every move would be turned off.
20+
const { deleted, renamed } = classify("R096\tsrc/old/name.ts\tsrc/new/name.ts\n");
21+
assert.deepEqual(deleted, []);
22+
assert.deepEqual(renamed, [{ from: "src/old/name.ts", to: "src/new/name.ts" }]);
23+
});
24+
25+
test("deletions and renames are separated in one diff", () => {
26+
const { deleted, renamed } = classify(
27+
"D\tdocs/gone.md\nR100\ta.ts\tb.ts\nM\tc.ts\nD\tdocs/also-gone.md\n",
28+
);
29+
assert.deepEqual(deleted, ["docs/gone.md", "docs/also-gone.md"]);
30+
assert.equal(renamed.length, 1);
31+
});
32+
33+
test("the base ref defaults, and an explicit one is honoured", () => {
34+
assert.equal(parseBase([]), "origin/main");
35+
assert.equal(parseBase(["--base", "origin/release"]), "origin/release");
36+
});
37+
38+
test("a --base with no value fails rather than silently defaulting", () => {
39+
// Silently falling back would diff against the wrong ref and report a pass.
40+
assert.throws(() => parseBase(["--base"]), /needs a ref/);
41+
assert.throws(() => parseBase(["--base", "--other"]), /needs a ref/);
42+
});

0 commit comments

Comments
 (0)