From be323e04943c98f3954c36f965dc03cec52639aa Mon Sep 17 00:00:00 2001 From: cyc Date: Sat, 19 Sep 2026 18:11:18 +0800 Subject: [PATCH] fix(hook): scope enforce_gh_english to this repository The same machine hosts a sibling repo (ycpss91255/worktool) whose issues and PRs are written in zh-TW, and its `gh --repo ` calls run from this repo's cwd, so this PreToolUse Bash hook fired on them and denied their CJK bodies. The English-only rule is this repository's rule, not the host's. The hook now allows early when a `gh issue|pr create|comment` command demonstrably targets another repository: an explicit --repo / -R / --repo= naming a repo other than this one (owner/name, host/owner/name, URL, .git and quoted forms normalised, case-insensitive), else $GH_REPO, else the origin of the directory the command runs in (`cd &&`, payload .cwd, or $PWD). "This repo" is derived from the checkout's `git remote get-url origin` with a literal ycpss91255/initialization fallback. An undeterminable target keeps the guard; behaviour for this repo is unchanged; settings.json untouched. Tests: six new cases in test/unit/hook/enforce_gh_english_spec.bats (RED before the change, GREEN after). CHANGELOG entry under [Unreleased] / Fixed. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01NX5H2vuMTv4mBmjpPYoS3s --- .agents/hook/enforce_gh_english.sh | 82 +++++++++++++++++++++ doc/changelog/CHANGELOG.md | 17 +++++ test/unit/hook/enforce_gh_english_spec.bats | 55 ++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/.agents/hook/enforce_gh_english.sh b/.agents/hook/enforce_gh_english.sh index 8b6e822c..1a3ef0c2 100755 --- a/.agents/hook/enforce_gh_english.sh +++ b/.agents/hook/enforce_gh_english.sh @@ -36,6 +36,20 @@ # - `gh repo` / `gh api` / other subcommands # - git commit messages (project rule covers issue/PR only) # +# Repository scoping (2026-09-19): the English-only rule is THIS repo's, not +# the host's. The same machine hosts sibling repos whose issues / PRs are +# written in another language, and `gh --repo ` calls for them run +# from this cwd (so this PreToolUse hook fires on them). The hook therefore +# allows early when the command targets another repository: +# - `--repo ` / `-R ` (also `--repo=`) +# names a repo other than this one, OR +# - no --repo is given and the cwd's git toplevel is not this checkout +# (`gh` then targets the cwd's own origin). +# "This repo" is derived from `git remote get-url origin` of the checkout the +# hook lives in (GH_ENGLISH_REPO overrides; falls back to the literal +# ycpss91255/initialization when origin is unreadable). Behaviour for this +# repo is unchanged. +# # Refs: project rule "GitHub interaction English-only" (2026-05-16 # session), aligns with ycpss91255-docker/docker_harness convention. # @@ -48,6 +62,65 @@ source "${LIB_DIR:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../../lib" && pwd -P)}/hook_bootstrap.sh" hook_bootstrap "enforce-gh-english" +# ── Repository scoping ─────────────────────────────────────────────────────── + +# Literal fallback for "this repo" when the checkout's origin cannot be read. +GH_ENGLISH_REPO_LITERAL="ycpss91255/initialization" + +# _repo_slug — normalise a repository reference to lower-case +# "owner/name". Accepts what `gh --repo` and `git remote` produce: owner/name, +# host/owner/name, https:// or ssh:// URLs, git@host:owner/name, an optional +# trailing .git, and optional surrounding quotes. Prints nothing when no +# owner/name pair can be found. +_repo_slug() { + local _v="${1:-}" _name _rest _owner + _v="${_v#\"}"; _v="${_v%\"}"; _v="${_v#\'}"; _v="${_v%\'}" + _v="${_v%/}" + _v="${_v%.git}" + _v="${_v//://}" + _name="${_v##*/}" + _rest="${_v%/*}" + _owner="${_rest##*/}" + [[ -n "${_owner}" && -n "${_name}" && "${_rest}" != "${_v}" ]] || return 0 + printf '%s/%s' "${_owner,,}" "${_name,,}" +} + +# _this_repo — owner/name of the checkout this hook lives in (REPO_ROOT's +# origin remote; a linked worktree shares it), else the literal fallback. +_this_repo() { + local _slug + _slug="$(_repo_slug "$(git -C "${REPO_ROOT}" remote get-url origin 2>/dev/null)")" + printf '%s' "${_slug:-${GH_ENGLISH_REPO_LITERAL}}" +} + +# _target_repo — owner/name the gh command targets: an explicit +# `--repo ` / `-R ` / `--repo=`, else $GH_REPO, else the origin +# of the directory the command runs in (a leading `cd &&`, the payload's +# .cwd, or $PWD). Prints nothing when the target cannot be determined. +_target_repo() { + local _cmd="$1" _ref="" _dir _cd + if [[ "${_cmd}" =~ (^|[[:space:]])(--repo|-R)[[:space:]]+([^[:space:]]+) ]]; then + _ref="${BASH_REMATCH[3]}" + elif [[ "${_cmd}" =~ (^|[[:space:]])--repo=([^[:space:]]+) ]]; then + _ref="${BASH_REMATCH[2]}" + elif [[ -n "${GH_REPO:-}" ]]; then + _ref="${GH_REPO}" + else + _dir="$(hook_field '.cwd')" + [[ -z "${_dir}" ]] && _dir="${PWD}" + if [[ "${_cmd}" =~ (^|[[:space:]\;\&])cd[[:space:]]+([^[:space:]\&\;]+)[[:space:]]*\&\& ]]; then + _cd="${BASH_REMATCH[2]}" + if [[ "${_cd}" == /* ]]; then + _dir="${_cd}" + else + _dir="${_dir}/${_cd}" + fi + fi + _ref="$(git -C "${_dir}" remote get-url origin 2>/dev/null)" + fi + _repo_slug "${_ref}" +} + main() { hook_read_input local cmd @@ -59,6 +132,15 @@ main() { return 0 fi + # Repository scoping: the English-only rule guards THIS repo's artifacts. + # A command that demonstrably targets another repository passes through; + # an undeterminable target keeps the guard (conservative, unchanged). + local target + target="$(_target_repo "${cmd}")" + if [[ -n "${target}" && "${target}" != "$(_this_repo)" ]]; then + return 0 + fi + # Extract body + title text to scan. Strategy: pull each flag's value. local to_scan="" diff --git a/doc/changelog/CHANGELOG.md b/doc/changelog/CHANGELOG.md index 436eec57..dc7f1480 100644 --- a/doc/changelog/CHANGELOG.md +++ b/doc/changelog/CHANGELOG.md @@ -486,6 +486,23 @@ not deferred to release. `release-tag.sh` promotes `[Unreleased]` → ### Fixed +- **`enforce_gh_english.sh` is scoped to this repository** + (`.agents/hook/enforce_gh_english.sh`): the same machine now hosts sibling + repos whose issues / PRs are written in another language, and their + `gh --repo ` calls run from this repo's cwd, so this PreToolUse Bash + hook fired on them and denied their CJK bodies. The hook now allows early + when the `gh issue|pr create|comment` command demonstrably targets another + repository: an explicit `--repo ` / `-R ` / `--repo=` + naming a repo other than this one (owner/name, host/owner/name, URL and + `.git` forms are normalised, case-insensitively), else `$GH_REPO`, else the + origin of the directory the command runs in (a leading `cd &&`, the + payload's `.cwd`, or `$PWD`) resolving to another repo. "This repo" is + derived from the checkout's `git remote get-url origin` with a literal + `ycpss91255/initialization` fallback. An undeterminable target keeps the + guard, and behaviour for this repo is unchanged. Covered by six new cases in + `test/unit/hook/enforce_gh_english_spec.bats` (other repo via `--repo`, + `-R`, `--repo=`; this repo via `--repo`; no `--repo` inside this repo still + denied; no `--repo` from a cwd outside this repo allowed). - **`just -f justfile.ci lint` no longer lints gitignored third-party files** (`script/ci/ci.sh`): `_find_lintable_sh` collected `*.sh`/`*.bash`/`*.bats` with a raw `find`, which also caught machine-local third-party skills diff --git a/test/unit/hook/enforce_gh_english_spec.bats b/test/unit/hook/enforce_gh_english_spec.bats index 9dab46e7..96da456b 100644 --- a/test/unit/hook/enforce_gh_english_spec.bats +++ b/test/unit/hook/enforce_gh_english_spec.bats @@ -90,3 +90,58 @@ _require_python3() { [ "${status}" -eq 0 ] [ -z "${output}" ] } + +# ── repository scoping: the rule is this repo's, not the host's ────────────── +# The same machine hosts sibling repos whose issues / PRs are written in +# another language, and `gh --repo ` for them runs from this cwd. The +# hook must only guard ycpss91255/initialization: an explicit --repo / -R +# naming another repo passes through, and so does a cwd outside this checkout. +# Without either signal (this repo, no --repo) the guard is unchanged. + +@test "allows CJK when --repo names another repository" { + _require_python3 + _run_hook 'gh issue create --repo other/repo --title "修复错误" --body "你好世界"' + [ "${status}" -eq 0 ] + [ -z "${output}" ] +} + +@test "allows CJK when -R names another repository" { + _require_python3 + _run_hook 'gh pr create -R other/repo --title "feat" --body "你好世界"' + [ "${status}" -eq 0 ] + [ -z "${output}" ] +} + +@test "allows CJK when --repo= (equals form) names another repository" { + _require_python3 + _run_hook 'gh issue comment 7 --repo=other/repo --body "ありがとう"' + [ "${status}" -eq 0 ] + [ -z "${output}" ] +} + +@test "still denies CJK when --repo names this repository" { + _require_python3 + _run_hook 'gh issue create --repo ycpss91255/initialization --title "bug" --body "你好世界"' + [ "${status}" -eq 0 ] + [[ "${output}" == *"deny"* ]] +} + +@test "still denies CJK without --repo inside this repository" { + _require_python3 + _run_hook 'gh issue create --title "bug" --body "你好世界"' + [ "${status}" -eq 0 ] + [[ "${output}" == *"deny"* ]] +} + +@test "allows CJK without --repo when cwd is outside this repository" { + _require_python3 + local _elsewhere="${BATS_TEST_TMPDIR}/elsewhere" + mkdir -p "${_elsewhere}" + git -C "${_elsewhere}" init -q + git -C "${_elsewhere}" remote add origin https://github.com/other/repo.git + run bash -c 'cd "$3" && printf "%s" "$1" | "$2"' _ \ + "$(_json 'gh issue create --title "bug" --body "你好世界"')" \ + "${HOOK_SH}" "${_elsewhere}" + [ "${status}" -eq 0 ] + [ -z "${output}" ] +}