#!/bin/sh
# Vendor git-forge's source into forge/ for the image build. Derived state:
# gitignored, and running this again replaces it.
#
#	GIT_FORGE_SRC	where to take it from, default ../git-forge
#	GIT_FORGE_REV	which commit, default the one in forge.rev
set -eu

src="${GIT_FORGE_SRC:-../git-forge}"
rev="${GIT_FORGE_REV:-$(cat forge.rev)}"

# Resolved before anything is removed, so a revision that is not there fails
# with the tree that was vendored last still in place. --verify, because plain
# rev-parse echoes a forty character sha back without looking for it.
sha=$(git -C "$src" rev-parse --verify "$rev^{commit}")

# Into a file rather than a pipe: a pipeline exits with the status of its last
# command, so a failed archive would be an empty forge/ and an exit of zero.
archive=$(mktemp)
trap 'rm -f "$archive"' EXIT
git -C "$src" archive -o "$archive" "$sha"

rm -rf forge
mkdir forge
tar -xf "$archive" -C forge
echo "$sha" >forge/.rev

echo "vendored git-forge $sha from $src"
