#!/usr/bin/env bash
# Blocks pushing any commit that contains game data, generated C++, Ghidra DBs, or memory snapshots.
pattern='(^|/)(game/|ghidra/|app/generated/default/|notes/snapshots/)|.*\.(iso|xex|pe|wmv)$'
zero=0000000000000000000000000000000000000000
status=0
while read -r local_ref local_sha remote_ref remote_sha; do
    [ "$local_sha" = "$zero" ] && continue  # branch deletion
    if [ "$remote_sha" = "$zero" ]; then
        range=$(git rev-list "$local_sha" --not --remotes)
    else
        range=$(git rev-list "$remote_sha..$local_sha")
    fi
    for commit in $range; do
        bad=$(git diff-tree --no-commit-id --name-only -r --root --diff-filter=ACMR "$commit" | grep -E "$pattern")
        if [ -n "$bad" ]; then
            echo "Push blocked: commit $commit contains game data or game-derived files:" >&2
            echo "$bad" >&2
            status=1
        fi
    done
done
exit $status
