🔧 chore(hooks): 禁止提交与推送 legacy complete 页面改动

This commit is contained in:
Abner
2026-04-11 23:12:51 +08:00
parent 35fc5a842d
commit 77673f2a6e
2 changed files with 62 additions and 1 deletions

View File

@@ -5,7 +5,8 @@
* 1) 仅处理 staged 文件安全格式化LF、行尾空白、文件尾换行
* 2) 检查 staged JS 语法;
* 3) 检查 staged JSON 语法;
* 4) 拦截代码文件中的智能引号,避免引号边界问题
* 4) 拦截代码文件中的智能引号,避免引号边界问题
* 5) 保护 legacy 完整版页面,禁止提交其变更。
*/
const fs = require('fs');
@@ -34,6 +35,10 @@ const TEXT_EXTENSIONS = new Set([
const JS_EXTENSIONS = new Set(['.js', '.mjs', '.cjs']);
const SMART_QUOTES_RE = /[\u2018\u2019\u201C\u201D]/u;
const PROTECTED_FILES = [
'src/giffgaff/giffgaff_complete_esim.html',
'src/simyo/simyo_complete_esim.html'
];
function git(args, options = {}) {
return execFileSync('git', args, {
@@ -54,6 +59,18 @@ function getStagedFiles() {
return output.split('\0').filter(Boolean);
}
function getTouchedProtectedFiles() {
const output = git([
'diff',
'--cached',
'--name-only',
'-z',
'--',
...PROTECTED_FILES
]);
return output.split('\0').filter(Boolean);
}
function shouldFormat(relPath) {
const ext = path.extname(relPath).toLowerCase();
if (TEXT_EXTENSIONS.has(ext)) {
@@ -164,6 +181,16 @@ function runChecks(stagedFiles) {
}
function main() {
const touchedProtectedFiles = getTouchedProtectedFiles();
if (touchedProtectedFiles.length > 0) {
console.error('\n❌ pre-commit 检查失败:');
for (const relPath of touchedProtectedFiles) {
console.error(` - ${relPath}: 该文件受保护,禁止提交改动。`);
}
console.error(' - 如确需调整,请改为维护对应 modular 版本文件。');
process.exit(1);
}
const stagedFiles = getStagedFiles();
if (stagedFiles.length === 0) {
process.exit(0);

View File

@@ -11,6 +11,10 @@ const { execFileSync, spawnSync } = require('child_process');
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const RELATED_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.json']);
const PROTECTED_FILES = [
'src/giffgaff/giffgaff_complete_esim.html',
'src/simyo/simyo_complete_esim.html'
];
function runGit(args) {
return execFileSync('git', args, {
@@ -77,6 +81,25 @@ function getRelatedFiles() {
});
}
function getTouchedProtectedFiles() {
const range = getDiffRange();
if (!range) {
return [];
}
const output = runGit([
'diff',
'--name-only',
'--diff-filter=ACMR',
'-z',
range,
'--',
...PROTECTED_FILES
]);
return output.split('\0').filter(Boolean);
}
function buildSteps(mode) {
const steps = [
{
@@ -132,6 +155,17 @@ function runStep(step) {
function main() {
const mode = resolveMode();
console.log(`\n🧭 pre-push 模式: ${mode}`);
const touchedProtectedFiles = getTouchedProtectedFiles();
if (touchedProtectedFiles.length > 0) {
console.error('\n❌ pre-push 检查失败:');
for (const relPath of touchedProtectedFiles) {
console.error(` - ${relPath}: 该文件受保护,禁止推送包含其改动的提交。`);
}
console.error(' - 如确需调整,请改为维护对应 modular 版本文件。');
process.exit(1);
}
const steps = buildSteps(mode);
for (const step of steps) {