Compare commits

...

5 commits

Author SHA1 Message Date
ptrcnull f80c534323 fix: don't exit when shell exits with errors
some shells keep exit code of the last executed process on ^D
2022-07-30 02:40:13 +02:00
ptrcnull 4e824d06f5 feat: even more fancy log messages 2022-07-30 02:40:01 +02:00
ptrcnull a85bf4c080 feat: keep original patch headers 2022-07-30 02:39:32 +02:00
ptrcnull 833280f1a9 feat: better log messages 2022-07-30 02:32:11 +02:00
ptrcnull 00d7fdace6 fix: exit on errors correctly 2022-07-30 02:28:33 +02:00

99
apatch
View file

@ -1,12 +1,43 @@
#!/bin/sh
startdir="$PWD"
# output functions from abuild/functions.sh
NORMAL="\033[1;0m"
STRONG="\033[1;1m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
[ -f APKBUILD ] || ( echo "please run this in a package directory"; exit 1 )
[ "$1" != "" ] || ( echo "usage: apatch <patch name>"; exit 1 )
_prefix="${BLUE}${STRONG}apatch${NORMAL}:"
msg() {
local prompt="$GREEN>>> ${_prefix}"
printf "${prompt} %s\n" "$1" >&2
}
warning() {
local prompt="${YELLOW}>>> WARNING: ${_prefix}"
printf "${prompt} %s\n" "$1" >&2
}
error() {
local prompt="${RED}>>> ERROR: ${_prefix}"
printf "${prompt} %s\n" "$1" >&2
}
if [ ! -f APKBUILD ]; then
error "please run this in a package directory"
exit 1
fi
if [ "$1" == "" ]; then
echo "usage: apatch <patch name>"
exit 1
fi
set -e
msg "running abuild"
abuild clean fetch unpack default_prepare
# obtain builddir
@ -23,6 +54,7 @@ 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
(
msg "reversing $patch"
cd "$builddir"
patch -R -p1 < "$patchpath"
)
@ -30,33 +62,54 @@ for patch in $source; do
fi
done
header=""
if [ -f "$patch" ]; then
msg "patch exists, trying to extract existing message"
# try to extract the original header from the patch
patch_start=$(grep -En '^(diff --git|--- )' "$patchpath" | cut -d: -f1 | head -n 1)
header="$(head -n $(( patch_start - 1 )) "$patchpath"; echo)"
fi
(
cd "$builddir"
git init
git add .
git commit -m "$pkgver" >/dev/null
cd "$builddir"
git init
git add .
git commit -m "$pkgver" >/dev/null
# if file exists, try to apply
if [ -e "$patchpath" ]; then
git apply --reject --whitespace=fix "$patchpath" || {
git status --porcelain | grep '\?\? .*\.rej$' | cut -c4- | while read -r reject; do
target="${reject%.rej}"
echo "[*] trying to wiggle $target"
wiggle --merge --replace "$target" "$reject" || {
echo "[!] could not merge $target, fix manually"
}
rm "$reject"
rm "$target.porig" # left by wiggle
done
rejects="$(git status --porcelain | grep '\?\? .*\.rej$' | cut -c4-)"
if [ -n "$rejects" ]; then
warning "$patch partially applied with rejects"
for reject in $rejects; do
target="${reject%.rej}"
msg "trying to wiggle $target"
wiggle --merge --replace "$target" "$reject" || {
warning "could not merge $target, fix manually"
}
rm "$reject"
rm "$target.porig" # left by wiggle
done
else
warning "$patch failed to apply"
fi
}
fi
msg "you can edit now!"
$SHELL
$SHELL || true
git add .
git diff --cached > "$patchpath"
if [ ! -s "$patchpath" ]; then
echo "[*] patch is empty, removing"
rm "$patchpath"
fi
git add .
msg "saving patch $patch"
echo -n "$header" > "$patchpath"
git diff --cached >> "$patchpath"
if [ ! -s "$patchpath" ]; then
msg "patch is empty, removing"
rm "$patchpath"
fi
rm -rf .git
)