Skip to content
2 changes: 2 additions & 0 deletions .github/release-lines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ pinned:
# v5-topup.yml's exclusions: it is not the branch this workflow watches (v4
# reaches v6 through v5), and v6 is the merge TARGET, not a trigger.
v6-topup.yml: [-v4, -v6]
# Tests the resolver v6-topup.yml runs; it lives on the same one line.
v6-topup-autoresolve-tests.yml: [-v4, -v6]
# Publishes src/crux to hivecommons/crux (#14138). It watches the ONE line
# the published module is cut from — v5, where the nested module lives and
# where `crux/vX.Y.Z` tags are made. v4 is feature-frozen and has no
Expand Down
113 changes: 113 additions & 0 deletions .github/scripts/test-v6-topup-autoresolve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env bash
# test-v6-topup-autoresolve.sh — exercises .github/scripts/v6-topup-autoresolve.sh
# against throwaway repositories that reproduce each known v5 -> v6 conflict
# pattern, plus the cases that must still fail for a human.
set -euo pipefail

RESOLVER="$(cd "$(dirname "$0")" && pwd)/v6-topup-autoresolve.sh"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
fail=0

pin_line() { printf "const TRUSTED_SCRIPTS_TREE = '%s';\n" "$1"; }
zeros=0000000000000000000000000000000000000000

# new_repo DIR: a base commit on v5 with the files every pattern touches,
# then a v6 branch. Leaves the repo on v5.
new_repo() {
local d="$1"
git init -q -b v5 "$d"
cd "$d"
git config user.name test
git config user.email test@example.com
mkdir -p .github/scripts src/scripts src/pkg/config src/pkg/dashboard/static-src src/pkg/dashboard/static discord
printf '# Changelog\n\n## Unreleased\n\n## 1.0.0\n\n- base\n' > CHANGELOG.md
{ echo "'use strict';"; pin_line "$zeros"; echo "const OTHER = 1;"; } > .github/scripts/integration-governance.cjs
echo base > src/scripts/a.sh
echo 'package config // base' > src/pkg/config/config.go
printf 'a.html\n# comment\n\nb.html\n' > src/pkg/dashboard/static-src/manifest.txt
echo A > src/pkg/dashboard/static-src/a.html
echo B > src/pkg/dashboard/static-src/b.html
printf 'A\nB\n' > src/pkg/dashboard/static/index.html
echo bot > discord/bot.go
echo other > other.txt
git add -A && git commit -qm base
git checkout -qb v6
git rm -q -r discord && git commit -qm "v6 drops discord"
git checkout -q v5
}

finish_pin() {
# Re-pin both sides honestly so each branch is self-consistent.
local tree
tree="$(git rev-parse HEAD:src/scripts)"
{ echo "'use strict';"; pin_line "$tree"; echo "const OTHER = 1;"; } > .github/scripts/integration-governance.cjs
git add -A && git commit -qm "$1 re-pin"
}

expect() {
local name="$1" want="$2"
shift 2
local rc=0
( "$@" ) >"$WORK/$name.log" 2>&1 || rc=$?
if { [ "$want" = ok ] && [ "$rc" -ne 0 ]; } || { [ "$want" = fail ] && [ "$rc" -eq 0 ]; }; then
echo "FAIL $name (want $want, rc=$rc)"; sed 's/^/ /' "$WORK/$name.log"; fail=1
else
echo "ok $name"
fi
}

case_all_known() {
new_repo "$WORK/known" && cd "$WORK/known"
# v5 side
echo v5bot > discord/bot.go
printf '# Changelog\n\n## Unreleased\n\n## 1.1.0 (v5)\n\n- v5 entry\n\n## 1.0.0\n\n- base\n' > CHANGELOG.md
echo v5 > src/scripts/b.sh
echo 'package config // v5 monolith edit' > src/pkg/config/config.go
echo A5 > src/pkg/dashboard/static-src/a.html
printf 'A5\nB\n' > src/pkg/dashboard/static/index.html
git add -A && git commit -qm v5 && finish_pin v5
# v6 side
git checkout -q v6
printf '# Changelog\n\n## Unreleased\n\n## 6.1.0 (v6)\n\n- v6 entry\n\n## 1.0.0\n\n- base\n' > CHANGELOG.md
echo v6 > src/scripts/c.sh
echo 'package config // v6 slim' > src/pkg/config/config.go
echo B6 > src/pkg/dashboard/static-src/b.html
printf 'A\nB6\n' > src/pkg/dashboard/static/index.html
git add -A && git commit -qm v6 && finish_pin v6
git merge --no-ff -q v5 -m merge >/dev/null 2>&1 && { echo "expected conflicts"; return 1; }
bash "$RESOLVER"
git commit -q --no-edit
[ ! -e discord/bot.go ] || { echo "discord not deleted"; return 1; }
grep -q 'v6 slim' src/pkg/config/config.go || { echo "config.go not v6"; return 1; }
grep -q '6.1.0 (v6)' CHANGELOG.md && grep -q '1.1.0 (v5)' CHANGELOG.md || { echo "changelog lost a side"; return 1; }
! grep -qE '^(<<<<<<<|=======|>>>>>>>)' CHANGELOG.md .github/scripts/integration-governance.cjs || { echo "markers left"; return 1; }
[ "$(cat src/pkg/dashboard/static/index.html)" = "$(printf 'A5\nB6')" ] || { echo "index not regenerated"; return 1; }
grep -qF "$(pin_line "$(git rev-parse HEAD:src/scripts)")" .github/scripts/integration-governance.cjs || { echo "pin not merged tree"; return 1; }
grep -q 'const OTHER = 1;' .github/scripts/integration-governance.cjs || { echo "pin file lost lines"; return 1; }
}

case_unknown_conflict() {
new_repo "$WORK/unknown" && cd "$WORK/unknown"
echo v5 > other.txt && git commit -qam v5
git checkout -q v6 && echo v6 > other.txt && git commit -qam v6
git merge --no-ff -q v5 -m merge >/dev/null 2>&1 || true
bash "$RESOLVER"
}

case_pin_file_real_edit() {
new_repo "$WORK/pinedit" && cd "$WORK/pinedit"
sed -i.bak 's/const OTHER = 1;/const OTHER = 5;/' .github/scripts/integration-governance.cjs && rm -f .github/scripts/integration-governance.cjs.bak
git commit -qam v5
git checkout -q v6
sed -i.bak 's/const OTHER = 1;/const OTHER = 6;/' .github/scripts/integration-governance.cjs && rm -f .github/scripts/integration-governance.cjs.bak
git commit -qam v6
git merge --no-ff -q v5 -m merge >/dev/null 2>&1 || true
bash "$RESOLVER"
}

expect all-known-patterns-resolve ok case_all_known
expect unknown-conflict-refused fail case_unknown_conflict
expect pin-file-real-edit-refused fail case_pin_file_real_edit

exit "$fail"
141 changes: 141 additions & 0 deletions .github/scripts/v6-topup-autoresolve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env bash
# v6-topup-autoresolve.sh — finish a conflicted v5 -> v6 forward-merge when
# every conflict is one of the known, mechanically resolvable patterns
# (.github/workflows/v6-topup.yml). Run it from the repo root with the merge
# still in progress. Exit 0: every conflict was resolved and staged (the caller
# commits). Exit 1: at least one conflict is not a known pattern, or a known
# file conflicts in a way this script does not recognize; nothing is staged
# that the caller needs to keep (it aborts the merge and reports).
#
# Known patterns:
# - DU discord/** and DU/UD .github/workflows/discord-syntax.yml: v6 removed
# discord/ (#9277) while v5 still develops it. Keep v6's deletion.
# - UU src/pkg/config/config.go: v6 split the monolith into topical files
# (#9386, #9956). Keep v6's side.
# - UU CHANGELOG.md: both lines prepend their own release sections under
# "## Unreleased" on every release, so the top of the file conflicts on
# almost every top-up. Keep both sides' lines (v6's block first, then
# v5's), which is the layout v6's CHANGELOG already has.
# - UU .github/scripts/integration-governance.cjs, when every conflict hunk
# is the TRUSTED_SCRIPTS_TREE literal on both sides: the literal pins
# `git rev-parse HEAD:src/scripts`, so whenever both lines touched
# src/scripts the two pins differ. Re-pin it to the merged src/scripts
# tree (the merge of two already-reviewed trees; the top-up PR shows it).
# - UU src/pkg/dashboard/static/index.html: a generated artifact. Regenerate
# it as the byte-exact concatenation of static-src/manifest.txt (what
# webstatic.ConcatIndex and TestIndexHTMLIsGenerated do), provided no
# static-src file is itself conflicted.
set -euo pipefail

PIN_FILE=.github/scripts/integration-governance.cjs
INDEX_FILE=src/pkg/dashboard/static/index.html
STATIC_SRC=src/pkg/dashboard/static-src
PIN_RE="^const TRUSTED_SCRIPTS_TREE = '[0-9a-f]{40}';\$"

unmerged="$(git status --porcelain | grep -E '^(DD|AU|UD|UA|DU|AA|UU) ' || true)"
[ -n "$unmerged" ] || { echo "v6-topup-autoresolve: no conflicts to resolve"; exit 1; }

known='^DU discord/|^(DU|UD) \.github/workflows/discord-syntax\.yml$|^UU src/pkg/config/config\.go$|^UU CHANGELOG\.md$|^UU \.github/scripts/integration-governance\.cjs$|^UU src/pkg/dashboard/static/index\.html$'
if unknown="$(printf '%s\n' "$unmerged" | grep -vE "$known")"; then
echo "v6-topup-autoresolve: conflicts outside the known patterns:"
printf '%s\n' "$unknown"
exit 1
fi

# Every hunk of the pin file must be exactly one TRUSTED_SCRIPTS_TREE line per
# side; anything else is a real edit and goes to a human.
pin_hunks_only() {
awk -v re="$PIN_RE" '
/^<<<<<<< / { side = 1; n1 = 0; n2 = 0; hunks++; next }
/^\|\|\|\|\|\|\| / && side == 1 { side = 3; next }
/^=======$/ && (side == 1 || side == 3) { side = 2; next }
/^>>>>>>> / && side == 2 {
if (n1 != 1 || n2 != 1) bad = 1
side = 0; next
}
side == 1 { n1++; if ($0 !~ re) bad = 1 }
side == 2 { n2++; if ($0 !~ re) bad = 1 }
END { exit (bad || hunks == 0 || side != 0) ? 1 : 0 }
' "$1"
}

regen_index=0
repin=0
resolved=0
while IFS= read -r entry; do
[ -n "$entry" ] || continue
status="${entry%% *}"
file="${entry#* }"
case "$status $file" in
"DU "*|"UD "*)
git rm -q -- "$file"
;;
"UU src/pkg/config/config.go")
git checkout --ours -- "$file"
git add -- "$file"
;;
"UU CHANGELOG.md")
tmp="$(mktemp -d)"
git show ":1:$file" > "$tmp/base"
git show ":2:$file" > "$tmp/ours"
git show ":3:$file" > "$tmp/theirs"
git merge-file --union "$tmp/ours" "$tmp/base" "$tmp/theirs"
cp "$tmp/ours" "$file"
rm -rf "$tmp"
git add -- "$file"
;;
"UU $PIN_FILE")
if ! pin_hunks_only "$file"; then
echo "v6-topup-autoresolve: $file conflicts outside the TRUSTED_SCRIPTS_TREE literal"
exit 1
fi
repin=1
;;
"UU $INDEX_FILE")
regen_index=1
;;
*)
echo "v6-topup-autoresolve: unhandled entry: $entry"
exit 1
;;
esac
resolved=$((resolved + 1))
done <<<"$unmerged"

if [ "$regen_index" = 1 ]; then
: > "$INDEX_FILE"
while IFS= read -r line || [ -n "$line" ]; do
line="$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
case "$line" in ''|'#'*) continue ;; esac
case "$line" in /*|..*) echo "v6-topup-autoresolve: unsafe manifest path $line"; exit 1 ;; esac
cat "$STATIC_SRC/$line" >> "$INDEX_FILE"
done < "$STATIC_SRC/manifest.txt"
git add -- "$INDEX_FILE"
fi

if [ "$repin" = 1 ]; then
# Stage a placeholder so the index has no unmerged entries, then read the
# merged src/scripts tree from it (the pin file is outside src/scripts).
# Keep v6's side of each (pin-only) hunk; the literal is rewritten below.
awk '
/^<<<<<<< / { side = 1; next }
/^\|\|\|\|\|\|\| / && side == 1 { side = 3; next }
/^=======$/ && (side == 1 || side == 3) { side = 2; next }
/^>>>>>>> / && side == 2 { side = 0; next }
side == 0 || side == 1 { print }
' "$PIN_FILE" > "$PIN_FILE.tmp"
mv "$PIN_FILE.tmp" "$PIN_FILE"
git add -- "$PIN_FILE"
tree="$(git write-tree)"
scripts_tree="$(git rev-parse "${tree}:src/scripts")"
sed -E -i.bak "s/^const TRUSTED_SCRIPTS_TREE = '[0-9a-f]{40}';\$/const TRUSTED_SCRIPTS_TREE = '${scripts_tree}';/" "$PIN_FILE"
rm -f "$PIN_FILE.bak"
git add -- "$PIN_FILE"
echo "v6-topup-autoresolve: re-pinned TRUSTED_SCRIPTS_TREE to ${scripts_tree}"
fi

if git status --porcelain | grep -qE '^(DD|AU|UD|UA|DU|AA|UU) '; then
echo "v6-topup-autoresolve: conflicts remain after resolution"
exit 1
fi
echo "v6-topup-autoresolve: resolved ${resolved} known conflict(s)"
34 changes: 34 additions & 0 deletions .github/workflows/v6-topup-autoresolve-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: v6 top-up auto-resolve tests

on:
pull_request:
paths:
- '.github/scripts/v6-topup-autoresolve.sh'
- '.github/scripts/test-v6-topup-autoresolve.sh'
- '.github/workflows/v6-topup.yml'
- '.github/workflows/v6-topup-autoresolve-tests.yml'
push:
branches: [v5]
paths:
- '.github/scripts/v6-topup-autoresolve.sh'
- '.github/scripts/test-v6-topup-autoresolve.sh'
- '.github/workflows/v6-topup.yml'
- '.github/workflows/v6-topup-autoresolve-tests.yml'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

jobs:
autoresolve:
name: Known-conflict resolver
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
# Builds throwaway repos reproducing each known v5 -> v6 conflict
# pattern and checks the resolver finishes them, and refuses others.
- name: Test v6-topup-autoresolve.sh
run: bash .github/scripts/test-v6-topup-autoresolve.sh
50 changes: 17 additions & 33 deletions .github/workflows/v6-topup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,49 +80,33 @@ jobs:
# Always rebuild the branch from v6's tip. Reusing a stale top-up
# branch would replay an older merge and could resurrect commits v6
# has since had reverted.
cp .github/scripts/v6-topup-autoresolve.sh "$RUNNER_TEMP/v6-topup-autoresolve.sh"
git checkout -B sync/v5-to-v6 origin/v6

# --signoff because the DCO audits treat a merge commit as a commit
# like any other.
if git merge --no-ff --signoff origin/v5 \
-m "🌱 Forward-merge v5 into v6 (${{ steps.drift.outputs.behind }} commits)"; then
echo "conflict=false" >> "$GITHUB_OUTPUT"
# Two guaranteed, permanent conflicts get auto-resolved instead of
# paging a human every time:
# - v6 removed discord/ (#9277) while v5 still develops it, so
# every v5 discord commit is a modify/delete conflict there.
# Keep v6's deletion. The same removal dropped the discord-only
# workflow .github/workflows/discord-syntax.yml, which v5 still
# edits (dependabot action bumps, #11551), so that one file is
# the same modify/delete conflict and keeps v6's deletion too.
# - v6 split src/pkg/config/config.go into topical files (#9386)
# and kept a slim config.go, while v5 keeps growing the old
# monolith, so every v5 edit to it conflicts here too (#9956).
# v6's file already carries the equivalent behavior in the
# split files, so keep v6's side.
# When the ONLY conflicts match one of those two patterns, resolve
# them and finish the merge. Anything else (other content
# conflicts, other deletions) still fails loudly below.
elif unmerged="$(git status --porcelain | grep -E '^(DD|AU|UD|UA|DU|AA|UU) ' || true)" \
&& [ -n "$unmerged" ] \
&& ! printf '%s\n' "$unmerged" | grep -qvE '^DU discord/|^(DU|UD) \.github/workflows/discord-syntax\.yml$|^UU src/pkg/config/config\.go$'; then
resolved=0
while IFS= read -r entry; do
[ -n "$entry" ] || continue
status="${entry%% *}"
file="${entry#* }"
if [ "$status" = "DU" ] || [ "$status" = "UD" ]; then
git rm -q -- "$file"
else
git checkout --ours -- "$file"
git add -- "$file"
fi
resolved=$((resolved + 1))
done <<<"$unmerged"
# Known, mechanically resolvable conflicts get auto-resolved
# instead of paging a human every time (the resolver's header lists
# them with their history):
# - discord/ + discord-syntax.yml modify/delete: keep v6's
# deletion (#9277, #11551).
# - src/pkg/config/config.go: keep v6's split (#9386, #9956).
# - CHANGELOG.md: both lines prepend release sections, keep both.
# - integration-governance.cjs TRUSTED_SCRIPTS_TREE literal only:
# re-pin to the merged src/scripts tree.
# - static/index.html: regenerate from static-src/manifest.txt.
# When the ONLY conflicts match those patterns, resolve them and
# finish the merge. Anything else (other content conflicts, other
# deletions, other hunks in the pin file) still fails loudly below.
# The resolver runs from the copy taken off the triggering ref
# before the checkout, not from v6's tree.
elif bash "$RUNNER_TEMP/v6-topup-autoresolve.sh"; then
# --cleanup=strip: --no-edit would otherwise keep the "# Conflicts:"
# comment lines git leaves in MERGE_MSG.
git commit --no-edit --signoff --cleanup=strip
echo "Auto-resolved: kept v6's side of ${resolved} known-structural conflict(s) (discord/ + discord-syntax.yml deletions, src/pkg/config/config.go split)."
echo "conflict=false" >> "$GITHUB_OUTPUT"
else
# Inventory the conflict BEFORE aborting (#7337): hunk and line
Expand Down
Loading