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>
This commit is contained in:
Jeremy Karst 2026-11-22 08:45:46 -05:00
parent 1bba2be945
commit 097d18f940
5 changed files with 190 additions and 86 deletions

View file

@ -247,15 +247,22 @@ release/vm.sh pull-out 14.3 26.1 # -> /scratch/mono-opnsense/out/26.1
recorded in the ports tree's distinfo, chiefly patches GitHub generates
on the fly (`github.com/<owner>/<repo>/commit/<hash>.patch`): GitHub
abbreviates the object names on their `index` lines with more digits as
a repository grows, so an old patch now differs by a few bytes. FreeBSD's
distfile cache keeps the originals of FreeBSD's ports, but nothing keeps
OPNsense's own (e.g. the patches of `ports-mgmt/pkg`).
- Such ports are fetched on the build host with `release/fetch-patch.sh`,
which shortens the abbreviations until the size matches distinfo; the
port's `checksum` target then verifies the SHA256, so only the original
bytes are accepted. GitHub turns automated requests away now and then
("Service Unavailable"), so the current patch is tried three times, a
minute apart. The results are appended to the distfiles set, which
a repository grows, so an old patch now differs by a few bytes. Codeberg
tag archives (`codeberg.org/<owner>/<repo>/archive/<tag>.tar.gz`) changed
too: Codeberg now compresses them with GNU gzip instead of git's own, and
the same tar comes out smaller. FreeBSD's distfile cache does not help
here. It keeps the originals only of FreeBSD's own ports, so it has
nothing for OPNsense's (e.g. the patches of `ports-mgmt/pkg`). For
`sysutils/sysinfo` it served the new archive as well (2026-11-22).
- Such ports are fetched on the build host with
`release/fetch-distfile.sh`. For a GitHub patch, it shortens the
abbreviations until the size matches distinfo. GitHub turns automated
requests away now and then ("Service Unavailable"), so the current
patch is tried three times, a minute apart. For a Codeberg archive, it
clones the tag and makes the archive with `git archive --format=tar.gz
-6`. Either way, the port's `checksum` target then verifies the
SHA256, so only the original bytes are accepted. The results are
appended to the distfiles set, which
tools extracts into every ports step's chroot. A port whose distfiles
the set has already is skipped, so a rerun downloads nothing for it.
- `ports-mgmt/pkg` is fetched this way first: the ports step's chroot
@ -502,7 +509,7 @@ reflash from Recovery Linux with the signed image from `/releases/` (README.md,
```sh
python3 -m unittest discover -s mirror/tests # gwmirror stage/verify/publish, incl. HTTPS
python3 -m unittest discover -s release/tests # migrate-device.sh, 20-kernel-update, fetch-patch.sh
python3 -m unittest discover -s release/tests # migrate-device.sh, 20-kernel-update, fetch-distfile.sh
release/vm.sh ssh 14.3 'cd /usr/deps && python3 -m unittest discover -s release/tests'
```

View file

@ -37,8 +37,8 @@ COMMITTER="-c user.name=gwmirror-build -c user.email=build@opnsense.karsttech.co
# the terminal of a daemon'd build), and a transfer that moves nothing for 10
# minutes is aborted
export GIT_TERMINAL_PROMPT=0 GIT_HTTP_LOW_SPEED_LIMIT=1 GIT_HTTP_LOW_SPEED_TIME=600
# for ports fetched on this host (make "${FETCH_PATCH}" ...)
FETCH_PATCH="FETCH_CMD=sh ${DEPS}/release/fetch-patch.sh"
# for ports fetched on this host (make "${FETCH_REPAIR}" ...)
FETCH_REPAIR="FETCH_CMD=sh ${DEPS}/release/fetch-distfile.sh"
PORTS_LOG=/var/tmp/gwbuild-ports.log
PORTS_RUN=/var/tmp/gwbuild-ports.run
@ -164,7 +164,7 @@ host_pkg()
mkdir -p /var/tmp/gwbuild-host
# shellcheck disable=SC2046
tar -xf "$(distfiles_set)" -C /var/tmp/gwbuild-host $(port_distfiles ports-mgmt/pkg)
make -C /usr/ports/ports-mgmt/pkg BATCH=yes "${FETCH_PATCH}" \
make -C /usr/ports/ports-mgmt/pkg BATCH=yes "${FETCH_REPAIR}" \
DISTDIR=/var/tmp/gwbuild-host/distfiles WRKDIRPREFIX=/var/tmp/gwbuild-host \
clean all reinstall clean
rm -rf /var/tmp/gwbuild-host
@ -270,8 +270,9 @@ fetch_failures()
}
# repair_distfiles PORT...: fetch the distfiles of the PORTs on this host with
# fetch-patch.sh, which recovers the patches GitHub now generates differently from
# the ones recorded in distinfo (the ports' checksum target verifies the results),
# fetch-distfile.sh, which recovers the GitHub patches and Codeberg archives their
# sites now generate differently from the ones recorded in distinfo (the ports'
# checksum target verifies the results),
# and add them to the distfiles set, which tools extracts into the ports step's
# chroot. They are appended: a file the set already has is extracted twice, both
# copies verified. A PORT whose distfiles the set has already is skipped, so a
@ -288,7 +289,7 @@ repair_distfiles()
continue
fi
make -C "/usr/ports/${_rport}" BATCH=yes ARCH=aarch64 DISTDIR="${_dir}/distfiles" \
"${FETCH_PATCH}" checksum || die "cannot fetch the distfiles of ${_rport}"
"${FETCH_REPAIR}" checksum || die "cannot fetch the distfiles of ${_rport}"
done
if [ -d "${_dir}/distfiles" ]; then
mkdir -p "${SETS}"

99
release/fetch-distfile.sh Normal file
View file

@ -0,0 +1,99 @@
#!/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

View file

@ -1,66 +0,0 @@
#!/bin/sh
#
# fetch-patch.sh: FETCH_CMD for the ports framework (make FETCH_CMD="sh fetch-patch.sh")
# that also recovers patches GitHub generates on the fly. 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 for a GitHub commit,
# pull request or compare patch (or diff) and the framework passed the expected size
# (-S), 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. 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) ;;
*) exit 1 ;;
esac
echo "${SIZE}" | grep -Eqx '[0-9]+' || exit 1
OUT=${OUT:-${URL##*/}}
case ${OUT} in
'' | */ | /* | . | .. | ../* | */.. | */../*) exit 1 ;;
esac
TMP=$(mktemp "./.fetch-patch.XXXXXX") || exit 1
trap 'rm -f "${TMP}" "${TMP}.n"' EXIT
# 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

View file

@ -21,7 +21,7 @@ HERE = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.dirname(os.path.dirname(HERE))
MIGRATE = os.path.join(REPO, "release", "migrate-device.sh")
KERNEL_HOOK = os.path.join(REPO, "rc.d", "20-kernel-update")
FETCH_PATCH = os.path.join(REPO, "release", "fetch-patch.sh")
FETCH_REPAIR = os.path.join(REPO, "release", "fetch-distfile.sh")
MIRROR = "https://opnsense.karsttech.com"
FP_NAME = "opnsense.karsttech.com.20261117"
@ -428,8 +428,8 @@ PATCH_10 = (PATCH_9.replace("0327cae46..0f9a634f2", "0327cae463..0f9a634f2e")
GH_URL = "https://github.com/opnsense/pkg/commit/a54ea1a2f7d858fe6eb2976d688c251e64acf8f8.patch"
class FetchPatchTest(unittest.TestCase):
"""release/fetch-patch.sh, the FETCH_CMD build.sh repairs distfiles with."""
class FetchDistfileTest(unittest.TestCase):
"""release/fetch-distfile.sh, the FETCH_CMD build.sh repairs distfiles with."""
def setUp(self):
self.tmp = tempfile.mkdtemp(prefix="gw-fetch-")
@ -457,7 +457,7 @@ echo direct > "${last##*/}"
def fetch(self, *args, **env):
e = {"PATH": f"{self.fx.bin}:/usr/bin:/bin", "FAKE_LOG": self.fx.log, "FAKE_SERVED": self.served}
e.update(env)
return subprocess.run(["sh", FETCH_PATCH, *args], env=e, cwd=self.distdir,
return subprocess.run(["sh", FETCH_REPAIR, *args], env=e, cwd=self.distdir,
capture_output=True, text=True)
def files(self):
@ -536,6 +536,69 @@ echo direct > "${last##*/}"
self.assertEqual(r.returncode, 1)
self.assertEqual(len(self.fx.calls()), 1)
CB_URL = "https://codeberg.org/BSDforge/sysinfo/archive/1.0.4.tar.gz?dummy=/sysinfo-1.0.4.tar.gz"
def codeberg(self, *args, archive=b"x" * 100, **env):
"""fetch-distfile.sh with a fake git whose archive is ARCHIVE."""
with open(os.path.join(self.tmp, "archive"), "wb") as f:
f.write(archive)
self.fx.make_fake("git", f"""case "$1" in
clone) [ -z "${{FAKE_CLONE_FAIL:-}}" ] || exit 128; for a; do last=$a; done; mkdir "$last" ;;
-C) cat "{self.tmp}/archive" ;;
esac
""")
return self.fetch(*args, **env)
def test_codeberg_archive_made_from_tag(self):
r = self.codeberg("-S", "100", self.CB_URL)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertIn("made from the tag 1.0.4 of codeberg.org/BSDforge/sysinfo", r.stdout)
self.assertEqual(self.files(), ["sysinfo-1.0.4.tar.gz"], "no temporary files left")
with open(os.path.join(self.distdir, "sysinfo-1.0.4.tar.gz"), "rb") as f:
self.assertEqual(f.read(), b"x" * 100)
git = [c for c in self.fx.calls() if c.startswith("git ")]
self.assertEqual(len(git), 2)
self.assertRegex(git[0], r"^git clone -q --bare --depth 1 --branch 1\.0\.4 "
r"https://codeberg\.org/BSDforge/sysinfo\.git \./\.fetch-distfile\.\w+\.git$")
self.assertRegex(git[1], r"^git -C \./\.fetch-distfile\.\w+\.git archive --format=tar\.gz -6 "
r"--prefix=sysinfo/ refs/tags/1\.0\.4$")
def test_codeberg_archive_plain_url_and_output(self):
url = "https://codeberg.org/o/my.repo/archive/v2.1+x.tar.gz"
r = self.codeberg("-S", "100", "-o", "out.tgz", url)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(self.files(), ["out.tgz"])
self.assertIn("--prefix=my.repo/ refs/tags/v2.1+x", self.fx.calls()[-1])
def test_codeberg_archive_wrong_size(self):
r = self.codeberg("-S", "99", self.CB_URL)
self.assertEqual(r.returncode, 1)
self.assertIn("does not have the size 99", r.stderr)
self.assertEqual(self.files(), [], "nothing written, no temporary files left")
def test_codeberg_clone_failure(self):
r = self.codeberg("-S", "100", self.CB_URL, FAKE_CLONE_FAIL="1")
self.assertEqual(r.returncode, 1)
self.assertEqual(self.files(), [])
def test_codeberg_rejects_crafted_urls(self):
for url in ("https://codeberg.org/-o/r/archive/1.0.tar.gz",
"https://codeberg.org/o/-r/archive/1.0.tar.gz",
"https://codeberg.org/o/r/archive/-1.0.tar.gz",
"https://codeberg.org/o/r/archive/--upload-pack=x.tar.gz",
"https://codeberg.org/o/r/archive/../../x.tar.gz",
"https://codeberg.org/o/r/x/archive/1.0.tar.gz",
"https://codeberg.org/o/r/archive/1;rm -rf x.tar.gz",
"https://codeberg.org/o/r/archive/$(id).tar.gz",
"https://codeberg.org/o/r/archive/1.0.zip",
"https://codeberg.org.evil.example/o/r/archive/1.0.tar.gz",
"https://evil.example/codeberg.org/o/r/archive/1.0.tar.gz",
"http://codeberg.org/o/r/archive/1.0.tar.gz"):
r = self.codeberg("-S", "100", url)
self.assertEqual(r.returncode, 1, url)
self.assertEqual([c for c in self.fx.calls() if c.startswith("git ")], [], "git never run")
self.assertEqual(self.files(), [])
def test_rejects_unsafe_output_names(self):
for out in ("../evil.patch", "/tmp/evil.patch", "a/../../evil.patch", "..", ".", "dir/"):
r = self.fetch("-S", str(len(PATCH_9)), "-o", out, GH_URL)