From f67822feabfb69119becd7dd5fd5203628d8c8f2 Mon Sep 17 00:00:00 2001
From: SpiritCroc <dev@spiritcroc.de>
Date: Sun, 5 May 2024 09:19:54 +0200
Subject: [PATCH] merge_upstream.sh --checkout: re-apply our patches

---
 merge_helpers.sh  | 19 +++++++++++++++++++
 merge_upstream.sh | 23 ++++++++++++++++++-----
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/merge_helpers.sh b/merge_helpers.sh
index f457d36..0e7881e 100755
--- a/merge_helpers.sh
+++ b/merge_helpers.sh
@@ -249,3 +249,22 @@ get_current_mxsdk_tags() {
     current_mxreactsdk_tag="v$(cat "$SCHILDI_ROOT/element-web/package.json" | jq '.dependencies["matrix-react-sdk"]' -r)"
     current_mxjssdk_tag="v$(cat "$SCHILDI_ROOT/element-web/package.json" | jq '.dependencies["matrix-js-sdk"]' -r)"
 }
+
+apply_patches() {
+    local repo="$(realpath "$1")"
+    local patch_dir="$SCHILDI_ROOT/patches/$(basename "$1")"
+    if [ ! -d "$repo" ]; then
+        echo "Unknown repo: $repo"
+        return 1
+    fi
+    if [ ! -d "$patch_dir" ]; then
+        echo "No patches found at $patch_dir"
+        return 1
+    fi
+    pushd "$repo"
+    for patch in "$patch_dir"/*; do
+        echo "Applying $patch to $repo..."
+        git am "$patch" || read -p "Applying $patch failed, please commit manually, then press enter: "
+    done
+    popd
+}
diff --git a/merge_upstream.sh b/merge_upstream.sh
index bd90235..b912513 100755
--- a/merge_upstream.sh
+++ b/merge_upstream.sh
@@ -15,12 +15,18 @@ pushd "$mydir" > /dev/null
 
 source ./merge_helpers.sh
 
-# Check branch
-check_branch $branch
-forall_repos check_branch $branch
+if [ "$git_action" = "checkout" ]; then
+    # Abandon all local submodule state
+    forall_repos git reset --hard
+    git submodule update -f --recursive
+else
+    # Check branch
+    check_branch $branch
+    forall_repos check_branch $branch
 
-# Ensure clean git state
-forall_repos check_clean_git
+    # Ensure clean git state
+    forall_repos check_clean_git
+fi
 
 # Fetch upstream
 forall_repos git fetch upstream
@@ -55,6 +61,13 @@ popd > /dev/null
 make clean
 make setup
 
+# Apply our patches
+if [ "$git_action" = "checkout" ]; then
+    #apply_patches matrix-react-sdk
+    #apply_patches element-web
+    apply_patches element-desktop
+fi
+
 # Automatic adjustments
 #automatic_i18n_adjustment
 automatic_packagejson_adjustment