#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2022-2023 Sarah Vaupel <sarah.vaupel@uniworx.de>, Gregor Kleen <gregor.kleen@ifi.lmu.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

[[ -n "${FORCE_RELEASE}" ]] && exit 0

set -e

if [ -n "$(git status --porcelain)" ]; then
    echo "Working directory isn't clean" >&2
    exit 1
fi

branch="$(git rev-parse --abbrev-ref HEAD)"

if [[ $branch != "master" && $branch != "test" ]]; then
    echo "Not on master or test" >&2
    exit 1
fi

ourHash=$(git rev-parse HEAD)
theirHash=$(git ls-remote origin -h refs/heads/$branch | awk '{ print $1; }')

if [ "$theirHash" != "$ourHash" ]; then
    echo "Local HEAD is not up to date with remote $branch" >&2
    exit 1
fi
