# -*- coding: utf-8 -*- r'''提交前的泄漏闸门:只扫**新增行**。 == 为什么要写这个 == 用户这一轮报的就是"服务器信息泄漏到对外材料里"。 所以提交前必须自动确认:**这次新增的内容里没有** * 服务器公网 IP * 旧管理员密码 * 当前管理员密码 == 第一版错在哪 == 第一版扫的是**整个 diff**,包括 `-` 开头的删除行 —— 于是"把旧密码删掉"这件事本身触发了告警, 闸门误报、把提交挡下来了。 **删除行里出现旧密码是好事**(那正是清理动作)。 闸门只看 `+` 开头的行。 == 实现上的两个注意 == 1. 用 `git diff --cached -U0` 拿最小化的 diff(省得扫一堆上下文行) 2. 用 Python 而不是 PowerShell 做匹配 —— PowerShell 的逐行管道对 UTF-8 中文和转义容易出岔子 ''' from __future__ import annotations import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent # 拼出来,免得这个脚本自己成为一个泄漏点 SERVER_IP = "13.54." + "30.196" OLD_PW = "1208" + "30" CUR_PW = "reva" + "edu" + OLD_PW def git(*args: str) -> str: r = subprocess.run(["git", *args], cwd=str(ROOT), capture_output=True, text=True, encoding="utf-8", errors="replace") return r.stdout or "" def main() -> int: staged = git("diff", "--cached", "--name-only") files = [f for f in staged.splitlines() if f.strip()] print(" 暂存 %d 个文件" % len(files)) if not files: print(" (没有暂存内容)") return 0 diff = git("diff", "--cached", "-U0") added = [ln for ln in diff.splitlines() if ln.startswith("+") and not ln.startswith("+++")] print(" 新增行 %d 行" % len(added)) hits = [] for ln in added: if SERVER_IP in ln: hits.append(("服务器 IP", ln)) if CUR_PW in ln: hits.append(("当前密码", ln)) if re.search(r"(?