'use strict'; const path = require('node:path'); const { REPO_ROOT, ROOT, runCommand } = require('./deploy-lib.cjs'); const BLOCKED_PATH_PATTERNS = [ /^config\/config\.local\.js$/, /^config\/config\.prod\.js$/, /^config\/deploy\.config\.local\.cjs$/, /^temp\/(?!docs\/).+/, /^logs\//, /^run\//, /^versioning\/(?!\.gitkeep$).+/, /^app\/public\/download\/(?!\.gitkeep$).+/, /^app\/public\/version\/(?!\.gitkeep$).+/, /\.pem$/i, /\.key$/i, /^\.env(?:\..+)?$/, ]; /** 仓库根视角的禁区:Android 正式签名与本地机器配置,绝不入库 */ const BLOCKED_REPO_PATH_PATTERNS = [ /(^|\/)keystore\.properties$/, /\.jks$/i, /(^|\/)local\.properties$/, /(^|\/)\.dev-adb\.local\.json$/, ]; function normalizePath(filePath) { return filePath.replace(/\\/g, '/'); } function listStagedFiles(cwd, relative) { const args = [ 'diff', '--cached', '--name-only', '--diff-filter=ACMR' ]; if (relative) args.push('--relative'); const output = runCommand('git', args, { cwd }); if (!output) { return []; } return output.split(/\r?\n/).map(item => normalizePath(item.trim())).filter(Boolean); } function main() { const blocked = [ ...listStagedFiles(ROOT, true).filter(filePath => BLOCKED_PATH_PATTERNS.some(pattern => pattern.test(filePath))), ...listStagedFiles(REPO_ROOT, false).filter(filePath => BLOCKED_REPO_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 new Set(blocked)) { console.error(`- ${filePath}`); } console.error('建议使用 git restore --staged 取消暂存,再继续提交。'); process.exit(1); } main();