#!/usr/bin/env bash
set -euo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT"

zero_sha="0000000000000000000000000000000000000000"
should_run=false

if [ -t 0 ]; then
    should_run=true
else
    while read -r local_ref local_oid remote_ref remote_oid; do
        if [[ -n "$local_oid" && "$local_oid" != "$zero_sha" && "$local_ref" != "(delete)" ]]; then
            should_run=true
        fi
    done
fi

if [[ "$should_run" == "false" ]]; then
    exit 0
fi

echo "=========================================="
echo "Running pre-push CI checks (lint & test)..."
echo "=========================================="

echo "--> [1/2] Running make lint..."
make lint

echo "--> [2/2] Running make test..."
make test

echo "=========================================="
echo "All pre-push checks passed successfully!"
echo "=========================================="
exit 0
