From 42e63a4a5632f9f14869e16f04d3c056cc751d6d Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Mon, 25 Jul 2022 07:07:38 +0200 Subject: [PATCH] feat: use git repo instead of arbitrary directories and busybox diff --- apatch | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/apatch b/apatch index ff60a54..7814d16 100755 --- a/apatch +++ b/apatch @@ -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 "; 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 +)