Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 64 additions & 43 deletions .github/scripts/merge-main-into-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# Usage: merge-main-into-release.sh <main-ref> <release-branch> [--commit-conflicts]
# Run from a clean release checkout. The caller owns fetching and pushing.
#
# Generated data groups changed on release since the merge base keep the
# release snapshot; untouched groups merge normally. See api_group,
# sample_group, and independent_data below. This also applies in repair mode.
#
# Exit codes:
# 0 main is merged (or was already an ancestor); HEAD is the result.
# 1 unexpected failure; nothing should be pushed.
Expand Down Expand Up @@ -61,28 +65,25 @@ case "$ancestor_status" in
*) fail "Could not check main ancestry (exit $ancestor_status)." ;;
esac

generated_files=(
# Generated data that a release branch regenerates for its own version. Each
# group is a set of Git pathspecs: if release changed anything in a group since
# the merge base, the whole group keeps the release snapshot during the merge.
api_group=(
# Every data subdirectory (pkgs, ts-modules, twoslash, schemas, and any
# added later). This glob only matches files inside a subdirectory.
':(glob)src/frontend/src/data/*/**'
# Top-level catalogs generated from the same packages.
src/frontend/src/data/aspire-integrations.json
src/frontend/src/data/github-stats.json
src/frontend/src/data/samples.json
src/frontend/src/data/twoslash/aspire.d.ts
src/frontend/src/data/integration-docs.json
src/frontend/src/data/api-generation-failures.json
)
generated_dirs=(
src/frontend/src/data/pkgs
src/frontend/src/data/ts-modules
sample_group=(
src/frontend/src/data/samples.json
src/frontend/src/assets/samples
)

is_generated() {
local path
for path in "${generated_files[@]}"; do
[[ "$1" != "$path" ]] || return 0
done
for path in "${generated_dirs[@]}"; do
case "$1" in "$path"|"$path"/*) return 0 ;; esac
done
return 1
}
# Every other top-level JSON file (stats, contributors, ...) is its own group.
# Hand-authored top-level TypeScript is not data; it merges normally.
independent_data=':(glob)src/frontend/src/data/*.json'

is_curated() {
case "$1" in
Expand All @@ -93,11 +94,11 @@ is_curated() {
}

keep_release() {
if git cat-file -e "$RELEASE_SHA:$1" 2>/dev/null; then
git checkout "$RELEASE_SHA" -- "$1"
else
git rm -q --force --ignore-unmatch -- "$1"
fi
# Reset matching paths even when the glob has no matches on release, so
# main-only files and release-side deletions are handled too.
git rm -r -q --force --ignore-unmatch -- "$1"
git reset -q "$RELEASE_SHA" -- "$1"
git ls-files -z -- "$1" | git checkout-index --force --stdin -z
}

conflicts_file="$(mktemp)"
Expand All @@ -114,23 +115,57 @@ cleanup() {
}
trap cleanup EXIT

release_changed() {
local status=0
git diff --quiet --no-renames "$MERGE_BASE" "$RELEASE_SHA" -- "$@" || status=$?
case "$status" in
0) return 1 ;;
1) return 0 ;;
*) fail "Could not diff release data (exit $status)." ;;
esac
}

# A release-side change owns its whole group, including files that main adds or
# renames without a conflict, so versions and catalogs stay consistent.
protected_paths=()
if release_changed "${api_group[@]}"; then
protected_paths+=("${api_group[@]}")
fi
if release_changed "${sample_group[@]}"; then
protected_paths+=("${sample_group[@]}")
fi
git diff --name-only --no-renames -z "$MERGE_BASE" "$RELEASE_SHA" -- \
"$independent_data" > "$conflicts_file"
mapfile -d '' -t changed_data_files < "$conflicts_file"
protected_paths+=(${changed_data_files[@]+"${changed_data_files[@]}"})
if [[ "${#protected_paths[@]}" -ne 0 ]]; then
printf '%s\0' "${protected_paths[@]}" | sort -zu > "$conflicts_file"
mapfile -d '' -t protected_paths < "$conflicts_file"
report "" "### Release-owned data preserved"
for path in "${protected_paths[@]}"; do
report "- \`$path\`"
done
fi

merge_status=0
git merge --no-commit --no-ff "$MAIN_SHA" || merge_status=$?
if [[ "$merge_status" -gt 1 ]] || ! git rev-parse -q --verify MERGE_HEAD >/dev/null; then
fail "Git merge failed (exit $merge_status); main is not already integrated."
fi
if [[ "$merge_status" -ne 0 && -z "$(git ls-files -u)" ]]; then
fail "Git merge failed without resolvable conflict entries (exit $merge_status)."
fi

for path in "${protected_paths[@]}"; do
keep_release "$path"
done

git diff --name-only --diff-filter=U -z > "$conflicts_file"
mapfile -d '' -t conflicts < "$conflicts_file"
if [[ "$merge_status" -ne 0 && "${#conflicts[@]}" -eq 0 ]]; then
fail "Git merge failed without resolvable conflict entries (exit $merge_status)."
fi

unresolved=()
for path in "${conflicts[@]}"; do
if is_generated "$path"; then
continue
elif is_curated "$path"; then
if is_curated "$path"; then
keep_release "$path"
else
unresolved+=("$path")
Expand Down Expand Up @@ -171,20 +206,6 @@ if [[ "${#unresolved[@]}" -ne 0 ]]; then
fi
fi

# Only these producer-owned paths are mirrored. Other data is hand-authored.
for path in "${generated_files[@]}" "${generated_dirs[@]}"; do
diff_status=0
git diff --quiet "$MAIN_SHA" -- "$path" || diff_status=$?
[[ "$diff_status" -le 1 ]] || fail "Could not compare generated path $path."
if [[ "$diff_status" -eq 0 && -z "$(git ls-files -u -- "$path")" ]]; then
continue
fi
git rm -r -q --force --ignore-unmatch -- "$path"
if git cat-file -e "$MAIN_SHA:$path" 2>/dev/null; then
git checkout "$MAIN_SHA" -- "$path"
fi
done

if [[ "${#unresolved[@]}" -ne 0 ]]; then
# Stage the conflicted files as-is for the repair branch. Text conflicts keep
# their markers; the workflow tells reviewers to decide the other kinds.
Expand Down
128 changes: 125 additions & 3 deletions .github/scripts/test-merge-main-into-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,134 @@ commit_all release-paths
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
git diff --exit-code main HEAD -- "${generated_files[@]}" "${generated_dirs[@]}"
git diff --exit-code "$release_sha" HEAD -- "${generated_files[@]}" "${generated_dirs[@]}"
[[ "$(cat "$curated")" == release && "$(cat "$asset")" == release ]] || fail "Curated content was overwritten."
[[ ! -e "$deleted" ]] || fail "Release-side deletion was lost."
[[ -f src/frontend/src/content/docs/new.mdx ]] || fail "Nonconflicting content did not merge."
[[ "$(cat src/frontend/src/data/hand-authored.json)" == release ]] || fail "Hand-authored data was overwritten."
echo "PASS: generated mirroring, curated conflicts/deletions, nonconflicting content"
echo "PASS: release-owned data, curated conflicts/deletions, nonconflicting content"

# Only the catalog changes on release. Main's nonconflicting API renames and
# additions must still be rejected as a group, but unrelated data can advance.
new_repo data-groups
catalog=src/frontend/src/data/aspire-integrations.json
package_dir=src/frontend/src/data/pkgs
stats=src/frontend/src/data/github-stats.json
samples=src/frontend/src/data/samples.json
sample_dir=src/frontend/src/assets/samples
write_file "$catalog" base
write_file "$package_dir/v13.json" base
write_file "$stats" base
write_file "$samples" base
write_file "$sample_dir/base.png" base
write_file src/frontend/src/data/editorial.json base
commit_all data-base
git switch -q main
git merge -q --ff-only release/test
write_file "$catalog" main
git mv "$package_dir/v13.json" "$package_dir/v14.json"
write_file "$package_dir/main-only.json" main
write_file src/frontend/src/data/ts-modules/main.json main
write_file src/frontend/src/data/future-api/nested/main.json main
write_file "$stats" first
write_file "$samples" main
git mv "$sample_dir/base.png" "$sample_dir/main.png"
write_file src/frontend/src/data/editorial.json main
commit_all main-data
git switch -q release/test
write_file "$catalog" release-v17
write_file src/frontend/src/data/go-modules/v17.json release
git rm -q src/frontend/src/data/editorial.json
commit_all release-data
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
git diff --exit-code "$release_sha" HEAD -- \
"$catalog" "$package_dir" src/frontend/src/data/ts-modules src/frontend/src/data/go-modules \
src/frontend/src/data/future-api \
src/frontend/src/data/editorial.json
git diff --exit-code main HEAD -- "$stats" "$samples" "$sample_dir"
echo "PASS: API group stays on release while untouched data groups accept main"

# The next merge base is the previously merged main, not the original fork.
# Release data must remain protected across subsequent syncs as well.
git switch -q main
write_file "$catalog" second-main
write_file "$package_dir/later.json" main
write_file "$stats" second
commit_all next-main-data
git switch -q release/test
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
git diff --exit-code "$release_sha" HEAD -- "$catalog" "$package_dir"
[[ "$(cat "$stats")" == second ]] || fail "Untouched data stopped following main."
echo "PASS: repeated sync preserves release ownership and advances untouched data"

new_repo future-data-directory
future_dir="src/frontend/src/data/future api/nested"
write_file "$future_dir/base.json" base
write_file "$catalog" base
write_file "$stats" base
commit_all future-base
git switch -q main
git merge -q --ff-only release/test
write_file "$future_dir/main-only.json" main
write_file src/frontend/src/data/another-api/nested/main.json main
write_file "$catalog" main
write_file "$package_dir/main.json" main
write_file "$stats" main
commit_all main-future-data
git switch -q release/test
git rm -q -- "$future_dir/base.json"
commit_all release-future-deletion
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
git diff --exit-code "$release_sha" HEAD -- src/frontend/src/data \
':(exclude)src/frontend/src/data/github-stats.json'
[[ "$(cat "$stats")" == main ]] || fail "An independent data file stopped following main."
echo "PASS: new data directories automatically protect the API group, including complete deletions"

new_repo samples-group
write_file "$samples" base
write_file "$sample_dir/base.png" base
write_file "$catalog" base
commit_all samples-base
git switch -q main
git merge -q --ff-only release/test
write_file "$samples" main
write_file "$sample_dir/main.png" main
write_file "$catalog" main
write_file "$package_dir/main.json" main
write_file src/frontend/src/data/future-api/nested/main.json main
commit_all main-samples
git switch -q release/test
git rm -q "$sample_dir/base.png"
commit_all release-samples
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
git diff --exit-code "$release_sha" HEAD -- "$samples" "$sample_dir"
git diff --exit-code main HEAD -- "$catalog" "$package_dir" src/frontend/src/data/future-api
echo "PASS: release sample deletion protects its catalog without blocking main API data"

new_repo hand-authored-data-code
code=src/frontend/src/data/dev-hub.ts
write_file "$code" $'one\ntwo\nthree'
commit_all code-base
git switch -q main
git merge -q --ff-only release/test
write_file "$code" $'one\ntwo\nthree-main'
commit_all main-code
git switch -q release/test
write_file "$code" $'one-release\ntwo\nthree'
commit_all release-code
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
[[ "$(cat "$code")" == $'one-release\ntwo\nthree-main' ]] || fail "Hand-authored data code did not merge normally."
echo "PASS: hand-authored data code merges normally instead of being release-owned"

new_repo unknown-conflict
write_file shared.txt release
Expand Down Expand Up @@ -180,7 +302,7 @@ assert_parents
[[ "$(cat "$TEST_ROOT/conflicts")" == $'text\tshared.txt' ]] || fail "Conflict list is wrong."
grep -q '^<<<<<<<' shared.txt || fail "Repair commit lost conflict markers."
[[ -z "$(git status --porcelain)" ]] || fail "Repair commit left a dirty checkout."
git diff --exit-code main HEAD -- "$generated" || fail "Generated data was not mirrored from main."
git diff --exit-code "$release_sha" HEAD -- "$generated" || fail "Release data was overwritten in repair mode."
echo "PASS: --commit-conflicts commits markers with both parents"

new_repo commit-conflicts-clean
Expand Down
Loading