#!/usr/bin/env bash # Port a change made in one Minecraft version's code to the other versions. # # Takes the diff under versions//src and applies it to versions//src # with `git apply --3way`: it applies cleanly where the files match, and leaves # normal merge conflicts only where a version really differs. Only src/ is # ported; build.gradle and gradle.properties are version-specific on purpose. # # Usage: # scripts/port.sh [--from VERSION] [--to V1,V2,...] [--commit REV] [--dry-run] # # --from version the change was made in. Default: the only version with # uncommitted changes (or changed by --commit). # --to comma-separated versions to port to. Default: all others. # --commit port the changes a commit made instead of uncommitted changes. # --dry-run report what would happen without changing anything. # # Exit status: 0 if every version applied cleanly (or already had the change), # 1 if any version has conflicts or failed. set -euo pipefail cd "$(git rev-parse --show-toplevel)" from="" to="" commit="" dry_run=false while [ $# -gt 0 ]; do case "$1" in --from) from="$2"; shift 2 ;; --to) to="$2"; shift 2 ;; --commit) commit="$2"; shift 2 ;; --dry-run) dry_run=true; shift ;; -h|--help) sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "Unknown option: $1 (see --help)" >&2; exit 2 ;; esac done all_versions=$(ls -d versions/*/ | xargs -n1 basename | sort -t. -k1,1n -k2,2n -k3,3n) version_exists() { [ -d "versions/$1/src" ] } # Versions with changes, from the working tree or from the given commit. changed_versions() { if [ -n "$commit" ]; then git show --name-only --format= "$commit" -- versions/ else git diff HEAD --name-only -- versions/ git ls-files --others --exclude-standard -- versions/ fi | sed -n 's#^versions/\([^/]*\)/src/.*#\1#p' | sort -u } if [ -z "$from" ]; then candidates=$(changed_versions) count=$(printf '%s\n' "$candidates" | grep -c . || true) if [ "$count" -ne 1 ]; then echo "Changes found in ${count} versions (${candidates//$'\n'/, }); pass --from to choose one." >&2 exit 2 fi from="$candidates" fi version_exists "$from" || { echo "No such version: $from" >&2; exit 2; } if [ -z "$to" ]; then to=$(printf '%s\n' $all_versions | grep -vx "$from" | paste -sd, -) fi # New files are only included in `git diff HEAD` once git knows about them. if [ -z "$commit" ]; then untracked=$(git ls-files --others --exclude-standard -- "versions/$from/src") if [ -n "$untracked" ]; then if $dry_run; then echo "note: new files in versions/$from/src are untracked and won't be ported in a dry run:" >&2 printf ' %s\n' $untracked >&2 else printf '%s\n' "$untracked" | xargs git add --intent-to-add -- fi fi fi # --full-index gives --3way the real blob ids to merge against. if [ -n "$commit" ]; then patch=$(git show --full-index --binary --format= "$commit" -- "versions/$from/src") else patch=$(git diff HEAD --full-index --binary -- "versions/$from/src") fi if [ -z "$patch" ]; then echo "No changes under versions/$from/src to port." >&2 exit 2 fi files=$(printf '%s\n' "$patch" | grep -c '^diff --git' || true) echo "Porting $files file(s) from $from${commit:+ (commit $commit)}" from_re=$(printf '%s' "$from" | sed 's/\./\\./g') tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT status=0 IFS=',' read -r -a targets <<< "$to" for target in "${targets[@]}"; do if [ "$target" = "$from" ]; then continue fi if ! version_exists "$target"; then printf ' %-8s skipped: no such version\n' "$target" status=1 continue fi # Point the patch's file headers at the target version. printf '%s\n' "$patch" | sed -E "/^(diff --git |--- |\+\+\+ |rename (from|to) |copy (from|to) )/ s#versions/${from_re}/#versions/${target}/#g" > "$tmp/$target.patch" if git apply --check --reverse "$tmp/$target.patch" 2>/dev/null; then printf ' %-8s already has this change\n' "$target" continue fi if $dry_run; then if git apply --check "$tmp/$target.patch" 2>/dev/null; then printf ' %-8s would apply cleanly\n' "$target" else printf ' %-8s would need a 3-way merge (possible conflicts)\n' "$target" fi continue fi if output=$(git apply --3way "$tmp/$target.patch" 2>&1); then printf ' %-8s applied\n' "$target" else conflicts=$(git diff --name-only --diff-filter=U -- "versions/$target") if [ -n "$conflicts" ]; then printf ' %-8s CONFLICTS in:\n' "$target" printf '%s\n' "$conflicts" | sed 's/^/ /' else printf ' %-8s FAILED:\n' "$target" printf '%s\n' "$output" | sed 's/^/ /' fi status=1 fi done if ! $dry_run; then echo echo "Ported changes are staged in the target versions. Resolve any conflicts, then build" echo "the affected versions, e.g. ./gradlew compileJava" fi exit $status