From 24803095bb57af8351de7510d491d8b5cce60777 Mon Sep 17 00:00:00 2001
From: su-ex <codeworks@supercable.onl>
Date: Sat, 16 Oct 2021 23:45:50 +0200
Subject: [PATCH] Don't rely on upstream/master

---
 merge_helpers.sh   | 46 ++++++++++++++++++++++++++++++++++++++--------
 merge_upstream.sh  | 13 ++++++++++++-
 regenerate_i18n.sh |  3 +--
 3 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/merge_helpers.sh b/merge_helpers.sh
index 98df3ae..1c4689d 100755
--- a/merge_helpers.sh
+++ b/merge_helpers.sh
@@ -85,9 +85,10 @@ check_clean_git() {
 
 revert_i18n_changes() {
     local i18n_path="$1"
-    local skip_commit="$2"
+    local revision="$2"
+    local skip_commit="$3"
 
-    git checkout upstream/master -- "$i18n_path"
+    git checkout "$revision" -- "$i18n_path"
 
     if [[ "$skip_commit" != [Yy]* ]]; then
         git commit -m "Automatic i18n reversion" || true
@@ -104,16 +105,23 @@ apply_i18n_changes() {
 automatic_i18n_reversion() {
     local skip_commit="$1"
 
+    local current_upstream_tag
+    get_current_upstream_tag
+
+    local current_mxjssdk_tag
+    local current_mxreactsdk_tag
+    get_current_mxsdk_tags
+
     pushd "$SCHILDI_ROOT/matrix-react-sdk" > /dev/null
-    revert_i18n_changes "$i18n_path" "$skip_commit"
+    revert_i18n_changes "$i18n_path" "$current_mxreactsdk_tag" "$skip_commit"
     popd > /dev/null
 
     pushd "$SCHILDI_ROOT/element-web" > /dev/null
-    revert_i18n_changes "$i18n_path" "$skip_commit"
+    revert_i18n_changes "$i18n_path" "$current_upstream_tag" "$skip_commit"
     popd > /dev/null
 
     pushd "$SCHILDI_ROOT/element-desktop" > /dev/null
-    revert_i18n_changes "$i18n_path" "$skip_commit"
+    revert_i18n_changes "$i18n_path" "$current_upstream_tag" "$skip_commit"
     popd > /dev/null
 }
 
@@ -196,9 +204,10 @@ bump_release_version() {
 
 revert_packagejson_changes() {
     local path="$1"
-    local skip_commit="$2"
+    local revision="$2"
+    local skip_commit="$3"
 
-    git checkout upstream/master -- "$path"
+    git checkout "$revision" -- "$path"
 
     if [[ "$skip_commit" != [Yy]* ]]; then
         git commit -m "Automatic package.json reversion" || true
@@ -220,7 +229,10 @@ apply_packagejson_overlay() {
 automatic_packagejson_reversion() {
     local skip_commit="$1"
 
-    forelement_repos revert_packagejson_changes "package.json" "$skip_commit"
+    local current_upstream_tag
+    get_current_upstream_tag
+
+    forelement_repos revert_packagejson_changes "package.json" "$current_upstream_tag" "$skip_commit"
 }
 
 automatic_packagejson_adjustment() {
@@ -233,3 +245,21 @@ automatic_packagejson_adjustment() {
     forelement_repos apply_packagejson_overlay "package.json" "overlay-package.json"
     forelement_repos write_version "package.json"
 }
+
+get_latest_upstream_tag() {
+    pushd "$SCHILDI_ROOT/element-web" > /dev/null
+    git fetch upstream
+    latest_upstream_tag=`git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags | sed -nr 's|refs/tags/(v[0-9]+(\.[0-9]+(\.[0-9]+)?)?) .*|\1|p' | tail -n 1`
+    popd > /dev/null
+}
+
+get_current_upstream_tag() {
+    local versions
+    get_current_versions
+    current_upstream_tag="v${versions[0]}"
+}
+
+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)"
+}
diff --git a/merge_upstream.sh b/merge_upstream.sh
index 5fba6ec..c3fef9c 100755
--- a/merge_upstream.sh
+++ b/merge_upstream.sh
@@ -23,7 +23,18 @@ automatic_i18n_reversion
 automatic_packagejson_reversion
 
 # Merge upstream
-forall_repos git merge upstream/master
+get_latest_upstream_tag
+forelement_repos git merge "$latest_upstream_tag"
+
+get_current_mxsdk_tags
+
+pushd "matrix-js-sdk" > /dev/null
+git merge "$current_mxjssdk_tag"
+popd > /dev/null
+
+pushd "matrix-react-sdk" > /dev/null
+git merge "$current_mxreactsdk_tag"
+popd > /dev/null
 
 # Refresh environment
 make clean
diff --git a/regenerate_i18n.sh b/regenerate_i18n.sh
index f21640c..1e6c300 100755
--- a/regenerate_i18n.sh
+++ b/regenerate_i18n.sh
@@ -9,8 +9,7 @@ pushd "$mydir" > /dev/null
 
 source ./merge_helpers.sh
 
-# Fetch to get latest strings for current version
-# ToDo: Find out actual versions in case it's not yet merged
+# Fetch to get upstream strings for current version
 forall_repos git fetch upstream
 
 # Check branch