diff options
| author | Igor Bressan <bressan.igor@gmail.com> | 2026-11-23 16:53:02 -0300 |
|---|---|---|
| committer | Igor Bressan <bressan.igor@gmail.com> | 2026-11-23 16:53:02 -0300 |
| commit | 21016cf7aeefad9ed96b9bb8c4e76069b7eb0bb5 (patch) | |
| tree | 97ef69625edf42760d96724868273a1867e94e83 /scripts | |
| parent | 867d922f09b86583859796a174df89555fb32da8 (diff) | |
fix(merge-check): detect duplicates across retained blank lines
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/merge_check.py | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/scripts/merge_check.py b/scripts/merge_check.py index 5deefb36e..e0d1dcf28 100644 --- a/scripts/merge_check.py +++ b/scripts/merge_check.py | |||
| @@ -104,9 +104,10 @@ def duplicates(repo, main, tree, min_length, block=3): | |||
| 104 | table or function body merged twice. A single repeated line is not reported: code and tables repeat lines | 104 | table or function body merged twice. A single repeated line is not reported: code and tables repeat lines |
| 105 | legitimately.""" | 105 | legitimately.""" |
| 106 | found = {} | 106 | found = {} |
| 107 | hunks, path, added = {}, None, None | 107 | additions, path, added = {}, None, None |
| 108 | patch = git(repo, "-c", "core.quotePath=true", "diff", "--unified=0", "--no-color", "--no-renames", | 108 | patch = git(repo, "-c", "core.quotePath=true", "diff", "--unified=0", "--no-color", "--no-renames", |
| 109 | "--src-prefix=a/", "--dst-prefix=b/", "--no-relative", "--no-ext-diff", "--no-textconv", | 109 | "--inter-hunk-context=0", "--src-prefix=a/", "--dst-prefix=b/", "--no-relative", |
| 110 | "--no-ext-diff", "--no-textconv", | ||
| 110 | main, tree).stdout | 111 | main, tree).stdout |
| 111 | for line in patch.split("\n"): | 112 | for line in patch.split("\n"): |
| 112 | if line.startswith("diff --git "): | 113 | if line.startswith("diff --git "): |
| @@ -114,35 +115,42 @@ def duplicates(repo, main, tree, min_length, block=3): | |||
| 114 | elif added is None and line.startswith("+++ "): | 115 | elif added is None and line.startswith("+++ "): |
| 115 | path = patch_destination(line) | 116 | path = patch_destination(line) |
| 116 | elif line.startswith("@@ ") and path: | 117 | elif line.startswith("@@ ") and path: |
| 117 | added = [] | 118 | # The new-file range is +start[,count]. With zero context, only added lines advance it. |
| 118 | hunks.setdefault(path, []).append(added) | 119 | line_number = int(line.split()[2][1:].split(",")[0]) |
| 120 | added = additions.setdefault(path, set()) | ||
| 119 | elif added is not None and line.startswith("+"): | 121 | elif added is not None and line.startswith("+"): |
| 120 | text = line[1:].strip() | 122 | added.add(line_number) |
| 121 | if text: | 123 | line_number += 1 |
| 122 | added.append(text) | 124 | |
| 123 | def windows(rev, path): | 125 | def windows(rev, path): |
| 124 | r = git(repo, "show", f"{rev}:{path}", check=False) | 126 | r = git(repo, "show", f"{rev}:{path}", check=False) |
| 125 | lines = [l.strip() for l in r.stdout.split("\n") if l.strip()] if r.returncode == 0 else [] | 127 | if r.returncode: |
| 126 | return r.returncode == 0, Counter(tuple(lines[i:i + block]) for i in range(len(lines) - block + 1)) | 128 | return None, Counter() |
| 127 | 129 | lines = [(number, text) for number, raw in enumerate(r.stdout.split("\n"), 1) if (text := raw.strip())] | |
| 128 | for path, groups in hunks.items(): | 130 | content = [text for _, text in lines] |
| 129 | exists, before = windows(main, path) | 131 | return lines, Counter(tuple(content[i:i + block]) for i in range(len(content) - block + 1)) |
| 130 | if not exists: | 132 | |
| 133 | for path, added in additions.items(): | ||
| 134 | if not added: | ||
| 135 | continue | ||
| 136 | before_lines, before = windows(main, path) | ||
| 137 | if before_lines is None: | ||
| 131 | continue # a new file: nothing to duplicate | 138 | continue # a new file: nothing to duplicate |
| 132 | _, after = windows(tree, path) | 139 | after_lines, after = windows(tree, path) |
| 133 | same = [] | 140 | if after_lines is None: |
| 134 | for added in groups: | 141 | continue |
| 135 | marked = [False] * len(added) | 142 | marked = set() |
| 136 | for i in range(len(added) - block + 1): | 143 | for i in range(len(after_lines) - block + 1): |
| 137 | run = tuple(added[i:i + block]) | 144 | window = after_lines[i:i + block] |
| 138 | # Doubled: on main already, and more copies after the merge. A block the PR moves (deleted where | 145 | # Blank lines are skipped, but each nonblank line in the window must be new. |
| 139 | # it was, added elsewhere) keeps its count and is not reported. | 146 | if not all(number in added and len(text) >= min_length for number, text in window): |
| 140 | if all(len(x) >= min_length for x in run) and before[run] and after[run] > before[run]: | 147 | continue |
| 141 | for j in range(i, i + block): | 148 | run = tuple(text for _, text in window) |
| 142 | marked[j] = True | 149 | # Moving a block instead of duplicating it keeps the count unchanged. |
| 143 | same += [x for x, m in zip(added, marked) if m] | 150 | if before[run] and after[run] > before[run]: |
| 144 | if same: | 151 | marked.update(number for number, _ in window) |
| 145 | found[path] = same | 152 | if marked: |
| 153 | found[path] = [text for number, text in after_lines if number in marked] | ||
| 146 | return found | 154 | return found |
| 147 | 155 | ||
| 148 | 156 | ||