|
| 1 | +#!/usr/bin/env bash |
| 2 | +# session-end.sh - Stop hook for session-end verification |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +REPO_ROOT=$(git -C "$(dirname "$0")" rev-parse --show-toplevel 2>/dev/null) || exit 0 |
| 6 | + |
| 7 | +# Files modified vs HEAD (unstaged + staged, deduplicated) |
| 8 | +MODIFIED=$(git -C "$REPO_ROOT" diff --name-only HEAD 2>/dev/null || true) |
| 9 | +STAGED=$(git -C "$REPO_ROOT" diff --name-only --cached 2>/dev/null || true) |
| 10 | +ALL_CHANGED=$(printf '%s\n%s\n' "$MODIFIED" "$STAGED" | sort -u | grep -v '^$' || true) |
| 11 | + |
| 12 | +[ -z "$ALL_CHANGED" ] && exit 0 |
| 13 | + |
| 14 | +ISSUES="" |
| 15 | + |
| 16 | +block() { |
| 17 | + printf '{"decision":"block","reason":"%s"}' "$1" |
| 18 | + exit 0 |
| 19 | +} |
| 20 | + |
| 21 | +# Check for merge conflict markers |
| 22 | +while IFS= read -r f; do |
| 23 | + FULL="$REPO_ROOT/$f" |
| 24 | + [ -f "$FULL" ] || continue |
| 25 | + if grep -qE '^(<{7}|>{7}|={7}) ' "$FULL" 2>/dev/null; then |
| 26 | + ISSUES="${ISSUES}\\n- Unresolved merge conflict in: $f" |
| 27 | + fi |
| 28 | +done <<< "$ALL_CHANGED" |
| 29 | + |
| 30 | +# Check for hardcoded secret patterns in modified non-test Go files |
| 31 | +GO_FILES=$(printf '%s\n' "$ALL_CHANGED" | grep '\.go$' | grep -v '_test\.go$' || true) |
| 32 | +SECRET_RE='(password|passwd|api_key|apikey|secret_key|auth_token|access_key)\s*[:=]\s*"[^"]+' |
| 33 | +while IFS= read -r f; do |
| 34 | + [ -z "$f" ] && continue |
| 35 | + FULL="$REPO_ROOT/$f" |
| 36 | + [ -f "$FULL" ] || continue |
| 37 | + if grep -iqE "$SECRET_RE" "$FULL" 2>/dev/null; then |
| 38 | + ISSUES="${ISSUES}\\n- Possible hardcoded secret in: $f" |
| 39 | + fi |
| 40 | +done <<< "$GO_FILES" |
| 41 | + |
| 42 | +# Check for stray debug prints in non-test Go files |
| 43 | +while IFS= read -r f; do |
| 44 | + [ -z "$f" ] && continue |
| 45 | + FULL="$REPO_ROOT/$f" |
| 46 | + [ -f "$FULL" ] || continue |
| 47 | + if grep -qE 'fmt\.(Print|Println|Printf)\(' "$FULL" 2>/dev/null; then |
| 48 | + ISSUES="${ISSUES}\\n- Stray fmt.Print* in: $f (use tracing.Log instead)" |
| 49 | + fi |
| 50 | +done <<< "$GO_FILES" |
| 51 | + |
| 52 | +[ -z "$ISSUES" ] && exit 0 |
| 53 | + |
| 54 | +block "Session-end verification found issues:${ISSUES}\\n\\nPlease review and resolve before finishing." |
0 commit comments