Files
lotherkandClaude Sonnet 5.5 551bfcdcd6 ext-sync: relative links in plain sh, NetBSD's realpath has no -m/--relative-to
On the MacBook every recipe mirror came out empty (realpath: unknown option
-- m), so userland.sh found 'don't know how to make install' for mcde,
splinter, tasksd.  Checked with a scratch tree: misc/tasksd links resolve,
no dangling link.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 06:34:07 +00:00

100 lines
3.4 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# ext-sync.sh -- make the recipe of a fork in ext/ visible to pkgsrc
#
# usage: tools/ext-sync.sh [PKGSRCDIR]
#
# A fork under ext/src/<name>/ keeps its recipe beside its source
# (ext/src/<name>/pkgsrc/, ext/AGENTS.md): that is the file a person edits and
# the file git tracks. pkgsrc builds a recipe only from inside its own tree --
# the recipe includes ../../mk/bsd.pkg.mk, and bmake resolves that relative to
# the *directory of the file* -- so the recipe is mirrored to
# PKGSRCDIR/<category>/<name>, the category being the first word of its
# CATEGORIES.
#
# A mirror, not a copy that lives on its own: each file is a symlink to the
# tracked original (files only -- a directory symlink would move .CURDIR out of
# the tree and break the ../../mk includes). One file to edit, one file to
# commit; the mirror is build output and pkgsrc's .git/info/exclude names it.
#
# The target is only ever replaced when upstream pkgsrc does not own that path
# (`git ls-files' of the pkgsrc import is empty for it): an upstream package of
# the same name is never touched.
#
# wkdir/ of the mirror is a small Makefile that points pkgsrc's work directory
# at the source tree the recipe builds from (no archive, no extraction), so a
# build compiles ext/src/<name> itself.
#
# Prints one line, "ext-sync: <category>/<name> ...", per recipe mirrored;
# tools/userland.sh reads the list back from it.
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
PKGSRCDIR=${1:-$TOP/pkgsrc}
[ -d "$PKGSRCDIR/mk" ] || { echo "ext-sync: no pkgsrc at $PKGSRCDIR" >&2; exit 1; }
mirror_one()
{
recipe=$1
name=$(basename "$(dirname "$recipe")")
part=
case ${recipe##*/} in
pkgsrc-*) part=-${recipe##*/pkgsrc-} ;;
esac
cat=$(sed -n 's/^CATEGORIES=[[:space:]]*\([^[:space:]]*\).*/\1/p' \
"$recipe/Makefile")
[ -n "$cat" ] || { echo "ext-sync: no CATEGORIES= in $recipe/Makefile" >&2; return 1; }
pkg=$name$part
dst=$PKGSRCDIR/$cat/$pkg
# upstream's own package of this name is never overwritten
if [ -n "$(git -C "$PKGSRCDIR" ls-files "$cat/$pkg" 2>/dev/null)" ]; then
echo "ext-sync: $cat/$pkg belongs to upstream pkgsrc; not mirrored" >&2
return 0
fi
rm -rf "$dst"
mkdir -p "$dst"
# every file of the recipe, by name; a directory ($FILESDIR, patches/)
# is mirrored file by file the same way
(cd "$recipe" && find . -type f | sed 's|^\./||') | while read -r f; do
dd=$dst
case $f in */*) dd=$dst/${f%/*} ;; esac
mkdir -p "$dd"
# relative link target in plain sh: one ../ per component of the
# link's directory below TOP, then the file's path below TOP.
# NetBSD's realpath(1) has no -m / --relative-to (GNU only).
case $dd:$recipe in
"$TOP"/*:"$TOP"/*)
up=
oldifs=$IFS; IFS=/
for c in ${dd#$TOP/}; do up=$up../; done
IFS=$oldifs
rel=$up${recipe#$TOP/}/$f
;;
*) rel=$(cd "$dd" && realpath -m --relative-to=. "$recipe/$f") ;;
esac
ln -s "$rel" "$dst/$f"
done
echo "ext-sync: $cat/$pkg from ${recipe#$TOP/}"
}
for recipe in "$TOP"/ext/src/*/pkgsrc "$TOP"/ext/src/*/pkgsrc-*; do
[ -f "$recipe/Makefile" ] || continue
mirror_one "$recipe" || exit 1
done
# a recipe that was removed from ext/ leaves nothing behind
for d in "$PKGSRCDIR"/*/*/; do
d=${d%/}
[ -L "$d/Makefile" ] || continue
tgt=$(readlink -f "$d/Makefile") || continue
case $tgt in
"$TOP"/ext/src/*) [ -f "$tgt" ] || { rm -rf "$d"; echo "ext-sync: removed stale $d"; } ;;
esac
done