#!/bin/sh
# Looks for secrets in what is about to be committed (pre-commit hook). The local gitleaks if there is one,
# otherwise the official Docker image, pinned; with neither it says so and lets the commit through: the
# continuous integration scans as well.
set -eu

IMAGE="zricethezav/gitleaks:v8.30.1"
root="$(git rev-parse --show-toplevel)"

if command -v gitleaks >/dev/null 2>&1; then
  exec gitleaks git "$root" --staged --redact --no-banner
fi

if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then
  exec docker run --rm -v "$root:/repo" \
    -e GIT_CONFIG_COUNT=1 -e GIT_CONFIG_KEY_0=safe.directory -e GIT_CONFIG_VALUE_0=/repo \
    "$IMAGE" git /repo --staged --redact --no-banner
fi

echo "gitleaks: neither the binary nor Docker is available, the secrets scan is skipped." >&2
