cursor-hook-authoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCursor Hook 設計
Cursor Hook 设计
発火タイミング
触发时机
- Cursor Cloud Agent 環境で Git hook を追加・変更する
- の自動付与と除去の連鎖を設計する
Co-authored-by: - lefthook 等の既存 hook と Cursor 管理 hook の共存を整理する
- 在 Cursor Cloud Agent 环境中添加或修改 Git hook
- 设计 自动添加和移除的链
Co-authored-by: - 整理 lefthook 等现有 hook 与 Cursor 管理的 hook 之间的共存关系
Git hook と Cursor Cloud の commit-msg 連鎖
Git hook 与 Cursor Cloud 的 commit-msg 链
実行順
执行顺序
Cursor Cloud Agent は を 経由で差し替える。 の実行順は次のとおり:
.git/hooks/<hook-name>.dispatchercommit-msg- ユーザー hook — が指すパス(通常
.cursor-original-hooks-path)に同名 hook があれば先に実行.git/hooks - Cursor 管理 hook — をファイル名の辞書順で順次実行
$HOOKS_DIR/$HOOK_NAME.cursor*
commit-msg| 順序 | ファイル名 | 役割 |
|---|---|---|
| 1 | | シークレットスキャン(commit ブロック) |
| 2 | | |
| 3 | | |
MUST: strip hook のベース名は とし、 より辞書順で後になる命名にする。NEVER: 等、co-author より前に実行される名前を使用する。
commit-msg.cursor.co-author-stripcommit-msg.cursor.co-authorcommit-msg.cursor.strip-co-authorCursor Cloud Agent 通过 替换 。 的执行顺序如下:
.dispatcher.git/hooks/<hook-name>commit-msg- 用户 hook — 如果 指向的路径(通常为
.cursor-original-hooks-path)中存在同名 hook,则先执行.git/hooks - Cursor 管理的 hook — 按照文件名的字典序,依次执行
$HOOKS_DIR/$HOOK_NAME.cursor*
commit-msg| 顺序 | 文件名 | 作用 |
|---|---|---|
| 1 | | secret 扫描(commit 内容块) |
| 2 | | 自动添加 |
| 3 | | 移除 |
必须:strip hook 的 basename 必须设为 ,并确保其命名在字典序上位于 之后。绝不能:使用 等会在 co-author 之前执行的名称。
commit-msg.cursor.co-author-stripcommit-msg.cursor.co-authorcommit-msg.cursor.strip-co-authorstrip hook 実装
strip hook 实现
- MUST: 除去ロジックは に置く
.cursor/hooks/strip-co-authored-by.sh - MUST: は当該スクリプトを呼び出す薄いラッパーとする
commit-msg.cursor.co-author-strip - MUST: strip 対象は /
Co-authored-by:行(大文字小文字不問)および直前の空行Co-Authored-By: - NEVER: コミットメッセージ本体(件名・説明)を削除しない
参考実装():
.cursor/hooks/strip-co-authored-by.shbash
#!/usr/bin/env bash
set -euo pipefail
COMMIT_MSG_FILE="${1:?commit message file required}"
if [ ! -f "$COMMIT_MSG_FILE" ]; then
exit 0
fi
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
awk '
BEGIN { skip_blank = 0 }
/^[Cc]o-[Aa]uthored-[Bb]y:/ { skip_blank = 1; next }
skip_blank && /^[[:space:]]*$/ { next }
{ skip_blank = 0; print }
' "$COMMIT_MSG_FILE" > "$tmp"
mv "$tmp" "$COMMIT_MSG_FILE"
trap - EXIT- 必须:将移除逻辑放在 中
.cursor/hooks/strip-co-authored-by.sh - 必须:必须仅作为调用该脚本的轻量 wrapper
commit-msg.cursor.co-author-strip - 必须:移除目标包括 /
Co-authored-by:行(不区分大小写)以及紧邻的空行Co-Authored-By: - 绝不能:删除 commit message 主体(主题和详细说明)
参考实现():
.cursor/hooks/strip-co-authored-by.shbash
#!/usr/bin/env bash
set -euo pipefail
COMMIT_MSG_FILE="${1:?commit message file required}"
if [ ! -f "$COMMIT_MSG_FILE" ]; then
exit 0
fi
tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT
awk '
BEGIN { skip_blank = 0 }
/^[Cc]o-[Aa]uthored-[Bb]y:/ { skip_blank = 1; next }
skip_blank && /^[[:space:]]*$/ { next }
{ skip_blank = 0; print }
' "$COMMIT_MSG_FILE" > "$tmp"
mv "$tmp" "$COMMIT_MSG_FILE"
trap - EXITinstall スクリプト
安装脚本
- MUST: で strip 連鎖を Cursor hook ディレクトリへ配置する
scripts/install-cursor-agent-hooks.sh - MUST: install 先は Cursor が を差し替えている実ディレクトリ(
.git/hooksで解決したパスの親、またはreadlink -f .git/hooksと併用).git/hooks/.cursor-original-hooks-path - MUST: に実行権限を付与する
commit-msg.cursor.co-author-strip - IF: install 先が特定できない; THEN MUST: 手動で を指定して実行する
$CURSOR_AGENT_HOOKS_DIR
- 必须:使用 将 strip 链部署到 Cursor hook 目录
scripts/install-cursor-agent-hooks.sh - 必须:安装目标必须是 Cursor 用来替换 的实际目录(通过
.git/hooks解析出的路径的父目录,或与readlink -f .git/hooks结合使用).git/hooks/.cursor-original-hooks-path - 必须:为 添加执行权限
commit-msg.cursor.co-author-strip - 如果:无法确定安装目标;那么必须:手动指定 并执行
$CURSOR_AGENT_HOOKS_DIR
lefthook 多層防御
lefthook 多层防御
- IF: リポジトリが lefthook を使用する; THEN SHOULD: ジョブに strip スクリプトを追加し、Cursor hook 連鎖と二重に
commit-msgを除去するCo-authored-by: - IF: 等で
git-guard.shを deny する; THEN MUST: strip 連鎖でgit commit --no-verifyなしに--no-verify禁止を満たすCo-Authored-By - IF: force push を git-guard で制限する; THEN MUST: 履歴修正が必要な場合は を一時設定し、作業後に元に戻す
GIT_GUARD_ALLOW_FORCE_PUSH=1
- 如果:仓库使用 lefthook;那么应该:将 strip 脚本添加到 job 中,通过 Cursor hook 链和 lefthook 双重移除
commit-msgCo-authored-by: - 如果:使用 等机制拒绝执行
git-guard.sh;那么必须:确保 strip 链在不依赖git commit --no-verify的情况下仍满足--no-verify禁止规则Co-Authored-By - 如果:使用 git-guard 限制 force push;那么必须:在需要修正历史时临时设置 ,并在操作完成后恢复原值
GIT_GUARD_ALLOW_FORCE_PUSH=1
テスト手順
测试步骤
- MUST: を実行する
scripts/install-cursor-agent-hooks.sh - MUST: テストコミットを作成し、で
git log -1 --format=%B/Co-authored-by:が含まれないことを確認するCo-Authored-By: - MUST: スキルの
github禁止ルールと矛盾しないことを確認するCo-Authored-By - IF: lefthook を使用する; THEN MUST: で strip を単体検証する
lefthook run commit-msg --commit-msg-file /tmp/test-msg
- 必须:执行
scripts/install-cursor-agent-hooks.sh - 必须:创建测试 commit,并通过 确认其中不包含
git log -1 --format=%B/Co-authored-by:Co-Authored-By: - 必须:确认测试结果与 skill 的
github禁止规则不冲突Co-Authored-By - 如果:使用 lefthook;那么必须:执行 ,对 strip 进行独立验证
lefthook run commit-msg --commit-msg-file /tmp/test-msg
関連スキル
相关 skill
- : コミット author 指定、
github禁止、Hook 迂回禁止Co-Authored-By - : スキル改変の上流提案
skill-escalation - : スキル追加時の整合性検査
refresh-skills
- :commit author 指定、
github禁止、禁止绕过 HookCo-Authored-By - :对 skill 变更提出上游建议
skill-escalation - :添加 skill 时进行一致性检查
refresh-skills