feat: use git repo instead of arbitrary directories and busybox diff

This commit is contained in:
ptrcnull 2022-07-25 07:07:38 +02:00
parent 5710359960
commit 42e63a4a56

51
apatch
View file

@ -1,23 +1,21 @@
#!/bin/sh
cleanup() {
rm -rf src-old
}
trap 'cleanup' EXIT INT HUP
startdir="$PWD"
[ -f APKBUILD ] || ( echo "please run this in a package directory"; exit 1 )
[ "$1" != "" ] || ( echo "usage: apatch <patch name>"; exit 1 )
abuild clean fetch unpack prepare || exit 1
set -e
abuild clean fetch unpack default_prepare
# obtain builddir
startdir="$PWD"
srcdir="$startdir/src"
. APKBUILD
[ -z "$builddir" ] && builddir="/$pkgname-$pkgver"
test -n "$builddir" || builddir="$srcdir/$pkgname-$pkgver"
cp -r src src-old
patchpath="$startdir/$1"
for patch in $source; do
if [ "$patch" = "$1" ]; then
@ -25,16 +23,39 @@ for patch in $source; do
# but we want the original src to not have it, so we can get a full diff of
# its original changes + our new ones
(
cd "src-old/$builddir"
patch -R -p1 < "$startdir/$1"
cd "$builddir"
patch -R -p1 < "$patchpath"
)
break
fi
done
(
cd "src$builddir"
$SHELL
)
cd "$builddir"
git init
git add .
git commit -m "$pkgver" >/dev/null
busybox diff -N -U3 -r "src-old$builddir" "src$builddir" | sed "s|src-old$builddir|a|g;s|src$builddir|b|g" > $1
# if file exists, try to apply
if [ -e "$patchpath" ]; then
git apply --reject --quiet --whitespace=fix "$patchpath" || {
git status --porcelain | grep '\?\? .*\.rej$' | cut -c4- | while read -r reject; do
target="${reject%.rej}"
wiggle --merge --replace "$target" "$reject" || {
echo "[!] could not merge $target, fix manually"
}
rm "$reject"
rm "$target.porig" # left by wiggle
done
}
fi
$SHELL
git add .
git diff --cached > "$patchpath"
if [ ! -s "$patchpath" ]; then
echo "[*] patch is empty, removing"
rm "$patchpath"
fi
)