diffly-cli 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
diffly_cli/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """Deterministic triage for large GitHub pull requests."""
2
+
3
+ __version__ = "0.4.0"
diffly_cli/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ import sys
2
+
3
+ from diffly_cli.cli import main
4
+
5
+ if __name__ == "__main__":
6
+ sys.exit(main())
diffly_cli/astmap.py ADDED
@@ -0,0 +1,121 @@
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from dataclasses import dataclass
5
+
6
+ from .diffparse import added_source, changed_line_numbers, enrich_file, language_for_path
7
+ from .models import ChangedFile
8
+
9
+
10
+ @dataclass
11
+ class Symbol:
12
+ name: str
13
+ start_line: int
14
+ end_line: int
15
+ kind: str
16
+
17
+
18
+ FUNCTION_NODES = {
19
+ "function_definition",
20
+ "function_declaration",
21
+ "method_definition",
22
+ "method_declaration",
23
+ "function_item",
24
+ "function_expression",
25
+ "arrow_function",
26
+ "function",
27
+ "method",
28
+ }
29
+
30
+
31
+ def _node_name(node, source: bytes) -> str:
32
+ for child in node.children:
33
+ if child.type in {"identifier", "property_identifier", "type_identifier", "name"}:
34
+ return source[child.start_byte : child.end_byte].decode("utf-8", errors="replace")
35
+ return node.type
36
+
37
+
38
+ def _walk(node):
39
+ yield node
40
+ for child in node.children:
41
+ yield from _walk(child)
42
+
43
+
44
+ def _tree_sitter_symbols(source_text: str, language: str) -> tuple[list[Symbol], list[tuple[str, int]]]:
45
+ from tree_sitter_language_pack import get_parser
46
+
47
+ source = source_text.encode("utf-8")
48
+ parser = get_parser(language)
49
+ tree = parser.parse(source)
50
+ symbols: list[Symbol] = []
51
+ calls: list[tuple[str, int]] = []
52
+ for node in _walk(tree.root_node):
53
+ if node.type in FUNCTION_NODES:
54
+ symbols.append(Symbol(
55
+ name=_node_name(node, source),
56
+ start_line=node.start_point[0] + 1,
57
+ end_line=node.end_point[0] + 1,
58
+ kind=node.type,
59
+ ))
60
+ if node.type in {"call", "call_expression", "invocation_expression", "call_expression"} and node.children:
61
+ first = node.children[0]
62
+ name = source[first.start_byte : first.end_byte].decode("utf-8", errors="replace")
63
+ calls.append((name, node.start_point[0] + 1))
64
+ return symbols, calls
65
+
66
+
67
+ def _regex_symbols(source_text: str, language: str) -> tuple[list[Symbol], list[tuple[str, int]]]:
68
+ symbols: list[Symbol] = []
69
+ calls: list[tuple[str, int]] = []
70
+ patterns = [
71
+ r"^\s*(?:async\s+)?def\s+([A-Za-z_][\w]*)\s*\(",
72
+ r"^\s*(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_][\w]*)\s*\(",
73
+ r"^\s*(?:public|private|protected|static|async|func|fn|function|def|void|int|string|bool|var|let|const|\w+\s+)+([A-Za-z_][\w]*)\s*\([^;]*\)\s*[{:]?",
74
+ ]
75
+ starts: list[tuple[str, int]] = []
76
+ lines = source_text.splitlines()
77
+ for index, line in enumerate(lines, start=1):
78
+ for pattern in patterns:
79
+ match = re.match(pattern, line)
80
+ if match:
81
+ name = match.group(1)
82
+ if name not in {"if", "for", "while", "switch", "catch"}:
83
+ starts.append((name, index))
84
+ break
85
+ for match in re.finditer(r"\b([A-Za-z_][\w]*)\s*\(", line):
86
+ if match.group(1) not in {"if", "for", "while", "switch", "catch", "def", "function"}:
87
+ calls.append((match.group(1), index))
88
+ for i, (name, start) in enumerate(starts):
89
+ end = starts[i + 1][1] - 1 if i + 1 < len(starts) else len(lines)
90
+ symbols.append(Symbol(name, start, end, "regex-definition"))
91
+ return symbols, calls
92
+
93
+
94
+ def analyze_file(file: ChangedFile) -> ChangedFile:
95
+ enrich_file(file)
96
+ language = language_for_path(file.path)
97
+ source_text = added_source(file.patch)
98
+ changed_lines = set(changed_line_numbers(file))
99
+ if not source_text.strip():
100
+ return file
101
+ try:
102
+ symbols, calls = _tree_sitter_symbols(source_text, language) if language else ([], [])
103
+ except Exception:
104
+ symbols, calls = _regex_symbols(source_text, language or "unknown")
105
+ if not symbols:
106
+ try:
107
+ symbols, calls = _regex_symbols(source_text, language or "unknown")
108
+ except Exception:
109
+ symbols, calls = [], []
110
+ touched = [symbol.name for symbol in symbols if any(symbol.start_line <= line <= symbol.end_line for line in changed_lines)]
111
+ if not touched and changed_lines:
112
+ touched = ["<top-level changes>"]
113
+ touched_set = set(touched)
114
+ callers = [name for name, _ in calls if name in touched_set]
115
+ file.touched_symbols = sorted(dict.fromkeys(touched))
116
+ file.callers = sorted(dict.fromkeys(callers))
117
+ return file
118
+
119
+
120
+ def analyze_files(files: list[ChangedFile]) -> list[ChangedFile]:
121
+ return [analyze_file(file) for file in files]