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): table or function body merged twice. A single repeated line is not reported: code and tables repeat lines legitimately.""" found = {} - hunks, path, added = {}, None, None + additions, path, added = {}, None, None patch = git(repo, "-c", "core.quotePath=true", "diff", "--unified=0", "--no-color", "--no-renames", - "--src-prefix=a/", "--dst-prefix=b/", "--no-relative", "--no-ext-diff", "--no-textconv", + "--inter-hunk-context=0", "--src-prefix=a/", "--dst-prefix=b/", "--no-relative", + "--no-ext-diff", "--no-textconv", main, tree).stdout for line in patch.split("\n"): if line.startswith("diff --git "): @@ -114,35 +115,42 @@ def duplicates(repo, main, tree, min_length, block=3): elif added is None and line.startswith("+++ "): path = patch_destination(line) elif line.startswith("@@ ") and path: - added = [] - hunks.setdefault(path, []).append(added) + # 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("+"): - text = line[1:].strip() - if text: - added.append(text) + added.add(line_number) + line_number += 1 + def windows(rev, path): r = git(repo, "show", f"{rev}:{path}", check=False) - lines = [l.strip() for l in r.stdout.split("\n") if l.strip()] if r.returncode == 0 else [] - return r.returncode == 0, Counter(tuple(lines[i:i + block]) for i in range(len(lines) - block + 1)) + if r.returncode: + return None, Counter() + 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, groups in hunks.items(): - exists, before = windows(main, path) - if not exists: + for path, added in additions.items(): + if not added: + continue + before_lines, before = windows(main, path) + if before_lines is None: continue # a new file: nothing to duplicate - _, after = windows(tree, path) - same = [] - for added in groups: - marked = [False] * len(added) - for i in range(len(added) - block + 1): - run = tuple(added[i:i + block]) - # Doubled: on main already, and more copies after the merge. A block the PR moves (deleted where - # it was, added elsewhere) keeps its count and is not reported. - if all(len(x) >= min_length for x in run) and before[run] and after[run] > before[run]: - for j in range(i, i + block): - marked[j] = True - same += [x for x, m in zip(added, marked) if m] - if same: - found[path] = same + after_lines, after = windows(tree, path) + if after_lines is None: + continue + 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 diff --git a/tests/test_merge_check.py b/tests/test_merge_check.py index 199b5fd7a..e6acf4592 100644 --- a/tests/test_merge_check.py +++ b/tests/test_merge_check.py @@ -169,6 +169,29 @@ class MergeCheckTest(unittest.TestCase): self.git("config", "core.quotePath", "false") self.assertEqual(self.run_check("18=duplicated"), (code, [result])) + def test_duplicate_blocks_span_retained_blank_lines_only(self): + first, second, third = PARAGRAPH.splitlines(keepends=True) + paragraph = first + "\n" + second + third + intro = paragraph + "Start of the changed section.\n" + ending = "End of the changed section.\n" + duplicate = "docs/notes.md" + partial_edit = "docs/partial.md" + self.write(duplicate, intro + "\n" + ending) + self.write(partial_edit, intro + second + ending) + self.commit("Add two documents and placeholders") + self.git("checkout", "-q", "-b", "fill-placeholders") + for path in (duplicate, partial_edit): + self.write(path, intro + paragraph + ending) + self.commit("Fill placeholders") + self.git("checkout", "-q", "main") + + # Git keeps the blank line in notes.md and the second content line in partial.md. + # Both split the additions into separate hunks, but only notes.md adds all three content lines. + code, [result] = self.run_check("20=fill-placeholders") + self.assertEqual((code, result["verdict"]), (1, "duplicates")) + self.assertEqual(result["duplicates"], {duplicate: 3}) + self.assertEqual(result["duplicate_samples"], {duplicate: PARAGRAPH.splitlines()}) + def test_duplicate_blocks_use_git_line_boundaries(self): unicode_lines = "".join(f"Paragraph {i} begins with enough text to count as a line.\u2028" "Its continuation is also longer than forty characters.\n" for i in range(3))