From 5049ca702a2317ae6d0f20fd6832646bbcea52f5 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Jun 2026 20:49:24 +0000 Subject: [PATCH 01/10] Push updated heads before retargeting PR bases in the fan-out path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The squash-merge fan-out retargeted every updated child PR onto the target branch and only afterwards pushed the new heads, batched into a single non-atomic push together with the merged-branch deletion. If the push failed (e.g. someone pushed to a child mid-run, rejecting the plain push) or a pr edit died partway through the loop, set -e aborted the run with PRs already retargeted but their heads stale - and unlike the conflict-resume path there is no label to re-trigger the action, so nothing ever repaired them. Apply the ordering the resume path already uses: push the updated heads first, then flip the bases, and delete the merged branch last (deleting a PR's base branch closes the PR, so every child must be off it first). A failed push now leaves the PRs untouched on their old base. The unit test captures the run transcript and asserts the push -> retarget -> delete order; it fails against the previous code. Also corrects the README: pushes are plain, not forced, and branch deletion is its own final step. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JHvKryT4QUpHYdNq9YEQxX --- README.md | 5 +++-- tests/test_update_pr_stack.sh | 20 ++++++++++++++++++-- update-pr-stack.sh | 22 ++++++++++++++-------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f55449e..9b6094e 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,9 @@ This action tries to fix that in a transparent way. Install it, and hopefully th 1. Triggers when a PR is squash merged 2. Finds PRs that were based on the merged branch (direct children only) 3. Creates a synthetic merge commit with three parents (child tip, deleted branch tip, squash commit) to preserve history without re-introducing code -4. Updates the direct child PRs to base on trunk now that the bottom change has landed -5. Force-pushes updated branches and deletes the merged branch +4. Pushes the updated branches +5. Updates the direct child PRs to base on trunk now that the bottom change has landed +6. Deletes the merged branch **Note:** Indirect descendants (grandchildren, etc.) are intentionally not modified. Their PR diffs remain correct because the merge-base calculation still worksβ€”the synthetic merge commit includes the original parent commit as an ancestor. When their direct parent is eventually merged, they become direct children and get updated at that point. diff --git a/tests/test_update_pr_stack.sh b/tests/test_update_pr_stack.sh index 4317f5e..2930aac 100755 --- a/tests/test_update_pr_stack.sh +++ b/tests/test_update_pr_stack.sh @@ -1,6 +1,6 @@ #!/bin/bash -set -e +set -eo pipefail # Get script directory (needed for static mock files) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -71,6 +71,8 @@ echo "Simulated Squash commit (via cherry-pick): $SQUASH_COMMIT" echo "Running update-pr-stack.sh..." # The update script sources command_utils.sh itself +# Capture stdout+stderr interleaved so command ordering can be asserted. +RUN_LOG="$TEST_REPO/update_run.log" run_update_pr_stack() { log_cmd \ env \ @@ -79,10 +81,24 @@ run_update_pr_stack() { TARGET_BRANCH=main \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ - $SCRIPT_DIR/../update-pr-stack.sh + $SCRIPT_DIR/../update-pr-stack.sh 2>&1 | tee "$RUN_LOG" } run_update_pr_stack +# The head must be pushed before the PR is retargeted (a failed push must leave +# the PR untouched on its old base), and the merged branch deleted only after +# the retarget (deleting a PR's base branch closes the PR). +push_line=$(grep -n "git push origin feature2" "$RUN_LOG" | head -1 | cut -d: -f1 || true) +edit_line=$(grep -n "pr edit feature2 --base main" "$RUN_LOG" | head -1 | cut -d: -f1 || true) +delete_line=$(grep -n "git push origin :feature1" "$RUN_LOG" | head -1 | cut -d: -f1 || true) +if [[ -n "$push_line" && -n "$edit_line" && -n "$delete_line" \ + && "$push_line" -lt "$edit_line" && "$edit_line" -lt "$delete_line" ]]; then + echo "βœ… Ordering: push head, then retarget base, then delete merged branch" +else + echo "❌ Wrong ordering (push=$push_line edit=$edit_line delete=$delete_line)" + exit 1 +fi + # Verify the results cd "$TEST_REPO" diff --git a/update-pr-stack.sh b/update-pr-stack.sh index a2d8ca5..0669f82 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -311,20 +311,26 @@ main() { fi done - # Only update base branches for successfully updated PRs + # Push the updated heads before retargeting, for the same reasons as the + # conflict-resume path: the head already contains TARGET_BRANCH when the + # base flips to it, keeping the PR mergeable (GitHub suppresses CI on a PR + # that conflicts with its base), and a failed push leaves the PR untouched + # on its old base instead of retargeted onto a stale head with nothing to + # re-trigger this action. + if [[ "${#UPDATED_TARGETS[@]}" -gt 0 ]]; then + log_cmd git push origin "${UPDATED_TARGETS[@]}" + fi + for BRANCH in "${UPDATED_TARGETS[@]}"; do log_cmd gh pr edit "$BRANCH" --base "$TARGET_BRANCH" done - # Push updated branches; only delete merged branch if no conflicts + # Delete the merged branch last: deleting a PR's base branch closes the PR, + # so every child must be retargeted off it first. Keep it while conflicted + # PRs remain so it can be referenced during manual resolution. if [[ "${#CONFLICTED_TARGETS[@]}" -eq 0 ]]; then - # No conflicts - safe to delete merged branch - log_cmd git push origin ":$MERGED_BRANCH" "${UPDATED_TARGETS[@]}" + log_cmd git push origin ":$MERGED_BRANCH" else - # Some conflicts - keep merged branch for reference during manual resolution - if [[ "${#UPDATED_TARGETS[@]}" -gt 0 ]]; then - log_cmd git push origin "${UPDATED_TARGETS[@]}" - fi echo "⚠️ Keeping branch '$MERGED_BRANCH' - still referenced by conflicted PRs: ${CONFLICTED_TARGETS[*]}" fi } From f1f2a0e8b907932b8a05afa89f924efedd833f33 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Jun 2026 21:07:27 +0000 Subject: [PATCH 02/10] Fix e2e merge-command assertion broken by the ff-only step MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since #40 the conflict comment's fast-forward step reads `git merge --ff-only origin/`, which assert_conflict_comment_merges picks up with its `^git merge` grep, so the extracted commands never match the expected conflict merges. Skip the --ff-only line when extracting. Also trim the new comments in the fan-out push/retarget/delete sequence. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JHvKryT4QUpHYdNq9YEQxX --- tests/test_e2e.sh | 2 +- update-pr-stack.sh | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/test_e2e.sh b/tests/test_e2e.sh index 1da4439..420f2cd 100755 --- a/tests/test_e2e.sh +++ b/tests/test_e2e.sh @@ -291,7 +291,7 @@ assert_conflict_comment_merges() { expected+="git merge $conflict"$'\n' done expected=${expected%$'\n'} - actual=$(echo "$comment" | grep -E '^git merge' | sed 's/ *#.*//' || true) + actual=$(echo "$comment" | grep -E '^git merge' | grep -v -- '--ff-only' | sed 's/ *#.*//' || true) if [[ "$actual" == "$expected" ]]; then echo >&2 "βœ… Verification Passed: conflict comment lists expected merge command(s)." diff --git a/update-pr-stack.sh b/update-pr-stack.sh index 0669f82..aa08cc7 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -311,12 +311,9 @@ main() { fi done - # Push the updated heads before retargeting, for the same reasons as the - # conflict-resume path: the head already contains TARGET_BRANCH when the - # base flips to it, keeping the PR mergeable (GitHub suppresses CI on a PR - # that conflicts with its base), and a failed push leaves the PR untouched - # on its old base instead of retargeted onto a stale head with nothing to - # re-trigger this action. + # Push the heads before retargeting: a failed push then leaves each PR + # intact on its old base, and the head already contains TARGET_BRANCH when + # the base flips to it. if [[ "${#UPDATED_TARGETS[@]}" -gt 0 ]]; then log_cmd git push origin "${UPDATED_TARGETS[@]}" fi @@ -325,9 +322,8 @@ main() { log_cmd gh pr edit "$BRANCH" --base "$TARGET_BRANCH" done - # Delete the merged branch last: deleting a PR's base branch closes the PR, - # so every child must be retargeted off it first. Keep it while conflicted - # PRs remain so it can be referenced during manual resolution. + # Deleting a PR's base branch closes the PR, so this must come after the + # retargets. Keep the branch for reference while conflicted PRs remain. if [[ "${#CONFLICTED_TARGETS[@]}" -eq 0 ]]; then log_cmd git push origin ":$MERGED_BRANCH" else From 972442ab373a1e3c9792ac788394bcae2b135482 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Jun 2026 21:31:02 +0000 Subject: [PATCH 03/10] Drop the e2e assertion fix, split out into its own PR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fix for the --ff-only line breaking assert_conflict_comment_merges moved to a separate PR; the e2e job here stays red until that lands and main is merged back in. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JHvKryT4QUpHYdNq9YEQxX --- tests/test_e2e.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_e2e.sh b/tests/test_e2e.sh index 420f2cd..1da4439 100755 --- a/tests/test_e2e.sh +++ b/tests/test_e2e.sh @@ -291,7 +291,7 @@ assert_conflict_comment_merges() { expected+="git merge $conflict"$'\n' done expected=${expected%$'\n'} - actual=$(echo "$comment" | grep -E '^git merge' | grep -v -- '--ff-only' | sed 's/ *#.*//' || true) + actual=$(echo "$comment" | grep -E '^git merge' | sed 's/ *#.*//' || true) if [[ "$actual" == "$expected" ]]; then echo >&2 "βœ… Verification Passed: conflict comment lists expected merge command(s)." From 735200350adeeee8c091718fbfec74b83d1b089b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Jun 2026 21:40:23 +0000 Subject: [PATCH 04/10] Address PRs by number instead of head branch name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A head branch can carry several PRs (one per base), so gh calls keyed by branch name can comment, label, or retarget the wrong one. Every gh call that acts on a specific PR now uses the PR number: the fan-out carries number/branch pairs from gh pr list, and the conflict-resolved run gets PR_NUMBER from the event payload via action.yml. The payload also already carries the PR's base branch, so the resume takes it from a new PR_BASE variable instead of querying the API; the resume test's gh mock no longer answers baseRefName queries, so a reintroduced lookup fails loudly. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JHvKryT4QUpHYdNq9YEQxX --- action.yml | 2 + tests/mock_gh.sh | 4 +- tests/test_conflict_resolution_resume.sh | 18 ++--- tests/test_update_pr_stack.sh | 2 +- update-pr-stack.sh | 85 ++++++++++++++---------- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/action.yml b/action.yml index 4eb9363..eb69e0e 100644 --- a/action.yml +++ b/action.yml @@ -54,6 +54,8 @@ runs: MERGED_BRANCH: ${{ github.event.pull_request.head.ref }} TARGET_BRANCH: ${{ github.event.pull_request.base.ref }} PR_BRANCH: ${{ github.event.pull_request.head.ref }} + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_BASE: ${{ github.event.pull_request.base.ref }} run: | echo "Running in $ACTION_MODE mode" ${{ github.action_path }}/update-pr-stack.sh diff --git a/tests/mock_gh.sh b/tests/mock_gh.sh index 74bbfd3..5a0b1ab 100755 --- a/tests/mock_gh.sh +++ b/tests/mock_gh.sh @@ -14,8 +14,8 @@ if [[ "$1" == "pr" && "$2" == "list" ]]; then done if [[ "$base" == "feature1" ]]; then - # feature2 is a direct child of feature1 - echo 'feature2' + # feature2 is a direct child of feature1 (PR #2) + echo '2 feature2' else # No other bases have direct children in our test scenario : diff --git a/tests/test_conflict_resolution_resume.sh b/tests/test_conflict_resolution_resume.sh index 3f0956a..205b101 100644 --- a/tests/test_conflict_resolution_resume.sh +++ b/tests/test_conflict_resolution_resume.sh @@ -21,8 +21,9 @@ ok() { echo "βœ… $1"; PASS=$((PASS+1)); } # Build a configurable gh mock in a temp dir. It records every invocation to # $CALLS and is driven by env vars set per scenario: # MOCK_LABELS newline-separated labels returned by `pr view --json labels` -# MOCK_BASE base branch returned by `pr view --json baseRefName` # MOCK_COMMENTS_FILE file whose contents are returned by `pr view --json comments` +# The PR's base branch is not mocked: the script must take it from PR_BASE +# (event payload), so a baseRefName query is an unhandled call and fails. make_mock_gh() { local dir="$1" cat > "$dir/mock_gh.sh" <<'EOF' @@ -32,7 +33,6 @@ echo "gh $*" >> "$CALLS" if [[ "$1 $2" == "pr view" ]]; then case "$*" in *--json\ labels*) printf '%s\n' "${MOCK_LABELS:-}";; - *--json\ baseRefName*) printf '%s\n' "${MOCK_BASE:-}";; *--json\ comments*) cat "${MOCK_COMMENTS_FILE:-/dev/null}";; *) echo "unhandled pr view: $*" >&2; exit 1;; esac @@ -88,9 +88,9 @@ setup_repo() { } run_resume() { - env ACTION_MODE=conflict-resolved PR_BRANCH=child \ + env ACTION_MODE=conflict-resolved PR_BRANCH=child PR_NUMBER=5 PR_BASE="$PR_BASE" \ GH="$MOCK_DIR/mock_gh.sh" GIT="$MOCK_DIR/mock_git.sh" \ - MOCK_LABELS="$MOCK_LABELS" MOCK_BASE="$MOCK_BASE" \ + MOCK_LABELS="$MOCK_LABELS" \ MOCK_COMMENTS_FILE="$MOCK_COMMENTS_FILE" CALLS="$CALLS" \ bash "$ROOT_DIR/update-pr-stack.sh" >"$WORK/out.log" 2>&1 || echo "EXIT=$?" >>"$WORK/out.log" } @@ -103,7 +103,7 @@ marker() { # base target squash echo "### Scenario A: user manually retargeted the base -> no mutation" setup_repo MOCK_LABELS="autorestack-needs-conflict-resolution" -MOCK_BASE="spark" # human changed it; marker says parent +PR_BASE="spark" # human changed it; marker says parent MOCK_COMMENTS_FILE="$WORK/comments.txt" { echo "### conflict"; echo; marker parent main "$SQUASH"; } > "$MOCK_COMMENTS_FILE" run_resume @@ -119,7 +119,7 @@ ok "A: manual retarget detected, no branch mutation, label removed" echo "### Scenario B: no state marker -> no mutation" setup_repo MOCK_LABELS="autorestack-needs-conflict-resolution" -MOCK_BASE="parent" +PR_BASE="parent" MOCK_COMMENTS_FILE="$WORK/comments.txt" { echo "### some old conflict comment with no marker"; } > "$MOCK_COMMENTS_FILE" run_resume @@ -137,13 +137,13 @@ setup_repo git -C "$WORK" merge -q --no-edit main git -C "$WORK" push -q origin child MOCK_LABELS="autorestack-needs-conflict-resolution" -MOCK_BASE="parent" # matches marker -> not a manual retarget +PR_BASE="parent" # matches marker -> not a manual retarget MOCK_COMMENTS_FILE="$WORK/comments.txt" { echo "### conflict"; echo; marker parent main "$SQUASH"; } > "$MOCK_COMMENTS_FILE" run_resume grep -q -- "git push origin child" "$CALLS" || fail "C: child not pushed" -grep -q -- "pr edit child --base main" "$CALLS" || fail "C: base not retargeted to main" +grep -q -- "pr edit 5 --base main" "$CALLS" || fail "C: base not retargeted to main" grep -q "remove-label autorestack-needs-conflict-resolution" "$CALLS" || fail "C: label not removed" push_line=$(grep -n -- "git push origin child" "$CALLS" | head -1 | cut -d: -f1) base_line=$(grep -n -- "--base main" "$CALLS" | head -1 | cut -d: -f1) @@ -156,7 +156,7 @@ ok "C: resume pushes, retargets base, then removes label" echo "### Scenario D: recorded target branch is gone -> give up cleanly" setup_repo MOCK_LABELS="autorestack-needs-conflict-resolution" -MOCK_BASE="parent" # matches marker -> not a manual retarget +PR_BASE="parent" # matches marker -> not a manual retarget MOCK_COMMENTS_FILE="$WORK/comments.txt" { echo "### conflict"; echo; marker parent ghost-target "$SQUASH"; } > "$MOCK_COMMENTS_FILE" run_resume diff --git a/tests/test_update_pr_stack.sh b/tests/test_update_pr_stack.sh index 2930aac..6c18b28 100755 --- a/tests/test_update_pr_stack.sh +++ b/tests/test_update_pr_stack.sh @@ -89,7 +89,7 @@ run_update_pr_stack # the PR untouched on its old base), and the merged branch deleted only after # the retarget (deleting a PR's base branch closes the PR). push_line=$(grep -n "git push origin feature2" "$RUN_LOG" | head -1 | cut -d: -f1 || true) -edit_line=$(grep -n "pr edit feature2 --base main" "$RUN_LOG" | head -1 | cut -d: -f1 || true) +edit_line=$(grep -n "pr edit 2 --base main" "$RUN_LOG" | head -1 | cut -d: -f1 || true) delete_line=$(grep -n "git push origin :feature1" "$RUN_LOG" | head -1 | cut -d: -f1 || true) if [[ -n "$push_line" && -n "$edit_line" && -n "$delete_line" \ && "$push_line" -lt "$edit_line" && "$edit_line" -lt "$delete_line" ]]; then diff --git a/update-pr-stack.sh b/update-pr-stack.sh index aa08cc7..eb7487b 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -2,11 +2,16 @@ # # Updates PR stack after merging a PR # -# Required environment variables: +# Required environment variables (squash-merge mode): # SQUASH_COMMIT - The hash of the squash commit that was merged # MERGED_BRANCH - The name of the branch that was merged and will be deleted # TARGET_BRANCH - The name of the branch that the PR was merged into # +# Required environment variables (conflict-resolved mode): +# PR_BRANCH - The head branch of the PR being resumed +# PR_NUMBER - Its PR number, from the event payload +# PR_BASE - Its base branch, from the event payload +# # Design note: # This script aims to output a transcript of "plain" git/gh commands that a # human could follow through manually. For this reason: @@ -36,8 +41,8 @@ format_state_marker() { # Echoes the most recent state-marker line found in our PR comments, or nothing. read_state_marker() { - local PR_BRANCH="$1" - gh pr view "$PR_BRANCH" --json comments --jq '.comments[].body' 2>/dev/null \ + local PR_NUMBER="$1" + gh pr view "$PR_NUMBER" --json comments --jq '.comments[].body' 2>/dev/null \ | { grep -F "$STATE_MARKER_PREFIX" || true; } | tail -n1 } @@ -74,9 +79,12 @@ has_squash_commit() { && git merge-base --is-ancestor SQUASH_COMMIT "$BRANCH" } +# Args: head branch, base branch, PR number. git commands use the branch; gh +# commands use the number, since a head branch can carry several PRs. update_direct_target() { local BRANCH="$1" local BASE_BRANCH="$2" + local PR_NUMBER="$3" # Checkout first to ensure the local branch exists (created from origin if # needed). This allows has_squash_commit to compare local refs, which matters @@ -144,10 +152,10 @@ update_direct_target() { echo "Once you push, this action will resume and finish updating this pull request." echo format_state_marker "$MERGED_BRANCH" "$TARGET_BRANCH" "$(git rev-parse SQUASH_COMMIT)" - } | log_cmd gh pr comment "$BRANCH" -F - + } | log_cmd gh pr comment "$PR_NUMBER" -F - # Create the label if it doesn't exist, then add it to the PR gh label create "$CONFLICT_LABEL" --description "PR needs manual conflict resolution" --color "d73a4a" 2>/dev/null || true - log_cmd gh pr edit "$BRANCH" --add-label "$CONFLICT_LABEL" + log_cmd gh pr edit "$PR_NUMBER" --add-label "$CONFLICT_LABEL" return 1 else log_cmd git merge --no-edit -s ours SQUASH_COMMIT @@ -167,9 +175,9 @@ See $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" # Check if a PR has the conflict resolution label pr_has_conflict_label() { - local BRANCH="$1" + local PR_NUMBER="$1" local LABELS - LABELS=$(gh pr view "$BRANCH" --json labels --jq '.labels[].name' 2>/dev/null || echo "") + LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name' 2>/dev/null || echo "") echo "$LABELS" | grep -q "^${CONFLICT_LABEL}$" } @@ -196,20 +204,22 @@ has_sibling_conflicts() { # the conflict label so this action stops re-triggering. Used for the dead-end # cases where we cannot or must not finish automatically. abandon_resume() { - local PR_BRANCH="$1" + local PR_NUMBER="$1" local MESSAGE="$2" - echo "$MESSAGE" | log_cmd gh pr comment "$PR_BRANCH" -F - - log_cmd gh pr edit "$PR_BRANCH" --remove-label "$CONFLICT_LABEL" + echo "$MESSAGE" | log_cmd gh pr comment "$PR_NUMBER" -F - + log_cmd gh pr edit "$PR_NUMBER" --remove-label "$CONFLICT_LABEL" } # Continue processing after user manually resolved conflicts continue_after_resolution() { check_env_var "PR_BRANCH" + check_env_var "PR_NUMBER" + check_env_var "PR_BASE" - echo "Checking if $PR_BRANCH needs continuation after conflict resolution..." + echo "Checking if PR #$PR_NUMBER ($PR_BRANCH) needs continuation after conflict resolution..." # Check if the PR has the conflict label - if ! pr_has_conflict_label "$PR_BRANCH"; then + if ! pr_has_conflict_label "$PR_NUMBER"; then echo "βœ“ $PR_BRANCH does not have conflict label; nothing to do" return fi @@ -221,10 +231,10 @@ continue_after_resolution() { # Recover them from the marker the squash-merge run left in the conflict # comment. local MARKER - MARKER=$(read_state_marker "$PR_BRANCH") + MARKER=$(read_state_marker "$PR_NUMBER") if [[ -z "$MARKER" ]]; then echo "⚠️ No autorestack state marker on $PR_BRANCH; cannot resume safely. Removing the label." - abandon_resume "$PR_BRANCH" "ℹ️ autorestack could not find its state marker on this PR, so it will not update the stack automatically. If this PR still needs its base updated, update its base manually." + abandon_resume "$PR_NUMBER" "ℹ️ autorestack could not find its state marker on this PR, so it will not update the stack automatically. If this PR still needs its base updated, update its base manually." return fi @@ -232,17 +242,12 @@ continue_after_resolution() { read -r OLD_BASE NEW_TARGET SQUASH_HASH < <(parse_state_marker "$MARKER") echo "Recorded state: base=$OLD_BASE target=$NEW_TARGET squash=$SQUASH_HASH" - # The base we left the PR on while waiting for conflict resolution was the - # merged parent branch. If it no longer matches, a human retargeted the PR - # (e.g. straight onto the integration branch); we are no longer the authority - # on its base, so we step back without touching the branch. This runs before - # any mutation: once the base diverges, the recorded target is stale and a - # merge built against it would be wrong. - local CURRENT_BASE - CURRENT_BASE=$(gh pr view "$PR_BRANCH" --json baseRefName --jq '.baseRefName') - if [[ "$CURRENT_BASE" != "$OLD_BASE" ]]; then - echo "⚠️ Base of $PR_BRANCH changed manually ($OLD_BASE -> $CURRENT_BASE); not updating the stack." - abandon_resume "$PR_BRANCH" "ℹ️ The base branch of this PR was changed manually, so autorestack stepped back and will not update it automatically." + # The PR was left based on the merged parent branch. If the payload shows a + # different base, a human retargeted the PR; the recorded target is stale, + # so step back before any mutation. + if [[ "$PR_BASE" != "$OLD_BASE" ]]; then + echo "⚠️ Base of $PR_BRANCH changed manually ($OLD_BASE -> $PR_BASE); not updating the stack." + abandon_resume "$PR_NUMBER" "ℹ️ The base branch of this PR was changed manually, so autorestack stepped back and will not update it automatically." return fi @@ -252,7 +257,7 @@ continue_after_resolution() { # can succeed, so give up cleanly rather than stranding the PR under the label. if ! git rev-parse --verify --quiet "origin/$NEW_TARGET" >/dev/null; then echo "⚠️ Recorded target branch '$NEW_TARGET' no longer exists; abandoning resume of $PR_BRANCH." - abandon_resume "$PR_BRANCH" "ℹ️ The branch this PR was being retargeted onto (\`$NEW_TARGET\`) no longer exists, so autorestack stepped back. If this PR still needs its base updated, update its base manually." + abandon_resume "$PR_NUMBER" "ℹ️ The branch this PR was being retargeted onto (\`$NEW_TARGET\`) no longer exists, so autorestack stepped back. If this PR still needs its base updated, update its base manually." return fi @@ -265,7 +270,7 @@ continue_after_resolution() { log_cmd git update-ref SQUASH_COMMIT "$SQUASH_HASH" MERGED_BRANCH="$OLD_BASE" TARGET_BRANCH="$NEW_TARGET" - if ! update_direct_target "$PR_BRANCH" "$NEW_TARGET"; then + if ! update_direct_target "$PR_BRANCH" "$NEW_TARGET" "$PR_NUMBER"; then echo "⚠️ '$PR_BRANCH' still conflicts; re-posted the conflict comment, will retry on next push" return 1 fi @@ -276,8 +281,8 @@ continue_after_resolution() { # NEW_TARGET when the base flips to it, keeping the PR mergeable (GitHub # suppresses CI on a PR that conflicts with its base). log_cmd git push origin "$PR_BRANCH" - log_cmd gh pr edit "$PR_BRANCH" --base "$NEW_TARGET" - log_cmd gh pr edit "$PR_BRANCH" --remove-label "$CONFLICT_LABEL" + log_cmd gh pr edit "$PR_NUMBER" --base "$NEW_TARGET" + log_cmd gh pr edit "$PR_NUMBER" --remove-label "$CONFLICT_LABEL" # Check if old base branch should be deleted if has_sibling_conflicts "$OLD_BASE" "$PR_BRANCH"; then @@ -297,17 +302,25 @@ main() { log_cmd git update-ref SQUASH_COMMIT "$SQUASH_COMMIT" # Find all PRs directly targeting the merged PR's head - INITIAL_TARGETS=($(log_cmd gh pr list --base "$MERGED_BRANCH" --json headRefName --jq '.[].headRefName')) + INITIAL_NUMBERS=() + INITIAL_TARGETS=() + while read -r NUMBER BRANCH; do + [[ -n "$BRANCH" ]] || continue + INITIAL_NUMBERS+=("$NUMBER") + INITIAL_TARGETS+=("$BRANCH") + done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') # Track successfully updated vs conflicted branches separately UPDATED_TARGETS=() + UPDATED_NUMBERS=() CONFLICTED_TARGETS=() - for BRANCH in "${INITIAL_TARGETS[@]}"; do - if update_direct_target "$BRANCH" "$TARGET_BRANCH"; then - UPDATED_TARGETS+=("$BRANCH") + for i in "${!INITIAL_TARGETS[@]}"; do + if update_direct_target "${INITIAL_TARGETS[$i]}" "$TARGET_BRANCH" "${INITIAL_NUMBERS[$i]}"; then + UPDATED_TARGETS+=("${INITIAL_TARGETS[$i]}") + UPDATED_NUMBERS+=("${INITIAL_NUMBERS[$i]}") else - CONFLICTED_TARGETS+=("$BRANCH") + CONFLICTED_TARGETS+=("${INITIAL_TARGETS[$i]}") fi done @@ -318,8 +331,8 @@ main() { log_cmd git push origin "${UPDATED_TARGETS[@]}" fi - for BRANCH in "${UPDATED_TARGETS[@]}"; do - log_cmd gh pr edit "$BRANCH" --base "$TARGET_BRANCH" + for NUMBER in "${UPDATED_NUMBERS[@]}"; do + log_cmd gh pr edit "$NUMBER" --base "$TARGET_BRANCH" done # Deleting a PR's base branch closes the PR, so this must come after the From 73a37f670818c9b5effa66d8d34ae5af7bc96e95 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 9 Jun 2026 22:02:55 +0000 Subject: [PATCH 05/10] Skip PRs merged with a merge commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The action triggers on any merged PR, but its merge sequence assumes the squash shape: SQUASH_COMMIT~ as "target just before the merge" and an -s ours record of the squash. A real merge commit satisfies neither - and needs no fixing at all, since history is not rewritten and stacked children keep correct diffs. Detect the second parent and bail out before touching anything. Rebase merges remain indistinguishable from squashes in the payload and are processed as before. πŸ€– Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JHvKryT4QUpHYdNq9YEQxX --- .github/workflows/tests.yml | 1 + README.md | 2 +- tests/test_merge_commit_skip.sh | 69 +++++++++++++++++++++++++++++++++ update-pr-stack.sh | 6 +++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100755 tests/test_merge_commit_skip.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 44d3915..8595e92 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,6 +17,7 @@ jobs: bash tests/test_rebase_workflow.sh bash tests/test_mixed_workflows.sh bash tests/test_conflict_resolution_resume.sh + bash tests/test_merge_commit_skip.sh e2e-tests: name: E2E Tests diff --git a/README.md b/README.md index 9b6094e..552689b 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ jobs: ### Notes -* Currently only supports squash merges +* Currently only supports squash merges; PRs merged with a merge commit are detected and skipped (history isn't rewritten, so stacked PRs stay valid as-is) * If a merge hits a conflict, you'll need to resolve it manually; pushing the resolution automatically continues the stack update * Very large stacks might hit GitHub rate limits diff --git a/tests/test_merge_commit_skip.sh b/tests/test_merge_commit_skip.sh new file mode 100755 index 0000000..dfc36f8 --- /dev/null +++ b/tests/test_merge_commit_skip.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# +# A PR merged with a merge commit (not squashed) must be left alone: history is +# not rewritten, so stacked children stay valid and need no synthetic merge. + +set -ueo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/../command_utils.sh" + +simulate_push() { + log_cmd git update-ref "refs/remotes/origin/$1" "$1" +} + +TEST_REPO=$(mktemp -d) +cd "$TEST_REPO" +echo "Created test repo at $TEST_REPO" + +log_cmd git init -b main +log_cmd git config user.email "test@example.com" +log_cmd git config user.name "Test User" + +echo "line" > file.txt +log_cmd git add file.txt +log_cmd git commit -m "Initial commit" +simulate_push main + +log_cmd git checkout -b feature1 +echo "f1" >> file.txt +log_cmd git add file.txt +log_cmd git commit -m "Add feature 1" +simulate_push feature1 + +log_cmd git checkout -b feature2 +echo "f2" >> file.txt +log_cmd git add file.txt +log_cmd git commit -m "Add feature 2" +simulate_push feature2 + +# Merge feature1 into main with a real merge commit +log_cmd git checkout main +log_cmd git merge --no-ff --no-edit feature1 +MERGE_COMMIT=$(git rev-parse HEAD) +simulate_push main + +FEATURE2_BEFORE=$(git rev-parse feature2) + +OUT=$(env \ + SQUASH_COMMIT="$MERGE_COMMIT" \ + MERGED_BRANCH=feature1 \ + TARGET_BRANCH=main \ + GH="$SCRIPT_DIR/mock_gh.sh" \ + GIT="$SCRIPT_DIR/mock_git.sh" \ + "$SCRIPT_DIR/../update-pr-stack.sh" 2>&1) +echo "$OUT" + +if ! grep -q "merged with a merge commit" <<<"$OUT"; then + echo "❌ Expected the merge-commit skip message" + exit 1 +fi +if grep -q "pr edit" <<<"$OUT"; then + echo "❌ No PR must be retargeted" + exit 1 +fi +if [[ "$(git rev-parse feature2)" != "$FEATURE2_BEFORE" ]]; then + echo "❌ feature2 must not be modified" + exit 1 +fi +echo "βœ… Merge-commit merge skipped, stack untouched" diff --git a/update-pr-stack.sh b/update-pr-stack.sh index eb7487b..da3cbc4 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -301,6 +301,12 @@ main() { log_cmd git update-ref SQUASH_COMMIT "$SQUASH_COMMIT" + # A merge-commit merge does not rewrite history; stacked PRs stay valid. + if git rev-parse --verify --quiet SQUASH_COMMIT^2 >/dev/null; then + echo "βœ“ '$MERGED_BRANCH' was merged with a merge commit, not squashed; nothing to do" + return 0 + fi + # Find all PRs directly targeting the merged PR's head INITIAL_NUMBERS=() INITIAL_TARGETS=() From a28f98069d9a03c17d8191269b0d64b88f75705f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Rubinstein?= Date: Wed, 10 Jun 2026 11:25:12 +0200 Subject: [PATCH 06/10] Retarget children instead of skipping on a merge-commit merge A full skip left child PRs based on the deleted-but-not-deleted merged branch forever: nothing retargeted them and nothing deleted the branch, the two things the action exists to automate. The children's heads need no rewriting (the merge commit carries the parent's commits into the target branch), so retarget them as-is and delete the branch. Co-Authored-By: Claude Fable 5 --- .github/workflows/tests.yml | 2 +- README.md | 2 +- ...mit_skip.sh => test_merge_commit_merge.sh} | 40 +++++++++++++++---- update-pr-stack.sh | 13 +++++- 4 files changed, 45 insertions(+), 12 deletions(-) rename tests/{test_merge_commit_skip.sh => test_merge_commit_merge.sh} (51%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8595e92..403a729 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,7 +17,7 @@ jobs: bash tests/test_rebase_workflow.sh bash tests/test_mixed_workflows.sh bash tests/test_conflict_resolution_resume.sh - bash tests/test_merge_commit_skip.sh + bash tests/test_merge_commit_merge.sh e2e-tests: name: E2E Tests diff --git a/README.md b/README.md index 552689b..0c518a5 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ jobs: ### Notes -* Currently only supports squash merges; PRs merged with a merge commit are detected and skipped (history isn't rewritten, so stacked PRs stay valid as-is) +* Built for squash merges. A PR merged with a merge commit keeps its history, so the action only retargets its children and deletes the branch. Rebase merges look like squash merges in the event payload and take the squash path; the result is correct, but a multi-commit rebase can require a manual conflict resolution that a squash would not have. * If a merge hits a conflict, you'll need to resolve it manually; pushing the resolution automatically continues the stack update * Very large stacks might hit GitHub rate limits diff --git a/tests/test_merge_commit_skip.sh b/tests/test_merge_commit_merge.sh similarity index 51% rename from tests/test_merge_commit_skip.sh rename to tests/test_merge_commit_merge.sh index dfc36f8..261ba5a 100755 --- a/tests/test_merge_commit_skip.sh +++ b/tests/test_merge_commit_merge.sh @@ -1,7 +1,9 @@ #!/bin/bash # -# A PR merged with a merge commit (not squashed) must be left alone: history is -# not rewritten, so stacked children stay valid and need no synthetic merge. +# A PR merged with a merge commit (not squashed) keeps its history, so stacked +# children already contain the parent's commits and their heads must not be +# rewritten. The action only retargets the children and deletes the merged +# branch. set -ueo pipefail @@ -55,15 +57,37 @@ OUT=$(env \ echo "$OUT" if ! grep -q "merged with a merge commit" <<<"$OUT"; then - echo "❌ Expected the merge-commit skip message" + echo "❌ Expected the merge-commit message" exit 1 fi -if grep -q "pr edit" <<<"$OUT"; then - echo "❌ No PR must be retargeted" +if [[ "$(git rev-parse feature2)" != "$FEATURE2_BEFORE" ]]; then + echo "❌ feature2's head must not be rewritten" exit 1 fi -if [[ "$(git rev-parse feature2)" != "$FEATURE2_BEFORE" ]]; then - echo "❌ feature2 must not be modified" + +# Children must be retargeted before the merged branch is deleted (deleting a +# PR's base branch closes the PR). +EDIT_LINE=$(grep -n "pr edit 2 --base main" <<<"$OUT" | cut -d: -f1 | head -1 || true) +DELETE_LINE=$(grep -n "push origin :feature1" <<<"$OUT" | cut -d: -f1 | head -1 || true) +if [[ -z "$EDIT_LINE" ]]; then + echo "❌ Child PR must be retargeted onto main" exit 1 fi -echo "βœ… Merge-commit merge skipped, stack untouched" +if [[ -z "$DELETE_LINE" ]]; then + echo "❌ Merged branch must be deleted" + exit 1 +fi +if [[ "$EDIT_LINE" -gt "$DELETE_LINE" ]]; then + echo "❌ Retarget must happen before the branch deletion (edit=$EDIT_LINE delete=$DELETE_LINE)" + exit 1 +fi + +# The untouched head keeps a clean diff against the new base +ACTUAL_DIFF=$(git diff main...feature2 | grep '^[+-]' | grep -v '^[+-][+-][+-]') +if [[ "$ACTUAL_DIFF" != "+f2" ]]; then + echo "❌ Diff main...feature2 should show only feature2's change, got:" + echo "$ACTUAL_DIFF" + exit 1 +fi + +echo "βœ… Merge-commit merge: children retargeted, heads untouched, branch deleted" diff --git a/update-pr-stack.sh b/update-pr-stack.sh index da3cbc4..6649197 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -301,9 +301,18 @@ main() { log_cmd git update-ref SQUASH_COMMIT "$SQUASH_COMMIT" - # A merge-commit merge does not rewrite history; stacked PRs stay valid. + # A merge-commit merge does not rewrite history: each child's head already + # contains the merged branch's commits, and the merge commit carries them + # into TARGET_BRANCH. The heads need no synthetic merge; just retarget the + # children and delete the merged branch. if git rev-parse --verify --quiet SQUASH_COMMIT^2 >/dev/null; then - echo "βœ“ '$MERGED_BRANCH' was merged with a merge commit, not squashed; nothing to do" + echo "βœ“ '$MERGED_BRANCH' was merged with a merge commit, not squashed; retargeting children without touching their heads" + while read -r NUMBER BRANCH; do + [[ -n "$BRANCH" ]] || continue + log_cmd gh pr edit "$NUMBER" --base "$TARGET_BRANCH" + done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') + # Deleting a PR's base branch closes the PR, so the retargets come first. + log_cmd git push origin ":$MERGED_BRANCH" return 0 fi From f918548b260e5316e6cd8101219fd9789aa77478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Rubinstein?= Date: Wed, 10 Jun 2026 11:45:58 +0200 Subject: [PATCH 07/10] Detect rebase merges and skip them with a comment on each child GitHub records the merge method nowhere (payload, REST, GraphQL), so detect it: a rebase leaves a patch-equivalent copy of every PR commit on the target, a multi-commit squash does not. Single-commit PRs merge identically either way and keep taking the squash path. Co-Authored-By: Claude Fable 5 --- .github/workflows/tests.yml | 1 + README.md | 2 +- tests/mock_gh.sh | 3 ++ tests/test_rebase_merge_skip.sh | 89 +++++++++++++++++++++++++++++++++ update-pr-stack.sh | 28 +++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100755 tests/test_rebase_merge_skip.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 403a729..856d69c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,6 +18,7 @@ jobs: bash tests/test_mixed_workflows.sh bash tests/test_conflict_resolution_resume.sh bash tests/test_merge_commit_merge.sh + bash tests/test_rebase_merge_skip.sh e2e-tests: name: E2E Tests diff --git a/README.md b/README.md index 0c518a5..6eb1dc3 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ jobs: ### Notes -* Built for squash merges. A PR merged with a merge commit keeps its history, so the action only retargets its children and deletes the branch. Rebase merges look like squash merges in the event payload and take the squash path; the result is correct, but a multi-commit rebase can require a manual conflict resolution that a squash would not have. +* Built for squash merges. A PR merged with a merge commit keeps its history, so the action only retargets its children and deletes the branch. Rebase merges are not supported: the action detects them (heuristically, since GitHub does not record the merge method anywhere) and comments on each child PR instead of acting. * If a merge hits a conflict, you'll need to resolve it manually; pushing the resolution automatically continues the stack update * Very large stacks might hit GitHub rate limits diff --git a/tests/mock_gh.sh b/tests/mock_gh.sh index 5a0b1ab..4e74a02 100755 --- a/tests/mock_gh.sh +++ b/tests/mock_gh.sh @@ -23,6 +23,9 @@ if [[ "$1" == "pr" && "$2" == "list" ]]; then elif [[ "$1" == "pr" && "$2" == "edit" ]]; then # Just log the edit command echo "Mock: gh pr edit $3 --base $5" +elif [[ "$1" == "pr" && "$2" == "comment" ]]; then + # Just log the comment command + echo "Mock: gh pr comment $3" else echo "Unknown gh command: $@" >&2 exit 1 diff --git a/tests/test_rebase_merge_skip.sh b/tests/test_rebase_merge_skip.sh new file mode 100755 index 0000000..44969dd --- /dev/null +++ b/tests/test_rebase_merge_skip.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# +# A PR merged with "Rebase and merge" is not supported: the commits on the +# target are new copies, so neither retargeting children as-is nor the squash +# sequence gives a correct result. The action must detect the rebase (every PR +# commit has a patch-equivalent copy on the target), comment on the children, +# and leave the stack alone. + +set -ueo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/../command_utils.sh" + +simulate_push() { + log_cmd git update-ref "refs/remotes/origin/$1" "$1" +} + +TEST_REPO=$(mktemp -d) +cd "$TEST_REPO" +echo "Created test repo at $TEST_REPO" + +log_cmd git init -b main +log_cmd git config user.email "test@example.com" +log_cmd git config user.name "Test User" + +for i in $(seq 1 15); do echo "line $i" >> file.txt; done +log_cmd git add file.txt +log_cmd git commit -m "Initial commit" +simulate_push main + +# feature1: two commits, so the rebase shape differs from a squash +log_cmd git checkout -b feature1 +sed -i '2s/.*/Feature 1 change A/' file.txt +log_cmd git add file.txt +log_cmd git commit -m "feature1: commit A" +sed -i '6s/.*/Feature 1 change B/' file.txt +log_cmd git add file.txt +log_cmd git commit -m "feature1: commit B" +simulate_push feature1 + +log_cmd git checkout -b feature2 +sed -i '14s/.*/Feature 2 content/' file.txt +log_cmd git add file.txt +log_cmd git commit -m "feature2: change" +simulate_push feature2 + +# main advances independently, then feature1 is rebase-merged: GitHub copies +# each PR commit onto the target, which cherry-pick reproduces +log_cmd git checkout main +sed -i '10s/.*/Main hotfix/' file.txt +log_cmd git add file.txt +log_cmd git commit -m "main: hotfix" +log_cmd git cherry-pick feature1~1 feature1 +MERGE_COMMIT=$(git rev-parse HEAD) +simulate_push main + +FEATURE2_BEFORE=$(git rev-parse feature2) + +OUT=$(env \ + SQUASH_COMMIT="$MERGE_COMMIT" \ + MERGED_BRANCH=feature1 \ + TARGET_BRANCH=main \ + GH="$SCRIPT_DIR/mock_gh.sh" \ + GIT="$SCRIPT_DIR/mock_git.sh" \ + "$SCRIPT_DIR/../update-pr-stack.sh" 2>&1) +echo "$OUT" + +if ! grep -q "rebase merges are not supported" <<<"$OUT"; then + echo "❌ Expected the rebase-merge skip message" + exit 1 +fi +if ! grep -q "Mock: gh pr comment 2" <<<"$OUT"; then + echo "❌ The child PR must be told the stack was not updated" + exit 1 +fi +if grep -q "pr edit" <<<"$OUT"; then + echo "❌ No PR must be retargeted" + exit 1 +fi +if grep -q "push origin :feature1" <<<"$OUT"; then + echo "❌ The merged branch must be kept" + exit 1 +fi +if [[ "$(git rev-parse feature2)" != "$FEATURE2_BEFORE" ]]; then + echo "❌ feature2's head must not be rewritten" + exit 1 +fi + +echo "βœ… Rebase merge detected: children warned, stack left alone" diff --git a/update-pr-stack.sh b/update-pr-stack.sh index 6649197..25b3a06 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -79,6 +79,21 @@ has_squash_commit() { && git merge-base --is-ancestor SQUASH_COMMIT "$BRANCH" } +# Heuristic: the event payload does not say which merge method was used +# (GitHub exposes it nowhere). A rebase merge copies every commit of the PR +# onto the target branch, so each original has a patch-equivalent there and +# git cherry marks it "-". A squash of two or more commits leaves only their +# combined patch on the target, so the originals show as "+". A single-commit +# PR merges identically under rebase and squash, so it takes the squash path. +# A rebase whose copies drifted (context changes) reads as a squash, so the +# heuristic errs toward processing, never toward skipping a real squash. +is_rebase_merge() { + local COMMIT_COUNT + COMMIT_COUNT=$(git rev-list --count --no-merges "origin/$TARGET_BRANCH..origin/$MERGED_BRANCH") + [[ "$COMMIT_COUNT" -ge 2 ]] || return 1 + ! git cherry "origin/$TARGET_BRANCH" "origin/$MERGED_BRANCH" | grep -q '^+' +} + # Args: head branch, base branch, PR number. git commands use the branch; gh # commands use the number, since a head branch can carry several PRs. update_direct_target() { @@ -316,6 +331,19 @@ main() { return 0 fi + # Rebase merges are not supported: the copies on the target are new + # commits, so a child retargeted as-is would show its parent's changes in + # its diff, and the squash sequence can raise spurious conflicts against + # the intermediate copies. Tell the children and leave everything alone. + if is_rebase_merge; then + echo "⚠️ '$MERGED_BRANCH' looks rebase-merged; rebase merges are not supported, leaving the stack alone" + while read -r NUMBER BRANCH; do + [[ -n "$BRANCH" ]] || continue + log_cmd gh pr comment "$NUMBER" --body "ℹ️ The base branch \`$MERGED_BRANCH\` of this PR was merged with \"Rebase and merge\", which autorestack does not support. Update this PR manually. \`$MERGED_BRANCH\` was kept so this PR stays open." + done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') + return 0 + fi + # Find all PRs directly targeting the merged PR's head INITIAL_NUMBERS=() INITIAL_TARGETS=() From ce57ddd53b7e589bf05cb23435a09ec9723b24bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Rubinstein?= Date: Wed, 10 Jun 2026 13:06:32 +0200 Subject: [PATCH 08/10] Detect rebase merges via GitHub's commit-PR association Replaces the patch-id heuristic: ask which PR introduced the commit just below the merge sha. An older PR or none means squash; this PR means a rebase copy. The association is computed asynchronously, so an empty answer is only trusted once the merge sha itself is associated. Co-Authored-By: Claude Fable 5 --- README.md | 2 +- tests/mock_gh.sh | 10 ++++++ tests/test_merge_commit_merge.sh | 1 + tests/test_mixed_workflows.sh | 1 + tests/test_rebase_merge_skip.sh | 10 +++--- tests/test_rebase_workflow.sh | 1 + tests/test_update_pr_stack.sh | 1 + update-pr-stack.sh | 56 ++++++++++++++++++++++++-------- 8 files changed, 64 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6eb1dc3..aebb1a9 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ jobs: ### Notes -* Built for squash merges. A PR merged with a merge commit keeps its history, so the action only retargets its children and deletes the branch. Rebase merges are not supported: the action detects them (heuristically, since GitHub does not record the merge method anywhere) and comments on each child PR instead of acting. +* Built for squash merges. A PR merged with a merge commit keeps its history, so the action only retargets its children and deletes the branch. Rebase merges are not supported: the action detects them through GitHub's commit-PR association (the merge method itself is recorded nowhere) and comments on each child PR instead of acting. * If a merge hits a conflict, you'll need to resolve it manually; pushing the resolution automatically continues the stack update * Very large stacks might hit GitHub rate limits diff --git a/tests/mock_gh.sh b/tests/mock_gh.sh index 4e74a02..3114109 100755 --- a/tests/mock_gh.sh +++ b/tests/mock_gh.sh @@ -26,6 +26,16 @@ elif [[ "$1" == "pr" && "$2" == "edit" ]]; then elif [[ "$1" == "pr" && "$2" == "comment" ]]; then # Just log the comment command echo "Mock: gh pr comment $3" +elif [[ "$1" == "api" && "$2" == repos/*/commits/*/pulls ]]; then + # Which PRs introduced this trunk commit (already --jq filtered to bare + # numbers). The merge commit belongs to the merged PR, and so does any sha + # listed in MOCK_REBASE_COPIES (space-separated); anything else was not + # introduced by a PR. SQUASH_COMMIT and PR_NUMBER come from the test's env. + sha="${2#*/commits/}" + sha="${sha%/pulls}" + if [[ "$sha" == "$SQUASH_COMMIT" || " ${MOCK_REBASE_COPIES:-} " == *" $sha "* ]]; then + echo "$PR_NUMBER" + fi else echo "Unknown gh command: $@" >&2 exit 1 diff --git a/tests/test_merge_commit_merge.sh b/tests/test_merge_commit_merge.sh index 261ba5a..af5b57a 100755 --- a/tests/test_merge_commit_merge.sh +++ b/tests/test_merge_commit_merge.sh @@ -50,6 +50,7 @@ FEATURE2_BEFORE=$(git rev-parse feature2) OUT=$(env \ SQUASH_COMMIT="$MERGE_COMMIT" \ MERGED_BRANCH=feature1 \ + PR_NUMBER=1 \ TARGET_BRANCH=main \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ diff --git a/tests/test_mixed_workflows.sh b/tests/test_mixed_workflows.sh index f121a2a..4fb8452 100755 --- a/tests/test_mixed_workflows.sh +++ b/tests/test_mixed_workflows.sh @@ -90,6 +90,7 @@ run_update_pr_stack() { env \ SQUASH_COMMIT=$SQUASH_COMMIT \ MERGED_BRANCH=feature1 \ + PR_NUMBER=1 \ TARGET_BRANCH=main \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ diff --git a/tests/test_rebase_merge_skip.sh b/tests/test_rebase_merge_skip.sh index 44969dd..7af6c50 100755 --- a/tests/test_rebase_merge_skip.sh +++ b/tests/test_rebase_merge_skip.sh @@ -2,9 +2,9 @@ # # A PR merged with "Rebase and merge" is not supported: the commits on the # target are new copies, so neither retargeting children as-is nor the squash -# sequence gives a correct result. The action must detect the rebase (every PR -# commit has a patch-equivalent copy on the target), comment on the children, -# and leave the stack alone. +# sequence gives a correct result. The action must detect the rebase (the +# commit below the merge sha was also introduced by this PR, per GitHub's +# commit-PR association), comment on the children, and leave the stack alone. set -ueo pipefail @@ -28,7 +28,7 @@ log_cmd git add file.txt log_cmd git commit -m "Initial commit" simulate_push main -# feature1: two commits, so the rebase shape differs from a squash +# feature1: two commits, so the rebase leaves a copy below the merge sha log_cmd git checkout -b feature1 sed -i '2s/.*/Feature 1 change A/' file.txt log_cmd git add file.txt @@ -59,7 +59,9 @@ FEATURE2_BEFORE=$(git rev-parse feature2) OUT=$(env \ SQUASH_COMMIT="$MERGE_COMMIT" \ MERGED_BRANCH=feature1 \ + PR_NUMBER=1 \ TARGET_BRANCH=main \ + MOCK_REBASE_COPIES="$(git rev-parse "$MERGE_COMMIT~")" \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ "$SCRIPT_DIR/../update-pr-stack.sh" 2>&1) diff --git a/tests/test_rebase_workflow.sh b/tests/test_rebase_workflow.sh index 9cbaa77..4b72418 100755 --- a/tests/test_rebase_workflow.sh +++ b/tests/test_rebase_workflow.sh @@ -85,6 +85,7 @@ run_update_pr_stack() { env \ SQUASH_COMMIT=$SQUASH_COMMIT \ MERGED_BRANCH=feature1 \ + PR_NUMBER=1 \ TARGET_BRANCH=main \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ diff --git a/tests/test_update_pr_stack.sh b/tests/test_update_pr_stack.sh index 6c18b28..9127597 100755 --- a/tests/test_update_pr_stack.sh +++ b/tests/test_update_pr_stack.sh @@ -78,6 +78,7 @@ run_update_pr_stack() { env \ SQUASH_COMMIT=$SQUASH_COMMIT \ MERGED_BRANCH=feature1 \ + PR_NUMBER=1 \ TARGET_BRANCH=main \ GH="$SCRIPT_DIR/mock_gh.sh" \ GIT="$SCRIPT_DIR/mock_git.sh" \ diff --git a/update-pr-stack.sh b/update-pr-stack.sh index 25b3a06..f267839 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -79,19 +79,48 @@ has_squash_commit() { && git merge-base --is-ancestor SQUASH_COMMIT "$BRANCH" } -# Heuristic: the event payload does not say which merge method was used -# (GitHub exposes it nowhere). A rebase merge copies every commit of the PR -# onto the target branch, so each original has a patch-equivalent there and -# git cherry marks it "-". A squash of two or more commits leaves only their -# combined patch on the target, so the originals show as "+". A single-commit -# PR merges identically under rebase and squash, so it takes the squash path. -# A rebase whose copies drifted (context changes) reads as a squash, so the -# heuristic errs toward processing, never toward skipping a real squash. +# Args: a commit sha. Echoes the numbers of the pull requests that introduced +# the commit to the repository, one per line. +commit_pull_numbers() { + gh api "repos/{owner}/{repo}/commits/$1/pulls" --jq '.[].number' \ + || { echo "❌ Could not list the pull requests that introduced commit $1" >&2; return 1; } +} + +# Args: the merged PR's number. The event payload does not say which merge +# method was used (GitHub records it nowhere), but GitHub associates every +# trunk commit with the PR that introduced it. A squash introduces a single +# commit, so the commit below SQUASH_COMMIT belongs to an older PR or to +# none; a rebase introduces a copy of each PR commit, so with two or more +# commits the one below SQUASH_COMMIT still belongs to this PR. A +# single-commit PR merges identically under rebase and squash and correctly +# reads as a squash here. is_rebase_merge() { - local COMMIT_COUNT - COMMIT_COUNT=$(git rev-list --count --no-merges "origin/$TARGET_BRANCH..origin/$MERGED_BRANCH") - [[ "$COMMIT_COUNT" -ge 2 ]] || return 1 - ! git cherry "origin/$TARGET_BRANCH" "origin/$MERGED_BRANCH" | grep -q '^+' + local PR_NUMBER="$1" + local MERGE_SHA PARENT_SHA NUMBERS + MERGE_SHA=$(git rev-parse SQUASH_COMMIT) + PARENT_SHA=$(git rev-parse SQUASH_COMMIT~) + NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 + if [[ -n "$NUMBERS" ]]; then + grep -qx "$PR_NUMBER" <<<"$NUMBERS" + return + fi + # The association is computed asynchronously, so right after the merge an + # empty answer is ambiguous: "no PR introduced this commit" (a squash on + # top of a direct push) or "not indexed yet" (a rebase copy). SQUASH_COMMIT + # itself always gets associated with this PR, and the commits of one merge + # are indexed together: once it shows up, an empty answer for the parent + # can be trusted. + for _ in $(seq 1 24); do + NUMBERS=$(commit_pull_numbers "$MERGE_SHA") || exit 1 + if grep -qx "$PR_NUMBER" <<<"$NUMBERS"; then + NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 + grep -qx "$PR_NUMBER" <<<"$NUMBERS" + return + fi + sleep "${ASSOCIATION_POLL_SECONDS:-5}" + done + echo "❌ GitHub never associated $MERGE_SHA with PR #$PR_NUMBER; cannot tell a squash from a rebase" >&2 + exit 1 } # Args: head branch, base branch, PR number. git commands use the branch; gh @@ -313,6 +342,7 @@ main() { check_env_var "SQUASH_COMMIT" check_env_var "MERGED_BRANCH" check_env_var "TARGET_BRANCH" + check_env_var "PR_NUMBER" log_cmd git update-ref SQUASH_COMMIT "$SQUASH_COMMIT" @@ -335,7 +365,7 @@ main() { # commits, so a child retargeted as-is would show its parent's changes in # its diff, and the squash sequence can raise spurious conflicts against # the intermediate copies. Tell the children and leave everything alone. - if is_rebase_merge; then + if is_rebase_merge "$PR_NUMBER"; then echo "⚠️ '$MERGED_BRANCH' looks rebase-merged; rebase merges are not supported, leaving the stack alone" while read -r NUMBER BRANCH; do [[ -n "$BRANCH" ]] || continue From 63ccc55bf24accf28fd1b0d6ea594b0f9ab51290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Rubinstein?= Date: Wed, 10 Jun 2026 13:44:13 +0200 Subject: [PATCH 09/10] Split the association wait out of is_rebase_merge Co-Authored-By: Claude Fable 5 --- update-pr-stack.sh | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/update-pr-stack.sh b/update-pr-stack.sh index f267839..b7ce705 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -94,28 +94,18 @@ commit_pull_numbers() { # commits the one below SQUASH_COMMIT still belongs to this PR. A # single-commit PR merges identically under rebase and squash and correctly # reads as a squash here. -is_rebase_merge() { - local PR_NUMBER="$1" - local MERGE_SHA PARENT_SHA NUMBERS - MERGE_SHA=$(git rev-parse SQUASH_COMMIT) - PARENT_SHA=$(git rev-parse SQUASH_COMMIT~) - NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 - if [[ -n "$NUMBERS" ]]; then - grep -qx "$PR_NUMBER" <<<"$NUMBERS" - return - fi - # The association is computed asynchronously, so right after the merge an - # empty answer is ambiguous: "no PR introduced this commit" (a squash on - # top of a direct push) or "not indexed yet" (a rebase copy). SQUASH_COMMIT - # itself always gets associated with this PR, and the commits of one merge - # are indexed together: once it shows up, an empty answer for the parent - # can be trusted. +# Args: the merge commit sha, the merged PR's number. The association is +# computed asynchronously, some time after the merge. The merge commit always +# belongs to the merged PR, so once it shows up the index has caught up with +# this merge; until then, an empty answer for any commit of the merge means +# nothing. Exits if the association never appears. +wait_for_pull_association() { + local MERGE_SHA="$1" PR_NUMBER="$2" + local NUMBERS for _ in $(seq 1 24); do NUMBERS=$(commit_pull_numbers "$MERGE_SHA") || exit 1 if grep -qx "$PR_NUMBER" <<<"$NUMBERS"; then - NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 - grep -qx "$PR_NUMBER" <<<"$NUMBERS" - return + return 0 fi sleep "${ASSOCIATION_POLL_SECONDS:-5}" done @@ -123,6 +113,24 @@ is_rebase_merge() { exit 1 } +is_rebase_merge() { + local PR_NUMBER="$1" + local MERGE_SHA PARENT_SHA NUMBERS + MERGE_SHA=$(git rev-parse SQUASH_COMMIT) + PARENT_SHA=$(git rev-parse SQUASH_COMMIT~) + + NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 + if [[ -z "$NUMBERS" ]]; then + # Ambiguous: "no PR introduced this commit" (a squash on top of a + # direct push) and "not indexed yet" (a rebase copy) both come back + # empty. Wait until the index has caught up with this merge, then ask + # again; this time empty really means no PR. + wait_for_pull_association "$MERGE_SHA" "$PR_NUMBER" + NUMBERS=$(commit_pull_numbers "$PARENT_SHA") || exit 1 + fi + grep -qx "$PR_NUMBER" <<<"$NUMBERS" +} + # Args: head branch, base branch, PR number. git commands use the branch; gh # commands use the number, since a head branch can carry several PRs. update_direct_target() { From 41aa30e77d4403c2f2d1b596eeb8accf699559a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Rubinstein?= Date: Wed, 10 Jun 2026 16:18:29 +0200 Subject: [PATCH 10/10] Clean up doc comments and factor the child-PR listing Co-Authored-By: Claude Fable 5 --- update-pr-stack.sh | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/update-pr-stack.sh b/update-pr-stack.sh index b7ce705..9a3eb2c 100755 --- a/update-pr-stack.sh +++ b/update-pr-stack.sh @@ -6,6 +6,7 @@ # SQUASH_COMMIT - The hash of the squash commit that was merged # MERGED_BRANCH - The name of the branch that was merged and will be deleted # TARGET_BRANCH - The name of the branch that the PR was merged into +# PR_NUMBER - The number of the PR that was merged # # Required environment variables (conflict-resolved mode): # PR_BRANCH - The head branch of the PR being resumed @@ -86,14 +87,6 @@ commit_pull_numbers() { || { echo "❌ Could not list the pull requests that introduced commit $1" >&2; return 1; } } -# Args: the merged PR's number. The event payload does not say which merge -# method was used (GitHub records it nowhere), but GitHub associates every -# trunk commit with the PR that introduced it. A squash introduces a single -# commit, so the commit below SQUASH_COMMIT belongs to an older PR or to -# none; a rebase introduces a copy of each PR commit, so with two or more -# commits the one below SQUASH_COMMIT still belongs to this PR. A -# single-commit PR merges identically under rebase and squash and correctly -# reads as a squash here. # Args: the merge commit sha, the merged PR's number. The association is # computed asynchronously, some time after the merge. The merge commit always # belongs to the merged PR, so once it shows up the index has caught up with @@ -113,6 +106,14 @@ wait_for_pull_association() { exit 1 } +# Args: the merged PR's number. The event payload does not say which merge +# method was used (GitHub records it nowhere), but GitHub associates every +# trunk commit with the PR that introduced it. A squash introduces a single +# commit, so the commit below SQUASH_COMMIT belongs to an older PR or to +# none; a rebase introduces a copy of each PR commit, so with two or more +# commits the one below SQUASH_COMMIT still belongs to this PR. A +# single-commit PR merges identically under rebase and squash and correctly +# reads as a squash here. is_rebase_merge() { local PR_NUMBER="$1" local MERGE_SHA PARENT_SHA NUMBERS @@ -131,6 +132,11 @@ is_rebase_merge() { grep -qx "$PR_NUMBER" <<<"$NUMBERS" } +# Echoes " " for each open PR based on the merged branch. +list_child_prs() { + log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"' +} + # Args: head branch, base branch, PR number. git commands use the branch; gh # commands use the number, since a head branch can carry several PRs. update_direct_target() { @@ -363,7 +369,7 @@ main() { while read -r NUMBER BRANCH; do [[ -n "$BRANCH" ]] || continue log_cmd gh pr edit "$NUMBER" --base "$TARGET_BRANCH" - done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') + done < <(list_child_prs) # Deleting a PR's base branch closes the PR, so the retargets come first. log_cmd git push origin ":$MERGED_BRANCH" return 0 @@ -378,7 +384,7 @@ main() { while read -r NUMBER BRANCH; do [[ -n "$BRANCH" ]] || continue log_cmd gh pr comment "$NUMBER" --body "ℹ️ The base branch \`$MERGED_BRANCH\` of this PR was merged with \"Rebase and merge\", which autorestack does not support. Update this PR manually. \`$MERGED_BRANCH\` was kept so this PR stays open." - done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') + done < <(list_child_prs) return 0 fi @@ -389,7 +395,7 @@ main() { [[ -n "$BRANCH" ]] || continue INITIAL_NUMBERS+=("$NUMBER") INITIAL_TARGETS+=("$BRANCH") - done < <(log_cmd gh pr list --base "$MERGED_BRANCH" --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"') + done < <(list_child_prs) # Track successfully updated vs conflicted branches separately UPDATED_TARGETS=()