#!/bin/sh
set -eu

if [ ! -d /model/.git ]; then
    find /model -mindepth 1 -maxdepth 1 -exec rm -rf {} +
    GIT_LFS_SKIP_SMUDGE=1 git clone --no-checkout "$MODEL_REPOSITORY" /model
fi

git -C /model fetch --depth 1 origin "$MODEL_BRANCH"
if [ "$MODEL_REQUIRE_BRANCH_HEAD_MATCH" = true ]; then
    test "$(git -C /model rev-parse FETCH_HEAD)" = "$MODEL_REF"
fi
GIT_LFS_SKIP_SMUDGE=1 git -C /model checkout --detach "$MODEL_REF"
git -C /model lfs pull
test "$(git -C /model rev-parse HEAD)" = "$MODEL_REF"
