RacesOfValheim/scripts/sync-localisation.sh

88 lines
2.7 KiB
Bash

#!/usr/bin/env bash
# Carry main forward into the translation branches after a release.
#
# origin/main -> localisation -> every localisation-<language>
#
# 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/<branch>, 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 <source> 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