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