#!/bin/sh
# ci-zit.sh CMD [ARG...] — run CMD with the zit this check-in pins first
# on PATH.
#
# The tests drive a real zit, and the pin is scripts/build-linux.sh's
# ZIT_VERSION: the zit a deploy of this check-in would ship. CI used to
# test with whatever zit the runner machine had, which is the one the
# LAST deploy shipped, so a branch that moved to a zit with a new verb
# could not pass until production had it (zit 0.3.35's merge-tree,
# 2026-09-29).
#
# When the zit on PATH is already the pinned one, nothing is fetched.
# Otherwise the release tarball is read from ZIT_RELEASE_DIR (the same
# directory a deploy reads), checked against its SHA256SUMS, and cached
# per version under ~/.cache/quarry-ci-zit. A pin nobody has placed there
# fails the task naming the version, rather than testing with another.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
want=$(sed -n 's/^ZIT_VERSION="\${ZIT_VERSION:-\([0-9.]*\)}"$/\1/p' "$here/build-linux.sh")
if [ -z "$want" ]; then
echo "ci-zit: no ZIT_VERSION pin in scripts/build-linux.sh" >&2
exit 1
fi
have=$(zit version 2>/dev/null | awk '{print $2}' || true)
if [ "$have" != "$want" ]; then
case "$(uname -m)" in
x86_64 | amd64) arch=x86_64 ;;
aarch64 | arm64) arch=aarch64 ;;
*) echo "ci-zit: no zit build for $(uname -m)" >&2; exit 1 ;;
esac
os=linux
[ "$(uname -s)" = Darwin ] && os=macos
cache="${XDG_CACHE_HOME:-$HOME/.cache}/quarry-ci-zit/$want"
if [ ! -x "$cache/zit" ]; then
rel="${ZIT_RELEASE_DIR:-}"
tarball="zit-$want-$os-$arch.tar.gz"
if [ -z "$rel" ] || [ ! -r "$rel/$tarball" ] || [ ! -r "$rel/SHA256SUMS" ]; then
echo "ci-zit: this check-in pins zit $want and the runner has ${have:-no zit};" \
"put $tarball and SHA256SUMS in ZIT_RELEASE_DIR (now '${rel}')" >&2
exit 1
fi
tmp=$(mktemp -d)
cp "$rel/$tarball" "$tmp/"
grep " $tarball\$" "$rel/SHA256SUMS" >"$tmp/sums" || true
if [ ! -s "$tmp/sums" ]; then
echo "ci-zit: $rel/SHA256SUMS has no line for $tarball" >&2
rm -rf "$tmp"
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
(cd "$tmp" && sha256sum -c sums >/dev/null) || { echo "ci-zit: checksum mismatch for $tarball" >&2; rm -rf "$tmp"; exit 1; }
else
(cd "$tmp" && shasum -a 256 -c sums >/dev/null) || { echo "ci-zit: checksum mismatch for $tarball" >&2; rm -rf "$tmp"; exit 1; }
fi
tar xzf "$tmp/$tarball" -C "$tmp"
mkdir -p "$cache"
install -m 0755 "$tmp/zit-$want/zit" "$cache/zit.new"
mv -f "$cache/zit.new" "$cache/zit"
rm -rf "$tmp"
fi
PATH="$cache:$PATH"
export PATH
echo "ci-zit: testing with zit $want (the runner has ${have:-none})" >&2
fi
exec "$@"