#!/bin/sh
set -e
if [ -z "$PUSH_KEY" ]; then
    echo "Please set PUSH_KEY in .env"
    exit 1
fi

if [ -z "$PUSH_DELAY" ]; then
    echo "Please set PUSH_DELAY in .env to a string compatible with sleep(1)"
    exit 1
fi

if [ -z "$PUSH_URL" ]; then
    echo "Please set PUSH_DELAY in .env to a string compatible with sleep(1)"
    exit 1
fi

cd /git
git rev-parse --git-dir || (echo "No repository detected at /git" && exit 1)

#use global config to not modify the repository
git config --global core.sshCommand "/usr/bin/ssh -o 'StrictHostKeyChecking no' -i /ssh/$PUSH_KEY"
while true; do
    #using --mirror causes errors when pushing, but --prune,--force, plus these refspecs should catch most stuff
    git push --force --prune "$PUSH_URL" '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*' '+refs/change/*:refs/change/*' || true
    sleep "$PUSH_DELAY"
done
