codiff 0.1.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.
- codiff/__init__.py +1 -0
- codiff/__main__.py +5 -0
- codiff/cli.py +247 -0
- codiff/db/__init__.py +16 -0
- codiff/db/engine.py +31 -0
- codiff/db/models.py +119 -0
- codiff/db/operations.py +238 -0
- codiff/diff/__init__.py +1 -0
- codiff/diff/analysis.py +145 -0
- codiff/diff/differ.py +45 -0
- codiff/diff/engine.py +80 -0
- codiff/diff/indexer.py +134 -0
- codiff/diff/snapshot.py +325 -0
- codiff/export/__init__.py +5 -0
- codiff/export/json.py +43 -0
- codiff/export/mermaid.py +475 -0
- codiff/export/terminal.py +866 -0
- codiff/languages/__init__.py +29 -0
- codiff/languages/parser.py +214 -0
- codiff/languages/python/__init__.py +0 -0
- codiff/languages/python/parser.py +927 -0
- codiff/languages/python/resolver.py +178 -0
- codiff/languages/repository.py +245 -0
- codiff/languages/resolver.py +582 -0
- codiff/languages/typescript/__init__.py +0 -0
- codiff/languages/typescript/parser.py +612 -0
- codiff/languages/typescript/resolver.py +95 -0
- codiff/mcp_server.py +83 -0
- codiff/schema/__init__.py +25 -0
- codiff/schema/diff.py +110 -0
- codiff/schema/parsing.py +76 -0
- codiff/setup.py +103 -0
- codiff/utils/__init__.py +0 -0
- codiff/utils/args.py +114 -0
- codiff/utils/files.py +62 -0
- codiff/utils/git.py +101 -0
- codiff/utils/instructions.py +14 -0
- codiff/utils/instructions.yaml +50 -0
- codiff/utils/terminal.py +10 -0
- codiff-0.1.0.dist-info/METADATA +487 -0
- codiff-0.1.0.dist-info/RECORD +45 -0
- codiff-0.1.0.dist-info/WHEEL +5 -0
- codiff-0.1.0.dist-info/entry_points.txt +3 -0
- codiff-0.1.0.dist-info/licenses/LICENSE +201 -0
- codiff-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from codiff.languages.parser import LanguageParser
|
|
2
|
+
from codiff.languages.python.parser import PythonParser
|
|
3
|
+
from codiff.languages.python.resolver import (
|
|
4
|
+
CallResolver,
|
|
5
|
+
PythonCallResolver,
|
|
6
|
+
resolve_internal_calls,
|
|
7
|
+
)
|
|
8
|
+
from codiff.languages.repository import ParsedRepository, parse_repository
|
|
9
|
+
from codiff.languages.resolver import BaseCallResolver
|
|
10
|
+
from codiff.languages.typescript.parser import TypeScriptParser, TypeScriptXParser
|
|
11
|
+
from codiff.languages.typescript.resolver import TypeScriptCallResolver
|
|
12
|
+
|
|
13
|
+
# Backward-compatible alias
|
|
14
|
+
CodeParser = PythonParser
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"LanguageParser",
|
|
18
|
+
"PythonParser",
|
|
19
|
+
"TypeScriptParser",
|
|
20
|
+
"TypeScriptXParser",
|
|
21
|
+
"CodeParser",
|
|
22
|
+
"parse_repository",
|
|
23
|
+
"ParsedRepository",
|
|
24
|
+
"CallResolver",
|
|
25
|
+
"PythonCallResolver",
|
|
26
|
+
"TypeScriptCallResolver",
|
|
27
|
+
"resolve_internal_calls",
|
|
28
|
+
"BaseCallResolver",
|
|
29
|
+
]
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""Abstract base class for language-specific parsers.
|
|
2
|
+
|
|
3
|
+
Defines the interface and shared infrastructure every language implementation
|
|
4
|
+
must satisfy. Concrete subclasses supply:
|
|
5
|
+
- language / extension properties
|
|
6
|
+
- _init_queries()
|
|
7
|
+
- _extract_function / _extract_class / _extract_imports / _extract_module_docstring
|
|
8
|
+
|
|
9
|
+
The parse_code() template method orchestrates the four steps in the right order;
|
|
10
|
+
subclasses never override it.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
from abc import ABC, abstractmethod
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from tree_sitter import Language, Parser, Query, QueryCursor
|
|
18
|
+
|
|
19
|
+
from codiff.schema.parsing import ClassChunk, FunctionChunk
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class LanguageParser(ABC):
|
|
23
|
+
# Subclasses must assign these in _init_queries()
|
|
24
|
+
function_query: Query
|
|
25
|
+
class_query: Query
|
|
26
|
+
import_query: Query
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None:
|
|
29
|
+
self.parser = Parser(self.language)
|
|
30
|
+
self.exclude_dirs: set[str] = {
|
|
31
|
+
".git",
|
|
32
|
+
".vscode",
|
|
33
|
+
".idea",
|
|
34
|
+
"build",
|
|
35
|
+
"dist",
|
|
36
|
+
"_build",
|
|
37
|
+
"htmlcov",
|
|
38
|
+
".coverage",
|
|
39
|
+
"node_modules",
|
|
40
|
+
}
|
|
41
|
+
self.exclude_dirs.update(self._extra_exclude_dirs())
|
|
42
|
+
self._init_queries()
|
|
43
|
+
|
|
44
|
+
# ------------------------------------------------------------------
|
|
45
|
+
# Abstract interface
|
|
46
|
+
# ------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def language(self) -> Language:
|
|
51
|
+
"""Tree-sitter Language object for this language."""
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
@abstractmethod
|
|
55
|
+
def extension(self) -> str:
|
|
56
|
+
"""File extension, e.g. '.py'."""
|
|
57
|
+
|
|
58
|
+
def _extra_exclude_dirs(self) -> set[str]:
|
|
59
|
+
"""Language-specific directories to exclude. Override as needed."""
|
|
60
|
+
return set()
|
|
61
|
+
|
|
62
|
+
@abstractmethod
|
|
63
|
+
def file_to_module_id(self, rel_path: str) -> str:
|
|
64
|
+
"""Convert a relative file path to its canonical module identifier.
|
|
65
|
+
|
|
66
|
+
Examples
|
|
67
|
+
--------
|
|
68
|
+
Python : 'pkg/sub/mod.py' → 'pkg.sub.mod'
|
|
69
|
+
TS/JS : 'src/utils/fn.ts' → 'src/utils/fn'
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
# ------------------------------------------------------------------
|
|
73
|
+
# Module resolution helpers (language-agnostic orchestration,
|
|
74
|
+
# language-specific details delegated to subclass methods)
|
|
75
|
+
# ------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
def build_modules_dict(self, repo_path: Path, gitignore=None) -> dict[str, str]:
|
|
78
|
+
"""Map every importable sub-path to its canonical module identifier.
|
|
79
|
+
|
|
80
|
+
Calls *self.file_to_module_id()* for each file — fully generic,
|
|
81
|
+
no language-specific logic here.
|
|
82
|
+
"""
|
|
83
|
+
from codiff.utils.files import is_venv_dir
|
|
84
|
+
from codiff.utils.git import is_dir_ignored
|
|
85
|
+
|
|
86
|
+
modules_dict: dict[str, str] = {}
|
|
87
|
+
init_modules: dict[str, str] = {}
|
|
88
|
+
repo_str = str(repo_path)
|
|
89
|
+
|
|
90
|
+
for root, dirs, files in os.walk(repo_path):
|
|
91
|
+
dirs[:] = [
|
|
92
|
+
d
|
|
93
|
+
for d in dirs
|
|
94
|
+
if d not in self.exclude_dirs
|
|
95
|
+
and not is_venv_dir(root, d)
|
|
96
|
+
and not is_dir_ignored(gitignore, repo_str, root, d)
|
|
97
|
+
]
|
|
98
|
+
for file in files:
|
|
99
|
+
if not file.endswith(self.extension):
|
|
100
|
+
continue
|
|
101
|
+
rel = str((Path(root) / file).relative_to(repo_path))
|
|
102
|
+
module_name = self.file_to_module_id(rel)
|
|
103
|
+
is_init = self._is_package_init(file)
|
|
104
|
+
parts = module_name.split(".")
|
|
105
|
+
for i in range(len(parts)):
|
|
106
|
+
for j in range(i + 1, len(parts) + 1):
|
|
107
|
+
sub = ".".join(parts[i:j])
|
|
108
|
+
if is_init:
|
|
109
|
+
init_modules[sub] = module_name
|
|
110
|
+
elif sub not in init_modules:
|
|
111
|
+
modules_dict[sub] = module_name
|
|
112
|
+
|
|
113
|
+
modules_dict.update(init_modules)
|
|
114
|
+
return modules_dict
|
|
115
|
+
|
|
116
|
+
@property
|
|
117
|
+
def resolver_class(self):
|
|
118
|
+
"""Resolver class to use for this language's functions.
|
|
119
|
+
|
|
120
|
+
Override in language-specific parsers to select the appropriate
|
|
121
|
+
BaseCallResolver subclass. Default returns PythonCallResolver so
|
|
122
|
+
that existing parsers keep working without changes.
|
|
123
|
+
"""
|
|
124
|
+
from codiff.languages import PythonCallResolver
|
|
125
|
+
|
|
126
|
+
return PythonCallResolver
|
|
127
|
+
|
|
128
|
+
def build_package_exports(self, repo_path: Path, gitignore=None) -> dict[str, str]:
|
|
129
|
+
"""Return a map of re-exported names to their real locations.
|
|
130
|
+
|
|
131
|
+
Default implementation returns {} (no re-export mechanism).
|
|
132
|
+
Override in language parsers that have package re-export conventions
|
|
133
|
+
(e.g. Python's __init__.py, TypeScript's index.ts barrel files).
|
|
134
|
+
"""
|
|
135
|
+
return {}
|
|
136
|
+
|
|
137
|
+
def _is_package_init(self, filename: str) -> bool:
|
|
138
|
+
"""True when *filename* is a package initialiser (e.g. __init__.py).
|
|
139
|
+
|
|
140
|
+
Override if the language uses a different convention.
|
|
141
|
+
"""
|
|
142
|
+
return False
|
|
143
|
+
|
|
144
|
+
@abstractmethod
|
|
145
|
+
def _init_queries(self) -> None:
|
|
146
|
+
"""Populate self.function_query, self.class_query, self.import_query."""
|
|
147
|
+
|
|
148
|
+
@abstractmethod
|
|
149
|
+
def _extract_function(self, captures: dict, path: str) -> "FunctionChunk | None":
|
|
150
|
+
"""Build a FunctionChunk from query captures, or None to skip."""
|
|
151
|
+
|
|
152
|
+
@abstractmethod
|
|
153
|
+
def _extract_class(self, captures: dict, path: str) -> "ClassChunk | None":
|
|
154
|
+
"""Build a ClassChunk from query captures, or None to skip."""
|
|
155
|
+
|
|
156
|
+
@abstractmethod
|
|
157
|
+
def _extract_imports(self, captures: dict, all_modules: dict, path: "str | None") -> dict:
|
|
158
|
+
"""Return {local_name: resolved_path} from query captures."""
|
|
159
|
+
|
|
160
|
+
@abstractmethod
|
|
161
|
+
def _extract_module_docstring(self, source: str) -> "str | None":
|
|
162
|
+
"""Extract the module-level docstring from source, or None."""
|
|
163
|
+
|
|
164
|
+
# ------------------------------------------------------------------
|
|
165
|
+
# Shared infrastructure
|
|
166
|
+
# ------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
def get_node(self, captures_dict: dict, capture_name: str):
|
|
169
|
+
"""Return the first captured node for *capture_name*, or None."""
|
|
170
|
+
if capture_name not in captures_dict:
|
|
171
|
+
return None
|
|
172
|
+
capture = captures_dict[capture_name]
|
|
173
|
+
if isinstance(capture, list):
|
|
174
|
+
return capture[0] if capture else None
|
|
175
|
+
return capture
|
|
176
|
+
|
|
177
|
+
def parse_functions(self, source_code: str, relative_path: str) -> list:
|
|
178
|
+
return self._collect(
|
|
179
|
+
self.function_query, source_code, self._extract_function, relative_path
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
def parse_classes(self, source_code: str, relative_path: str) -> list:
|
|
183
|
+
return self._collect(self.class_query, source_code, self._extract_class, relative_path)
|
|
184
|
+
|
|
185
|
+
def parse_imports(
|
|
186
|
+
self, source_code: str, all_modules: dict, relative_path: "str | None" = None
|
|
187
|
+
) -> dict:
|
|
188
|
+
tree = self.parser.parse(bytes(source_code, "utf8"))
|
|
189
|
+
cursor = QueryCursor(self.import_query)
|
|
190
|
+
imports: dict = {}
|
|
191
|
+
for _, captures in cursor.matches(tree.root_node):
|
|
192
|
+
result = self._extract_imports(captures, all_modules, relative_path)
|
|
193
|
+
if result:
|
|
194
|
+
imports.update(result)
|
|
195
|
+
return imports
|
|
196
|
+
|
|
197
|
+
def parse_code(self, source_code: str, relative_path: str, all_modules: dict) -> tuple:
|
|
198
|
+
"""Parse source and return (functions, classes, imports, module_docstring)."""
|
|
199
|
+
classes = self.parse_classes(source_code, relative_path)
|
|
200
|
+
functions = self.parse_functions(source_code, relative_path)
|
|
201
|
+
imports = self.parse_imports(source_code, all_modules, relative_path)
|
|
202
|
+
docstring = self._extract_module_docstring(source_code)
|
|
203
|
+
return functions, classes, imports, docstring
|
|
204
|
+
|
|
205
|
+
def _collect(self, query, source_code: str, extractor, *args) -> list:
|
|
206
|
+
"""Run *query* on *source_code*, call *extractor* per match, collect non-None."""
|
|
207
|
+
tree = self.parser.parse(bytes(source_code, "utf8"))
|
|
208
|
+
cursor = QueryCursor(query)
|
|
209
|
+
results = []
|
|
210
|
+
for _, captures in cursor.matches(tree.root_node):
|
|
211
|
+
item = extractor(captures, *args)
|
|
212
|
+
if item is not None:
|
|
213
|
+
results.append(item)
|
|
214
|
+
return results
|
|
File without changes
|