#!/usr/bin/env bash
# Recover a commit in a temporary repository. Does not touch the current checkout.
set -euo pipefail

echo "git_version=$(git --version)"
tmp="$(mktemp -d "${TMPDIR:-/tmp}/fm-git-lab.XXXXXX")"
cleanup() { rm -rf "$tmp"; }
trap cleanup EXIT

git -C "$tmp" init -b main >/dev/null
git -C "$tmp" config user.email "lab@example.invalid"
git -C "$tmp" config user.name "Lab"

printf 'sku,price\nA1,10\n' >"$tmp/catalog.csv"
git -C "$tmp" add catalog.csv
git -C "$tmp" commit -m "add catalog" >/dev/null

printf 'sku,price\nA1,12\n' >"$tmp/catalog.csv"
git -C "$tmp" add catalog.csv
git -C "$tmp" commit -m "raise price" >/dev/null

git -C "$tmp" reset --hard HEAD~1 >/dev/null
git -C "$tmp" reset --hard 'HEAD@{1}' >/dev/null

echo "commits_after_recovery=$(git -C "$tmp" rev-list --count HEAD)"
echo "head_subject=$(git -C "$tmp" log -1 --format=%s)"
echo "lab=ok"
