codiff 0.1.3__tar.gz → 0.1.4__tar.gz
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-0.1.3 → codiff-0.1.4}/PKG-INFO +3 -3
- {codiff-0.1.3 → codiff-0.1.4}/codiff/__main__.py +2 -1
- {codiff-0.1.3 → codiff-0.1.4}/codiff/db/__init__.py +2 -1
- {codiff-0.1.3 → codiff-0.1.4}/codiff/db/models.py +13 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/db/operations.py +42 -3
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/analysis.py +4 -1
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/engine.py +8 -11
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/indexer.py +11 -17
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/snapshot.py +139 -94
- {codiff-0.1.3 → codiff-0.1.4}/codiff/export/terminal.py +3 -2
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/parser.py +63 -28
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/python/parser.py +65 -56
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/repository.py +33 -13
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/resolver.py +133 -26
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/typescript/parser.py +82 -2
- {codiff-0.1.3 → codiff-0.1.4}/codiff/schema/diff.py +5 -1
- codiff-0.1.4/codiff/utils/processes.py +23 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/PKG-INFO +3 -3
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/SOURCES.txt +5 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/requires.txt +2 -2
- {codiff-0.1.3 → codiff-0.1.4}/pyproject.toml +3 -3
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_cli.py +20 -6
- codiff-0.1.4/tests/test_determinism.py +257 -0
- codiff-0.1.4/tests/test_import_scoping.py +431 -0
- codiff-0.1.4/tests/test_index_format.py +131 -0
- codiff-0.1.4/tests/test_module_ids.py +112 -0
- {codiff-0.1.3 → codiff-0.1.4}/LICENSE +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/README.md +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/cli.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/db/engine.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/diff/differ.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/export/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/export/json.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/export/mermaid.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/python/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/python/resolver.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/typescript/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/languages/typescript/resolver.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/mcp_server.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/schema/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/schema/parsing.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/setup.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/__init__.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/args.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/files.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/git.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/instructions.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/instructions.yaml +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff/utils/terminal.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/dependency_links.txt +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/entry_points.txt +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/codiff.egg-info/top_level.txt +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/setup.cfg +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_call_patterns.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_call_resolver.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_code_parser.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_db.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_diff.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_export_json.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_export_mermaid.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_export_terminal.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_indexer.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_setup.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_snapshot.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_ts_integration.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_ts_parser.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_ts_resolver.py +0 -0
- {codiff-0.1.3 → codiff-0.1.4}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: codiff
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Structural call-graph diff for codebases, built for coding agents
|
|
5
5
|
Author-email: Issa Hammoud <issa.hammoud@outlook.com>
|
|
6
6
|
License: Apache License
|
|
@@ -220,12 +220,12 @@ Requires-Python: >=3.11
|
|
|
220
220
|
Description-Content-Type: text/markdown
|
|
221
221
|
License-File: LICENSE
|
|
222
222
|
Requires-Dist: sqlalchemy>=2.0.25
|
|
223
|
-
Requires-Dist: tree-sitter
|
|
223
|
+
Requires-Dist: tree-sitter<0.26,>=0.24.0
|
|
224
224
|
Requires-Dist: tree-sitter-python>=0.23.6
|
|
225
225
|
Requires-Dist: tree-sitter-typescript>=0.23.2
|
|
226
226
|
Requires-Dist: pathspec>=0.12.0
|
|
227
227
|
Requires-Dist: rich>=13.0.0
|
|
228
|
-
Requires-Dist: mcp
|
|
228
|
+
Requires-Dist: mcp<2,>=1.0.0
|
|
229
229
|
Requires-Dist: pyyaml>=6.0
|
|
230
230
|
Requires-Dist: tomli-w>=1.0.0
|
|
231
231
|
Provides-Extra: dev
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from codiff.db.engine import get_db_path, make_sync_engine, make_sync_session
|
|
2
|
-
from codiff.db.models import Base, CallEdge, Class, CommitMeta, Function, Repository
|
|
2
|
+
from codiff.db.models import Base, CallEdge, Class, CommitMeta, Function, IndexMeta, Repository
|
|
3
3
|
|
|
4
4
|
__all__ = [
|
|
5
5
|
# models
|
|
@@ -9,6 +9,7 @@ __all__ = [
|
|
|
9
9
|
"Class",
|
|
10
10
|
"CallEdge",
|
|
11
11
|
"CommitMeta",
|
|
12
|
+
"IndexMeta",
|
|
12
13
|
# engine helpers
|
|
13
14
|
"get_db_path",
|
|
14
15
|
"make_sync_engine",
|
|
@@ -117,3 +117,16 @@ class CommitMeta(Base):
|
|
|
117
117
|
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=_new_uuid)
|
|
118
118
|
commit_sha: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
119
119
|
indexed_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(UTC))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class IndexMeta(Base):
|
|
123
|
+
"""Key/value facts about how the database was built (e.g. index format).
|
|
124
|
+
|
|
125
|
+
Kept as its own table so it can be read from a database written by any
|
|
126
|
+
codiff version: ``create_all`` never alters existing tables.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
__tablename__ = "index_meta"
|
|
130
|
+
|
|
131
|
+
key: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
132
|
+
value: Mapped[str] = mapped_column(Text, nullable=False)
|
|
@@ -13,16 +13,29 @@ from contextlib import contextmanager
|
|
|
13
13
|
from typing import TYPE_CHECKING, Generator
|
|
14
14
|
|
|
15
15
|
from sqlalchemy import text
|
|
16
|
+
from sqlalchemy.exc import SQLAlchemyError
|
|
16
17
|
from sqlalchemy.orm import Session, load_only, sessionmaker
|
|
17
18
|
|
|
18
19
|
from codiff.db.engine import make_sync_engine
|
|
19
|
-
from codiff.db.models import Base, CallEdge, Class, CommitMeta, Function, Repository
|
|
20
|
+
from codiff.db.models import Base, CallEdge, Class, CommitMeta, Function, IndexMeta, Repository
|
|
20
21
|
from codiff.schema.diff import GraphSnapshot, NodeInfo
|
|
21
22
|
|
|
22
23
|
if TYPE_CHECKING:
|
|
23
24
|
from codiff.languages.repository import ParsedRepository
|
|
24
25
|
|
|
25
26
|
|
|
27
|
+
# Version of what the indexer writes. Bump it whenever a change makes rows
|
|
28
|
+
# written by an older codiff wrong for the current one: function/class id
|
|
29
|
+
# format, resolution rules that change stored ``calls``, or the table schema.
|
|
30
|
+
# A database with any other version (or none: written before versioning) is
|
|
31
|
+
# discarded and rebuilt from scratch instead of being updated incrementally.
|
|
32
|
+
# 1 — implicit, before versioning
|
|
33
|
+
# 2 — module ids strip only the trailing ".py"; imports resolved per file
|
|
34
|
+
INDEX_FORMAT_VERSION = 2
|
|
35
|
+
|
|
36
|
+
_INDEX_FORMAT_KEY = "index_format"
|
|
37
|
+
|
|
38
|
+
|
|
26
39
|
# ---------------------------------------------------------------------------
|
|
27
40
|
# Session helper
|
|
28
41
|
# ---------------------------------------------------------------------------
|
|
@@ -63,6 +76,32 @@ def get_indexed_sha(db_path: str) -> str | None:
|
|
|
63
76
|
return None
|
|
64
77
|
|
|
65
78
|
|
|
79
|
+
def get_index_format(db_path: str) -> int | None:
|
|
80
|
+
"""Return the index format stored in *db_path*, or None if missing/unknown."""
|
|
81
|
+
if not os.path.exists(db_path):
|
|
82
|
+
return None
|
|
83
|
+
try:
|
|
84
|
+
with _session(db_path) as db:
|
|
85
|
+
row = db.get(IndexMeta, _INDEX_FORMAT_KEY)
|
|
86
|
+
return int(row.value) if row else None
|
|
87
|
+
except (SQLAlchemyError, ValueError):
|
|
88
|
+
return None # no index_meta table (older DB), or a corrupt value
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def discard_if_outdated(db_path: str) -> bool:
|
|
92
|
+
"""Delete *db_path* if it was written with another index format.
|
|
93
|
+
|
|
94
|
+
Returns True when a database was discarded. The file is removed rather
|
|
95
|
+
than emptied so that older table layouts do not survive either.
|
|
96
|
+
"""
|
|
97
|
+
if not os.path.exists(db_path) or get_index_format(db_path) == INDEX_FORMAT_VERSION:
|
|
98
|
+
return False
|
|
99
|
+
for path in (db_path, db_path + "-wal", db_path + "-shm"):
|
|
100
|
+
if os.path.exists(path):
|
|
101
|
+
os.remove(path)
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
|
|
66
105
|
def load_snapshot(db_path: str) -> GraphSnapshot:
|
|
67
106
|
"""Load the indexed snapshot from the SQLite database, including resolved edges."""
|
|
68
107
|
snapshot = GraphSnapshot()
|
|
@@ -94,8 +133,7 @@ def load_snapshot(db_path: str) -> GraphSnapshot:
|
|
|
94
133
|
code=func.code or "",
|
|
95
134
|
)
|
|
96
135
|
for cls in db.query(Class).options(load_only(Class.class_id, Class.superclasses)).all():
|
|
97
|
-
|
|
98
|
-
snapshot.class_parents[cls.class_id] = list(cls.superclasses)
|
|
136
|
+
snapshot.class_parents[cls.class_id] = list(cls.superclasses or [])
|
|
99
137
|
|
|
100
138
|
all_ids = set(snapshot.nodes)
|
|
101
139
|
for node in snapshot.nodes.values():
|
|
@@ -134,6 +172,7 @@ def write_full_snapshot(db_path: str, parsed: ParsedRepository, sha: str) -> Non
|
|
|
134
172
|
repo.total_classes = len(parsed.classes)
|
|
135
173
|
repo.is_parsed = True
|
|
136
174
|
db.add(CommitMeta(commit_sha=sha))
|
|
175
|
+
db.merge(IndexMeta(key=_INDEX_FORMAT_KEY, value=str(INDEX_FORMAT_VERSION)))
|
|
137
176
|
|
|
138
177
|
|
|
139
178
|
def write_incremental(
|
|
@@ -32,7 +32,10 @@ def analyze(
|
|
|
32
32
|
added=_added(diff, added_ids, head, head_reverse),
|
|
33
33
|
modified=_modified(diff, added_ids, base, head, head_reverse),
|
|
34
34
|
removed=_removed(diff, base_reverse),
|
|
35
|
-
|
|
35
|
+
# Snapshots list every class (parentless ones with []) for the resolver's
|
|
36
|
+
# class index; the report only needs actual inheritance. Sorted so the
|
|
37
|
+
# output does not depend on DB row order.
|
|
38
|
+
class_parents={cid: ps for cid, ps in sorted(head.class_parents.items()) if ps},
|
|
36
39
|
)
|
|
37
40
|
|
|
38
41
|
|
|
@@ -21,7 +21,7 @@ def compute_diff(
|
|
|
21
21
|
from codiff.diff.analysis import analyze
|
|
22
22
|
from codiff.diff.differ import diff_snapshots
|
|
23
23
|
from codiff.diff.indexer import ensure_indexed
|
|
24
|
-
from codiff.diff.snapshot import
|
|
24
|
+
from codiff.diff.snapshot import build_snapshot_pair, load_from_db
|
|
25
25
|
|
|
26
26
|
repo_path = os.path.abspath(repo_path)
|
|
27
27
|
log.info("Diffing %s → %s", base_ref, head_ref or "working tree")
|
|
@@ -37,20 +37,17 @@ def compute_diff(
|
|
|
37
37
|
"[timing] load_from_db: %.2fs (%d nodes)", time.perf_counter() - t, len(db_snapshot.nodes)
|
|
38
38
|
)
|
|
39
39
|
|
|
40
|
+
# The DB is anchored at base_ref, so db_sha *is* the base. Both sides are
|
|
41
|
+
# built together so the changed files are resolved identically on each.
|
|
40
42
|
t = time.perf_counter()
|
|
41
|
-
base =
|
|
42
|
-
repo_path, db_snapshot, db_sha, base_ref, max_workers=max_workers
|
|
43
|
-
)
|
|
44
|
-
log.debug(
|
|
45
|
-
"[timing] build base snapshot: %.2fs (%d nodes)", time.perf_counter() - t, len(base.nodes)
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
t = time.perf_counter()
|
|
49
|
-
head = build_snapshot_incremental(
|
|
43
|
+
base, head = build_snapshot_pair(
|
|
50
44
|
repo_path, db_snapshot, db_sha, head_ref, max_workers=max_workers
|
|
51
45
|
)
|
|
52
46
|
log.debug(
|
|
53
|
-
"[timing] build
|
|
47
|
+
"[timing] build snapshots: %.2fs (base %d nodes, head %d nodes)",
|
|
48
|
+
time.perf_counter() - t,
|
|
49
|
+
len(base.nodes),
|
|
50
|
+
len(head.nodes),
|
|
54
51
|
)
|
|
55
52
|
|
|
56
53
|
t = time.perf_counter()
|
|
@@ -15,6 +15,8 @@ import tempfile
|
|
|
15
15
|
|
|
16
16
|
from codiff.db import get_db_path
|
|
17
17
|
from codiff.db.operations import (
|
|
18
|
+
INDEX_FORMAT_VERSION,
|
|
19
|
+
discard_if_outdated,
|
|
18
20
|
get_indexed_sha,
|
|
19
21
|
load_snapshot,
|
|
20
22
|
update_sha,
|
|
@@ -32,6 +34,7 @@ current_indexed_sha = get_indexed_sha
|
|
|
32
34
|
def ensure_indexed(repo_path: str, ref: str = "HEAD", max_workers: int = 4) -> str:
|
|
33
35
|
"""Ensure .codiff.db is indexed at *ref*.
|
|
34
36
|
|
|
37
|
+
- DB written with another index format: discarded, then full index.
|
|
35
38
|
- Empty DB: full parse and index.
|
|
36
39
|
- SHA changed: incremental update (re-parse changed files + stale callers).
|
|
37
40
|
- Same SHA: no-op.
|
|
@@ -41,6 +44,8 @@ def ensure_indexed(repo_path: str, ref: str = "HEAD", max_workers: int = 4) -> s
|
|
|
41
44
|
repo_path = os.path.abspath(repo_path)
|
|
42
45
|
db_path = get_db_path(repo_path)
|
|
43
46
|
sha = resolve_sha(repo_path, ref)
|
|
47
|
+
if discard_if_outdated(db_path):
|
|
48
|
+
logger.info("DB uses an older index format — rebuilding (format %d)", INDEX_FORMAT_VERSION)
|
|
44
49
|
current = get_indexed_sha(db_path)
|
|
45
50
|
if current == sha:
|
|
46
51
|
logger.info("DB already at %s — skipping re-index", sha[:8])
|
|
@@ -90,14 +95,14 @@ def _full_index(repo_path: str, db_path: str, ref: str, sha: str, max_workers: i
|
|
|
90
95
|
def _incremental_update_db(
|
|
91
96
|
repo_path: str, db_path: str, db_sha: str, new_sha: str, max_workers: int = 4
|
|
92
97
|
) -> None:
|
|
93
|
-
"""Update the DB from db_sha to
|
|
98
|
+
"""Update the DB from db_sha to new_sha incrementally.
|
|
94
99
|
|
|
95
|
-
Re-parses files changed between db_sha and
|
|
100
|
+
Re-parses files changed between db_sha and new_sha. If function IDs were deleted
|
|
96
101
|
or renamed, also re-parses files whose call references became stale.
|
|
97
102
|
"""
|
|
98
|
-
from codiff.diff.snapshot import
|
|
103
|
+
from codiff.diff.snapshot import _parse_and_expand_stale
|
|
99
104
|
|
|
100
|
-
changed_files = changed_files_between(repo_path, db_sha,
|
|
105
|
+
changed_files = changed_files_between(repo_path, db_sha, new_sha)
|
|
101
106
|
if not changed_files:
|
|
102
107
|
update_sha(db_path, new_sha)
|
|
103
108
|
return
|
|
@@ -110,24 +115,13 @@ def _incremental_update_db(
|
|
|
110
115
|
)
|
|
111
116
|
|
|
112
117
|
db_snapshot = load_snapshot(db_path)
|
|
113
|
-
node_stubs = [_NodeStub(n) for n in db_snapshot.nodes.values()]
|
|
114
|
-
class_stubs = [_ClassStub(cid, supers) for cid, supers in db_snapshot.class_parents.items()]
|
|
115
|
-
old_ids_in_changed: set[str] = {
|
|
116
|
-
nid for nid, node in db_snapshot.nodes.items() if node.file_path in changed_files
|
|
117
|
-
}
|
|
118
118
|
|
|
119
|
-
tar_archive = git_archive(repo_path,
|
|
119
|
+
tar_archive = git_archive(repo_path, new_sha)
|
|
120
120
|
with tempfile.TemporaryDirectory(prefix="codiff_inc_") as tmpdir:
|
|
121
121
|
with tarfile.open(fileobj=tar_archive) as tf:
|
|
122
122
|
tf.extractall(tmpdir, filter="data")
|
|
123
123
|
fresh, files_to_update = _parse_and_expand_stale(
|
|
124
|
-
tmpdir,
|
|
125
|
-
set(changed_files),
|
|
126
|
-
old_ids_in_changed,
|
|
127
|
-
db_snapshot,
|
|
128
|
-
node_stubs,
|
|
129
|
-
class_stubs,
|
|
130
|
-
max_workers,
|
|
124
|
+
tmpdir, set(changed_files), db_snapshot, max_workers
|
|
131
125
|
)
|
|
132
126
|
|
|
133
127
|
old_func_ids: set[str] = {
|
|
@@ -7,6 +7,9 @@ Loaders:
|
|
|
7
7
|
- build_snapshot_incremental(...) — builds a snapshot by re-parsing only changed
|
|
8
8
|
files relative to a DB anchor, reusing the DB
|
|
9
9
|
snapshot for everything else
|
|
10
|
+
- build_snapshot_pair(...) — builds matching (base, head) snapshots for a
|
|
11
|
+
diff, re-parsing the changed files on both
|
|
12
|
+
sides with the same resolution context
|
|
10
13
|
|
|
11
14
|
All loaders return a GraphSnapshot with the same structure.
|
|
12
15
|
"""
|
|
@@ -17,6 +20,8 @@ import subprocess
|
|
|
17
20
|
import tarfile
|
|
18
21
|
import tempfile
|
|
19
22
|
import time
|
|
23
|
+
from collections.abc import Iterator
|
|
24
|
+
from contextlib import contextmanager
|
|
20
25
|
|
|
21
26
|
from codiff.languages.repository import ParsedRepository
|
|
22
27
|
from codiff.schema.diff import GraphSnapshot, NodeInfo
|
|
@@ -109,22 +114,46 @@ def _chunk_to_node_info(chunk) -> NodeInfo:
|
|
|
109
114
|
)
|
|
110
115
|
|
|
111
116
|
|
|
112
|
-
|
|
117
|
+
@contextmanager
|
|
118
|
+
def _tree_at(repo_path: str, ref: str | None) -> Iterator[str]:
|
|
119
|
+
"""Yield a directory holding the tree at *ref*; None means the working tree."""
|
|
120
|
+
if ref is None:
|
|
121
|
+
yield repo_path
|
|
122
|
+
return
|
|
123
|
+
t = time.perf_counter()
|
|
124
|
+
proc = subprocess.run(
|
|
125
|
+
["git", "archive", ref, "--format=tar"],
|
|
126
|
+
cwd=repo_path,
|
|
127
|
+
capture_output=True,
|
|
128
|
+
check=True,
|
|
129
|
+
)
|
|
130
|
+
with tempfile.TemporaryDirectory(prefix="codiff_snap_") as tmpdir:
|
|
131
|
+
with tarfile.open(fileobj=io.BytesIO(proc.stdout)) as tf:
|
|
132
|
+
tf.extractall(tmpdir, filter="data")
|
|
133
|
+
logger.debug("[timing] git archive + extract %s: %.2fs", ref, time.perf_counter() - t)
|
|
134
|
+
yield tmpdir
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _parse_files(
|
|
113
138
|
parse_root: str,
|
|
114
139
|
files_to_parse: set[str],
|
|
115
|
-
old_ids_in_changed: set[str],
|
|
116
140
|
db_snapshot: GraphSnapshot,
|
|
117
|
-
node_stubs: list,
|
|
118
|
-
class_stubs: list,
|
|
119
141
|
max_workers: int,
|
|
120
|
-
) ->
|
|
121
|
-
"""Parse files_to_parse
|
|
142
|
+
) -> ParsedRepository:
|
|
143
|
+
"""Parse *files_to_parse*, resolving calls against the rest of the DB snapshot.
|
|
122
144
|
|
|
123
|
-
|
|
145
|
+
The resolver index is the fresh parse of *files_to_parse* plus stubs for
|
|
146
|
+
every DB function outside those files. Stubs for the re-parsed files are
|
|
147
|
+
left out: they describe the old version of those files and would otherwise
|
|
148
|
+
shadow the fresh functions (e.g. keep a deleted function resolvable).
|
|
124
149
|
"""
|
|
125
150
|
from codiff.languages import parse_repository
|
|
126
151
|
|
|
127
|
-
|
|
152
|
+
node_stubs = [
|
|
153
|
+
_NodeStub(n) for n in db_snapshot.nodes.values() if n.file_path not in files_to_parse
|
|
154
|
+
]
|
|
155
|
+
class_stubs = [_ClassStub(cid, supers) for cid, supers in db_snapshot.class_parents.items()]
|
|
156
|
+
return parse_repository(
|
|
128
157
|
parse_root,
|
|
129
158
|
files_to_parse=files_to_parse,
|
|
130
159
|
extra_index_functions=node_stubs,
|
|
@@ -132,6 +161,22 @@ def _parse_and_expand_stale(
|
|
|
132
161
|
max_workers=max_workers,
|
|
133
162
|
)
|
|
134
163
|
|
|
164
|
+
|
|
165
|
+
def _parse_and_expand_stale(
|
|
166
|
+
parse_root: str,
|
|
167
|
+
files_to_parse: set[str],
|
|
168
|
+
db_snapshot: GraphSnapshot,
|
|
169
|
+
max_workers: int,
|
|
170
|
+
) -> tuple[ParsedRepository, set[str]]:
|
|
171
|
+
"""Parse files_to_parse; if function IDs were deleted, expand to stale callers and re-parse.
|
|
172
|
+
|
|
173
|
+
Returns (fresh_parsed, final_files_to_parse).
|
|
174
|
+
"""
|
|
175
|
+
fresh = _parse_files(parse_root, files_to_parse, db_snapshot, max_workers)
|
|
176
|
+
|
|
177
|
+
old_ids_in_changed = {
|
|
178
|
+
nid for nid, node in db_snapshot.nodes.items() if node.file_path in files_to_parse
|
|
179
|
+
}
|
|
135
180
|
deleted_ids = old_ids_in_changed - {fn.id for fn in fresh.functions}
|
|
136
181
|
if not deleted_ids:
|
|
137
182
|
return fresh, files_to_parse
|
|
@@ -153,16 +198,33 @@ def _parse_and_expand_stale(
|
|
|
153
198
|
len(deleted_ids),
|
|
154
199
|
)
|
|
155
200
|
expanded = files_to_parse | stale_files
|
|
156
|
-
fresh =
|
|
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
|
-
)
|
|
201
|
+
fresh = _parse_files(parse_root, expanded, db_snapshot, max_workers)
|
|
163
202
|
return fresh, expanded
|
|
164
203
|
|
|
165
204
|
|
|
205
|
+
def _assemble_snapshot(
|
|
206
|
+
db_snapshot: GraphSnapshot,
|
|
207
|
+
files_parsed: set[str],
|
|
208
|
+
fresh_parsed: ParsedRepository,
|
|
209
|
+
) -> GraphSnapshot:
|
|
210
|
+
"""DB nodes outside *files_parsed*, plus the fresh parse of those files."""
|
|
211
|
+
snapshot = GraphSnapshot()
|
|
212
|
+
snapshot.class_parents = dict(db_snapshot.class_parents)
|
|
213
|
+
|
|
214
|
+
for node in db_snapshot.nodes.values():
|
|
215
|
+
if node.file_path not in files_parsed:
|
|
216
|
+
snapshot.nodes[node.function_id] = node
|
|
217
|
+
|
|
218
|
+
for chunk in fresh_parsed.functions:
|
|
219
|
+
snapshot.nodes[chunk.id] = _chunk_to_node_info(chunk)
|
|
220
|
+
|
|
221
|
+
for cls in fresh_parsed.classes:
|
|
222
|
+
snapshot.class_parents[cls.id] = list(cls.superclasses or [])
|
|
223
|
+
|
|
224
|
+
_build_edges(snapshot)
|
|
225
|
+
return snapshot
|
|
226
|
+
|
|
227
|
+
|
|
166
228
|
# ---------------------------------------------------------------------------
|
|
167
229
|
# Public loaders
|
|
168
230
|
# ---------------------------------------------------------------------------
|
|
@@ -197,8 +259,7 @@ def build_from_path(repo_path: str, max_workers: int = 4) -> GraphSnapshot:
|
|
|
197
259
|
|
|
198
260
|
snapshot = GraphSnapshot()
|
|
199
261
|
for cls in parsed.classes:
|
|
200
|
-
|
|
201
|
-
snapshot.class_parents[cls.id] = list(cls.superclasses)
|
|
262
|
+
snapshot.class_parents[cls.id] = list(cls.superclasses or [])
|
|
202
263
|
for chunk in parsed.functions:
|
|
203
264
|
snapshot.nodes[chunk.id] = _chunk_to_node_info(chunk)
|
|
204
265
|
|
|
@@ -220,106 +281,90 @@ def build_snapshot_incremental(
|
|
|
220
281
|
When function IDs disappear in the changed files (rename, deletion), callers of
|
|
221
282
|
those IDs in unchanged files are included in the re-parse set so their call
|
|
222
283
|
references stay correct.
|
|
284
|
+
|
|
285
|
+
To diff two states, use build_snapshot_pair instead: comparing this
|
|
286
|
+
snapshot with db_snapshot directly mixes two resolution contexts (see there).
|
|
223
287
|
"""
|
|
224
|
-
t = time.perf_counter()
|
|
225
288
|
changed_files = _git_changed_files(repo_path, db_sha, target_ref)
|
|
289
|
+
if not changed_files:
|
|
290
|
+
logger.info(
|
|
291
|
+
"No changed files between %s and %s — snapshot equals DB",
|
|
292
|
+
db_sha[:8],
|
|
293
|
+
target_ref or "working tree",
|
|
294
|
+
)
|
|
295
|
+
return db_snapshot
|
|
296
|
+
|
|
297
|
+
with _tree_at(repo_path, target_ref) as root:
|
|
298
|
+
fresh_parsed, files_parsed = _parse_and_expand_stale(
|
|
299
|
+
root, changed_files, db_snapshot, max_workers
|
|
300
|
+
)
|
|
301
|
+
return _assemble_snapshot(db_snapshot, files_parsed, fresh_parsed)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def build_snapshot_pair(
|
|
305
|
+
repo_path: str,
|
|
306
|
+
db_snapshot: GraphSnapshot,
|
|
307
|
+
db_sha: str,
|
|
308
|
+
head_ref: str | None,
|
|
309
|
+
max_workers: int = 4,
|
|
310
|
+
) -> tuple[GraphSnapshot, GraphSnapshot]:
|
|
311
|
+
"""Build (base, head) snapshots for diffing db_sha against head_ref.
|
|
312
|
+
|
|
313
|
+
head_ref=None means the working tree. The files changed between the two
|
|
314
|
+
(plus stale callers of deleted functions) are re-parsed on *both* sides
|
|
315
|
+
with the same resolution context; every other file comes from the DB and
|
|
316
|
+
is identical on both sides.
|
|
317
|
+
|
|
318
|
+
Re-parsing the base side, instead of reusing the DB rows for it, is what
|
|
319
|
+
makes the diff deterministic. The DB rows were resolved in a different
|
|
320
|
+
context than an incremental parse — a full parse sees every file's
|
|
321
|
+
imports, an incremental parse only the changed files' — and the DB's own
|
|
322
|
+
context depends on its history (full index vs. incremental updates). Mixing
|
|
323
|
+
the two made unchanged functions show up as "modified" depending on what
|
|
324
|
+
previous runs had left in the DB.
|
|
325
|
+
"""
|
|
326
|
+
t = time.perf_counter()
|
|
327
|
+
changed_files = _git_changed_files(repo_path, db_sha, head_ref)
|
|
226
328
|
logger.debug(
|
|
227
329
|
"[timing] git diff --name-only: %.2fs (%d changed files)",
|
|
228
330
|
time.perf_counter() - t,
|
|
229
331
|
len(changed_files),
|
|
230
332
|
)
|
|
231
|
-
|
|
232
333
|
if not changed_files:
|
|
233
334
|
logger.info(
|
|
234
|
-
"No changed files between %s and %s —
|
|
335
|
+
"No changed files between %s and %s — nothing to diff",
|
|
235
336
|
db_sha[:8],
|
|
236
|
-
|
|
337
|
+
head_ref or "working tree",
|
|
237
338
|
)
|
|
238
|
-
return db_snapshot
|
|
339
|
+
return db_snapshot, db_snapshot
|
|
239
340
|
|
|
240
341
|
logger.info(
|
|
241
|
-
"Incremental
|
|
342
|
+
"Incremental diff: %d changed file(s) out of ~%d base nodes",
|
|
242
343
|
len(changed_files),
|
|
243
344
|
len(db_snapshot.nodes),
|
|
244
345
|
)
|
|
245
346
|
|
|
246
347
|
t = time.perf_counter()
|
|
247
|
-
|
|
248
|
-
|
|
348
|
+
with _tree_at(repo_path, head_ref) as head_root:
|
|
349
|
+
head_parsed, files_parsed = _parse_and_expand_stale(
|
|
350
|
+
head_root, changed_files, db_snapshot, max_workers
|
|
351
|
+
)
|
|
249
352
|
logger.debug(
|
|
250
|
-
"[timing]
|
|
353
|
+
"[timing] parse+resolve head: %.2fs (%d files, %d functions)",
|
|
251
354
|
time.perf_counter() - t,
|
|
252
|
-
len(
|
|
253
|
-
len(
|
|
355
|
+
len(files_parsed),
|
|
356
|
+
len(head_parsed.functions),
|
|
254
357
|
)
|
|
255
358
|
|
|
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
359
|
t = time.perf_counter()
|
|
305
|
-
|
|
306
|
-
|
|
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)
|
|
360
|
+
with _tree_at(repo_path, db_sha) as base_root:
|
|
361
|
+
base_parsed = _parse_files(base_root, files_parsed, db_snapshot, max_workers)
|
|
320
362
|
logger.debug(
|
|
321
|
-
"[timing]
|
|
363
|
+
"[timing] parse+resolve base: %.2fs (%d functions)",
|
|
322
364
|
time.perf_counter() - t,
|
|
323
|
-
len(
|
|
365
|
+
len(base_parsed.functions),
|
|
324
366
|
)
|
|
325
|
-
|
|
367
|
+
|
|
368
|
+
base = _assemble_snapshot(db_snapshot, files_parsed, base_parsed)
|
|
369
|
+
head = _assemble_snapshot(db_snapshot, files_parsed, head_parsed)
|
|
370
|
+
return base, head
|
|
@@ -13,6 +13,7 @@ Color scheme
|
|
|
13
13
|
- Existing / pre-diff functions are rendered dim (gray).
|
|
14
14
|
"""
|
|
15
15
|
|
|
16
|
+
import os
|
|
16
17
|
from collections import defaultdict
|
|
17
18
|
from typing import Optional
|
|
18
19
|
|
|
@@ -145,7 +146,7 @@ def _display_name(fn: object) -> str:
|
|
|
145
146
|
# Detect nested: strip the module prefix (derived from file_path) and check
|
|
146
147
|
# if what remains is "outer.inner" rather than a bare function name.
|
|
147
148
|
file_path = getattr(fn, "file_path", "")
|
|
148
|
-
module = file_path.replace("/", ".")
|
|
149
|
+
module = os.path.splitext(file_path)[0].replace("/", ".")
|
|
149
150
|
if function_id.startswith(module + "."):
|
|
150
151
|
remaining = function_id[len(module) + 1 :] # e.g. "build_from_path.walk"
|
|
151
152
|
parts = remaining.split(".")
|
|
@@ -658,7 +659,7 @@ def _build_file_panel(
|
|
|
658
659
|
# ── Detect intra-file relationships ──────────────────────────────────────
|
|
659
660
|
# class_name → list of (rel_type, target_class_name) for classes in this file
|
|
660
661
|
all_class_names = {cn for cn in class_buckets if cn is not None}
|
|
661
|
-
file_module = file_path.replace("/", ".")
|
|
662
|
+
file_module = os.path.splitext(file_path)[0].replace("/", ".")
|
|
662
663
|
|
|
663
664
|
# Inheritance: use class_parents (ClassChunk.superclasses)
|
|
664
665
|
intra_inherit: dict[str, list[str]] = defaultdict(list)
|