Files
makronix/tools/ext-resync.sh
T

142 lines
4.4 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# ext-resync.sh -- bring the work directory of a fork up to date by what changed
#
# usage: tools/ext-resync.sh resync PKGPATH e.g. wm/mcde
# tools/ext-resync.sh snapshot PKGPATH
#
# A fork (ext/src/<name>, ext/AGENTS.md) is built by pkgsrc from an archive of
# its last commit. pkgsrc extracts it once; a new archive then meant `bmake
# clean' and a build from nothing -- ten minutes of MCDE for one changed file
# (build/logs/userland-2026-11-14p.log). `resync' puts into the work directory
# only the files that differ between the archive pkgsrc extracted (kept, as
# build/pkgsrc-work/PKGPATH/.pristine) and the new one. A changed file has a new
# time stamp and make builds what depends on it, nothing else.
#
# resync exits 0 when the work directory is up to date (or has nothing to
# resync) and 1 when it cannot say: no work directory, no .pristine, a file
# that pkgsrc changed after extracting it (its substitutions) or a file that
# decides how the tree is configured (configure.ac, a new Makefile.am, m4).
# The caller then does `bmake clean', and after the build `snapshot' keeps the
# archive that was extracted.
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
PKGSRC=$TOP/pkgsrc
BMAKE=$TOP/build/pkg/bin/bmake
MAKRONIX_TOP=$TOP
export MAKRONIX_TOP
die() { echo "ext-resync: $*" >&2; exit 2; }
[ $# -eq 2 ] || die "usage: $0 resync|snapshot PKGPATH"
cmd=$1 p=$2
[ -f "$PKGSRC/$p/Makefile" ] || die "no $PKGSRC/$p"
var() { (cd "$PKGSRC/$p" && "$BMAKE" show-var VARNAME="$1"); }
wd=$TOP/build/pkgsrc-work/$p
pristine=$wd/.pristine
distdir=$(var DISTDIR)
distfile=$(var DISTFILES | awk '{print $1}')
archive=$distdir/$distfile
[ -f "$archive" ] || die "no archive $archive"
tmp=${TMPDIR:-/tmp}/ext-resync.$$
trap 'rm -rf "$tmp"' EXIT HUP INT TERM
mkdir -p "$tmp"
extract_to() { # DIR: the archive's files, in DIR/<top>
mkdir -p "$1"
tar -xzf "$archive" -C "$1"
}
case $cmd in
snapshot)
rm -rf "$pristine"
extract_to "$pristine"
echo "ext-resync: $p: archive kept in ${pristine#$TOP/}"
exit 0 ;;
resync) ;;
*) die "unknown command $cmd" ;;
esac
wrksrc=$(var WRKSRC)
[ -d "$wrksrc" ] || exit 1
[ -d "$pristine" ] || { echo "ext-resync: $p: no .pristine, cleaning"; exit 1; }
top=$(ls "$pristine" | head -1)
[ -n "$top" ] || exit 1
extract_to "$tmp/new"
newtop=$(ls "$tmp/new" | head -1)
old=$pristine/$top
new=$tmp/new/$newtop
(cd "$new" && find . -type f | sort) > "$tmp/newfiles"
(cd "$old" && find . -type f | sort) > "$tmp/oldfiles"
: > "$tmp/changed"
: > "$tmp/added"
while IFS= read -r f; do
if [ ! -f "$old/$f" ]; then
echo "$f" >> "$tmp/added"
elif ! cmp -s "$old/$f" "$new/$f"; then
echo "$f" >> "$tmp/changed"
fi
done < "$tmp/newfiles"
comm -23 "$tmp/oldfiles" "$tmp/newfiles" > "$tmp/removed"
n=$(cat "$tmp/changed" "$tmp/added" "$tmp/removed" | wc -l | tr -d ' ')
if [ "$n" -eq 0 ]; then
echo "ext-resync: $p: nothing changed"
exit 0
fi
# what decides the configuration needs a work directory of its own
while IFS= read -r f; do
case $f in
./configure.ac|*/configure.ac|*/acinclude.m4|*.m4)
echo "ext-resync: $p: $f changed, cleaning"; exit 1 ;;
esac
done < "$tmp/changed"
while IFS= read -r f; do
case $f in
*/Makefile.am|*/Makefile.in.in)
echo "ext-resync: $p: $f is new, cleaning"; exit 1 ;;
esac
done < "$tmp/added"
# a file pkgsrc changed after extracting it (SUBST, patches) is no longer the
# archive's: putting the new one over it would lose that
while IFS= read -r f; do
# text for people (AGENTS.md, *.md, *.txt) is not built: the new one goes in
# as it is, pkgsrc's substitution in it does not matter
case $f in *.md|*.txt|*/AGENTS.md) continue ;; esac
if [ -f "$wrksrc/$f" ] && ! cmp -s "$old/$f" "$wrksrc/$f"; then
echo "ext-resync: $p: pkgsrc changed $f, cleaning"; exit 1
fi
done < "$tmp/changed"
while IFS= read -r f; do
mkdir -p "$wrksrc/$(dirname "$f")"
cp "$new/$f" "$wrksrc/$f"
done < "$tmp/changed"
while IFS= read -r f; do
mkdir -p "$wrksrc/$(dirname "$f")"
cp "$new/$f" "$wrksrc/$f"
done < "$tmp/added"
while IFS= read -r f; do
rm -f "$wrksrc/$f"
done < "$tmp/removed"
# build and install again (pkgsrc's cookies); configure stays
wdir=$(dirname "$wrksrc")
rm -f "$wdir/.build_done" "$wdir/.install_done" "$wdir"/.PLIST*
rm -rf "$pristine"
mv "$tmp/new" "$pristine"
echo "ext-resync: $p: $(wc -l < "$tmp/changed" | tr -d ' ') changed," \
"$(wc -l < "$tmp/added" | tr -d ' ') new, $(wc -l < "$tmp/removed" | tr -d ' ') removed"
exit 0