#!/bin/sh
# pre-commit: block any commit that adds invisible Unicode (tag characters, zero-width, direction controls) to a text file.
# Install: cp pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit   (hidden.py beside it, or set HIDDEN_PY)
HIDDEN_PY="${HIDDEN_PY:-$(git rev-parse --show-toplevel)/.githooks/hidden.py}"
[ -f "$HIDDEN_PY" ] || HIDDEN_PY="$(dirname "$0")/hidden.py"
files=$(git diff --cached --name-only --diff-filter=ACM)
[ -z "$files" ] && exit 0
tmp=$(mktemp -d)
for f in $files; do                       # check what is being committed (the staged copy), not the working file
  mkdir -p "$tmp/$(dirname "$f")"
  git show ":$f" > "$tmp/$f" 2>/dev/null
done
( cd "$tmp" && python3 "$HIDDEN_PY" $files )
status=$?
rm -r "$tmp"
if [ $status -ne 0 ]; then
  echo "commit blocked: hidden characters in the files above (python3 hidden.py --strip FILE removes them)"
  exit 1
fi
exit 0
