Files
RustBox/scripts/sync-upstream.sh
Mateusz Gruszczyński a643e7e3ab
RustBox CI / rustbox (push) Waiting to run
v0.0.5
2026-10-30 12:06:44 +01:00

88 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
REMOTE="upstream-oxi"
TARGET="${1:-upstream-oxi/main}"
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "error: run this inside a RustBox Git checkout" >&2
exit 1
fi
if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
echo "error: create the initial RustBox commit before syncing Oxi" >&2
exit 1
fi
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "error: working tree must be clean before Oxi sync" >&2
exit 1
fi
if ! git remote get-url "$REMOTE" >/dev/null 2>&1; then
echo "error: Oxi remote is not configured; run ./scripts/setup-upstream.sh" >&2
exit 1
fi
git fetch "$REMOTE" --tags
BASE="$(tr -d '[:space:]' < UPSTREAM_BASE)"
TARGET_SHA="$(git rev-parse "$TARGET^{commit}")"
if [ "$BASE" = "$TARGET_SHA" ]; then
echo "RustBox already consumes Oxi $TARGET_SHA"
exit 0
fi
if ! git merge-base --is-ancestor "$BASE" "$TARGET_SHA"; then
echo "error: $TARGET_SHA does not descend from recorded Oxi base $BASE" >&2
echo "Refuse automatic mapping; review the upstream history manually." >&2
exit 1
fi
branch="sync/oxi-$(date -u +%Y%m%d-%H%M%S)"
git switch -c "$branch"
tmp_raw="$(mktemp)"
tmp_mapped="$(mktemp)"
cleanup() { rm -f "$tmp_raw" "$tmp_mapped"; }
trap cleanup EXIT
git diff --binary --find-renames "$BASE..$TARGET_SHA" -- backend/ > "$tmp_raw"
sed \
-e 's#^diff --git a/backend/#diff --git a/#' \
-e 's# b/backend/# b/#' \
-e 's#^--- a/backend/#--- a/#' \
-e 's#^+++ b/backend/#+++ b/#' \
-e 's#^rename from backend/#rename from #' \
-e 's#^rename to backend/#rename to #' \
-e 's#^copy from backend/#copy from #' \
-e 's#^copy to backend/#copy to #' \
-e 's#^Binary files a/backend/#Binary files a/#' \
-e 's# and b/backend/# and b/#' \
"$tmp_raw" > "$tmp_mapped"
PENDING_FILE="$(git rev-parse --git-path rustbox-upstream-pending)"
printf 'base=%s\ntarget=%s\nbranch=%s\n' "$BASE" "$TARGET_SHA" "$branch" > "$PENDING_FILE"
if [ -s "$tmp_mapped" ]; then
if ! git apply --3way --index "$tmp_mapped"; then
echo >&2
echo "Oxi backend changes produced conflicts on branch $branch." >&2
echo "Resolve them in the flat RustBox paths, then run ./scripts/finish-upstream.sh." >&2
echo "UPSTREAM_BASE has NOT been advanced." >&2
exit 1
fi
fi
if find . -path ./.git -prune -o -type f \( -name '*.ts' -o -name '*.tsx' \) -print -quit | grep -q .; then
echo "error: TypeScript appeared in the RustBox source tree; remove/port it before continuing" >&2
exit 1
fi
printf '\nMapped Oxi backend %s -> %s into RustBox on branch %s.\n' "$BASE" "$TARGET_SHA" "$branch"
printf 'UPSTREAM_BASE remains at %s until the review is explicitly finished.\n' "$BASE"
printf '\nFrontend changes were NOT imported. Review them with:\n'
printf ' git diff --name-status %s..%s -- frontend/\n' "$BASE" "$TARGET_SHA"
printf ' git diff %s..%s -- frontend/\n' "$BASE" "$TARGET_SHA"
printf '\nOther Oxi root changes were NOT imported. Review them with:\n'
printf " git diff %s..%s -- . ':(exclude)backend/**' ':(exclude)frontend/**'\n" "$BASE" "$TARGET_SHA"
printf '\nAfter resolving/porting and testing, run ./scripts/finish-upstream.sh.\n'