Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .cursor/hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"hooks": {
"beforeShellExecution": [
{
"command": ".cursor/hooks/check-git-commit.sh",
"matcher": "git\\s+commit",
"failClosed": true,
"timeout": 30
}
]
}
}
81 changes: 81 additions & 0 deletions .cursor/hooks/check-git-commit.sh
Original file line number Diff line number Diff line change
@@ -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:]]*<linuxcity\.jn@gmail\.com>'; then
deny "git commit blocked: do not put 'Co-authored-by: Zhe Li <linuxcity.jn@gmail.com>' 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."
26 changes: 26 additions & 0 deletions .githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Git hooks

Repo hooks for Cloud Agents and local commits.

| Hook | Purpose |
| --- | --- |
| `commit-msg` | Strip `Co-authored-by: Zhe Li <linuxcity.jn@gmail.com>` (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`.
5 changes: 5 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions .githooks/strip-coauthored-by.sh
Original file line number Diff line number Diff line change
@@ -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:]]*<linuxcity\.jn@gmail\.com>[[:space:]]*$' \
"${msg_file}" > "${tmp}" || true
mv "${tmp}" "${msg_file}"
trap - EXIT
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ tmp/
.vscode/

# AI files
.cursor/
.cursor/*
!.cursor/hooks.json
!.cursor/hooks/
!.cursor/hooks/**
.claude/
AGENTS.md
CLAUDE.md
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions scripts/ci/check-authorship.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

set -euo pipefail

# Matched case-insensitively against a whole "Name <email>" 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 <base-ref> [head-ref]" >&2
Expand Down
15 changes: 15 additions & 0 deletions scripts/ci/coding-tool-identity.sh
Original file line number Diff line number Diff line change
@@ -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
}
143 changes: 143 additions & 0 deletions scripts/ci/commit-hooks_test.sh
Original file line number Diff line number Diff line change
@@ -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 <linuxcity.jn@gmail.com>
Co-authored-by: Someone Else <other@example.com>
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 <other@example.com>"
assert_file_contains "keep body" "${msg}" "Body text."

cat > "${msg}" <<'EOF'
subject

co-authored-by: zhe li <linuxcity.jn@gmail.com>
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 <linuxcity.jn@gmail.com>\"","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"
49 changes: 49 additions & 0 deletions scripts/ci/ensure-cursor-commit-hooks.sh
Original file line number Diff line number Diff line change
@@ -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}" <<EOF
#!/usr/bin/env bash
set -euo pipefail
exec bash "${strip_src}" "\$1"
EOF
chmod +x "${zz_hook}"
echo "installed ${zz_hook}"