codiff 0.1.2__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.
Files changed (71) hide show
  1. {codiff-0.1.2 → codiff-0.1.4}/PKG-INFO +42 -13
  2. {codiff-0.1.2 → codiff-0.1.4}/README.md +39 -10
  3. {codiff-0.1.2 → codiff-0.1.4}/codiff/__main__.py +2 -1
  4. {codiff-0.1.2 → codiff-0.1.4}/codiff/db/__init__.py +2 -1
  5. {codiff-0.1.2 → codiff-0.1.4}/codiff/db/models.py +13 -0
  6. {codiff-0.1.2 → codiff-0.1.4}/codiff/db/operations.py +42 -3
  7. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/analysis.py +4 -1
  8. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/engine.py +9 -12
  9. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/indexer.py +11 -17
  10. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/snapshot.py +139 -94
  11. {codiff-0.1.2 → codiff-0.1.4}/codiff/export/terminal.py +3 -2
  12. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/parser.py +63 -28
  13. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/python/parser.py +65 -56
  14. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/repository.py +33 -13
  15. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/resolver.py +133 -26
  16. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/typescript/parser.py +82 -2
  17. {codiff-0.1.2 → codiff-0.1.4}/codiff/schema/diff.py +5 -1
  18. codiff-0.1.4/codiff/utils/processes.py +23 -0
  19. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/PKG-INFO +42 -13
  20. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/SOURCES.txt +5 -0
  21. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/requires.txt +2 -2
  22. {codiff-0.1.2 → codiff-0.1.4}/pyproject.toml +3 -3
  23. {codiff-0.1.2 → codiff-0.1.4}/tests/test_cli.py +22 -8
  24. codiff-0.1.4/tests/test_determinism.py +257 -0
  25. codiff-0.1.4/tests/test_import_scoping.py +431 -0
  26. codiff-0.1.4/tests/test_index_format.py +131 -0
  27. codiff-0.1.4/tests/test_module_ids.py +112 -0
  28. {codiff-0.1.2 → codiff-0.1.4}/LICENSE +0 -0
  29. {codiff-0.1.2 → codiff-0.1.4}/codiff/__init__.py +0 -0
  30. {codiff-0.1.2 → codiff-0.1.4}/codiff/cli.py +0 -0
  31. {codiff-0.1.2 → codiff-0.1.4}/codiff/db/engine.py +0 -0
  32. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/__init__.py +0 -0
  33. {codiff-0.1.2 → codiff-0.1.4}/codiff/diff/differ.py +0 -0
  34. {codiff-0.1.2 → codiff-0.1.4}/codiff/export/__init__.py +0 -0
  35. {codiff-0.1.2 → codiff-0.1.4}/codiff/export/json.py +0 -0
  36. {codiff-0.1.2 → codiff-0.1.4}/codiff/export/mermaid.py +0 -0
  37. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/__init__.py +0 -0
  38. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/python/__init__.py +0 -0
  39. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/python/resolver.py +0 -0
  40. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/typescript/__init__.py +0 -0
  41. {codiff-0.1.2 → codiff-0.1.4}/codiff/languages/typescript/resolver.py +0 -0
  42. {codiff-0.1.2 → codiff-0.1.4}/codiff/mcp_server.py +0 -0
  43. {codiff-0.1.2 → codiff-0.1.4}/codiff/schema/__init__.py +0 -0
  44. {codiff-0.1.2 → codiff-0.1.4}/codiff/schema/parsing.py +0 -0
  45. {codiff-0.1.2 → codiff-0.1.4}/codiff/setup.py +0 -0
  46. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/__init__.py +0 -0
  47. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/args.py +0 -0
  48. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/files.py +0 -0
  49. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/git.py +0 -0
  50. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/instructions.py +0 -0
  51. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/instructions.yaml +0 -0
  52. {codiff-0.1.2 → codiff-0.1.4}/codiff/utils/terminal.py +0 -0
  53. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/dependency_links.txt +0 -0
  54. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/entry_points.txt +0 -0
  55. {codiff-0.1.2 → codiff-0.1.4}/codiff.egg-info/top_level.txt +0 -0
  56. {codiff-0.1.2 → codiff-0.1.4}/setup.cfg +0 -0
  57. {codiff-0.1.2 → codiff-0.1.4}/tests/test_call_patterns.py +0 -0
  58. {codiff-0.1.2 → codiff-0.1.4}/tests/test_call_resolver.py +0 -0
  59. {codiff-0.1.2 → codiff-0.1.4}/tests/test_code_parser.py +0 -0
  60. {codiff-0.1.2 → codiff-0.1.4}/tests/test_db.py +0 -0
  61. {codiff-0.1.2 → codiff-0.1.4}/tests/test_diff.py +0 -0
  62. {codiff-0.1.2 → codiff-0.1.4}/tests/test_export_json.py +0 -0
  63. {codiff-0.1.2 → codiff-0.1.4}/tests/test_export_mermaid.py +0 -0
  64. {codiff-0.1.2 → codiff-0.1.4}/tests/test_export_terminal.py +0 -0
  65. {codiff-0.1.2 → codiff-0.1.4}/tests/test_indexer.py +0 -0
  66. {codiff-0.1.2 → codiff-0.1.4}/tests/test_setup.py +0 -0
  67. {codiff-0.1.2 → codiff-0.1.4}/tests/test_snapshot.py +0 -0
  68. {codiff-0.1.2 → codiff-0.1.4}/tests/test_ts_integration.py +0 -0
  69. {codiff-0.1.2 → codiff-0.1.4}/tests/test_ts_parser.py +0 -0
  70. {codiff-0.1.2 → codiff-0.1.4}/tests/test_ts_resolver.py +0 -0
  71. {codiff-0.1.2 → 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.2
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>=0.24.0
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>=1.0.0
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
@@ -238,6 +238,9 @@ Dynamic: license-file
238
238
 
239
239
  # codiff
240
240
 
241
+ [![PyPI](https://img.shields.io/pypi/v/codiff)](https://pypi.org/project/codiff/)
242
+ [![GitHub Marketplace](https://img.shields.io/badge/GitHub%20Marketplace-codiff%20action-blue?logo=github)](https://github.com/marketplace/actions/codiff-action)
243
+
241
244
  A structural call-graph diff tool for multi-language codebases, built to work with coding agents.
242
245
 
243
246
  Instead of a line diff, codiff shows what changed **at the function level**: which functions were added and where they hook into existing code, which were modified, which were removed — with call relationships mapped across files.
@@ -291,31 +294,57 @@ codiff diff --base main --format mermaid > mermaid.md
291
294
 
292
295
  Open `mermaid.md` in any Markdown previewer (VS Code, GitHub, etc.) to visualise the diagram.
293
296
 
297
+ ## GitHub Action
298
+
299
+ codiff is available as a GitHub Action that posts a structural diff comment on every pull request — no AI coding agent required. The comment is created when the PR opens and updated on every subsequent push.
300
+
301
+ Run once in your repository to set it up:
302
+
303
+ ```bash
304
+ codiff init --agent github-actions
305
+ ```
306
+
307
+ This creates `.github/workflows/codiff.yml`:
308
+
309
+ ```yaml
310
+ name: codiff
311
+
312
+ on:
313
+ pull_request:
314
+ types: [opened, synchronize, reopened]
315
+
316
+ jobs:
317
+ codiff:
318
+ runs-on: ubuntu-latest
319
+ permissions:
320
+ pull-requests: write
321
+ steps:
322
+ - uses: issahammoud/codiff-action@v1
323
+ ```
324
+
325
+ Commit and push the file to activate the action.
326
+
327
+ > **Note:** Use either the GitHub Action or an AI coding agent integration, not both. If both are active, codiff detects the redundancy automatically and the action stays silent when the agent has already embedded a diagram in the PR description.
328
+
294
329
  ## Agent integration (MCP)
295
330
 
296
331
  Run once per project to wire codiff into your coding agent:
297
332
 
298
333
  ```bash
299
- codiff init --agent claude # Claude Code
300
- codiff init --agent codex # OpenAI Codex CLI
301
- codiff init --agent gemini # Gemini CLI
302
- codiff init --agent vibe # Mistral Vibe
303
- codiff init --agent github-actions # GitHub Actions
334
+ codiff init --agent claude # Claude Code
335
+ codiff init --agent codex # OpenAI Codex CLI
336
+ codiff init --agent gemini # Gemini CLI
337
+ codiff init --agent vibe # Mistral Vibe
304
338
  ```
305
339
 
306
340
  This writes the MCP server config and a project instructions file that tells the agent to call `codiff_diff` when creating pull requests. The agent then automatically embeds a structural diagram in every PR description.
307
341
 
308
- The `github-actions` option creates `.github/workflows/codiff.yml`, which posts the diagram as a PR comment on every pull request — no AI coding agent required.
309
-
310
- > **Note:** Use either an agent integration or the GitHub Action, not both. If both are active, codiff detects the redundancy automatically and the action stays silent when the agent has already embedded a diagram in the PR description.
311
-
312
342
  | Agent | MCP config | Instructions file |
313
343
  |---|---|---|
314
344
  | `claude` | `.mcp.json` | `CLAUDE.md` |
315
345
  | `codex` | `.codex/config.toml` | `AGENTS.md` |
316
346
  | `gemini` | `~/.gemini/settings.json` | `GEMINI.md` |
317
347
  | `vibe` | `~/.vibe/config.toml` | — |
318
- | `github-actions` | — | `.github/workflows/codiff.yml` |
319
348
 
320
349
 
321
350
  Here's the output of `codiff diff --base b8dde67 --head d25c385 --format mermaid` run on codiff's own codebase:
@@ -1,5 +1,8 @@
1
1
  # codiff
2
2
 
3
+ [![PyPI](https://img.shields.io/pypi/v/codiff)](https://pypi.org/project/codiff/)
4
+ [![GitHub Marketplace](https://img.shields.io/badge/GitHub%20Marketplace-codiff%20action-blue?logo=github)](https://github.com/marketplace/actions/codiff-action)
5
+
3
6
  A structural call-graph diff tool for multi-language codebases, built to work with coding agents.
4
7
 
5
8
  Instead of a line diff, codiff shows what changed **at the function level**: which functions were added and where they hook into existing code, which were modified, which were removed — with call relationships mapped across files.
@@ -53,31 +56,57 @@ codiff diff --base main --format mermaid > mermaid.md
53
56
 
54
57
  Open `mermaid.md` in any Markdown previewer (VS Code, GitHub, etc.) to visualise the diagram.
55
58
 
59
+ ## GitHub Action
60
+
61
+ codiff is available as a GitHub Action that posts a structural diff comment on every pull request — no AI coding agent required. The comment is created when the PR opens and updated on every subsequent push.
62
+
63
+ Run once in your repository to set it up:
64
+
65
+ ```bash
66
+ codiff init --agent github-actions
67
+ ```
68
+
69
+ This creates `.github/workflows/codiff.yml`:
70
+
71
+ ```yaml
72
+ name: codiff
73
+
74
+ on:
75
+ pull_request:
76
+ types: [opened, synchronize, reopened]
77
+
78
+ jobs:
79
+ codiff:
80
+ runs-on: ubuntu-latest
81
+ permissions:
82
+ pull-requests: write
83
+ steps:
84
+ - uses: issahammoud/codiff-action@v1
85
+ ```
86
+
87
+ Commit and push the file to activate the action.
88
+
89
+ > **Note:** Use either the GitHub Action or an AI coding agent integration, not both. If both are active, codiff detects the redundancy automatically and the action stays silent when the agent has already embedded a diagram in the PR description.
90
+
56
91
  ## Agent integration (MCP)
57
92
 
58
93
  Run once per project to wire codiff into your coding agent:
59
94
 
60
95
  ```bash
61
- codiff init --agent claude # Claude Code
62
- codiff init --agent codex # OpenAI Codex CLI
63
- codiff init --agent gemini # Gemini CLI
64
- codiff init --agent vibe # Mistral Vibe
65
- codiff init --agent github-actions # GitHub Actions
96
+ codiff init --agent claude # Claude Code
97
+ codiff init --agent codex # OpenAI Codex CLI
98
+ codiff init --agent gemini # Gemini CLI
99
+ codiff init --agent vibe # Mistral Vibe
66
100
  ```
67
101
 
68
102
  This writes the MCP server config and a project instructions file that tells the agent to call `codiff_diff` when creating pull requests. The agent then automatically embeds a structural diagram in every PR description.
69
103
 
70
- The `github-actions` option creates `.github/workflows/codiff.yml`, which posts the diagram as a PR comment on every pull request — no AI coding agent required.
71
-
72
- > **Note:** Use either an agent integration or the GitHub Action, not both. If both are active, codiff detects the redundancy automatically and the action stays silent when the agent has already embedded a diagram in the PR description.
73
-
74
104
  | Agent | MCP config | Instructions file |
75
105
  |---|---|---|
76
106
  | `claude` | `.mcp.json` | `CLAUDE.md` |
77
107
  | `codex` | `.codex/config.toml` | `AGENTS.md` |
78
108
  | `gemini` | `~/.gemini/settings.json` | `GEMINI.md` |
79
109
  | `vibe` | `~/.vibe/config.toml` | — |
80
- | `github-actions` | — | `.github/workflows/codiff.yml` |
81
110
 
82
111
 
83
112
  Here's the output of `codiff diff --base b8dde67 --head d25c385 --format mermaid` run on codiff's own codebase:
@@ -2,4 +2,5 @@
2
2
 
3
3
  from codiff.cli import main
4
4
 
5
- main()
5
+ if __name__ == "__main__":
6
+ main()
@@ -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
- if cls.superclasses:
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
- class_parents=head.class_parents,
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,13 +21,13 @@ 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 build_snapshot_incremental, load_from_db
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")
28
28
 
29
29
  t = time.perf_counter()
30
- db_sha = ensure_indexed(repo_path, "HEAD", max_workers=max_workers)
30
+ db_sha = ensure_indexed(repo_path, base_ref, max_workers=max_workers)
31
31
  log.debug("[timing] ensure_indexed: %.2fs", time.perf_counter() - t)
32
32
 
33
33
  db = get_db_path(repo_path)
@@ -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 = build_snapshot_incremental(
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 head snapshot: %.2fs (%d nodes)", time.perf_counter() - t, len(head.nodes)
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 HEAD incrementally.
98
+ """Update the DB from db_sha to new_sha incrementally.
94
99
 
95
- Re-parses files changed between db_sha and HEAD. If function IDs were deleted
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 _ClassStub, _NodeStub, _parse_and_expand_stale
103
+ from codiff.diff.snapshot import _parse_and_expand_stale
99
104
 
100
- changed_files = changed_files_between(repo_path, db_sha, "HEAD")
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, "HEAD")
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] = {