#!/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 -eu
set -x

# Only run for specific branches
read_branches() {
  while IFS=' ' read -r _ _ remote_ref _; do
    case "$remote_ref" in
      refs/heads/testing|refs/heads/staging|refs/heads/main|refs/heads/production)
        return 0
        ;;
    esac
  done
  return 1
}

check_submodule_remotes() {
  git submodule foreach --quiet "
    if ! git remote get-url \"$remote\" > /dev/null 2>&1; then
      echo \"Error: submodule '\$name' missing remote '$remote'.\"
      exit 1
    fi
  "
}

if ! read_branches; then
  echo "Skipping tests: not pushing testing, staging, main, or production."
else
  COMMIT_HASH=$(git rev-parse HEAD)

  if ! "./scripts/pre-push-tests.sh" "$COMMIT_HASH"; then
    echo "Tests failed. Aborting push."
    exit 1
  fi
fi

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