#!/bin/bash # check-excluded-terms.sh - blocks any tracked text, path or commit message in # this repository that matches the operator's local excluded-terms list. # # The list is READ WHERE IT LIVES and never copied into this repository: # ${OKF_WIKI_EXCLUDED_TERMS:-$HOME/.claude/runtime/excluded-terms.local.txt} # One extended regex per line, case-insensitive; '#' lines and blank lines are # skipped. A missing or empty list is a FAIL, never a silent pass: a check that # cannot run has measured nothing. # # Known-positive: the combined pattern must match at least one line of the list # itself, or the query is dead and a zero would be meaningless. # # Usage: # tools/check-excluded-terms.sh scan every tracked file (path + content) # tools/check-excluded-terms.sh --staged scan staged files (pre-commit) # tools/check-excluded-terms.sh --message F scan a commit message file (commit-msg) # add -v as the LAST argument to list the matching paths (never the terms) # # Exit 0 = no match; 1 = match or the check could not run. # bash 3.2 safe, ASCII only. set -u MODE="tree" MSG="" VERBOSE=0 case "${1:-}" in --staged) MODE="staged" ;; --message) MODE="message"; MSG="${2:-}" ;; -v) VERBOSE=1 ;; esac for a in "$@"; do [ "$a" = "-v" ] && VERBOSE=1; done LIST="${OKF_WIKI_EXCLUDED_TERMS:-$HOME/.claude/runtime/excluded-terms.local.txt}" if [ ! -f "$LIST" ]; then echo "FAIL excluded-terms: no term list at $LIST - nothing measured (set OKF_WIKI_EXCLUDED_TERMS)" >&2 exit 1 fi PAT=$(grep -v '^#' "$LIST" | grep -v '^[[:space:]]*$' | paste -sd'|' -) if [ -z "$PAT" ]; then echo "FAIL excluded-terms: term list is empty - nothing measured" >&2 exit 1 fi KP=$(grep -ciE "$PAT" "$LIST") if [ "$?" -eq 2 ] || [ "$KP" -eq 0 ]; then echo "FAIL excluded-terms: pattern does not compile or matches nothing in the list itself - query is dead" >&2 exit 1 fi ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "FAIL excluded-terms: not inside a git repository" >&2; exit 1; } TMP=$(mktemp -d "${TMPDIR:-/tmp}/okf-wiki-terms.XXXXXX") || exit 1 trap 'rm -rf "$TMP"' EXIT : > "$TMP/hits" if [ "$MODE" = "message" ]; then if [ -z "$MSG" ] || [ ! -f "$MSG" ]; then echo "FAIL excluded-terms: no commit message file given" >&2 exit 1 fi if grep -v '^#' "$MSG" | grep -qiE "$PAT"; then echo "FAIL excluded-terms: the commit message matches the excluded-terms list" >&2 exit 1 fi exit 0 fi if [ "$MODE" = "staged" ]; then git -C "$ROOT" diff --cached --name-only -z --diff-filter=ACMR > "$TMP/files" else git -C "$ROOT" ls-files -z > "$TMP/files" fi TOTAL=$(tr -cd '\0' < "$TMP/files" | wc -c | tr -d ' ') while IFS= read -r -d '' f; do if printf '%s\n' "$f" | grep -qiE "$PAT"; then echo "$f" >> "$TMP/hits" continue fi if [ "$MODE" = "staged" ]; then git -C "$ROOT" show ":$f" 2>/dev/null | grep -IqiE "$PAT" && echo "$f" >> "$TMP/hits" else [ -f "$ROOT/$f" ] || continue grep -IqiE "$PAT" "$ROOT/$f" 2>/dev/null && echo "$f" >> "$TMP/hits" fi done < "$TMP/files" HITS=$(wc -l < "$TMP/hits" | tr -d ' ') [ "$VERBOSE" -eq 1 ] && sed 's/^/ hit: /' "$TMP/hits" if [ "$HITS" -eq 0 ]; then echo "PASS excluded-terms ($MODE): 0 of $TOTAL files match (known-positive: $KP list lines)" exit 0 fi echo "FAIL excluded-terms ($MODE): $HITS of $TOTAL files match (add -v to list the paths)" >&2 exit 1