#!/usr/bin/env bash
# .githooks/pre-commit - refuse to commit plaintext into secrets/.
# Enable once per clone:  git config core.hooksPath .githooks
set -euo pipefail
fail=0
while IFS= read -r -d '' f; do
  case "$f" in secrets/README.md|secrets/.gitkeep) continue ;; esac
  case "$f" in
    *.enc.yaml|*.enc.yml|*.enc.json|*.enc.env) ;;
    *) echo "pre-commit: $f must be named *.enc.yaml, *.enc.json or *.enc.env" >&2; fail=1; continue ;;
  esac
  # Check the STAGED copy (what will be committed), not the working tree.
  if ! git show ":$f" | grep -qE '^sops:|"sops": *\{|^sops_'; then
    echo "pre-commit: $f looks like PLAINTEXT. Encrypt it before committing." >&2; fail=1
  fi
done < <(git diff --cached --name-only -z --diff-filter=ACMR -- secrets/)
exit "$fail"
