Files
mandy/scripts/merge_check.py
AlxcBTW d083a2cc37 Make main's tests green: 12 test files that drifted from the code (#9896)
Twelve test files failed on main at 2d53a8055 (CI's check and semantic-tests steps, run locally): most are a test
and the code it checks changed by different pull requests in the same merge round. Each fix below is either taken
from a closed pull request that already had it, or restores what an earlier merged change intended.

From closed pull requests:
- test_make_game_resume: FakeProc gets poll(), which translate() now calls (#7018)
- test_make_game_empty_translation: the Exited stub gets poll() and the failed-shard path is checked (#7214)
- test_shader_checks: stub table_fetch_test in the Wave64Checks harness and count it (#7433)
- test_runtime_paths: runtime.yml's path filter lists tools/replace_retry.py, which rdna2spirv.py imports (#7029)
- test_psin_names: patch the same_report_path helpers that report_paths now calls (#8719)
- test_report_source_collision: the fixture is a real Bink 2 header; bink_index refuses other files (#7408)
- test_missing_input_exit_codes: tile_probe checks its inputs before it asks for numpy (#4771)

Restored:
- merge_check: a failed `git show` is an error again (5b88d58); 21016cf brought back the lenient read that
  treated it as a new file, while new files are already left out when the diff is read
- rpf_probe: a symlinked --out updates its target again (44d3595); 55d21cc's staged write replaced the link

Updated to the code:
- test_shader_stage_resume: TRANSLATION_SOURCES lists fssm_unpack.py and shader_stage_manifest.py, which the
  translator imports; existing work folders translate once more after this
- test_tool_stdout_encoding: the 15 programs merged without the stdout guard get the same block as the others
- test_rdna2spirv_decode: drop the test of decode_any_format, which SHADER-849 replaced with the table decode
  that tests/semantic/test_format_decode_table.py checks

Co-authored-by: jewmaster1337 <102986566+jewmaster1337@users.noreply.github.com>
Co-authored-by: TanmaySatyam <248538550+TanmaySatyam@users.noreply.github.com>
Co-authored-by: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com>
Co-authored-by: slint6614-dev <359954236+slint6614-dev@users.noreply.github.com>
Co-authored-by: BlackNife <108077267+Poussistar@users.noreply.github.com>
Co-authored-by: fiestorra <65175643+fiestorra@users.noreply.github.com>
Co-authored-by: fearkov <289226582+fearkov@users.noreply.github.com>
Co-authored-by: DoubleD17 <48109637+DoubleD17@users.noreply.github.com>
2026-11-24 00:21:26 +01:00

222 lines
11 KiB
Python

"""Before a merge round: is a pull request already on main, and would merging it again duplicate text?
python -I scripts/merge_check.py [--main REF] [--json] [--min-length N] PR=REF [PR=REF ...]
PR is the pull request number and REF its head (a branch, a fetched `refs/pull/PR/head`, a commit). For each one,
against `--main` (default `origin/main`), using one history scan for the whole batch:
- `merges`: first-parent commits on main whose subject is "Merge pull request #PR" or ends with " (#PR)" (squash
merges; "Revert ..." subjects are ignored), newest first. A number that is already there is the case the repeated
merges come from: a rebased copy merged again keeps both copies of what it added.
- `commits`: the PR's commits (merge base..REF) and how many `git cherry` finds on main already (same patch).
- `duplicates`: runs of three or more consecutive lines the merge would add (each at least `--min-length`
characters once stripped) that already stand in the same order in the same file on main. This is what a second
merge leaves behind as a doubled paragraph, table or function, whether or not the commits match by patch; a single
repeated line is not reported, since code and tables repeat lines legitimately. Only text files (.md, .txt,
.rst) make the verdict `duplicates`; a block in code is listed, since equal lines in two functions are ordinary.
- `conflict`: whether `git merge-tree` reports a conflict (the duplicate check is then on the conflicted tree).
Verdict per PR: `already-merged` (a prior merge exists and either every commit is on main or the conflict-free merged
tree matches main, as after a multi-commit squash merge), `duplicates`, `conflict` or `clean`.
Exit 0 when every PR is clean, 1 when any is not, 2 on a usage or git error. Read-only: `merge-tree --write-tree`
writes only objects, no refs. Standard library only; needs git 2.38 or newer for `merge-tree --write-tree`.
"""
import argparse
import codecs
import json
import re
import subprocess
import sys
from collections import Counter
MERGE_SUBJECT = re.compile(r"^Merge pull request #(\d+)\b")
SQUASH_SUBJECT = re.compile(r"(?:^|\s)\(#(\d+)\)\s*$")
REVERT_SUBJECT = re.compile(r"^Revert\b", re.IGNORECASE)
# Where a doubled block is the repeated-merge damage. In code, three equal lines in two functions are ordinary, so a
# block there is listed but does not change the verdict.
TEXT_SUFFIXES = (".md", ".txt", ".rst")
class GitError(Exception):
pass
def git(repo, *args, check=True):
r = subprocess.run(["git", "-C", repo, *args], capture_output=True)
# Decode without universal-newline translation: a bare CR inside a Git line is content.
r.stdout = r.stdout.decode("utf-8", "replace")
r.stderr = r.stderr.decode("utf-8", "replace")
if check and r.returncode != 0:
raise GitError(f"git {' '.join(args)}: {r.stderr.strip() or r.returncode}")
return r
def _merged_pr_number(subject):
m = MERGE_SUBJECT.match(subject)
if m:
return int(m.group(1))
if REVERT_SUBJECT.match(subject):
return None
m = SQUASH_SUBJECT.search(subject)
return int(m.group(1)) if m else None
def prior_merges(repo, main, numbers, depth):
out = git(repo, "log", main, "--first-parent", f"-n{depth}", "--format=%H%x09%s").stdout
hits = {number: [] for number in numbers}
for line in out.splitlines():
sha, _, subject = line.partition("\t")
number = _merged_pr_number(subject)
if number in hits:
hits[number].append(sha[:12])
return hits
def cherry(repo, main, ref):
"""(commits on the PR side, those whose patch is already on main)."""
lines = [l for l in git(repo, "cherry", main, ref).stdout.splitlines() if l.strip()]
return len(lines), sum(1 for l in lines if l.startswith("-"))
def rev_tree(repo, rev):
return git(repo, "rev-parse", f"{rev}^{{tree}}").stdout.strip()
def merged_tree(repo, main, ref):
r = git(repo, "merge-tree", "--write-tree", main, ref, check=False)
if r.returncode not in (0, 1) or not r.stdout.strip():
raise GitError(f"git merge-tree {main} {ref}: {r.stderr.strip() or r.returncode}")
return r.stdout.split()[0], r.returncode == 1
def patch_destination(header):
"""Decode Git's +++ header. An unquoted path containing spaces can have a trailing tab."""
name = header[4:].removesuffix("\t")
if name.startswith('"') and name.endswith('"'):
# Git's octal escapes describe filesystem bytes, not Unicode code points.
name = codecs.escape_decode(name[1:-1].encode("utf-8"))[0].decode("utf-8", "surrogateescape")
return name[2:] if name.startswith("b/") else None # Deleted files say /dev/null.
def duplicates(repo, main, tree, min_length, block=3):
"""{path: [line, ...]} for runs of `block` or more consecutive added lines (each at least `min_length` characters
once stripped; blank lines skipped) that already stand in the same order in the same file on main: a paragraph,
table or function body merged twice. A single repeated line is not reported: code and tables repeat lines
legitimately."""
found = {}
additions, path, added = {}, None, None
new_file = False
# Gitlink updates describe submodule commits and have no file text to compare.
patch = git(repo, "-c", "core.quotePath=true", "diff", "--unified=0", "--no-color", "--no-renames",
"--inter-hunk-context=0", "--src-prefix=a/", "--dst-prefix=b/", "--no-relative",
"--no-ext-diff", "--no-textconv", "--ignore-submodules=all",
main, tree).stdout
for line in patch.split("\n"):
if line.startswith("diff --git "):
path, added = None, None
new_file = False
elif added is None and line == "--- /dev/null":
new_file = True
elif added is None and line.startswith("+++ "):
path = None if new_file else patch_destination(line)
elif line.startswith("@@ ") and path:
# The new-file range is +start[,count]. With zero context, only added lines advance it.
line_number = int(line.split()[2][1:].split(",")[0])
added = additions.setdefault(path, set())
elif added is not None and line.startswith("+"):
added.add(line_number)
line_number += 1
def windows(rev, path):
# New files are already left out above (--- /dev/null), so a failed read is a real Git error, not a new file.
r = git(repo, "show", f"{rev}:{path}")
lines = [(number, text) for number, raw in enumerate(r.stdout.split("\n"), 1) if (text := raw.strip())]
content = [text for _, text in lines]
return lines, Counter(tuple(content[i:i + block]) for i in range(len(content) - block + 1))
for path, added in additions.items():
if not added:
continue
_, before = windows(main, path)
after_lines, after = windows(tree, path)
marked = set()
for i in range(len(after_lines) - block + 1):
window = after_lines[i:i + block]
# Blank lines are skipped, but each nonblank line in the window must be new.
if not all(number in added and len(text) >= min_length for number, text in window):
continue
run = tuple(text for _, text in window)
# Moving a block instead of duplicating it keeps the count unchanged.
if before[run] and after[run] > before[run]:
marked.update(number for number, _ in window)
if marked:
found[path] = [text for number, text in after_lines if number in marked]
return found
def check(repo, main, number, ref, min_length, merges):
total, on_main = cherry(repo, main, ref)
tree, conflict = merged_tree(repo, main, ref)
unchanged = not conflict and tree == rev_tree(repo, main)
dups = {} if unchanged else duplicates(repo, main, tree, min_length)
text_dups = {p: v for p, v in dups.items() if p.lower().endswith(TEXT_SUFFIXES)}
if merges and (total == on_main or unchanged):
verdict = "already-merged"
elif text_dups:
verdict = "duplicates"
elif conflict:
verdict = "conflict"
else:
verdict = "clean"
return {"pr": number, "ref": ref, "verdict": verdict, "merges": merges,
"commits": {"total": total, "on_main": on_main}, "conflict": conflict,
"duplicates": {p: len(v) for p, v in dups.items()},
"duplicate_samples": {p: v[:3] for p, v in dups.items()}}
def parse_pair(text):
number, sep, ref = text.partition("=")
if not sep or not number.lstrip("#").isdigit() or not ref:
raise argparse.ArgumentTypeError(f"expected PR=REF, got {text!r}")
return int(number.lstrip("#")), ref
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0])
ap.add_argument("pairs", nargs="+", type=parse_pair, metavar="PR=REF")
ap.add_argument("--main", default="origin/main", help="the branch the round merges into (default origin/main)")
ap.add_argument("--repo", default=".", help="repository folder (default: the current one)")
ap.add_argument("--min-length", type=int, default=40, help="shortest stripped line counted as a duplicate (40)")
ap.add_argument("--depth", type=int, default=5000, help="first-parent commits searched for earlier merges (5000)")
ap.add_argument("--json", action="store_true", help="one JSON report instead of text")
args = ap.parse_args(argv)
try:
history = prior_merges(args.repo, args.main, (n for n, _ in args.pairs), args.depth)
results = [check(args.repo, args.main, n, ref, args.min_length, history[n]) for n, ref in args.pairs]
except GitError as e:
print(f"merge_check: {e}", file=sys.stderr)
return 2
if args.json:
print(json.dumps(results, indent=2))
else:
for r in results:
c = r["commits"]
print(f"#{r['pr']} {r['verdict']}: {c['on_main']}/{c['total']} commit(s) already on main"
+ (", merged tree matches main"
if r["verdict"] == "already-merged" and c["on_main"] != c["total"] else "")
+ (f", merged before at {', '.join(r['merges'])}" if r["merges"] else "")
+ (", conflicts" if r["conflict"] else ""))
for path, n in sorted(r["duplicates"].items()):
note = "" if path.lower().endswith(TEXT_SUFFIXES) else " (code: listed, not counted)"
print(f" {path}: {n} added line(s) already there in the same order{note}, "
f"e.g. {r['duplicate_samples'][path][0][:90]!r}")
return 0 if all(r["verdict"] == "clean" for r in results) else 1
if __name__ == "__main__":
try:
sys.stdout.reconfigure(errors="replace")
except (AttributeError, ValueError, OSError):
pass
sys.exit(main())