Skip to content
Merged
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
92 changes: 92 additions & 0 deletions .github/scripts/repair-release-sync-ancestry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash
# Usage: repair-release-sync-ancestry.sh <main-ref> <release-branch> <repair-pr-head-ref>
# Run from a clean release checkout. The caller owns fetching and pushing.
#
# A repair PR holds a merge of main into release. Landing it with a squash or
# rebase copies its content but not its history, so main's commits stay
# unmerged and the next sync re-conflicts on content that was already resolved.
# This records the newest main commit the repair PR head carries with a
# tree-preserving merge (-s ours), as a merge commit would have. HEAD's tree is
# never changed, and nothing happens when that main commit is already merged.
#
# Exit codes:
# 0 nothing to record, or the ancestry was recorded and HEAD is the new merge.
# 1 unexpected failure; nothing should be pushed.
set -euo pipefail

MAIN_SHA="$(git rev-parse --verify "${1:?main ref required}^{commit}")"
BRANCH="${2:?release branch required}"
REPAIR_HEAD="$(git rev-parse --verify "${3:?repair PR head ref required}^{commit}")"
RELEASE_SHA="$(git rev-parse HEAD)"

fail() {
echo "::error::$*" >&2
exit 1
}

[[ -z "$(git status --porcelain)" ]] || fail "Release checkout must be clean."
if git rev-parse -q --verify MERGE_HEAD >/dev/null; then
fail "A merge is already in progress."
fi

report() {
printf '%s\n' "$@"
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
printf '%s\n' "$@" >> "$GITHUB_STEP_SUMMARY"
fi
}

carried_status=0
CARRIED="$(git merge-base "$MAIN_SHA" "$REPAIR_HEAD")" || carried_status=$?
case "$carried_status" in
0) ;;
1)
echo "::warning::Repair PR head $REPAIR_HEAD shares no history with main; nothing to record."
exit 0
;;
*) fail "Could not find the main history carried by $REPAIR_HEAD (exit $carried_status)." ;;
esac

ancestor_status=0
git merge-base --is-ancestor "$CARRIED" HEAD || ancestor_status=$?
case "$ancestor_status" in
0)
echo "Main commit $CARRIED from the repair PR is already an ancestor of $BRANCH; nothing to record."
exit 0
;;
1) ;;
*) fail "Could not check main ancestry (exit $ancestor_status)." ;;
esac

cleanup() {
local status=$?
if [[ "$status" -ne 0 ]] && git rev-parse -q --verify MERGE_HEAD >/dev/null; then
if ! git merge --abort; then
echo "::error::Could not abort the failed ancestry merge." >&2
status=1
fi
fi
exit "$status"
}
trap cleanup EXIT

git merge --no-commit --no-ff -s ours "$CARRIED"
git commit \
-m "Merge commit '$CARRIED' into $BRANCH" \
-m "Records the main history carried by repair PR head $REPAIR_HEAD. The PR was squashed or rebased instead of merged, so release never received it. The tree is unchanged because the PR already copied the content." \
--trailer "Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>"

read -r commit first_parent second_parent extra <<< "$(git rev-list --parents -n 1 HEAD)"
if [[ "$first_parent" != "$RELEASE_SHA" || "$second_parent" != "$CARRIED" || -n "$extra" ]]; then
fail "Expected a two-parent release/main merge commit; do not push."
fi
[[ "$(git rev-parse 'HEAD^{tree}')" == "$(git rev-parse "$RELEASE_SHA^{tree}")" ]] \
|| fail "Ancestry repair changed the release tree; do not push."
git merge-base --is-ancestor "$CARRIED" HEAD || fail "Ancestry repair did not record main; do not push."

report "## Release sync: recorded repair PR history for $BRANCH" \
"- Repair PR head: \`$REPAIR_HEAD\`" \
"- Main commit recorded as merged: \`$CARRIED\`" \
"- Merge commit (tree unchanged): \`$commit\`" \
"" \
"The repair PR was squashed or rebased, which copies content but not history. Recording main as merged keeps the next sync from re-conflicting on content that was already resolved."
126 changes: 126 additions & 0 deletions .github/scripts/test-merge-main-into-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ unset GITHUB_STEP_SUMMARY

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
HELPER="$SCRIPT_DIR/merge-main-into-release.sh"
REPAIR_HELPER="$SCRIPT_DIR/repair-release-sync-ancestry.sh"
TEST_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/release-sync-tests.XXXXXX")"
trap 'cd "$SCRIPT_DIR"; rm -rf -- "$TEST_ROOT"' EXIT

Expand Down Expand Up @@ -47,6 +48,15 @@ run_helper() {
fi
}

run_repair_helper() {
if bash "$REPAIR_HELPER" main release/test "$repair_head" > "$TEST_ROOT/output" 2>&1; then
return 0
else
cat "$TEST_ROOT/output" >&2
return 1
fi
}

expect_failure() {
helper_status=0
bash "$HELPER" main release/test > "$TEST_ROOT/output" 2>&1 || helper_status=$?
Expand Down Expand Up @@ -290,3 +300,119 @@ run_helper
assert_parents
[[ "$(cat shared.txt)" == reconciled && -f next.txt ]] || fail "Next sync revisited repaired content."
echo "PASS: real repair avoids repeating squash-era conflicts"

# A repair PR conflicted on shared.txt, a human resolved it, and it landed on
# release by <squash|merge>. Main then edits region.txt again. A squash leaves
# the first main commit unmerged, so the next sync uses a stale merge base and
# re-conflicts on content the repair already resolved.
new_landed_repair_repo() {
new_repo "$1"
git switch -q main
write_file region.txt $'top\nbottom'
commit_all region-base
git switch -q release/test
git merge -q --ff-only main
write_file shared.txt release
commit_all release-edit
git switch -q main
write_file shared.txt main
write_file region.txt $'top\nfirst\nbottom'
commit_all first-main
first_main="$(git rev-parse HEAD)"
git switch -q release/test
git switch -qc release-sync/test
bash "$HELPER" main release/test --commit-conflicts > "$TEST_ROOT/output" 2>&1 \
|| { cat "$TEST_ROOT/output"; fail "Repair fixture could not commit its conflicts."; }
write_file shared.txt reconciled
commit_all human-resolution
repair_head="$(git rev-parse HEAD)"
git switch -q release/test
case "$2" in
squash)
git merge -q --squash release-sync/test > /dev/null
git commit -qm squash-repair
;;
merge)
git merge -q --no-ff release-sync/test -m merge-repair
;;
esac
write_file later.txt later
commit_all release-later
git switch -q main
write_file region.txt $'top\nsecond\nbottom'
commit_all second-main
git switch -q release/test
release_sha="$(git rev-parse HEAD)"
}

new_landed_repair_repo heal-squash squash
if git merge-base --is-ancestor "$first_main" HEAD; then
fail "Squash fixture unexpectedly preserved ancestry."
fi
# Control: without the repair, the stale merge base conflicts on resolved content.
expect_failure "conflicts require human review"
[[ "$helper_status" == 3 ]] || fail "Stale merge base must conflict with exit 3, got $helper_status."
grep -q 'region.txt' "$TEST_ROOT/output" || { cat "$TEST_ROOT/output"; fail "Stale merge base did not conflict on region.txt."; }
export GITHUB_STEP_SUMMARY="$TEST_ROOT/repair-summary"
: > "$GITHUB_STEP_SUMMARY"
run_repair_helper
grep -q "$first_main" "$GITHUB_STEP_SUMMARY" || fail "Summary omitted the recorded main commit."
unset GITHUB_STEP_SUMMARY
[[ "$(git show -s --format=%P HEAD)" == "$release_sha $first_main" ]] || fail "Ancestry repair parents differ."
[[ "$(git rev-parse 'HEAD^{tree}')" == "$(git rev-parse "$release_sha^{tree}")" ]] || fail "Ancestry repair changed the release tree."
git merge-base --is-ancestor "$first_main" HEAD || fail "Ancestry repair did not record main history."
[[ -z "$(git status --porcelain)" ]] || fail "Ancestry repair left a dirty checkout."
release_sha="$(git rev-parse HEAD)"
run_helper
assert_parents
[[ "$(cat region.txt)" == $'top\nsecond\nbottom' ]] || fail "Main's later change was not merged."
[[ "$(cat shared.txt)" == reconciled ]] || fail "The repaired content was overwritten."
[[ -f later.txt ]] || fail "Release-only work was lost."
release_sha="$(git rev-parse HEAD)"
run_repair_helper
[[ "$(git rev-parse HEAD)" == "$release_sha" ]] || fail "Repeating the ancestry repair created a commit."
grep -q "already an ancestor" "$TEST_ROOT/output" || fail "Missing idempotence diagnostic."
echo "PASS: squashed repair PR history is recorded so the next sync stays clean"

new_landed_repair_repo heal-merge merge
git merge-base --is-ancestor "$first_main" HEAD || fail "Merge fixture did not preserve ancestry."
export GITHUB_STEP_SUMMARY="$TEST_ROOT/repair-summary"
: > "$GITHUB_STEP_SUMMARY"
run_repair_helper
[[ ! -s "$GITHUB_STEP_SUMMARY" ]] || fail "No-op ancestry repair wrote a summary."
unset GITHUB_STEP_SUMMARY
[[ "$(git rev-parse HEAD)" == "$release_sha" ]] || fail "Ancestry repair touched a merge-landed repair PR."
grep -q "already an ancestor" "$TEST_ROOT/output" || fail "Missing no-op diagnostic."
run_helper
assert_parents
[[ "$(cat region.txt)" == $'top\nsecond\nbottom' && "$(cat shared.txt)" == reconciled ]] || fail "Next sync revisited repaired content."
echo "PASS: merge-landed repair PR needs no ancestry repair"

new_landed_repair_repo heal-unrelated squash
unrelated_head="$(git commit-tree "$(git rev-parse 'HEAD^{tree}')" -m unrelated)"
repair_head="$unrelated_head"
run_repair_helper
grep -q "shares no history" "$TEST_ROOT/output" || fail "Missing no-shared-history warning."
[[ "$(git rev-parse HEAD)" == "$release_sha" && -z "$(git status --porcelain)" ]] || fail "Unrelated repair head changed release."
echo "PASS: repair PR head without main history is skipped"

new_landed_repair_repo heal-refusals squash
write_file untracked.txt dirty
repair_status=0
bash "$REPAIR_HELPER" main release/test "$repair_head" > "$TEST_ROOT/output" 2>&1 || repair_status=$?
[[ "$repair_status" == 1 ]] || fail "Dirty checkout must exit 1, got $repair_status."
grep -q "must be clean" "$TEST_ROOT/output" || { cat "$TEST_ROOT/output"; fail "Missing dirty-checkout diagnostic."; }
[[ "$(git rev-parse HEAD)" == "$release_sha" ]] || fail "Refused ancestry repair changed release HEAD."
rm -- untracked.txt
# Refuse Git's index lock without introducing tracked or untracked changes.
write_file .git/index.lock locked
repair_status=0
bash "$REPAIR_HELPER" main release/test "$repair_head" > "$TEST_ROOT/output" 2>&1 || repair_status=$?
[[ "$repair_status" -ne 0 ]] || fail "A failed ancestry merge was reported as success."
[[ "$(git rev-parse HEAD)" == "$release_sha" ]] || fail "A failed ancestry merge changed release HEAD."
rm -- .git/index.lock
if git rev-parse -q --verify MERGE_HEAD >/dev/null; then
fail "Failed ancestry merge was not aborted."
fi
[[ -z "$(git status --porcelain)" ]] || fail "Failed ancestry merge left a dirty checkout."
echo "PASS: ancestry repair refuses dirty checkouts and fails without side effects"
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ jobs:
shell: bash
run: |
bash -n .github/scripts/merge-main-into-release.sh
bash -n .github/scripts/repair-release-sync-ancestry.sh
bash -n .github/scripts/test-merge-main-into-release.sh
bash .github/scripts/test-merge-main-into-release.sh

Expand Down
44 changes: 41 additions & 3 deletions .github/workflows/update-release-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,43 @@ jobs:
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

- name: Record main history from a landed repair PR
if: ${{ steps.find-branch.outputs.branch != '' && steps.repair-pr.outputs.url == '' }}
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
GH_REPO: ${{ github.repository }}
BRANCH: ${{ steps.find-branch.outputs.branch }}
shell: bash
run: |
set -euo pipefail

# Squashing a repair PR copies its content but drops the main history
# it merged, so the next sync would re-conflict on content that was
# already resolved. Only the newest merged repair PR matters: main only
# moves forward, so the commit it carries includes every older one.
pr=$(gh api --method GET "repos/$GH_REPO/pulls" \
-f state=closed \
-f head="${GH_REPO%%/*}:release-sync/${BRANCH#release/}" \
-f base="$BRANCH" \
-f per_page=100 \
--jq 'map(select(.merged_at != null)) | sort_by(.merged_at) | last | .number // empty')

if [[ -z "$pr" ]]; then
echo "No merged repair PR for $BRANCH; nothing to record."
exit 0
fi
echo "Newest merged repair PR: #$pr"

git fetch origin main
MAIN_SHA="$(git rev-parse origin/main)"
git fetch origin "refs/pull/$pr/head"
REPAIR_HEAD="$(git rev-parse FETCH_HEAD)"

# Any commit stays local: the merge step below pushes it with the sync,
# so release sees a single push (and a single CI run).
git show "$MAIN_SHA:.github/scripts/repair-release-sync-ancestry.sh" > "$RUNNER_TEMP/repair-release-sync-ancestry.sh"
bash "$RUNNER_TEMP/repair-release-sync-ancestry.sh" "$MAIN_SHA" "$BRANCH" "$REPAIR_HEAD"

- name: Merge main into release branch
id: merge
if: ${{ steps.find-branch.outputs.branch != '' && steps.repair-pr.outputs.url == '' }}
Expand Down Expand Up @@ -156,7 +193,8 @@ jobs:
repair_branch="release-sync/${BRANCH#release/}"
conflicts_file="$RUNNER_TEMP/release-sync-conflicts.txt"

# The failed attempt was aborted, so HEAD is still the release tip.
# The failed attempt was aborted, so HEAD is unchanged: the release tip,
# plus any history-repair commit recorded above.
# The repair branch is bot-owned and rolling: reset any stale leftover.
git checkout -B "$repair_branch"
RELEASE_SYNC_CONFLICTS_FILE="$conflicts_file" \
Expand Down Expand Up @@ -188,9 +226,9 @@ jobs:
echo
echo "1. Check out \`$repair_branch\` and resolve each item above without discarding release-only work."
echo "2. Push the fix, mark the PR ready for review, and wait for checks."
echo "3. Merge with **Create a merge commit**, never squash or rebase. Copying content alone doesn't repair ancestry."
echo "3. Prefer **Create a merge commit**. If it's squashed instead, the next sync still records \`main\` as merged, so it won't re-conflict."
echo
echo "Release sync is paused while this PR is open. After it merges, verify \`git merge-base --is-ancestor $MAIN_SHA origin/$BRANCH\`; the next push to main resumes syncing."
echo "Release sync is paused while this PR is open. After it merges, the next push to main resumes syncing."
echo
echo "## Third-party links and affiliations"
echo
Expand Down
Loading