From f33e8cd6834435b99d5927e52434205800128452 Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Fri, 1 Dec 2023 20:19:08 +0100
Subject: clang-format: rework container script

Build a container image for the code formatting, and run that.
---
 tools/clang-format-container.sh | 57 +++++++++++++++++++----------------------
 tools/clang-format.sh           |  5 ++++
 2 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/tools/clang-format-container.sh b/tools/clang-format-container.sh
index a9445e7..69a68b1 100755
--- a/tools/clang-format-container.sh
+++ b/tools/clang-format-container.sh
@@ -16,36 +16,31 @@ FEDORA_VERSION="$(sed -n 's/^      image: fedora:\([0-9]\+\)$/\1/p' .github/work
 
 test -n "$FEDORA_VERSION" || die "Could not detect the Fedora version in .github/workflows/ci.yml"
 
-PODNAME="libnl-code-format-f$FEDORA_VERSION"
-
-RENEW=0
-for a; do
-    case "$a" in
-        -f)
-            RENEW=1
-            ;;
-        *)
-            die "invalid argument \"$a\""
-            ;;
-    esac
-done
-
-set -x
-
-if [ "$RENEW" == 1 ]; then
-    if podman container exists "$PODNAME" ; then
-        podman rm "$PODNAME"
-    fi
+IMAGENAME="libnl-code-format-f$FEDORA_VERSION"
+
+ARGS=( "$@" )
+
+if ! podman image exists "$IMAGENAME" ; then
+    echo "Building image \"$IMAGENAME\"..."
+    podman build \
+        --squash-all \
+        --tag "$IMAGENAME" \
+        -f <(cat <<EOF
+FROM fedora:$FEDORA_VERSION
+RUN dnf upgrade -y
+RUN dnf install -y git /usr/bin/clang-format
+EOF
+)
 fi
 
-if ! podman container exists "$PODNAME" ; then
-    podman run \
-        --name="$PODNAME" \
-        -v "$DIR:/tmp/libnl3:Z" \
-        -w /tmp/libnl3 \
-        "fedora:$FEDORA_VERSION" \
-        /bin/bash -c 'dnf upgrade -y && dnf install -y git /usr/bin/clang-format && tools/clang-format.sh -i'
-    exit 0
-fi
-
-podman start -a "$PODNAME"
+CMD=( ./tools/clang-format.sh "${ARGS[@]}" )
+
+podman run \
+    --rm \
+    --name "libnm-code-format-f$FEDORA_VERSION" \
+    -v "$DIR:/tmp/NetworkManager:Z" \
+    -w /tmp/NetworkManager \
+    -e "_LIBNL_CODE_FORMAT_CONTAINER=$IMAGENAME" \
+    -ti \
+    "$IMAGENAME" \
+    "${CMD[@]}"
diff --git a/tools/clang-format.sh b/tools/clang-format.sh
index 1dcc8aa..6a0db2a 100755
--- a/tools/clang-format.sh
+++ b/tools/clang-format.sh
@@ -400,6 +400,11 @@ usage() {
     printf "    -F|--fast             Same as \`-u HEAD^\`.\n"
     printf "    -l|--show-filenames   Only print the filenames that would be checked/formatted\n"
     printf "    --                    Separate options from filenames/directories\n"
+    if [ -n "${_LIBNL_CODE_FORMAT_CONTAINER+x}" ] ; then
+        printf "\n"
+        printf "Command runs inside container image \"$_LIBNL_CODE_FORMAT_CONTAINER\".\n"
+        printf "Delete/renew image with \`podman rmi \"$_LIBNL_CODE_FORMAT_CONTAINER\"\`.\n"
+    fi
 }
 
 ls_files_exist() {
-- 
cgit v0.12