|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Shared helpers for gitignored banned-attribution patterns (no literals in callers). |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +banned_patterns_file() { |
| 6 | + local root="${1:-}" |
| 7 | + if [[ -z "$root" ]]; then |
| 8 | + root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" |
| 9 | + fi |
| 10 | + local private="${root}/.cursor/private/banned-attribution-patterns" |
| 11 | + if [[ -f "$private" && -s "$private" ]]; then |
| 12 | + printf '%s' "$private" |
| 13 | + return 0 |
| 14 | + fi |
| 15 | + local openclaw="${OPENCLAW_ATTRIBUTION_PATTERNS:-${HOME:-}/.cursor/openclaw/banned-attribution-patterns}" |
| 16 | + if [[ -f "$openclaw" && -s "$openclaw" ]]; then |
| 17 | + printf '%s' "$openclaw" |
| 18 | + return 0 |
| 19 | + fi |
| 20 | + printf '%s' "$private" |
| 21 | +} |
| 22 | + |
| 23 | +# banned_patterns_ready reports whether a valid banned-attribution patterns file exists and is non-empty. |
| 24 | +# banned_patterns_ready accepts an optional root directory argument used to resolve the patterns file; it exits with status 0 if the resolved file exists and has size > 0, non-zero otherwise. |
| 25 | +banned_patterns_ready() { |
| 26 | + local f |
| 27 | + f="$(banned_patterns_file "${1:-}")" |
| 28 | + [[ -f "$f" && -s "$f" ]] |
| 29 | +} |
| 30 | + |
| 31 | +# list_banned_pattern_tokens streams banned-attribution pattern tokens (one per line) from the repository or user patterns file. |
| 32 | +# It resolves the patterns file (optional `root` argument), reads it line-by-line, removes inline comments (`#`) and all whitespace, skips empty tokens, and writes each remaining token to stdout on its own line. |
| 33 | +# Usage: while read -r token; do ...; done < <(list_banned_pattern_tokens "$root") |
| 34 | +# Parameters: |
| 35 | +# root (optional) — repository root directory to use when resolving the patterns file; if omitted, the script determines the root automatically. |
| 36 | +# Exit: |
| 37 | +# Returns non-zero if the resolved patterns file does not exist or cannot be read. |
| 38 | +list_banned_pattern_tokens() { |
| 39 | + local f token |
| 40 | + f="$(banned_patterns_file "${1:-}")" |
| 41 | + if [[ ! -f "$f" ]]; then |
| 42 | + return 1 |
| 43 | + fi |
| 44 | + while IFS= read -r token || [[ -n "$token" ]]; do |
| 45 | + token="${token%%#*}" |
| 46 | + token="$(printf '%s' "$token" | tr -d '[:space:]')" |
| 47 | + [[ -n "$token" ]] || continue |
| 48 | + printf '%s\n' "$token" |
| 49 | + done <"$f" |
| 50 | +} |
| 51 | + |
| 52 | +# first_banned_pattern_token outputs the first non-empty, non-comment banned-attribution pattern token from the resolved patterns file (takes an optional root directory argument). |
| 53 | +# It prints the token to stdout and returns success; if no file or no token is found it returns a non-zero status. |
| 54 | +first_banned_pattern_token() { |
| 55 | + local f token |
| 56 | + f="$(banned_patterns_file "${1:-}")" |
| 57 | + if [[ ! -f "$f" ]]; then |
| 58 | + return 1 |
| 59 | + fi |
| 60 | + while IFS= read -r token || [[ -n "$token" ]]; do |
| 61 | + token="${token%%#*}" |
| 62 | + token="$(printf '%s' "$token" | tr -d '[:space:]')" |
| 63 | + [[ -n "$token" ]] || continue |
| 64 | + printf '%s' "$token" |
| 65 | + return 0 |
| 66 | + done <"$f" |
| 67 | + return 1 |
| 68 | +} |
| 69 | + |
| 70 | +# line_matches_banned_pattern checks whether a lowercased line contains any banned-attribution pattern token; tokens are lowercased before matching and are read from the resolved patterns file. |
| 71 | +line_matches_banned_pattern() { |
| 72 | + local line_lc="$1" |
| 73 | + local root="${2:-}" |
| 74 | + local token token_lc |
| 75 | + while IFS= read -r token; do |
| 76 | + token_lc="$(printf '%s' "$token" | tr '[:upper:]' '[:lower:]')" |
| 77 | + if [[ "$line_lc" == *"$token_lc"* ]]; then |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + done < <(list_banned_pattern_tokens "$root" 2>/dev/null || true) |
| 81 | + return 1 |
| 82 | +} |
| 83 | + |
| 84 | +openclaw_workspace_root() { |
| 85 | + local root="${1:-}" |
| 86 | + if [[ -z "$root" ]]; then |
| 87 | + root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" |
| 88 | + fi |
| 89 | + local cur="$root" |
| 90 | + while [[ "$cur" != "/" && -n "$cur" ]]; do |
| 91 | + if [[ -d "$cur/orama-system" ]]; then |
| 92 | + printf '%s' "$cur" |
| 93 | + return 0 |
| 94 | + fi |
| 95 | + cur="$(dirname "$cur")" |
| 96 | + done |
| 97 | + printf '%s' "$root" |
| 98 | +} |
| 99 | + |
| 100 | +verboten_literals_file() { |
| 101 | + local root="${1:-}" |
| 102 | + if [[ -n "${OPENCLAW_VERBOTEN_LITERALS:-}" ]]; then |
| 103 | + printf '%s' "$OPENCLAW_VERBOTEN_LITERALS" |
| 104 | + return 0 |
| 105 | + fi |
| 106 | + printf '%s/.verboten-literals.local' "$(openclaw_workspace_root "$root")" |
| 107 | +} |
| 108 | + |
| 109 | +list_private_literal_values() { |
| 110 | + local root="${1:-}" selector="${2:-}" f raw key value |
| 111 | + f="$(verboten_literals_file "$root")" |
| 112 | + [[ -f "$f" ]] || return 1 |
| 113 | + while IFS= read -r raw || [[ -n "$raw" ]]; do |
| 114 | + raw="${raw%%#*}" |
| 115 | + raw="$(printf '%s' "$raw" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')" |
| 116 | + [[ -n "$raw" ]] || continue |
| 117 | + case "$raw" in |
| 118 | + *=*) |
| 119 | + key="${raw%%=*}" |
| 120 | + value="${raw#"$key="}" |
| 121 | + value="$(printf '%s' "$value" | tr -d '[:space:]')" |
| 122 | + [[ -n "$value" ]] || continue |
| 123 | + [[ -z "$selector" || "$key" == "$selector" ]] || continue |
| 124 | + printf '%s\n' "$value" |
| 125 | + ;; |
| 126 | + esac |
| 127 | + done <"$f" |
| 128 | +} |
| 129 | + |
| 130 | +private_owner_email_ok() { |
| 131 | + local email_lc="$1" root="${2:-}" token token_lc |
| 132 | + while IFS= read -r token; do |
| 133 | + token_lc="$(printf '%s' "$token" | tr '[:upper:]' '[:lower:]')" |
| 134 | + [[ "$email_lc" == "$token_lc" ]] && return 0 |
| 135 | + done < <(list_private_literal_values "$root" owner_gmail 2>/dev/null || true) |
| 136 | + return 1 |
| 137 | +} |
| 138 | + |
| 139 | +private_owner_name_ok() { |
| 140 | + local name_lc="$1" root="${2:-}" token token_lc |
| 141 | + while IFS= read -r token; do |
| 142 | + token_lc="$(printf '%s' "$token" | tr '[:upper:]' '[:lower:]')" |
| 143 | + [[ "$name_lc" == *"$token_lc"* ]] && return 0 |
| 144 | + done < <(list_private_literal_values "$root" owner_name 2>/dev/null || true) |
| 145 | + return 1 |
| 146 | +} |
| 147 | + |
| 148 | +line_matches_private_forbidden_literal() { |
| 149 | + local line_lc="$1" root="${2:-}" token token_lc |
| 150 | + while IFS= read -r token; do |
| 151 | + token_lc="$(printf '%s' "$token" | tr '[:upper:]' '[:lower:]')" |
| 152 | + [[ "$line_lc" == *"$token_lc"* ]] && return 0 |
| 153 | + done < <(list_private_literal_values "$root" forbidden_attribution 2>/dev/null || true) |
| 154 | + return 1 |
| 155 | +} |
| 156 | + |
| 157 | +# banned_attribution_hit returns 0 when author/committer/body metadata matches a banned pattern. |
| 158 | +banned_attribution_hit() { |
| 159 | + local ae_lc="$1" an_lc="$2" ce_lc="$3" cn_lc="$4" body_lc="$5" |
| 160 | + local root="${6:-}" |
| 161 | + if ! banned_patterns_ready "$root"; then |
| 162 | + return 1 |
| 163 | + fi |
| 164 | + line_matches_banned_pattern "$ae_lc" "$root" && return 0 |
| 165 | + line_matches_private_forbidden_literal "$ae_lc" "$root" && return 0 |
| 166 | + line_matches_banned_pattern "$an_lc" "$root" && return 0 |
| 167 | + line_matches_private_forbidden_literal "$an_lc" "$root" && return 0 |
| 168 | + line_matches_banned_pattern "$ce_lc" "$root" && return 0 |
| 169 | + line_matches_private_forbidden_literal "$ce_lc" "$root" && return 0 |
| 170 | + line_matches_banned_pattern "$cn_lc" "$root" && return 0 |
| 171 | + line_matches_private_forbidden_literal "$cn_lc" "$root" && return 0 |
| 172 | + local line line_lc |
| 173 | + while IFS= read -r line; do |
| 174 | + line_lc="$(printf '%s' "$line" | tr '[:upper:]' '[:lower:]')" |
| 175 | + case "$line_lc" in |
| 176 | + co-authored-by:*) |
| 177 | + if line_matches_banned_pattern "$line_lc" "$root"; then |
| 178 | + return 0 |
| 179 | + fi |
| 180 | + if line_matches_private_forbidden_literal "$line_lc" "$root"; then |
| 181 | + return 0 |
| 182 | + fi |
| 183 | + ;; |
| 184 | + esac |
| 185 | + done <<< "$body_lc" |
| 186 | + return 1 |
| 187 | +} |
0 commit comments