-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidate-atomic-commit.js
More file actions
278 lines (248 loc) · 8.27 KB
/
Copy pathvalidate-atomic-commit.js
File metadata and controls
278 lines (248 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env node
// @ts-check
/**
* validate-atomic-commit.js
*
* Enforces "atomic commit" discipline: one logical task per commit.
*
* Strategy: classify every staged file into a logical CATEGORY based on its
* path, then reject the commit when it spans unrelated categories. Some
* categories are designed to travel together (e.g. a source change plus its
* co-located test and the docs that describe it), so they are grouped into
* compatible "domains". A commit is atomic when all touched categories
* collapse into a single domain.
*
* This script reads the staged file list from git directly; it takes no
* arguments. Exit code 0 = atomic, 1 = rejected, 0 with notice = nothing to
* validate (e.g. merge commit, no staged files).
*
* Escape hatch: set ATOMIC_COMMIT_BYPASS=1 to skip (logged loudly). Intended
* only for mechanical mass changes (formatting sweeps, dependency bumps) that
* legitimately touch many categories. CI re-checks via Danger regardless.
*/
import { execSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
const BYPASS = process.env.ATOMIC_COMMIT_BYPASS === '1';
// This validator enforces *developer* commit discipline. Automated commits made
// by CI tooling (notably the @semantic-release/git plugin, which commits a
// version bump across package.json + CHANGELOG.md) legitimately span multiple
// domains and must never be blocked. Skip when running in CI or when the commit
// message is a semantic-release / automated release commit.
const IS_CI = process.env.CI === 'true' || process.env.CI === '1';
/**
* Ordered classification rules. First match wins, so put the most specific
* patterns first. Each category belongs to exactly one domain.
*
* @type {ReadonlyArray<{ category: string, domain: string, test: (p: string) => boolean }>}
*/
const RULES = [
// --- CI / automation / governance -------------------------------------
{
category: 'ci',
domain: 'infra',
test: (p) => p.startsWith('.github/workflows/')
},
{
category: 'git-hooks',
domain: 'infra',
test: (p) => p.startsWith('.husky/') || p === 'dangerfile.js'
},
{
category: 'automation-scripts',
domain: 'infra',
test: (p) => p.startsWith('scripts/')
},
// --- dependencies ------------------------------------------------------
{
category: 'deps',
domain: 'deps',
test: (p) =>
p === 'package.json' ||
p === 'pnpm-lock.yaml' ||
p === 'package-lock.json' ||
p === 'yarn.lock' ||
p === 'pnpm-workspace.yaml'
},
// --- build / tooling config -------------------------------------------
{
category: 'build-config',
domain: 'infra',
test: (p) =>
/^(vite|vitest|tsconfig|tsconfig\.[\w.]+|postcss|tailwind|eslint|\.prettierrc|\.commitlintrc|\.lintstagedrc|vercel|docker-compose)\b/.test(
p
) ||
p === 'Dockerfile' ||
p === 'nginx.conf' ||
p.endsWith('.config.js') ||
p.endsWith('.config.ts')
},
// --- tests (co-located *.test.* or anything under a tests dir) --------
{
category: 'tests',
domain: 'src',
test: (p) =>
/\.(test|spec)\.[jt]sx?$/.test(p) || /(^|\/)(__tests__|tests?)\//.test(p)
},
// --- documentation -----------------------------------------------------
{
category: 'docs',
domain: 'src',
test: (p) =>
p.endsWith('.md') ||
p.startsWith('docs/') ||
p.startsWith('.github/ISSUE_TEMPLATE/') ||
p === '.github/pull_request_template.md'
},
// --- application source -----------------------------------------------
{
category: 'source',
domain: 'src',
test: (p) => p.startsWith('src/') && /\.[jt]sx?$/.test(p)
},
{
category: 'styles',
domain: 'src',
test: (p) => /\.(css|scss)$/.test(p)
},
{
category: 'assets',
domain: 'src',
test: (p) =>
p.startsWith('public/') || /\.(png|jpe?g|gif|svg|webp|ico)$/.test(p)
},
// --- database / backend schema ----------------------------------------
{
category: 'database',
domain: 'db',
test: (p) => p.startsWith('supabase/')
}
];
const FALLBACK = { category: 'other', domain: 'misc' };
/** @param {string} filePath */
function classify(filePath) {
for (const rule of RULES) {
if (rule.test(filePath)) {
return { category: rule.category, domain: rule.domain };
}
}
return FALLBACK;
}
/** @returns {string[]} staged file paths (added/copied/modified/renamed/deleted) */
function getStagedFiles() {
// -z + NUL split keeps paths with spaces/unicode intact.
const raw = execSync('git diff --cached --name-only --diff-filter=ACMRD -z', {
encoding: 'utf8'
});
return raw.split('\0').filter((p) => p.length > 0);
}
function gitDir() {
return execSync('git rev-parse --git-dir', { encoding: 'utf8' }).trim();
}
function isMidMergeOrRebase() {
try {
const dir = gitDir();
return (
existsSync(`${dir}/MERGE_HEAD`) ||
existsSync(`${dir}/rebase-merge`) ||
existsSync(`${dir}/rebase-apply`)
);
} catch {
return false;
}
}
/**
* Detect an automated release commit (semantic-release / release bots). These
* intentionally bundle a version bump (package.json, pnpm-lock.yaml) with the
* generated CHANGELOG.md and must not be treated as non-atomic.
*
* The prepared commit message is staged at .git/COMMIT_EDITMSG before the
* pre-commit hook runs, so we can read it directly.
*/
function isAutomatedReleaseCommit() {
try {
const msg = readFileSync(`${gitDir()}/COMMIT_EDITMSG`, 'utf8');
// semantic-release uses "chore(release): <version> [skip ci]"; also honor
// the conventional [skip ci] / [ci skip] markers used by release tooling.
return (
/^chore\(release\):/m.test(msg) ||
/\[skip ci\]/i.test(msg) ||
/\[ci skip\]/i.test(msg)
);
} catch {
return false;
}
}
function main() {
// Automated CI commits (semantic-release version bumps, release bots) span
// many domains by design — never block them.
if (IS_CI || isAutomatedReleaseCommit()) {
process.exit(0);
}
// Merges/rebases legitimately combine many categories — never block them.
if (isMidMergeOrRebase()) {
process.exit(0);
}
const files = getStagedFiles();
if (files.length === 0) {
// Nothing staged (or hook fired with an empty index) — let git decide.
process.exit(0);
}
/** @type {Map<string, string[]>} domain -> files */
const byDomain = new Map();
/** @type {Map<string, Set<string>>} domain -> categories */
const categoriesByDomain = new Map();
for (const file of files) {
const { category, domain } = classify(file);
if (!byDomain.has(domain)) {
byDomain.set(domain, []);
categoriesByDomain.set(domain, new Set());
}
byDomain.get(domain)?.push(file);
categoriesByDomain.get(domain)?.add(category);
}
const domains = [...byDomain.keys()];
if (domains.length <= 1) {
// Single logical domain — atomic. Done.
process.exit(0);
}
// More than one domain touched → not atomic.
const lines = [];
lines.push('');
lines.push(' ✖ Non-atomic commit detected.');
lines.push('');
lines.push(
' This commit mixes changes from unrelated areas. Each commit must'
);
lines.push(' represent ONE logical task. Split the staged changes:');
lines.push('');
for (const domain of domains) {
const cats = [...(categoriesByDomain.get(domain) ?? [])].join(', ');
lines.push(` ▸ ${domain} (${cats})`);
for (const file of byDomain.get(domain) ?? []) {
lines.push(` ${file}`);
}
lines.push('');
}
lines.push(' How to split:');
lines.push(' git reset # unstage everything');
lines.push(' git add <files for task A> # stage one domain');
lines.push(' git commit -m "feat: task A"');
lines.push(' git add <files for task B> # stage the next');
lines.push(' git commit -m "docs: task B"');
lines.push('');
lines.push(
' Mechanical mass changes (format sweep, dep bump) may bypass with:'
);
lines.push('ATOMIC_COMMIT_BYPASS=1 git commit ...');
lines.push('');
if (BYPASS) {
process.stderr.write(`${lines.join('\n')}\n`);
process.stderr.write(
'ATOMIC_COMMIT_BYPASS=1 set — proceeding despite the above.\n\n'
);
process.exit(0);
}
process.stderr.write(`${lines.join('\n')}\n`);
process.exit(1);
}
main();