From 2f744cf3b32b6348787b4606def71f5a3bf73035 Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 6 Oct 2026 13:46:46 +0200 Subject: [PATCH] scripts: add sync-localisation.sh to carry main into the translation branches --- .gitattributes | 1 + scripts/sync-localisation.sh | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 .gitattributes create mode 100644 scripts/sync-localisation.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfdb8b7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sh text eol=lf diff --git a/scripts/sync-localisation.sh b/scripts/sync-localisation.sh new file mode 100644 index 0000000..187844f --- /dev/null +++ b/scripts/sync-localisation.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# Carry main forward into the translation branches after a release. +# +# origin/main -> localisation -> every localisation- +# +# Each merge is a --no-ff merge commit, pushed right away. On the first conflict +# the script stops and leaves the merge in progress, so you can resolve it by hand +# (or back out with: git merge --abort). +# +# Run from anywhere inside the repo, with a clean working tree: +# bash scripts/sync-localisation.sh + +set -euo pipefail + +cd "$(git rev-parse --show-toplevel)" + +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then + echo "Working tree has uncommitted changes. Commit or stash them first." >&2 + exit 1 +fi + +start_branch="$(git rev-parse --abbrev-ref HEAD)" +summary=() + +# Check out a local branch that matches origin/, creating it if needed. +# Refuses when the local branch has commits that origin doesn't. +checkout_synced() { + local branch="$1" + if git show-ref --verify --quiet "refs/heads/$branch"; then + git checkout --quiet "$branch" + if ! git merge --quiet --ff-only "origin/$branch"; then + echo "Local $branch has diverged from origin/$branch. Sort that out first." >&2 + exit 1 + fi + else + git checkout --quiet -b "$branch" --track "origin/$branch" + fi +} + +# Merge into the current branch and push it. +merge_and_push() { + local source="$1" target="$2" + local before + before="$(git rev-parse HEAD)" + if ! git merge --no-ff --no-edit "$source"; then + echo + echo "CONFLICT merging $source into $target. Stopped here." + echo "Resolve it, commit, push $target, then run this script again." + echo "To back out instead: git merge --abort" + if [ ${#summary[@]} -gt 0 ]; then + echo + echo "Done before the conflict:" + printf ' %s\n' "${summary[@]}" + fi + exit 1 + fi + if [ "$(git rev-parse HEAD)" = "$before" ]; then + summary+=("$source -> $target: already up to date") + else + git push --quiet origin "$target" + summary+=("$source -> $target: merged and pushed ($(git rev-parse --short HEAD))") + fi +} + +echo "Fetching origin..." +git fetch --prune origin + +checkout_synced localisation +merge_and_push origin/main localisation + +language_branches=() +while IFS= read -r ref; do + language_branches+=("${ref#origin/}") +done < <(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/localisation-*') + +for branch in "${language_branches[@]}"; do + checkout_synced "$branch" + merge_and_push localisation "$branch" +done + +git checkout --quiet "$start_branch" + +echo +echo "Summary:" +printf ' %s\n' "${summary[@]}" +if [ ${#language_branches[@]} -eq 0 ]; then + echo " (no localisation-* branches on origin)" +fi