upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/merge_check.py
blob: c4cbdcecacbbb1a4dba9ef6a1d1eaf3695442035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""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())