diff --git a/.cursor/hooks.json b/.cursor/hooks.json new file mode 100644 index 00000000..46f601d9 --- /dev/null +++ b/.cursor/hooks.json @@ -0,0 +1,13 @@ +{ + "version": 1, + "hooks": { + "beforeShellExecution": [ + { + "command": ".cursor/hooks/check-git-commit.sh", + "matcher": "git\\s+commit", + "failClosed": true, + "timeout": 30 + } + ] + } +} diff --git a/.cursor/hooks/check-git-commit.sh b/.cursor/hooks/check-git-commit.sh new file mode 100755 index 00000000..2b2c727f --- /dev/null +++ b/.cursor/hooks/check-git-commit.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" +# shellcheck source=../../scripts/ci/coding-tool-identity.sh +source "${repo_root}/scripts/ci/coding-tool-identity.sh" + +input="$(cat)" +command="$(printf '%s' "${input}" | jq -r '.command // empty')" + +allow() { + jq -nc --arg msg "${1:-}" \ + '{permission:"allow"} + (if $msg == "" then {} else {agent_message:$msg} end)' + exit 0 +} + +deny() { + local msg="$1" + jq -nc --arg msg "${msg}" '{permission:"deny",agent_message:$msg,user_message:$msg}' + exit 0 +} + +if ! printf '%s' "${command}" | grep -Eq '(^|[[:space:];|&])git([[:space:]]+|-c[[:space:]]|-)([^;|&]*[[:space:]])?commit([[:space:]]|$)'; then + allow +fi + +if [ -x "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" ]; then + bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null 2>&1 || true +fi + +identity_is_coding_tool() { + printf '%s' "$1" | grep -Eiq "${CODING_TOOL_IDENTITY_PATTERN}" +} + +resolve_identity_part() { + local var="$1" env_val="$2" + local from_cmd + from_cmd="$(env_value_from_command "${var}" "${command}")" + if [ -n "${from_cmd}" ]; then + printf '%s' "${from_cmd}" + return + fi + if [ -n "${env_val}" ]; then + printf '%s' "${env_val}" + return + fi + case "${var}" in + GIT_AUTHOR_NAME | GIT_COMMITTER_NAME) + git -C "${repo_root}" config --get user.name 2>/dev/null || true + ;; + GIT_AUTHOR_EMAIL | GIT_COMMITTER_EMAIL) + git -C "${repo_root}" config --get user.email 2>/dev/null || true + ;; + esac +} + +author_name="$(resolve_identity_part GIT_AUTHOR_NAME "${GIT_AUTHOR_NAME:-}")" +author_email="$(resolve_identity_part GIT_AUTHOR_EMAIL "${GIT_AUTHOR_EMAIL:-}")" +committer_name="$(resolve_identity_part GIT_COMMITTER_NAME "${GIT_COMMITTER_NAME:-}")" +committer_email="$(resolve_identity_part GIT_COMMITTER_EMAIL "${GIT_COMMITTER_EMAIL:-}")" + +author="${author_name} <${author_email}>" +committer="${committer_name} <${committer_email}>" + +if identity_is_coding_tool "${author}" || identity_is_coding_tool "${committer}"; then + deny "git commit blocked: author/committer looks like a coding tool (${author} / ${committer}). Prefix the same shell command with human GIT_AUTHOR_* and GIT_COMMITTER_* (the gate reads those from the command text). Example: +GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \\ +GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \\ +git commit ..." +fi + +if printf '%s' "${command}" | grep -Eiq 'Co-authored-by:[[:space:]]*Zhe Li[[:space:]]*'; then + deny "git commit blocked: do not put 'Co-authored-by: Zhe Li ' in the commit message. Use human author/committer instead; the repo hooks strip Cursor's injected copy of that trailer." +fi + +if printf '%s' "${command}" | grep -Eq -- '--no-verify|--no-v\b'; then + deny "git commit blocked: --no-verify skips the commit-msg hook that strips Cursor's Co-authored-by trailer and would fail Check commit authorship. Omit --no-verify." +fi + +allow "Commit authorship looks human. Repo hooks will strip Co-authored-by: Zhe Li if Cursor re-injects it." diff --git a/.githooks/README.md b/.githooks/README.md new file mode 100755 index 00000000..e2d2a0e6 --- /dev/null +++ b/.githooks/README.md @@ -0,0 +1,26 @@ +# Git hooks + +Repo hooks for Cloud Agents and local commits. + +| Hook | Purpose | +| --- | --- | +| `commit-msg` | Strip `Co-authored-by: Zhe Li ` (Cursor Cloud initiator trailer) | +| `strip-coauthored-by.sh` | Shared implementation | + +## Why the ensure script exists + +Cursor Cloud sets `core.hooksPath` to a managed dispatcher under +`~/.cursor/agent-hooks/…`. That dispatcher: + +1. Runs hooks from `.cursor-original-hooks-path` (we point this at `.githooks/`) +2. Then runs Cursor’s own `commit-msg.cursor.co-author`, which **re-adds** the trailer + +So we also install `commit-msg.cursor.zz-strip-coauthor` into the managed +hooks dir so it runs **after** Cursor’s co-author hook. + +```bash +bash scripts/ci/ensure-cursor-commit-hooks.sh +``` + +The Cursor Agent hook `.cursor/hooks/check-git-commit.sh` runs that ensure +step before every `git commit`. diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 00000000..9d15ab4d --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail + +hook_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +bash "${hook_dir}/strip-coauthored-by.sh" "$1" diff --git a/.githooks/strip-coauthored-by.sh b/.githooks/strip-coauthored-by.sh new file mode 100755 index 00000000..bd548a85 --- /dev/null +++ b/.githooks/strip-coauthored-by.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -euo pipefail + +msg_file="${1:-}" +if [ -z "${msg_file}" ] || [ ! -f "${msg_file}" ]; then + exit 0 +fi + +tmp="$(mktemp)" +trap 'rm -f "${tmp}"' EXIT + +grep -viE '^[[:space:]]*Co-authored-by:[[:space:]]*Zhe Li[[:space:]]*[[:space:]]*$' \ + "${msg_file}" > "${tmp}" || true +mv "${tmp}" "${msg_file}" +trap - EXIT diff --git a/.gitignore b/.gitignore index fd6fabe9..d256a498 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,10 @@ tmp/ .vscode/ # AI files -.cursor/ +.cursor/* +!.cursor/hooks.json +!.cursor/hooks/ +!.cursor/hooks/** .claude/ AGENTS.md CLAUDE.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e225ae06..a4c17d76 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -144,6 +144,11 @@ Rules: - Linters enabled: `govet`, `ineffassign`, `staticcheck`, `unused` - Formatters enabled: `gofmt`, `goimports` - Pre-commit hook available: install with `uvx pre-commit install` +- Cloud Agent commit hooks (strip Cursor’s initiator `Co-authored-by`, + block AI authorship on `git commit`): see [`.githooks/README.md`](.githooks/README.md) + and [`.cursor/hooks.json`](.cursor/hooks.json). Run + `bash scripts/ci/ensure-cursor-commit-hooks.sh` once per Cloud Agent + boot if commits happen before the Agent hook fires. ### Build diff --git a/scripts/ci/check-authorship.sh b/scripts/ci/check-authorship.sh index a91d543b..2afe0b89 100755 --- a/scripts/ci/check-authorship.sh +++ b/scripts/ci/check-authorship.sh @@ -8,9 +8,10 @@ set -euo pipefail -# Matched case-insensitively against a whole "Name " identity, with -# non-alphanumeric boundaries so names like "Claudia" or "precursor" pass. -PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' +_ci_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=coding-tool-identity.sh +source "${_ci_dir}/coding-tool-identity.sh" +PATTERN="${CODING_TOOL_IDENTITY_PATTERN}" usage() { echo "usage: $0 [head-ref]" >&2 diff --git a/scripts/ci/coding-tool-identity.sh b/scripts/ci/coding-tool-identity.sh new file mode 100755 index 00000000..b96ca15d --- /dev/null +++ b/scripts/ci/coding-tool-identity.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# shellcheck shell=bash + +CODING_TOOL_IDENTITY_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' + +env_value_from_command() { + local var="$1" text="$2" + if [[ "${text}" =~ (^|[[:space:]])${var}=\'([^\']*)\' ]]; then + printf '%s' "${BASH_REMATCH[2]}" + elif [[ "${text}" =~ (^|[[:space:]])${var}=\"([^\"]*)\" ]]; then + printf '%s' "${BASH_REMATCH[2]}" + elif [[ "${text}" =~ (^|[[:space:]])${var}=([^[:space:]]+) ]]; then + printf '%s' "${BASH_REMATCH[2]}" + fi +} diff --git a/scripts/ci/commit-hooks_test.sh b/scripts/ci/commit-hooks_test.sh new file mode 100755 index 00000000..fed1d6c4 --- /dev/null +++ b/scripts/ci/commit-hooks_test.sh @@ -0,0 +1,143 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" +tmp="$(mktemp -d)" +hooks_mutated=0 +old_hooks="" + +cleanup() { + rm -rf "${tmp}" + if [ "${hooks_mutated}" = 1 ]; then + if [ -n "${old_hooks}" ]; then + git -C "${repo_root}" config core.hooksPath "${old_hooks}" + else + git -C "${repo_root}" config --unset core.hooksPath || true + fi + fi +} +trap cleanup EXIT + +assert_eq() { + local name="$1" got="$2" exp="$3" + if [ "${got}" != "${exp}" ]; then + echo "FAIL: ${name}" >&2 + echo " got: ${got}" >&2 + echo " expected: ${exp}" >&2 + exit 1 + fi +} + +assert_file_lacks() { + local name="$1" file="$2" needle="$3" + if grep -Fiq "${needle}" "${file}"; then + echo "FAIL: ${name} — ${file} still contains: ${needle}" >&2 + exit 1 + fi +} + +assert_file_contains() { + local name="$1" file="$2" needle="$3" + if ! grep -Fq "${needle}" "${file}"; then + echo "FAIL: ${name} — ${file} missing: ${needle}" >&2 + exit 1 + fi +} + +msg="${tmp}/msg" +cat > "${msg}" <<'EOF' +docs: example + +Body text. + +Co-authored-by: Zhe Li +Co-authored-by: Someone Else +EOF + +bash "${repo_root}/.githooks/strip-coauthored-by.sh" "${msg}" +assert_file_lacks "strip initiator" "${msg}" "Zhe Li" +assert_file_contains "keep other" "${msg}" "Co-authored-by: Someone Else " +assert_file_contains "keep body" "${msg}" "Body text." + +cat > "${msg}" <<'EOF' +subject + +co-authored-by: zhe li +EOF +bash "${repo_root}/.githooks/strip-coauthored-by.sh" "${msg}" +assert_file_lacks "case fold" "${msg}" "linuxcity" + +hook="${repo_root}/.cursor/hooks/check-git-commit.sh" +chmod +x "${hook}" "${repo_root}/.githooks/"* "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" + +node -e "process.exit(new RegExp('git\\\\s+commit').test('git commit -m test') ? 0 : 1)" \ + || { echo "FAIL: hooks.json matcher must be a JS RegExp that matches git commit" >&2; exit 1; } + +out="$( + printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Cursor Agent' GIT_AUTHOR_EMAIL='cursoragent@cursor.com' \ + GIT_COMMITTER_NAME='Cursor Agent' GIT_COMMITTER_EMAIL='cursoragent@cursor.com' \ + bash "${hook}" +)" +assert_eq "deny ai author permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +out="$( + printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "allow human author permission" "$(printf '%s' "${out}" | jq -r .permission)" "allow" + +out="$( + printf '%s' '{"command":"GIT_AUTHOR_NAME='\''Zhe Li'\'' GIT_AUTHOR_EMAIL='\''linuxcity.jn@gmail.com'\'' GIT_COMMITTER_NAME='\''Zhe Li'\'' GIT_COMMITTER_EMAIL='\''linuxcity.jn@gmail.com'\'' git commit -m test","cwd":"'"${repo_root}"'"}' | \ + env -u GIT_AUTHOR_NAME -u GIT_AUTHOR_EMAIL -u GIT_COMMITTER_NAME -u GIT_COMMITTER_EMAIL \ + bash "${hook}" +)" +assert_eq "allow human author from command text" "$(printf '%s' "${out}" | jq -r .permission)" "allow" + +out="$( + printf '%s' '{"command":"git commit -m \"msg\n\nCo-authored-by: Zhe Li \"","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "deny explicit trailer permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +out="$( + printf '%s' '{"command":"git commit --no-verify -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "deny no-verify permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +fake="${tmp}/agent-hooks" +mkdir -p "${fake}" +printf '%s' '/tmp/original' > "${fake}/.cursor-original-hooks-path" +touch "${fake}/.dispatcher" +chmod +x "${fake}/.dispatcher" +old_hooks="$(git -C "${repo_root}" config --get core.hooksPath || true)" +hooks_mutated=1 +git -C "${repo_root}" config core.hooksPath "${fake}" +bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null +assert_file_contains "original path" "${fake}/.cursor-original-hooks-path" "${repo_root}/.githooks" +[ -x "${fake}/commit-msg.cursor.zz-strip-coauthor" ] || { + echo "FAIL: zz-strip hook not installed" >&2 + exit 1 +} +if [ -n "${old_hooks}" ]; then + git -C "${repo_root}" config core.hooksPath "${old_hooks}" +else + git -C "${repo_root}" config --unset core.hooksPath || true +fi +hooks_mutated=0 + +bash -n "${repo_root}/.githooks/commit-msg" +bash -n "${repo_root}/.githooks/strip-coauthored-by.sh" +bash -n "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" +bash -n "${repo_root}/scripts/ci/coding-tool-identity.sh" +bash -n "${hook}" + +echo "OK: commit-hooks" diff --git a/scripts/ci/ensure-cursor-commit-hooks.sh b/scripts/ci/ensure-cursor-commit-hooks.sh new file mode 100755 index 00000000..73b225b7 --- /dev/null +++ b/scripts/ci/ensure-cursor-commit-hooks.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" +githooks="${repo_root}/.githooks" +strip_src="${githooks}/strip-coauthored-by.sh" + +if [ ! -x "${githooks}/commit-msg" ] || [ ! -f "${strip_src}" ]; then + echo "error: missing executable .githooks/commit-msg or strip-coauthored-by.sh" >&2 + exit 1 +fi + +chmod +x "${githooks}/commit-msg" "${strip_src}" 2>/dev/null || true + +hooks_path="$(git -C "${repo_root}" config --get core.hooksPath || true)" +if [ -z "${hooks_path}" ]; then + git -C "${repo_root}" config core.hooksPath "${githooks}" + echo "set core.hooksPath=${githooks}" + exit 0 +fi + +case "${hooks_path}" in + /*) managed="${hooks_path}" ;; + *) managed="${repo_root}/${hooks_path}" ;; +esac + +if [ ! -d "${managed}" ]; then + echo "warning: core.hooksPath=${hooks_path} is not a directory; skipping Cursor wire-up" >&2 + exit 0 +fi + +if [ ! -f "${managed}/.dispatcher" ] && [ ! -f "${managed}/.cursor-original-hooks-path" ]; then + echo "core.hooksPath=${hooks_path} is not a Cursor agent-hooks dir; leaving as-is" + exit 0 +fi + +printf '%s\n' "${githooks}" > "${managed}/.cursor-original-hooks-path" +echo "pointed .cursor-original-hooks-path -> ${githooks}" + +# Post-hook: must sort after commit-msg.cursor.co-author. +zz_hook="${managed}/commit-msg.cursor.zz-strip-coauthor" +cat > "${zz_hook}" <