47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
'use strict';
|
||
|
||
const path = require('node:path');
|
||
const { runCommandSafe } = require('./deploy-lib.cjs');
|
||
|
||
const BLOCKED_PATH_PATTERNS = [
|
||
/^config\/config\.local\.js$/,
|
||
/^config\/deploy\.config\.local\.cjs$/,
|
||
/^tmp\/(?!deploy\/)(?!docs\/).+/,
|
||
/^logs\//,
|
||
/^run\//,
|
||
/\.pem$/i,
|
||
/\.key$/i,
|
||
/^\.env(?:\..+)?$/,
|
||
];
|
||
|
||
function normalizePath(filePath) {
|
||
return filePath.replace(/\\/g, '/');
|
||
}
|
||
|
||
function listStagedFiles() {
|
||
const output = runCommandSafe('git', [ 'diff', '--cached', '--name-only', '--diff-filter=ACMR' ]);
|
||
if (!output) {
|
||
return [];
|
||
}
|
||
return output.split(/\r?\n/).map(item => normalizePath(item.trim())).filter(Boolean);
|
||
}
|
||
|
||
function main() {
|
||
const stagedFiles = listStagedFiles();
|
||
const blocked = stagedFiles.filter(filePath => BLOCKED_PATH_PATTERNS.some(pattern => pattern.test(filePath)));
|
||
|
||
if (blocked.length === 0) {
|
||
console.log('git safety check passed');
|
||
return;
|
||
}
|
||
|
||
console.error('以下文件禁止提交到 git,请先取消暂存:');
|
||
for (const filePath of blocked) {
|
||
console.error(`- ${filePath}`);
|
||
}
|
||
console.error('建议使用 git restore --staged <file> 取消暂存,再继续提交。');
|
||
process.exit(1);
|
||
}
|
||
|
||
main();
|