# has_minimum_git_sha.sh
# Script to guarantee that the given branch under test is at the least up to date with a given sha.

HAS_MINIMUM_SHA=1

# Only enforce the minimum sha constraint on PRs targeting develop/firmware; others will be given
# a free pass here.
if [ "$bamboo_pull_request_target" != "develop/firmware" ]; then
    echo "Skipping non-applicable PR target branch: $bamboo_pull_request_target"
    echo "HAS_MINIMUM_SHA=$HAS_MINIMUM_SHA" > sha_check_vars.sh
    exit 0
fi


# Checkout the plan branch since the second repo `driving-full` in this plan always defaults
# to master
echo git checkout $bamboo_planRepository_branchName
git checkout $bamboo_planRepository_branchName


# Verify that the branch under test has at least the given commit sha as part of its ancestry
# This is to prevent known broken builds from corrupting our tester machines and other hardware
# and tester assets.
# NOTE, FW_MIN_GIT_SHA is a plan variable and can be updated at
# https://bamboo.zooxlabs.com/chain/admin/config/configureChainVariables.action?buildKey=FWVAL-FTG
git merge-base --is-ancestor ${bamboo.FW_MIN_GIT_SHA} HEAD

ret_code="$?"
if [ "$ret_code" -ne 0 ]
then
    curBranch=`git branch | grep -E "^[*]" | awk '{print $2}'`
    echo "FAILURE--this branch $curBranch must rebase beyond commit sha=${bamboo.FW_MIN_GIT_SHA} [code=$ret_code]."
    HAS_MINIMUM_SHA=0

    # Dump Debug Information
    git log -n 1
fi

# Generate sha_check_vars.sh
echo "HAS_MINIMUM_SHA=$HAS_MINIMUM_SHA" > sha_check_vars.sh

exit $ret_code
