|
| 1 | +--- |
| 2 | +name: agent-commit |
| 3 | +description: "跨项目 Git 提交规范。当用户要求提交代码、写 commit message、整理改动准备提交时使用。覆盖 Conventional Commits、分类提交、文件粒度、绝对禁止 Agent/大模型/IDE trailer(如 Co-authored-by: Cursor)、Agent 仅在用户明确要求时 commit、不执行 push。scope 枚举与 trailer 以本仓 AGENTS.md 为准。按意图触发。" |
| 4 | +--- |
| 5 | + |
| 6 | +# Git 提交规范(通用核心) |
| 7 | + |
| 8 | +## 目标 |
| 9 | + |
| 10 | +生成规范、准确、可追溯的 Git 提交。message 与 staged 文件一一对应;按模块 / 主题分类;控制单条文件数。 |
| 11 | + |
| 12 | +## 触发场景 |
| 13 | + |
| 14 | +- 用户要求提交 / commit / 写 commit message / 整理改动提交 |
| 15 | +- **用户未要求时:不主动 commit、更不 push**(交付结果 ≠ 提交) |
| 16 | + |
| 17 | +## 前置:读取暂存区与最近提交 |
| 18 | + |
| 19 | +```bash |
| 20 | +git status --short |
| 21 | +git diff --cached --stat |
| 22 | +git diff --cached |
| 23 | +git log --oneline -10 |
| 24 | +``` |
| 25 | + |
| 26 | +**禁止**不读 `git diff --cached` 就写 message;**禁止**只根据文件名猜测。 |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Commit 格式 |
| 31 | + |
| 32 | +``` |
| 33 | +<type>(<scope>): <subject> |
| 34 | +# 或 |
| 35 | +<type>: <subject> |
| 36 | +``` |
| 37 | + |
| 38 | +| 字段 | 约定 | |
| 39 | +|------|------| |
| 40 | +| **type** | `feat` · `fix` · `docs` · `style` · `refactor` · `perf` · `test` · `build` · `ci` · `chore` · `revert` · `release`(本仓可增减,以 `AGENTS.md` 为准) | |
| 41 | +| **scope** | **可选**;取值以本仓 `AGENTS.md` 的 scope 表为准(应用名 / 包名 / 模块名) | |
| 42 | +| **subject** | 祈使、简洁;写「为何改」与关键变化点;语言风格贴近 `git log` | |
| 43 | + |
| 44 | +### type 速查 |
| 45 | + |
| 46 | +| type | 场景 | |
| 47 | +|------|------| |
| 48 | +| `feat` | 新功能 | |
| 49 | +| `fix` | 修复 | |
| 50 | +| `docs` | 文档 | |
| 51 | +| `refactor` | 重构(无行为变化) | |
| 52 | +| `perf` | 性能 | |
| 53 | +| `style` | 格式/样式 | |
| 54 | +| `test` | 测试 | |
| 55 | +| `build` | 构建/依赖 | |
| 56 | +| `ci` | CI | |
| 57 | +| `chore` | 杂项 | |
| 58 | +| `revert` | 回滚 | |
| 59 | +| `release` | 发版(若本仓使用) | |
| 60 | + |
| 61 | +### Subject / Body 反引号(Markdown 可读) |
| 62 | + |
| 63 | +涉及**英文单词、短语、标识符、路径、变量、API、配置键**时,用 Markdown 反引号包裹,便于 GitHub / 文档渲染: |
| 64 | + |
| 65 | +| 宜加 `` `...` `` | 示例 | |
| 66 | +|---|---| |
| 67 | +| 包名 / 应用名(正文中) | `` `@142vip/utils` `` · `` `fairy-cli` `` | |
| 68 | +| 变量 / 常量 / 类型 | `` `compactMap` `` · `` `DateFormatTemplate` `` · `` `TTL` `` | |
| 69 | +| 路径 / 路由 / 文件 | `` `/api/v1` `` · `` `.agents/skills` `` · `` `sidebar.ts` `` | |
| 70 | +| 英文专名 / 协议词 | `` `OIDC` `` · `` `CDN` `` · `` `ESM` `` | |
| 71 | + |
| 72 | +中文产品简称一般不加反引号。 |
| 73 | + |
| 74 | +``` |
| 75 | +feat(@142vip/utils): `vipLodash` 挂载 `compactMap` 与 JSON 守卫 |
| 76 | +fix(docs): 补充 `BaseVo` 使用说明 |
| 77 | +``` |
| 78 | + |
| 79 | +### 发版类 commit(若本仓使用 `release` type) |
| 80 | + |
| 81 | +- message 前缀须与 CI 触发规则**逐字匹配**(如 `` release(<pkg>): publish `vX.Y.Z` ``);前缀写错 → 流水线不触发 |
| 82 | +- 发版前与用户二次确认清单与顺序;一次只处理一个模块(CD 只看 HEAD commit message) |
| 83 | +- 错误 message 已推送时:**禁止** force push;可补同版本空 commit 修正 message,由用户手动推送 |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 分类提交(强制) |
| 88 | + |
| 89 | +用户要求交付/整理/提交时,禁止把多类改动揉进单个 commit。 |
| 90 | + |
| 91 | +``` |
| 92 | +应用或包 |
| 93 | + → 功能域 / 模块 |
| 94 | + → 单次改动主题 |
| 95 | +``` |
| 96 | + |
| 97 | +| 原则 | 要求 | |
| 98 | +|------|------| |
| 99 | +| 边界 | scope 与 staged 路径一致;避免跨无关 app 混提(边界见 `AGENTS.md`) | |
| 100 | +| 主题 | 一条 commit 一个共同目的 | |
| 101 | +| 文件数 | 默认 ≤15 个路径;超出再拆(本仓可另行规定) | |
| 102 | +| 暂存 | 只 `git add` 本批路径;禁止随手 `git add -A` | |
| 103 | +| 可编译 | 每批尽量保持中间态可构建 | |
| 104 | + |
| 105 | +建议顺序(同模块内):类型/接口 → 数据/常量 → 工具 → 配置 → 组件/页面 → 文档 |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## 文案真实性(强制) |
| 110 | + |
| 111 | +- 标题/正文与 `git diff --cached --name-only` 可追溯对应 |
| 112 | +- 禁止标题写 A 却 stage B |
| 113 | +- 禁止空洞套话单独成句(「优化体验」「提升稳定性」) |
| 114 | +- 优先「为何改」与关键变化点 |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +### Commit trailer(绝对禁止) |
| 119 | + |
| 120 | +| 规则 | 要求 | |
| 121 | +|------|------| |
| 122 | +| 默认 | 只写 `type(scope): subject` 与必要正文;**零 trailer** | |
| 123 | +| **绝对禁止** | message 中出现任何 **Agent / IDE / 大模型** 品牌痕迹,包括但不限于:`Co-authored-by: Cursor <cursoragent@cursor.com>`、`Made-with: Cursor`、Claude / GPT / Copilot / Codex 等 `Co-authored-by`、`Generated by …`、`via Agent` | |
| 124 | +| message 正文 | 即使用词描述「清理署名」,也**禁止**在 subject/body 写 IDE/Agent 品牌名;用「工具署名」「共署名字样」等中性表述 | |
| 125 | +| 用户署名 | 仅当用户**明文要求**某一固定行时才追加;格式以本仓 `AGENTS.md` 为准;**仍禁止**上表产品 trailer | |
| 126 | +| 勿信设置 | IDE / CLI 的 Commit Attribution 已关仍可能被 Agent Shell 注入 trailer;**不得以「用户已关设置」跳过清 trailer** | |
| 127 | + |
| 128 | +**铁律**:宁可不写 trailer,也不允许把 Cursor / Agent / 大模型写进 git 历史。 |
| 129 | + |
| 130 | +### Commit 后清 trailer(强制 · 每条 commit) |
| 131 | + |
| 132 | +> Cursor Agent 经 Shell 执行 `git commit` 时,环境可能在 message **之外**追加 `Co-authored-by: Cursor …`(与 HEREDOC 内容无关)。**skill 层兜底**:每条 commit 成功后**立即**执行下方步骤,未 push 前提下允许且必须 `git commit --amend` 仅用于剔除禁止 trailer(不算「擅自 amend」)。 |
| 133 | +
|
| 134 | +**禁止 trailer 行模式**(整行删除,大小写不敏感): |
| 135 | + |
| 136 | +- `Co-authored-by: Cursor …` |
| 137 | +- `Made-with: Cursor` |
| 138 | +- 含 `cursoragent@cursor.com` 的 `Co-authored-by:` |
| 139 | + |
| 140 | +**Agent 每条 `git commit` 成功后必须执行**(可复制;多批提交时每条各跑一次): |
| 141 | + |
| 142 | +```bash |
| 143 | +strip_commit_message() { |
| 144 | + git log -1 --format=%B | sed -E \ |
| 145 | + '/^[[:space:]]*Co-authored-by:[[:space:]]*Cursor /Id;/^[[:space:]]*Made-with:[[:space:]]*Cursor/Id;/^[[:space:]]*Co-authored-by:.*cursoragent@cursor\.com/Id' \ |
| 146 | + | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' |
| 147 | +} |
| 148 | + |
| 149 | +raw="$(git log -1 --format=%B)" |
| 150 | +clean="$(strip_commit_message)" |
| 151 | +if [ "$raw" != "$clean" ]; then |
| 152 | + git commit --amend -m "$clean" |
| 153 | +fi |
| 154 | +git log -1 --format=%B | grep -Eiq 'Co-authored-by:[[:space:]]*Cursor|Made-with:[[:space:]]*Cursor|cursoragent@cursor\.com' \ |
| 155 | + && echo 'FATAL: prohibited commit trailer remains' && exit 1 |
| 156 | +``` |
| 157 | + |
| 158 | +整轮提交结束后,对本轮新增的每条 commit 再抽查:`git log -N --format=%B`(`N` = 本轮条数)均不得含上表禁止行。 |
| 159 | + |
| 160 | +**未推送且 amend 仍残留**:用 `git commit-tree` 重写该 commit 对象(见各仓 Git 规范深文),禁止带 `--no-verify` 跳过。 |
| 161 | + |
| 162 | +**skill 边界**:上述流程保证 Agent 遵守时历史干净;无法阻止 IDE 注入本身。若需与 Agent 无关的硬兜底,在仓库加 `commit-msg` hook(可选,非本 skill 必须项)。 |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +## Agent 约束 |
| 167 | + |
| 168 | +| 操作 | 条件 | |
| 169 | +|------|------| |
| 170 | +| **commit** | 用户明确要求时可执行;只 stage 相关文件 | |
| 171 | +| **push** | **agent 不执行**;提交完成后列出 commit 摘要,由用户手动 `git push` | |
| 172 | +| **PR** | 用户明确要求才创建 | |
| 173 | +| **未要求 commit** | 不主动提交 | |
| 174 | + |
| 175 | +其它: |
| 176 | + |
| 177 | +- hook 失败:修复后新 commit;不 amend 已推送 |
| 178 | +- 禁止 `--no-verify`、对主分支 force push(除非用户明确要求) |
| 179 | +- 禁止改 `git config` |
| 180 | + |
| 181 | +### 执行补充 |
| 182 | + |
| 183 | +- 工作区已有大量无关 diff 时不要混入本批 |
| 184 | +- commit 失败先修再新 commit;**仅**为剔除禁止 trailer 的 `git commit --amend` 允许(见上节);其它擅自 amend 禁止;push 一律由用户手动 |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## 执行步骤 |
| 189 | + |
| 190 | +> **门闩**:仅当用户明确要求提交(commit / 写 message / 整理改动提交)时才进入本流程;未要求 → 不 stage、不 commit、不 push,到此为止仅交付结果。 |
| 191 | +
|
| 192 | +1. 读 status / cached diff / 近期 log |
| 193 | +2. 暂存区空 → 说明无法提交 |
| 194 | +3. 归纳主语义;多类则拆批 |
| 195 | +4. scope / 文风对齐本仓与 `git log` |
| 196 | +5. 分批 `git add` + `git commit` |
| 197 | +6. **每条 commit 后立即**执行「Commit 后清 trailer」脚本;失败则停止,不得继续下一批或 push |
| 198 | +7. 整轮结束后对本轮全部 commit 做 `%B` 抽查 |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +## 边界情况 |
| 203 | + |
| 204 | +- **多类混杂**:诚实概括或拆 commit,不假装单一目的 |
| 205 | +- **过于分散**:可用较宽 `chore` 概括,仍须可追溯 |
| 206 | +- **push**:agent 不执行;提交完成后给出 commit 摘要,由用户手动 `git push` |
| 207 | + |
| 208 | +## 参考 |
| 209 | + |
| 210 | +- 项目 L0:`AGENTS.md` §Git |
| 211 | +- 执行管线:`workflow` · 开发:`code-dev` · 自检 / TODO:`self-check` |
0 commit comments