From e72803ba3caf1cbe9de0124b902e2a633df4b32f Mon Sep 17 00:00:00 2001 From: yunyingk Date: Fri, 2 Oct 2026 16:32:39 +0800 Subject: [PATCH] Add device branch-prefix rule with hook enforcement --- .agents/rules/branch-naming.md | 32 ++++++++++++++++++++++++++++ .githooks/branch-guard.sh | 38 ++++++++++++++++++++++++++++++++++ .githooks/pre-commit | 6 ++++++ .githooks/pre-push | 12 +++++++++++ AGENTS.md | 3 ++- 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 .agents/rules/branch-naming.md create mode 100755 .githooks/branch-guard.sh create mode 100755 .githooks/pre-commit diff --git a/.agents/rules/branch-naming.md b/.agents/rules/branch-naming.md new file mode 100644 index 0000000..3156f2d --- /dev/null +++ b/.agents/rules/branch-naming.md @@ -0,0 +1,32 @@ +--- +description: "本设备创建分支的命名规范(设备前缀)与 git hook 机械强制" +trigger: "always" +--- + +# 分支命名规范 (设备前缀 + 机械强制) + +为了让"哪台设备/哪个 Agent 开的分支"一眼可辨,本仓库的分支命名按创建设备加前缀。规则文档随仓库走,前缀的值留在各设备本地。 + +## 1. 命名规则 + +- **格式**:`<设备键>/<小写连字符主题>`。本机(这台 Mac)的设备键是 `mac15`,例如:`mac15/improve-corp-search`。 +- **设备键声明**:每个克隆只声明一次,写入本地 git 配置(存于 `.git/config`,不提交、随设备走): + ```bash + git config seal.branchPrefix mac15 # 这台 Mac;其他设备换自己的键,不设置则本规则休眠 + ``` +- **Agent 行为要求**:任何 Agent(Codex/ZCode/Claude 等)创建分支前,先执行 `git config seal.branchPrefix`;有值则分支名必须以 `<值>/` 开头,没有值时用普通主题名即可。 + +## 2. 保留名(不受设备前缀约束) + +- `main`:主分支。 +- `codex/release-vX.Y.Z`:发版自动化专用(`bun run release` 创建),沿用既有约定。 + +## 3. 机械强制(不依赖 Agent 自觉) + +- 钩子:`.githooks/pre-commit` 与 `.githooks/pre-push` 通过 `.githooks/branch-guard.sh` 校验分支名,违规即拒绝并给出改名命令。`package.json` 的 `prepare` 脚本在 `bun install` 时自动设置 `core.hooksPath=.githooks`,新克隆无需手动接线。 +- **存量分支豁免**:已存在于 origin 的旧分支(如云端 Agent 创建的 `codex/*`)不拦,避免影响在途 PR。 +- **逃生门**:`git commit --no-verify` / `git push --no-verify` 可一次性绕过,仅用于特殊情况,不要养成习惯。 + +## 4. 相关规则 + +- 分支/工作树清理见 `.agents/rules/workspace-hygiene.md`;PR 工作流见 `.agents/rules/issue-pr-workflow.md`。 diff --git a/.githooks/branch-guard.sh b/.githooks/branch-guard.sh new file mode 100755 index 0000000..8dc84f6 --- /dev/null +++ b/.githooks/branch-guard.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# Shared branch-naming guard: .agents/rules/branch-naming.md +# Devices opt in per clone: git config seal.branchPrefix (e.g. mac15) +# Sourced by pre-commit and pre-push. + +# guard_branch_name — returns 1 with guidance when the name breaks the rule. +guard_branch_name() { + local branch="$1" + [ -n "$branch" ] || return 0 + + # Reserved names: mainline branches and the release automation. + case "$branch" in + main|master|develop) return 0 ;; + codex/release-*) return 0 ;; + esac + + # Enforcement is opt-in per device; without a configured prefix this guard is dormant. + local prefix + prefix="$(git config seal.branchPrefix 2>/dev/null || true)" + [ -n "$prefix" ] || return 0 + + case "$branch" in + "$prefix"/*) return 0 ;; + esac + + # Branches that already exist on origin (created elsewhere, e.g. cloud agents) stay usable. + if git rev-parse -q --verify "refs/remotes/origin/$branch" >/dev/null 2>&1; then + return 0 + fi + + echo "" >&2 + echo "🛑 [branch-guard] Branch '$branch' violates the device branch-prefix rule (.agents/rules/branch-naming.md)." >&2 + echo " Branches created on this device must start with '${prefix}/'." >&2 + echo " 👉 Rename and retry: git branch -m '${prefix}/${branch##*/}'" >&2 + echo " (One-off bypass: git commit --no-verify / git push --no-verify)" >&2 + echo "" >&2 + return 1 +} diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..457fe5c --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,6 @@ +#!/bin/bash +# Git pre-commit hook: enforce the device branch-prefix rule +# (.agents/rules/branch-naming.md) via .githooks/branch-guard.sh +source "$(dirname "$0")/branch-guard.sh" + +guard_branch_name "$(git branch --show-current)" diff --git a/.githooks/pre-push b/.githooks/pre-push index 30d76b3..3e48796 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,12 +1,24 @@ #!/bin/bash # Git pre-push hook: Prevent accidental direct push to origin/main or GitHub/main # Enforces the PR workflow required by AGENTS.md & .agents/rules/issue-pr-workflow.md +# Also enforces the device branch-prefix rule via .githooks/branch-guard.sh + +source "$(dirname "$0")/branch-guard.sh" remote="$1" url="$2" zero="0000000000000000000000000000000000000000" while read local_ref local_oid remote_ref remote_oid; do + # Device branch-prefix rule (.agents/rules/branch-naming.md); deletions are exempt + case "$local_ref" in + refs/heads/*) + if [ "$local_oid" != "$zero" ]; then + guard_branch_name "${local_ref#refs/heads/}" || exit 1 + fi + ;; + esac + # If pushing the main branch (or pushing to remote main) if [ "$remote_ref" = "refs/heads/main" ] || [ "$local_ref" = "refs/heads/main" ]; then # When pushing to origin or GitHub (main repository) diff --git a/AGENTS.md b/AGENTS.md index 460177a..c6c2554 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,7 +51,8 @@ Use `bun:test` with `describe`, `test`, and `expect`. Place tests next to covere ## Commit & Pull Request Guidelines -Recent commits use concise imperative subjects, often `Add ...` or `Refine ...`, such as `Add compact approval rule listing`. Keep subjects under roughly 72 characters. Specific workflows and domain guidelines are maintained in `.agents/rules/`: +Recent commits use concise imperative subjects, often `Add ...` or `Refine ...`, such as `Add compact approval rule listing`. Keep subjects under roughly 72 characters. Branches created on a workstation must carry that device's prefix from `git config seal.branchPrefix` (e.g. `mac15/improve-corp-search` on this Mac), enforced by the `.githooks` pre-commit/pre-push guards. Specific workflows and domain guidelines are maintained in `.agents/rules/`: +- Branch naming (device-prefix rule & mechanical enforcement): `.agents/rules/branch-naming.md` - Issue triage & PR workflow (No premature release): `.agents/rules/issue-pr-workflow.md` - CLI stream & exit code protocol: `.agents/rules/cli-protocol.md` - Testing conventions & isolation: `.agents/rules/testing.md`