]> juplo.de Git - demos/kafka/training/commitdiff
push.sh: Backup-Tags nur bei Bedarf und nur im Remote anlegen
authorKai Moritz <kai.milan.moritz@googlemail.com>
Sun, 31 May 2026 13:28:15 +0000 (13:28 +0000)
committerKai Moritz <kai.milan.moritz@googlemail.com>
Sun, 31 May 2026 13:28:15 +0000 (13:28 +0000)
Bisher wurden automatisch für jeden Branch zwei lokale Backup-Tags
(--BACKUP-ORIGIN--<TIMESTAMP> und --BACKUP-ORIGIN--LAST) erzeugt und
gepusht. Das neue Verhalten:

- Branches werden weiterhin per --force gepusht.
- Backup-Tags werden nur noch dann erzeugt, wenn mindestens ein Branch
  durch den Push umgeschrieben (nicht nur erweitert) würde und der
  bisherige Remote-Stand in diesem Branch noch keinen Tag im Remote hat.
  In diesem Fall werden TIMESTAMP-Tags für alle Remote-Stände direkt im
  Remote-Repository angelegt.
- Existiert lokal ein gemeinsames Tag-Suffix (d.h. für jeden Branch B
  existiert ein lokaler Tag B--S, z.B. --claude-5), wird dieses
  zusammen mit den Branches in den Remote gepusht, sofern es dort noch
  nicht vorhanden ist.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
push.sh

diff --git a/push.sh b/push.sh
index c10aebf30a7597cdb4a3d5d74caa1c4fd6f4d77c..cc8358d84e4b0183db10b6e9672cd5eeb2242c61 100755 (executable)
--- a/push.sh
+++ b/push.sh
@@ -3,12 +3,83 @@ set -e
 
 source branches.sh
 
-TIMESTAMP=$(date +'%Y-%m-%d--%H-%M-%S')
-
+# Build list of all branch names
+BRANCH_LIST=()
 for i in grundlagen__docker $BRANCHES; do
-  declare -n branch=${i}
+  declare -n _b=${i}
+  BRANCH_LIST+=("$_b")
+done
+
+# Fetch remote tags once to avoid repeated network calls
+remote_tags=$(git ls-remote --tags origin)
+
+# Returns 0 if the given commit hash has any tag in the remote repository
+remote_has_tag() {
+  echo "$remote_tags" | awk '{print $1}' | grep -qxF "$1"
+}
+
+# Step 1: Check if any branch would be rewritten (not just extended) and has no remote tag
+need_timestamp_backup=false
+for branch in "${BRANCH_LIST[@]}"; do
+  remote_commit=$(git rev-parse "refs/remotes/origin/$branch" 2>/dev/null) || continue
+  git rev-parse --verify "$branch" &>/dev/null || continue
+  # Skip if remote would only be extended (fast-forward)
+  git merge-base --is-ancestor "origin/$branch" "$branch" 2>/dev/null && continue
+  # Remote would be rewritten — check if it has any tag in the remote repository
+  if ! remote_has_tag "$remote_commit"; then
+    need_timestamp_backup=true
+    break
+  fi
+done
+
+# Step 2: If needed, create TIMESTAMP backup tags for all remote branch states
+if [ "$need_timestamp_backup" = true ]; then
+  TIMESTAMP=$(date +%Y%m%d%H%M%S)
+  echo -e "\nCreating backup tags with timestamp $TIMESTAMP in remote..."
+  for branch in "${BRANCH_LIST[@]}"; do
+    remote_commit=$(git rev-parse "refs/remotes/origin/$branch" 2>/dev/null) || continue
+    tag_name="${branch}--${TIMESTAMP}"
+    echo "  Tagging $branch -> $tag_name"
+    git tag "$tag_name" "$remote_commit"
+    git push origin "$tag_name"
+  done
+fi
+
+# Step 3: Find common local tag suffixes (suffix S where every branch B has local tag B--S)
+common_suffixes=()
+first_branch="${BRANCH_LIST[0]}"
+while IFS= read -r tag; do
+  prefix="${first_branch}--"
+  [[ "$tag" == "$prefix"* ]] || continue
+  suffix="${tag#$prefix}"
+  all_have=true
+  for branch in "${BRANCH_LIST[@]}"; do
+    if ! git rev-parse --verify "${branch}--${suffix}" &>/dev/null; then
+      all_have=false
+      break
+    fi
+  done
+  if [ "$all_have" = true ]; then
+    common_suffixes+=("$suffix")
+  fi
+done < <(git tag)
+
+# Step 4: Push all branches
+for branch in "${BRANCH_LIST[@]}"; do
   echo -e "\nPushing $branch over origin/$branch"
-  git tag -f "${branch}--BACKUP-ORIGIN--${TIMESTAMP}" "origin/$branch" || echo "origin/$branch existiert (noch) nicht!"
-  git tag -f "${branch}--BACKUP-ORIGIN--LAST" "origin/$branch" || echo "origin/$branch existiert (noch) nicht!"
   git push --force origin "$branch:$branch"
 done
+
+# Step 5: Push common local tags that are not yet in the remote
+if [ ${#common_suffixes[@]} -gt 0 ]; then
+  echo ""
+  for suffix in "${common_suffixes[@]}"; do
+    for branch in "${BRANCH_LIST[@]}"; do
+      tag_name="${branch}--${suffix}"
+      if ! echo "$remote_tags" | grep -qF "refs/tags/${tag_name}"; then
+        echo "Pushing tag: $tag_name"
+        git push origin "$tag_name"
+      fi
+    done
+  done
+fi