opnsense-deps/release/fetch-distfile.sh
Jeremy Karst 097d18f940 Recover Codeberg tag archives; fetch-patch.sh is now fetch-distfile.sh
Codeberg now compresses tag archives with GNU gzip instead of git's own,
so sysutils/sysinfo's 1.0.4 archive is 15546 bytes, not distinfo's 15618,
and FreeBSD's distfile cache serves the new one too. git archive of the
tag with git's gzip at level 6 reproduces the original exactly (SHA256
checked by the port). The FETCH_CMD wrapper does that for Codeberg tag
archives, after checking owner, repository and tag strictly, and is
renamed for the second kind of repair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 08:45:46 -05:00

99 lines
3.4 KiB
Bash

#!/bin/sh
#
# fetch-distfile.sh: FETCH_CMD for the ports framework (make FETCH_CMD="sh
# fetch-distfile.sh") that also recovers distfiles their sites now generate
# differently. build.sh uses it to repair distfiles that no longer download as recorded
# in distinfo.
#
# It runs fetch(1) as the default FETCH_CMD does. When that fails and the framework
# passed the expected size (-S), two kinds are recovered:
# - a GitHub commit, pull request or compare patch (or diff): it downloads the current
# patch and shortens the abbreviated object names on its "index" lines until the
# size matches. GitHub abbreviates them with more digits as a repository grows,
# which is how its old patches change.
# - a Codeberg tag archive (codeberg.org/OWNER/REPO/archive/TAG.tar.gz): it clones the
# tag and makes the archive with git's own gzip, as Codeberg did before it changed
# compressors (2026: sysutils/sysinfo).
# Only the size is checked here; the port's checksum target verifies the file against
# distinfo's SHA256, so a wrong guess is rejected there.
#
# Copyright 2026 Karst Tech
# SPDX-License-Identifier: BSD-2-Clause
set -u
fetch -Fpr "$@" && exit 0
SIZE=
URL=
OUT=
while [ $# -gt 0 ]; do
case ${1} in
-S) SIZE=${2:-}; shift ;;
-o) OUT=${2:-}; shift ;;
-[NT]) shift ;;
-*) ;;
*) URL=${1} ;;
esac
shift
done
case ${URL} in
https://github.com/*/*/commit/*.patch | https://github.com/*/*/commit/*.diff | \
https://github.com/*/*/pull/*.patch | https://github.com/*/*/pull/*.diff | \
https://github.com/*/*/compare/*.patch | https://github.com/*/*/compare/*.diff)
KIND=patch ;;
https://codeberg.org/*/*/archive/*.tar.gz | https://codeberg.org/*/*/archive/*.tar.gz\?dummy=/*)
KIND=codeberg ;;
*) exit 1 ;;
esac
echo "${SIZE}" | grep -Eqx '[0-9]+' || exit 1
OUT=${OUT:-${URL##*/}}
case ${OUT} in
'' | */ | /* | . | .. | ../* | */.. | */../*) exit 1 ;;
esac
TMP=$(mktemp "./.fetch-distfile.XXXXXX") || exit 1
trap 'rm -rf "${TMP}" "${TMP}.n" "${TMP}.git"' EXIT
if [ "${KIND}" = codeberg ]; then
# owner, repository and tag as Codeberg allows them; a tag never starts with "-"
_path=${URL#https://codeberg.org/}
_path=${_path%%\?*}
echo "${_path}" | grep -Eqx '[A-Za-z0-9][A-Za-z0-9._-]*/[A-Za-z0-9][A-Za-z0-9._-]*/archive/[A-Za-z0-9][A-Za-z0-9._+-]*\.tar\.gz' ||
exit 1
OWNER=${_path%%/*}
REPO=${_path#*/}
REPO=${REPO%%/*}
TAG=${_path##*/}
TAG=${TAG%.tar.gz}
git clone -q --bare --depth 1 --branch "${TAG}" "https://codeberg.org/${OWNER}/${REPO}.git" "${TMP}.git" ||
exit 1
git -C "${TMP}.git" archive --format=tar.gz -6 --prefix="${REPO}/" "refs/tags/${TAG}" > "${TMP}" ||
exit 1
if [ "$(wc -c < "${TMP}")" -ne "${SIZE}" ]; then
echo "=> ${OUT}: the archive of ${OWNER}/${REPO} ${TAG} does not have the size ${SIZE}" >&2
exit 1
fi
mv "${TMP}" "${OUT}" || exit 1
echo "=> ${OUT}: made from the tag ${TAG} of codeberg.org/${OWNER}/${REPO}"
exit 0
fi
# GitHub turns requests away now and then ("Service Unavailable")
for TRY in 1 2 3; do
fetch -q -o "${TMP}" "${URL}" && break
[ "${TRY}" -lt 3 ] || exit 1
sleep 60
done
for N in 7 8 9 10 11 12; do
sed -E "s/^index ([0-9a-f]{${N}})[0-9a-f]*\.\.([0-9a-f]{${N}})[0-9a-f]*/index \1..\2/" \
"${TMP}" > "${TMP}.n" || exit 1
if [ "$(wc -c < "${TMP}.n")" -eq "${SIZE}" ]; then
mv "${TMP}.n" "${OUT}" || exit 1
echo "=> ${OUT}: recovered from GitHub's current patch (index lines at ${N} digits)"
exit 0
fi
done
echo "=> ${OUT}: no abbreviation of GitHub's current patch has the size ${SIZE}" >&2
exit 1