#!/usr/bin/env bash
# pre-commit hook — installed by scripts/setup-hooks.sh
#
# Blocks a commit that would put a credential into git history.
#
# This is the highest-value security control in the repo, for one reason:
# a secret that reaches a commit is not fixed by deleting it in the next
# commit. It stays in history, and if the branch was ever pushed it is
# public forever and the only real remedy is rotating the key. Catching it
# here is the difference between a two-second fix and a rotation.
#
# Scope: staged content only. Files ignored by git cannot be committed, so
# .env and everything under orgs/ are out of reach by construction — those
# are the weekly audit's job (the maintainer's security-audit script (not shipped in the package)), not this hook's.
#
# To bypass in a genuine emergency:
#   git commit --no-verify
# If you find yourself reaching for that, the right move is almost always to
# unstage the file instead.

set -euo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"
# shellcheck source=../lib/secret-patterns.sh
source "$REPO_ROOT/scripts/lib/secret-patterns.sh"

# Nothing staged (e.g. `git commit` during a merge with no changes) — allow.
if git diff --cached --quiet; then
  exit 0
fi

FINDINGS=""

# Walk staged files individually rather than scanning one big diff, so a
# finding can name its file and line. --diff-filter=d skips deletions: removing
# a secret must never be blocked, which is exactly the commit someone makes to
# fix one of these.
while IFS= read -r file; do
  [[ -z "$file" ]] && continue
  [[ ! -f "$file" ]] && continue

  if printf '%s' "$file" | grep -Eq -- "$SECRET_SCAN_EXCLUDE_RE"; then
    continue
  fi

  # Skip binaries — a false positive inside a PNG helps nobody.
  if ! grep -Iq . "$file" 2>/dev/null; then
    continue
  fi

  # Scan the STAGED blob, not the working tree. They differ whenever something
  # was edited after `git add`, and it is the staged bytes that get committed.
  staged="$(git show ":$file" 2>/dev/null)" || continue

  while IFS= read -r hit; do
    [[ -z "$hit" ]] && continue
    lineno="${hit%%:*}"
    text="${hit#*:}"
    secret_is_placeholder "$text" && continue
    label="$(secret_label_for "$text")"
    FINDINGS+="  $file:$lineno — $label"$'\n'
  done < <(printf '%s' "$staged" | grep -nE -f <(secret_patterns_regex) || true)

done < <(git diff --cached --name-only --diff-filter=d)

if [[ -n "$FINDINGS" ]]; then
  echo "" >&2
  echo "[pre-commit] BLOCKED — credential-shaped text in staged content:" >&2
  echo "" >&2
  printf '%s' "$FINDINGS" >&2
  echo "" >&2
  echo "A secret in a commit is not fixed by a later commit. It stays in" >&2
  echo "history. Take it out now:" >&2
  echo "" >&2
  echo "  1. Move the value into .env (gitignored) and read it from there." >&2
  echo "  2. git reset HEAD <file>, fix the file, stage it again." >&2
  echo "" >&2
  echo "If this is a placeholder rather than a live key, rename the file to" >&2
  echo "*.example, or add a narrow rule to SECRET_SCAN_EXCLUDE_RE in" >&2
  echo "scripts/lib/secret-patterns.sh — with a comment saying why." >&2
  echo "" >&2
  echo "Commit aborted." >&2
  exit 1
fi

exit 0
