Skip to content

merge_check: say when a PR is already on main without a merge of its own (stacked PRs) - #8666

Closed
berkay8987 wants to merge 1 commit into
ProjectLeonida:mainfrom
berkay8987:merge-check-already-on-main
Closed

berkay8987 wants to merge 1 commit into
ProjectLeonida:mainfrom
berkay8987:merge-check-already-on-main

Conversation

@berkay8987

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new BUILD item, #8504 (comment)

Depends on: #7468, #7660 and #8291, in the sense the template asks for: each edits scripts/merge_check.py close to the lines this one does and git merge-tree reports a conflict with each, so whichever lands second needs a small rebase. It also goes together with #8564 (no textual conflict); #8564's commit 3198b20 keeps its --depth 5000 test true with this change.

What and why

already-merged needs a merge under the PR's own number ("Merge pull request #N" or a subject ending in "(#N)"). A PR whose change landed another way never has one:

The script calls those clean, or conflict once main has edited their lines, never "already on main". Both versions of the script on the real PRs, read-only, against main 867d922f:

PR how it landed before after
#5518 under another commit clean already-on-main
#6461 under another commit (its one commit is on main by patch) clean already-on-main
#1712 inside #2359's squash; its test file was reworded since conflict conflict, nothing outside the conflicts
#3885 its one line is main's conflict conflict, nothing outside the conflicts
#3777 an open PR with work of its own (control) conflict conflict

Two additions:

  • already-on-main: no merge of the PR is found, but the conflict-free merged tree matches main, so merging it changes nothing. It ranks after already-merged and before duplicates, and exits 1 like every verdict but clean.
  • on_main_outside_conflicts (a JSON field, and a clause in the text line): with a conflict, whether the merge with every conflicting hunk resolved to main's side (merge-tree -X ours) leaves main as it is. The verdict stays conflict, because a PR whose every change conflicts looks the same; it is a hint for whoever reads the report, not a verdict. It is null without a conflict and where git cannot make that merge (a conflict -X ours does not settle, or a git whose merge-tree takes no -X), so the script still runs on git 2.38.

One existing expectation changes. In test_a_multi_commit_squash_merged_pr_is_already_merged_despite_cherry_miss, 999=sq2 still finds no merge of #999, which is what that check is about; but sq2's content is on main, so it now reads already-on-main (exit 1) instead of clean.

Tested

  • python -I tests/test_merge_check.py on this branch's tree: 19 tests, OK (16, plus 3 new: a stacked PR squash-merged inside the one above it; the same once main edits its lines; a conflicting PR that adds more, which is not said to be on main). With the script change taken out, the three new tests and the changed 999=sq2 check fail.
  • Together with merge_check: search the whole first-parent history for earlier merges, not the newest 5000 #8564 (its two commits and this one): 20 tests, OK.
  • The table above, both versions of the script on the real PRs.
  • python -m compileall and python -I scripts/check_no_game_data.py on both files: OK.
  • Windows 11 Pro 26200, Python 3.12.7, git 2.52.0. No GPU involved.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule here.)

🤖 Generated with Claude Code

A stacked PR squash-merged inside the PR above it, or a change that
landed under another commit, has no merge under its own number, so the
script called it `clean` (or `conflict` once main had edited its lines),
never "already on main".

- `already-on-main`: no merge of the PR is found, but the conflict-free
  merged tree matches main, so merging it changes nothing.
- `on_main_outside_conflicts`: with a conflict, whether the merge with
  every conflicting hunk resolved to main's side (merge-tree -X ours)
  leaves main as it is. The verdict stays `conflict`; null where git
  cannot make that merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@YordiLorenzo

Copy link
Copy Markdown

Tested 68a3362 on Linux, Python 3.14.4, git 2.53.0: pass

  • python -I tests/test_merge_check.py: OK (the head is on main 867d922)
  • Negative control, scripts/merge_check.py from main: 2 failures and 2 errors, the three new stack tests and the 999=sq2 case
  • Read: a git without merge-tree -X gives a usage error, so on_main_outside_conflicts falls back to null as the docstring says

@kristofbarany

Copy link
Copy Markdown

Tested 68a3362 merged onto main 6312c05 (clean merge) on Windows 11 Pro 26300, CPython 3.12.10: pass.

  • python -I tests/test_merge_check.py: Ran 19 tests, OK; main has 16, also OK.

No game data used. AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified 68a3362 against main 6312c05 (clean merge, tree 7f5326485; recheck clean at 5d74c27). tests/test_merge_check.py on the merged tree: Ran 19, OK (re-run; incl. the re-targeted #999/sq2 assertion — its content is on main, so already-on-main with exit 1 is the honest verdict). Same file on clean main: FAILED (failures=2, errors=2). Mutation probes: forcing on_main_outside_conflicts=True turns the conflicting-PR test red (1F); neutering the already-on-main branch turns two red (2F); restores verified clean. No supersession: main has no equivalent verdict path. Flag: no CI checks reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Running-Agama

Copy link
Copy Markdown
Contributor

Checked 68a3362fd (the diff applied to main at 5d74c2723) on Windows 11 25H2 (10.0.26100.9974), Python 3.15.0, git 2.56.0.windows.2: pass.

  • python -I tests/test_merge_check.py: 19 tests OK (63.7 s), including the three new ones and the changed 999=sq2 expectation.
  • With only the scripts/merge_check.py hunk backed out, the same file fails: test_a_stacked_pr_squash_merged_inside_the_one_above_it_is_already_on_main (0 != 1), test_a_multi_commit_squash_merged_pr_is_already_merged_despite_cherry_miss ('clean' != 'already-on-main'), and the two on_main_outside_conflicts cases error on the missing key. So the new tests do exercise the change.
  • python -m compileall -q scripts/merge_check.py tests/test_merge_check.py and python -I scripts/check_no_game_data.py on both files: exit 0.
  • Independent edge case the tests do not cover: a modify/delete conflict (the PR edits a file that main deleted). git merge-tree --write-tree -X ours cannot settle that one, and the script reports "verdict": "conflict", "on_main_outside_conflicts": null, exit 1, no traceback: the documented fallback path, which is also what a git before -X support gets.
  • Code read: the verdict order (already-merged, already-on-main, duplicates, conflict, clean) matches the docstring; the extra merge-tree only runs for a conflicting PR; the new text clause and JSON field are additive, and nothing else in the tree reads merge_check's output (grep over tools/, scripts/, tests/ and the workflows).
  • One note for whoever merges, not a defect: 999=sq2 now exits 1 instead of 0. That is the point of the change and it is in the PR body, but a reader used to exit 0 meaning "safe to re-merge" sees the new code for exactly this kind of PR.

Host caveat: this machine has Python 3.15 while CI pins 3.12; nothing in the hunk is version-sensitive.

AI-assisted: yes (Windows host, CPU-only, no GPU and no game files).

@AsgerCode

Copy link
Copy Markdown

Tested 68a3362 on Windows 11 (NTFS), Python 3.12.10, git 2.53.0.windows.2 (CPU only, no GPU, no game files): pass

The earlier comments cover the unit tests, the negative control and the modify/delete fallback. I added an end-to-end run on a hand-built repository, a check that the script stays offline and read-only, and the overlap with #8807.

What I ran:

  • python -I tests/test_merge_check.py: 19 tests, OK. With main's scripts/merge_check.py: 2 failures and 2 errors (the same four as reported above).
  • Merge with current main (867d922): clean. The head is on main.
  • A synthetic repository in <tmp> (git init, local commits only, an origin remote at an unreachable https://example.invalid/... URL, refs/remotes/origin/main set locally). PRs reach main in each of the ways below. I ran both versions of the script on all 14 PRs in one batch, --json and text:
PR how it reached main main's script this PR
11 stacked under 10; 10 squash-merged as ... (#10) clean already-on-main
10 squash, own number already-merged already-merged
14 2 commits squash-merged, own number already-merged already-merged
20 git rebase onto main + fast-forward (new SHAs, no number in subjects), 2/2 by patch clean already-on-main
30 git cherry-pick -x, 1/1 by patch clean already-on-main
40 real merge commit Merge pull request #40 ... already-merged already-merged
42 real merge commit, on top of 41 already-merged already-merged
41 stacked under 42; 42 merged with a merge commit (41's head is an ancestor of main, 0/0) clean already-on-main
12 squash (#12), own number, then main reworded a line in it conflict conflict, outside = true
13 stacked under 12 (squash (#12)), then main reworded 13's line conflict conflict, outside = true
60 cherry-picked, then main edited the same line conflict conflict, outside = true
50 not on main, own work clean clean
51 not on main, conflicts and adds a file conflict conflict, outside = false
70 not on main: one commit and its git revert, so the net diff is empty clean already-on-main

The exit code is 1 for both versions on this batch. The extra merge-tree -X ours runs exactly 4 times, once for each conflicting PR (12, 13, 51, 60).

  • Offline and read-only: I ran the script through a wrapper that blocks import socket and logs every subprocess.Popen. A gh call would have been mocked, but none happened. The only program started was git. GIT_TRACE shows only cherry, diff, log, merge-tree, rev-parse and show. There was no fetch and no remote helper, although origin exists. The run did not change the refs, HEAD, the index, the config, packed-refs, git status, the reflogs or the top-level .git entries. It added only loose objects (96 to 106, from merge-tree --write-tree, as the docstring says).

Nit, not blocking:

  1. PR 70 has an empty net diff (a commit and its revert). It now reads already-on-main with "merged tree matches main, though no merge of it is found", but nothing of it ever landed. The verdict is literally true ("merging it changes nothing"), but the wording suggests that its content is on main. If you want to separate the two cases, a git diff --quiet <merge-base> <ref> check could label it as empty.

Overlap: #7468, #7660 and #8291 conflict with this PR, as the description says. #8807 (yours, "a merge that main has reverted no longer counts as a prior merge") conflicts too, in scripts/merge_check.py and tests/test_merge_check.py. It is not in this PR's list, but #8807 lists this PR. Watch the order of the verdicts in that rebase. With #8807, a PR whose merge commit main reverted has merges == [], and its merged tree matches main. I checked this on a synthetic repository: #8807 gives reverted, with merges=[]. If elif unchanged: already-on-main comes before elif reverted and unchanged, that PR reads already-on-main, although its code was reverted off main. reverted must rank first, or already-on-main must need not reverted. These open merge_check PRs merge cleanly with this PR: #7291, #7443, #7918, #8016, #8412, #8564, #8634 and #8980.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants