From 45a8c0dd79c8edfcf6c534b0e56fb1e74915befc Mon Sep 17 00:00:00 2001
From: Sertonix <sertonix@posteo.net>
Date: Tue, 8 Oct 2024 22:06:15 +0200
Subject: [PATCH] abuild: deduplicate reproducible tarball creation

---
 abuild.in | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/abuild.in b/abuild.in
index b30e8fda..85a2ae22 100644
--- a/abuild.in
+++ b/abuild.in
@@ -1760,6 +1760,13 @@ human_size() {
 		printf("%.1f %s\n", y, type[i+2]) }'
 }
 
+apk_tar() {
+	tar --format=posix \
+		--pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0 \
+		--mtime="@$SOURCE_DATE_EPOCH" \
+		--no-recursion --null -T - -f - -c
+}
+
 create_apks() {
 	local file= dir= name= ver= apk= datadir= size= i= gzip=
 	gzip=$(command -v pigz || echo gzip)
@@ -1825,15 +1832,11 @@ create_apks() {
 		find "$@" -exec touch -h -d "@$SOURCE_DATE_EPOCH" {} +
 
 		# explicitly sort package content
-		find "$@" -print0 | LC_ALL=C sort -z | tar --xattrs \
+		find "$@" -print0 | LC_ALL=C sort -z | apk_tar --xattrs \
 			--xattrs-exclude=security.selinux \
-			--format=posix \
-			--pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0 \
-			--mtime="@${SOURCE_DATE_EPOCH}" \
 			--owner-map="$pkgbasedir"/.owner-map \
 			--group-map="$pkgbasedir"/.group-map \
-			--no-recursion --null -T - \
-			-f - -c | abuild-tar --hash | $gzip -n -9 >"$dir"/data.tar.gz
+			| abuild-tar --hash | $gzip -n -9 >"$dir"/data.tar.gz
 
 		msg "Create checksum..."
 		# append the hash for data.tar.gz
@@ -1843,12 +1846,7 @@ create_apks() {
 
 		# control.tar.gz
 		cd "$dir"
-		cat "$dir"/.metafiles | tar \
-			--format=posix \
-			--pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0 \
-			--mtime="@${SOURCE_DATE_EPOCH}" \
-			--no-recursion --null -T - \
-			-f - -c | abuild-tar --cut \
+		apk_tar < .metafiles | abuild-tar --cut \
 			| $gzip -n -9 > control.tar.gz
 		abuild-sign -q control.tar.gz || exit 1
 
-- 
GitLab