fix(cli): 视觉审计在样式表有未提交改动时归因到工作区

原实现总是回查最近提交,工作区脏时会误报成上一次提交引入。改为先看
git status:脏了就提示「来自工作区未提交的改动」,干净再逐提交倒推。
This commit is contained in:
HIsheR
2026-10-26 16:50:10 +08:00
parent d048717e76
commit a3943ecb19
+8 -1
View File
@@ -30,6 +30,9 @@ function countInLine(line, key) {
/*** 作用:按 git 历史倒序找最可能引入该类硬编码的提交(只查样式表最近 40 次改动) ***/
function suspectCommit(root, key) {
// 工作区先脏了就先报未提交改动:此时逐提交归因只会指向「上一次提交」,对排查没帮助
const dirty = spawnSync('git', ['status', '--porcelain', '--', UI_CSS_REL], { cwd: root, encoding: 'utf8' });
if (dirty.status === 0 && String(dirty.stdout).trim()) return { hash: '(工作区未提交的改动)', hit: null };
const log = spawnSync('git', ['log', '-n', '40', '--pretty=%h', '--', UI_CSS_REL], { cwd: root, encoding: 'utf8' });
if (log.status !== 0) return null; // 非 git 环境(打包分发)跳过归因
for (const hash of String(log.stdout).split('\n').map((s) => s.trim()).filter(Boolean)) {
@@ -121,7 +124,11 @@ export function visualAudit(args, root) {
const now = result.counts[meta.key] ?? 0;
const was = baseline.counts[meta.key] ?? 0;
const sus = suspectCommit(root, meta.key);
const who = sus ? `疑似由提交 ${sus.hash} 引入(该提交新增 ${sus.hit} 处)` : '未能定位引入提交(可能来自未提交改动)';
const who = sus && sus.hit === null
? `来自${sus.hash},提交前应先改回令牌`
: sus
? `疑似由提交 ${sus.hash} 引入(该提交新增 ${sus.hit} 处)`
: '未能定位引入提交(可能来自未提交改动)';
console.error(`audit:visual: ${meta.label}(${meta.key})涨了 ${now - was} 处(基线 ${was} → 当前 ${now}),${meta.hint};${who}`);
}
console.error(`视觉基线失败:${grown.length} 类超过基线。禁止新增硬编码视觉字面量,改用 --ua-* 令牌`);