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
codiff/diff/snapshot.py
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"""GraphSnapshot: a lightweight, in-memory view of a call graph.
|
|
2
|
+
|
|
3
|
+
Loaders:
|
|
4
|
+
- load_from_db(db_path) — reads the indexed base commit from SQLite
|
|
5
|
+
- build_from_path(repo_path) — parses the working tree in memory (no DB writes)
|
|
6
|
+
- build_from_ref(repo_path, ref) — parses a git ref in memory (no DB writes)
|
|
7
|
+
- build_snapshot_incremental(...) — builds a snapshot by re-parsing only changed
|
|
8
|
+
files relative to a DB anchor, reusing the DB
|
|
9
|
+
snapshot for everything else
|
|
10
|
+
|
|
11
|
+
All loaders return a GraphSnapshot with the same structure.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import io
|
|
15
|
+
import logging
|
|
16
|
+
import subprocess
|
|
17
|
+
import tarfile
|
|
18
|
+
import tempfile
|
|
19
|
+
import time
|
|
20
|
+
|
|
21
|
+
from codiff.languages.repository import ParsedRepository
|
|
22
|
+
from codiff.schema.diff import GraphSnapshot, NodeInfo
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# ---------------------------------------------------------------------------
|
|
28
|
+
# Stub types for the incremental resolver index
|
|
29
|
+
# ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class _NodeStub:
|
|
33
|
+
"""Duck-typed FunctionChunk built from a NodeInfo, used only as a resolver
|
|
34
|
+
index entry. calls=[] prevents the resolver from re-resolving base calls."""
|
|
35
|
+
|
|
36
|
+
__slots__ = (
|
|
37
|
+
"id",
|
|
38
|
+
"name",
|
|
39
|
+
"file_path",
|
|
40
|
+
"class_name",
|
|
41
|
+
"calls",
|
|
42
|
+
"return_type",
|
|
43
|
+
"var_types",
|
|
44
|
+
"var_sources",
|
|
45
|
+
"decorators",
|
|
46
|
+
"nested",
|
|
47
|
+
"parameters",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def __init__(self, node: NodeInfo) -> None:
|
|
51
|
+
self.id = node.function_id
|
|
52
|
+
self.name = node.name
|
|
53
|
+
self.file_path = node.file_path
|
|
54
|
+
self.class_name = node.class_name
|
|
55
|
+
self.calls: list = []
|
|
56
|
+
self.return_type = node.return_type
|
|
57
|
+
self.var_types: dict = {}
|
|
58
|
+
self.var_sources: dict = {}
|
|
59
|
+
self.decorators: list = []
|
|
60
|
+
self.nested = None
|
|
61
|
+
self.parameters = node.parameters
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _ClassStub:
|
|
65
|
+
"""Duck-typed ClassChunk built from a class_id + superclass list."""
|
|
66
|
+
|
|
67
|
+
__slots__ = ("id", "name", "superclasses")
|
|
68
|
+
|
|
69
|
+
def __init__(self, class_id: str, superclasses: list[str]) -> None:
|
|
70
|
+
self.id = class_id
|
|
71
|
+
self.name = class_id.split(".")[-1]
|
|
72
|
+
self.superclasses = superclasses
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# ---------------------------------------------------------------------------
|
|
76
|
+
# Helpers
|
|
77
|
+
# ---------------------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _build_edges(snapshot: GraphSnapshot) -> None:
|
|
81
|
+
"""Populate snapshot.edges from node call lists (in-place)."""
|
|
82
|
+
all_ids = set(snapshot.nodes)
|
|
83
|
+
for node in snapshot.nodes.values():
|
|
84
|
+
for callee_id in node.calls:
|
|
85
|
+
if callee_id in all_ids:
|
|
86
|
+
snapshot.edges.add((node.function_id, callee_id))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _git_changed_files(repo_path: str, base_ref: str, head_ref: str | None) -> set[str]:
|
|
90
|
+
"""Return relative paths of files changed between base_ref and HEAD (or head_ref)."""
|
|
91
|
+
cmd = ["git", "diff", "--name-only", base_ref]
|
|
92
|
+
if head_ref is not None:
|
|
93
|
+
cmd.append(head_ref)
|
|
94
|
+
result = subprocess.run(cmd, cwd=repo_path, capture_output=True, text=True, check=True)
|
|
95
|
+
raw = result.stdout.strip()
|
|
96
|
+
return set(raw.split("\n")) if raw else set()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _chunk_to_node_info(chunk) -> NodeInfo:
|
|
100
|
+
return NodeInfo(
|
|
101
|
+
function_id=chunk.id,
|
|
102
|
+
name=chunk.name,
|
|
103
|
+
file_path=chunk.file_path,
|
|
104
|
+
class_name=chunk.class_name,
|
|
105
|
+
parameters=[p.to_dict() for p in chunk.parameters] if chunk.parameters else [],
|
|
106
|
+
return_type=chunk.return_type,
|
|
107
|
+
calls=chunk.calls or [],
|
|
108
|
+
code=chunk.code or "",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _parse_and_expand_stale(
|
|
113
|
+
parse_root: str,
|
|
114
|
+
files_to_parse: set[str],
|
|
115
|
+
old_ids_in_changed: set[str],
|
|
116
|
+
db_snapshot: GraphSnapshot,
|
|
117
|
+
node_stubs: list,
|
|
118
|
+
class_stubs: list,
|
|
119
|
+
max_workers: int,
|
|
120
|
+
) -> tuple[ParsedRepository, set[str]]:
|
|
121
|
+
"""Parse files_to_parse; if function IDs were deleted, expand to stale callers and re-parse.
|
|
122
|
+
|
|
123
|
+
Returns (fresh_parsed, final_files_to_parse).
|
|
124
|
+
"""
|
|
125
|
+
from codiff.languages import parse_repository
|
|
126
|
+
|
|
127
|
+
fresh = parse_repository(
|
|
128
|
+
parse_root,
|
|
129
|
+
files_to_parse=files_to_parse,
|
|
130
|
+
extra_index_functions=node_stubs,
|
|
131
|
+
extra_index_classes=class_stubs,
|
|
132
|
+
max_workers=max_workers,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
deleted_ids = old_ids_in_changed - {fn.id for fn in fresh.functions}
|
|
136
|
+
if not deleted_ids:
|
|
137
|
+
return fresh, files_to_parse
|
|
138
|
+
|
|
139
|
+
id_to_file = {nid: node.file_path for nid, node in db_snapshot.nodes.items()}
|
|
140
|
+
stale_files = {
|
|
141
|
+
id_to_file[caller_id]
|
|
142
|
+
for caller_id, callee_id in db_snapshot.edges
|
|
143
|
+
if callee_id in deleted_ids
|
|
144
|
+
and caller_id in id_to_file
|
|
145
|
+
and id_to_file[caller_id] not in files_to_parse
|
|
146
|
+
}
|
|
147
|
+
if not stale_files:
|
|
148
|
+
return fresh, files_to_parse
|
|
149
|
+
|
|
150
|
+
logger.info(
|
|
151
|
+
"Expanding to %d stale caller file(s) after %d deletion(s)",
|
|
152
|
+
len(stale_files),
|
|
153
|
+
len(deleted_ids),
|
|
154
|
+
)
|
|
155
|
+
expanded = files_to_parse | stale_files
|
|
156
|
+
fresh = parse_repository(
|
|
157
|
+
parse_root,
|
|
158
|
+
files_to_parse=expanded,
|
|
159
|
+
extra_index_functions=node_stubs,
|
|
160
|
+
extra_index_classes=class_stubs,
|
|
161
|
+
max_workers=max_workers,
|
|
162
|
+
)
|
|
163
|
+
return fresh, expanded
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# ---------------------------------------------------------------------------
|
|
167
|
+
# Public loaders
|
|
168
|
+
# ---------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def load_from_db(db_path: str) -> GraphSnapshot:
|
|
172
|
+
"""Load the base snapshot from the indexed SQLite database."""
|
|
173
|
+
from codiff.db.operations import load_snapshot
|
|
174
|
+
|
|
175
|
+
return load_snapshot(db_path)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def build_from_ref(repo_path: str, ref: str, max_workers: int = 4) -> GraphSnapshot:
|
|
179
|
+
"""Parse a git ref in memory and return a GraphSnapshot (no DB written)."""
|
|
180
|
+
proc = subprocess.run(
|
|
181
|
+
["git", "archive", ref, "--format=tar"],
|
|
182
|
+
cwd=repo_path,
|
|
183
|
+
capture_output=True,
|
|
184
|
+
check=True,
|
|
185
|
+
)
|
|
186
|
+
with tempfile.TemporaryDirectory(prefix="codiff_base_") as tmpdir:
|
|
187
|
+
with tarfile.open(fileobj=io.BytesIO(proc.stdout)) as tf:
|
|
188
|
+
tf.extractall(tmpdir, filter="data")
|
|
189
|
+
return build_from_path(tmpdir, max_workers=max_workers)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def build_from_path(repo_path: str, max_workers: int = 4) -> GraphSnapshot:
|
|
193
|
+
"""Parse the working tree at *repo_path* in memory and return a GraphSnapshot."""
|
|
194
|
+
from codiff.languages import parse_repository
|
|
195
|
+
|
|
196
|
+
parsed = parse_repository(repo_path, max_workers=max_workers)
|
|
197
|
+
|
|
198
|
+
snapshot = GraphSnapshot()
|
|
199
|
+
for cls in parsed.classes:
|
|
200
|
+
if cls.superclasses:
|
|
201
|
+
snapshot.class_parents[cls.id] = list(cls.superclasses)
|
|
202
|
+
for chunk in parsed.functions:
|
|
203
|
+
snapshot.nodes[chunk.id] = _chunk_to_node_info(chunk)
|
|
204
|
+
|
|
205
|
+
_build_edges(snapshot)
|
|
206
|
+
return snapshot
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def build_snapshot_incremental(
|
|
210
|
+
repo_path: str,
|
|
211
|
+
db_snapshot: GraphSnapshot,
|
|
212
|
+
db_sha: str,
|
|
213
|
+
target_ref: str | None,
|
|
214
|
+
max_workers: int = 4,
|
|
215
|
+
) -> GraphSnapshot:
|
|
216
|
+
"""Build a snapshot by re-parsing only files changed between db_sha and target_ref.
|
|
217
|
+
|
|
218
|
+
Unchanged files are taken from db_snapshot. target_ref=None means the working tree.
|
|
219
|
+
|
|
220
|
+
When function IDs disappear in the changed files (rename, deletion), callers of
|
|
221
|
+
those IDs in unchanged files are included in the re-parse set so their call
|
|
222
|
+
references stay correct.
|
|
223
|
+
"""
|
|
224
|
+
t = time.perf_counter()
|
|
225
|
+
changed_files = _git_changed_files(repo_path, db_sha, target_ref)
|
|
226
|
+
logger.debug(
|
|
227
|
+
"[timing] git diff --name-only: %.2fs (%d changed files)",
|
|
228
|
+
time.perf_counter() - t,
|
|
229
|
+
len(changed_files),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
if not changed_files:
|
|
233
|
+
logger.info(
|
|
234
|
+
"No changed files between %s and %s — snapshot equals DB",
|
|
235
|
+
db_sha[:8],
|
|
236
|
+
target_ref or "working tree",
|
|
237
|
+
)
|
|
238
|
+
return db_snapshot
|
|
239
|
+
|
|
240
|
+
logger.info(
|
|
241
|
+
"Incremental snapshot: %d changed file(s) out of ~%d base nodes",
|
|
242
|
+
len(changed_files),
|
|
243
|
+
len(db_snapshot.nodes),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
t = time.perf_counter()
|
|
247
|
+
node_stubs = [_NodeStub(n) for n in db_snapshot.nodes.values()]
|
|
248
|
+
class_stubs = [_ClassStub(cid, supers) for cid, supers in db_snapshot.class_parents.items()]
|
|
249
|
+
logger.debug(
|
|
250
|
+
"[timing] build stubs: %.2fs (%d func, %d class)",
|
|
251
|
+
time.perf_counter() - t,
|
|
252
|
+
len(node_stubs),
|
|
253
|
+
len(class_stubs),
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
old_ids_in_changed: set[str] = {
|
|
257
|
+
nid for nid, node in db_snapshot.nodes.items() if node.file_path in changed_files
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if target_ref is None:
|
|
261
|
+
t = time.perf_counter()
|
|
262
|
+
fresh_parsed, files_parsed = _parse_and_expand_stale(
|
|
263
|
+
repo_path,
|
|
264
|
+
set(changed_files),
|
|
265
|
+
old_ids_in_changed,
|
|
266
|
+
db_snapshot,
|
|
267
|
+
node_stubs,
|
|
268
|
+
class_stubs,
|
|
269
|
+
max_workers,
|
|
270
|
+
)
|
|
271
|
+
logger.debug(
|
|
272
|
+
"[timing] parse+resolve: %.2fs (%d fresh functions)",
|
|
273
|
+
time.perf_counter() - t,
|
|
274
|
+
len(fresh_parsed.functions),
|
|
275
|
+
)
|
|
276
|
+
else:
|
|
277
|
+
t = time.perf_counter()
|
|
278
|
+
proc = subprocess.run(
|
|
279
|
+
["git", "archive", target_ref, "--format=tar"],
|
|
280
|
+
cwd=repo_path,
|
|
281
|
+
capture_output=True,
|
|
282
|
+
check=True,
|
|
283
|
+
)
|
|
284
|
+
with tempfile.TemporaryDirectory(prefix="codiff_snap_") as tmpdir:
|
|
285
|
+
with tarfile.open(fileobj=io.BytesIO(proc.stdout)) as tf:
|
|
286
|
+
tf.extractall(tmpdir, filter="data")
|
|
287
|
+
logger.debug("[timing] git archive + extract: %.2fs", time.perf_counter() - t)
|
|
288
|
+
t = time.perf_counter()
|
|
289
|
+
fresh_parsed, files_parsed = _parse_and_expand_stale(
|
|
290
|
+
tmpdir,
|
|
291
|
+
set(changed_files),
|
|
292
|
+
old_ids_in_changed,
|
|
293
|
+
db_snapshot,
|
|
294
|
+
node_stubs,
|
|
295
|
+
class_stubs,
|
|
296
|
+
max_workers,
|
|
297
|
+
)
|
|
298
|
+
logger.debug(
|
|
299
|
+
"[timing] parse+resolve: %.2fs (%d fresh functions)",
|
|
300
|
+
time.perf_counter() - t,
|
|
301
|
+
len(fresh_parsed.functions),
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
t = time.perf_counter()
|
|
305
|
+
snapshot = GraphSnapshot()
|
|
306
|
+
snapshot.class_parents = dict(db_snapshot.class_parents)
|
|
307
|
+
|
|
308
|
+
for node in db_snapshot.nodes.values():
|
|
309
|
+
if node.file_path not in files_parsed:
|
|
310
|
+
snapshot.nodes[node.function_id] = node
|
|
311
|
+
|
|
312
|
+
for chunk in fresh_parsed.functions:
|
|
313
|
+
snapshot.nodes[chunk.id] = _chunk_to_node_info(chunk)
|
|
314
|
+
|
|
315
|
+
for cls in fresh_parsed.classes:
|
|
316
|
+
if cls.superclasses:
|
|
317
|
+
snapshot.class_parents[cls.id] = list(cls.superclasses)
|
|
318
|
+
|
|
319
|
+
_build_edges(snapshot)
|
|
320
|
+
logger.debug(
|
|
321
|
+
"[timing] assemble snapshot + build edges: %.2fs (%d total nodes)",
|
|
322
|
+
time.perf_counter() - t,
|
|
323
|
+
len(snapshot.nodes),
|
|
324
|
+
)
|
|
325
|
+
return snapshot
|
codiff/export/json.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Serialize AnalysisResult to JSON for consumption by external tools (e.g. VSCode extension).
|
|
2
|
+
|
|
3
|
+
Schema version is included in every output so consumers can detect breaking changes.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from dataclasses import asdict
|
|
8
|
+
|
|
9
|
+
from codiff.schema.diff import AnalysisResult
|
|
10
|
+
|
|
11
|
+
SCHEMA_VERSION = "1.0"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def render_json(
|
|
15
|
+
result: AnalysisResult,
|
|
16
|
+
base_ref: str = "HEAD",
|
|
17
|
+
head_ref: str = "working tree",
|
|
18
|
+
indent: int = 2,
|
|
19
|
+
) -> str:
|
|
20
|
+
"""Return a JSON string representation of *result*.
|
|
21
|
+
|
|
22
|
+
Top-level structure:
|
|
23
|
+
{
|
|
24
|
+
"schema_version": "1.0",
|
|
25
|
+
"base_ref": "HEAD",
|
|
26
|
+
"head_ref": "working tree",
|
|
27
|
+
"summary": { ... },
|
|
28
|
+
"added": [ { function_id, file_path, class_name, is_entry_point,
|
|
29
|
+
new_callers, existing_callers, new_calls, existing_calls } ],
|
|
30
|
+
"modified": [ { function_id, file_path, class_name, signature_changed,
|
|
31
|
+
old_params, new_params, old_return_type, new_return_type,
|
|
32
|
+
calls_added_new, calls_added_existing, calls_removed,
|
|
33
|
+
callers } ],
|
|
34
|
+
"removed": [ { function_id, file_path, class_name, was_called_by } ]
|
|
35
|
+
}
|
|
36
|
+
"""
|
|
37
|
+
payload = {
|
|
38
|
+
"schema_version": SCHEMA_VERSION,
|
|
39
|
+
"base_ref": base_ref,
|
|
40
|
+
"head_ref": head_ref,
|
|
41
|
+
**asdict(result),
|
|
42
|
+
}
|
|
43
|
+
return json.dumps(payload, indent=indent, ensure_ascii=False)
|