--- name: pre-commit-crud-check description: "Pre-commit check: blocks direct edits to CRUD intermediate files and build artifacts." version: 1.0.0 author: Hermes Agent platforms: [linux] metadata: hermes: tags: [crud, pre-commit, build-artifacts, sage, brick] related_skills: [requesting-code-review] --- # Pre-Commit CRUD & Build Artifact Check 提交代码前必须执行的检查,确保不提交不该提交的文件。 ## 触发条件 用户说「提交」「commit」「push」「git commit」等,或任务涉及 git 操作时。 ## 检查步骤 ### 1. 获取待提交的文件列表 ```bash cd && git status ``` 同时检查 staged 和 unstaged 的修改文件。 ### 2. 检查 CRUD 中间文件(严格禁止直接修改) CRUD 中间文件是: - `wwwroot/<列表名>/*.dspy` - `wwwroot/<列表名>/*.ui` 这些文件由 `json/*.json` 通过 xls2ui 生成,绝不能直接修改。 如果发现这些文件有改动: - **阻止提交**,告知用户 - 改动应该在 `json/<列表名>.json` 中做,然后重新跑 xls2ui 生成 - `wwwroot/api/<独立api>.dspy` 例外:api 目录下的是独立 API,不是 CRUD 中间文件,可以正常修改 ### 3. 检查构建产物(不应提交) - `*.egg-info/` — pip install 产物 - `__pycache__/` — Python 缓存 - `*.pyc` — 编译缓存 - `build/`、`dist/` — 构建输出 - `*.pyo` — 优化编译缓存 - `node_modules/` — npm 依赖 发现这些文件在待提交列表中时: - 如果是 untracked:删掉(`rm -rf`) - 如果是 tracked 且被修改:确认是否应该提交,通常不应提交 ### 4. 检查 untracked 文件冲突 如果 `git pull/push` 报错 untracked 文件会被覆盖: - CRUD 中间文件:直接删掉本地 untracked 的 - egg-info 等构建产物:直接删掉 - 不确定的文件:询问用户 ## 5. 检查硬编码数据库名(Sage 模块通用) 切勿在 `sqlorContext()` 中硬编码数据库名字符串: ```python # ❌ 硬编码 — 生产环境 dbname 可能不同 async with db.sqlorContext('sage') as sor: async with DBPools().sqlorContext('llmage') as sor: # ✅ 动态获取 dbname = ServerEnv().get_module_dbname('modulename') async with DBPools().sqlorContext(dbname) as sor: ``` `DBPools()` 不是单例,新实例的 databases 为空。务必通过 `getConfig().databases` 或 `_get_sor()` 获取配置好的 DBPools。 ## 铁律 - **永远不要直接改 `wwwroot/<列表名>/` 下的 CRUD 中间文件** - **永远不要提交 egg-info、__pycache__、build/dist 等构建产物** - **永远不要在 `sqlorContext()` 中硬编码数据库名** - `DBPools()` 不是单例,新实例 databases 为空,用 `ServerEnv().get_module_dbname()` 动态获取 - 独立 API 放在 `wwwroot/api/` 下,不受此限制 - 独立 API 放在 `wwwroot/api/` 下,不受此限制 ### Sage RBAC 模块常见 CRUD 目录 RBAC 模块的 11 个 CRUD 自动生成目录列表和清理步骤见 `references/rbac-crud-directories.md`,处理 rbac 仓库时先参考。 ## 清理已被远程跟踪的构建产物 如果远程仓库已经提交了 `egg-info/`、`__pycache__/`、`wwwroot/<列表名>/` 等不该跟踪的文件: ```bash # 清理 CRUD 中间文件(wwwroot/<列表名>/) rm -rf wwwroot/<列表名>/ git rm -r --cached wwwroot/<列表名>/ echo "wwwroot/<列表名>/" >> .gitignore # 清理构建产物 git rm -r --cached .egg-info/ git rm -r --cached /__pycache__/ git rm -r --cached scripts/__pycache__/ # 确保 .gitignore 有这些规则 cat >> .gitignore << 'EOF' *.egg-info/ __pycache__/ *.pyc *.pyo wwwroot/<列表名>/ EOF # 提交并推送 git add .gitignore git commit -m "chore: remove build artifacts and CRUD intermediate files from tracking" git push ```