#!/bin/sh
# Push content subrepo before pushing main repo
remote="$1"
#export GIT_TRACE=1
#export GIT_TRACE_PACKET=1
#export GIT_TRACE_PERFORMANCE=1
#export GIT_SSH_COMMAND="ssh -vvv"

set -euo pipefail
# set -x


export TEST_SCHEMA="http"

# Path to store last tested commit hash
TEST_CACHE_FILE=".last_tested_commit"

# Get current commit hash
CURRENT_COMMIT=$(git rev-parse HEAD)

if [ -f "$TEST_CACHE_FILE" ] && grep -q "$CURRENT_COMMIT" "$TEST_CACHE_FILE"; then
  echo "Tests already passed for commit $CURRENT_COMMIT. Skipping tests."
else
  npm run test:prepush

  if [ $? -ne 0 ]; then
    echo "Tests failed. Push aborted."
    exit 1
  fi
fi

cd content
git push "$remote" main
cd ..
# set +x
