|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Unified diff parser shared by all code-review checker scripts (stdlib only).""" |
| 7 | +import re |
| 8 | + |
| 9 | +_HUNK_RE = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@") |
| 10 | + |
| 11 | + |
| 12 | +def _strip_prefix(path): |
| 13 | + """Strip git's a/ b/ prefixes.""" |
| 14 | + if path.startswith(("a/", "b/")): |
| 15 | + return path[2:] |
| 16 | + return path |
| 17 | + |
| 18 | + |
| 19 | +def parse_unified_diff(text): |
| 20 | + """Parse a unified diff into a list of per-file dicts with new-file line numbers.""" |
| 21 | + files = [] |
| 22 | + current = None |
| 23 | + old_no = new_no = 0 |
| 24 | + for line in text.splitlines(): |
| 25 | + if line.startswith("diff --git") or line.startswith("Index: "): |
| 26 | + current = None |
| 27 | + continue |
| 28 | + if line.startswith("--- "): |
| 29 | + raw_old = line[4:].split("\t")[0].strip() |
| 30 | + current = { |
| 31 | + "path": "", |
| 32 | + "old_path": "" if raw_old == "/dev/null" else _strip_prefix(raw_old), |
| 33 | + "is_new": raw_old == "/dev/null", |
| 34 | + "is_deleted": False, |
| 35 | + "hunks": [], |
| 36 | + "added_lines": [], |
| 37 | + "removed_lines": [], |
| 38 | + } |
| 39 | + files.append(current) |
| 40 | + continue |
| 41 | + if line.startswith("+++ "): |
| 42 | + if current is None: |
| 43 | + continue |
| 44 | + raw_new = line[4:].split("\t")[0].strip() |
| 45 | + if raw_new == "/dev/null": |
| 46 | + current["is_deleted"] = True |
| 47 | + current["path"] = current["old_path"] |
| 48 | + else: |
| 49 | + current["path"] = _strip_prefix(raw_new) |
| 50 | + continue |
| 51 | + m = _HUNK_RE.match(line) |
| 52 | + if m and current is not None: |
| 53 | + old_no, new_no = int(m.group(1)), int(m.group(3)) |
| 54 | + current["hunks"].append({"old_start": old_no, "new_start": new_no, "lines": []}) |
| 55 | + continue |
| 56 | + if current is None or not current["hunks"]: |
| 57 | + continue |
| 58 | + hunk = current["hunks"][-1] |
| 59 | + if line.startswith("+"): |
| 60 | + hunk["lines"].append({"tag": "+", "new_lineno": new_no, "old_lineno": None, "text": line[1:]}) |
| 61 | + current["added_lines"].append({"line": new_no, "text": line[1:]}) |
| 62 | + new_no += 1 |
| 63 | + elif line.startswith("-"): |
| 64 | + hunk["lines"].append({"tag": "-", "new_lineno": None, "old_lineno": old_no, "text": line[1:]}) |
| 65 | + current["removed_lines"].append({"line": old_no, "text": line[1:]}) |
| 66 | + old_no += 1 |
| 67 | + elif line.startswith(" ") or line == "": |
| 68 | + hunk["lines"].append({"tag": " ", "new_lineno": new_no, "old_lineno": old_no, "text": line[1:]}) |
| 69 | + old_no += 1 |
| 70 | + new_no += 1 |
| 71 | + # "\ No newline at end of file" markers are intentionally ignored. |
| 72 | + return files |
| 73 | + |
| 74 | + |
| 75 | +def summarize(files): |
| 76 | + """Return a compact summary dict for a parsed diff.""" |
| 77 | + return { |
| 78 | + "files_changed": len(files), |
| 79 | + "additions": sum(len(f["added_lines"]) for f in files), |
| 80 | + "deletions": sum(len(f["removed_lines"]) for f in files), |
| 81 | + "files": [f["path"] or f["old_path"] for f in files], |
| 82 | + } |
0 commit comments