intentumdiff-python 0.0.1__py3-none-win_arm64.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.
Files changed (165) hide show
  1. intentumdiff/__init__.py +134 -0
  2. intentumdiff/_differ_gate.py +210 -0
  3. intentumdiff/_differ_presentation.py +786 -0
  4. intentumdiff/_differ_runtime.py +320 -0
  5. intentumdiff/analysis/__init__.py +1 -0
  6. intentumdiff/analysis/compile_commands.py +151 -0
  7. intentumdiff/analysis/cross_file.py +71 -0
  8. intentumdiff/analysis/diagnostics.py +309 -0
  9. intentumdiff/analysis/guardrail_reports.py +254 -0
  10. intentumdiff/analysis/guardrails.py +318 -0
  11. intentumdiff/analysis/invariances.py +1207 -0
  12. intentumdiff/analysis/keyed_profiles.py +25 -0
  13. intentumdiff/analysis/resource_profiles.py +14 -0
  14. intentumdiff/analysis/schema_resolver.py +816 -0
  15. intentumdiff/analysis/text_review.py +46 -0
  16. intentumdiff/analysis/user_schemas.py +430 -0
  17. intentumdiff/assets.py +45 -0
  18. intentumdiff/cache/__init__.py +37 -0
  19. intentumdiff/cache/duckdb_store.py +98 -0
  20. intentumdiff/cache/sqlite_store.py +149 -0
  21. intentumdiff/cache/store.py +190 -0
  22. intentumdiff/cli/__init__.py +199 -0
  23. intentumdiff/cli/_commands.py +2141 -0
  24. intentumdiff/cli/_parser.py +1061 -0
  25. intentumdiff/cli/_shared.py +644 -0
  26. intentumdiff/content_type.py +59 -0
  27. intentumdiff/core/__init__.py +1 -0
  28. intentumdiff/core/commit_differ.py +599 -0
  29. intentumdiff/core/config.py +51 -0
  30. intentumdiff/core/cst_serializer.py +101 -0
  31. intentumdiff/core/diffignore.py +87 -0
  32. intentumdiff/core/index.py +189 -0
  33. intentumdiff/core/indexer.py +792 -0
  34. intentumdiff/core/models.py +837 -0
  35. intentumdiff/differ.py +2195 -0
  36. intentumdiff/github_app.py +287 -0
  37. intentumdiff/intentumdiff_rust_core/__init__.py +7 -0
  38. intentumdiff/intentumdiff_rust_core/ffi.py +8 -0
  39. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  40. intentumdiff/invariances/__init__.py +2 -0
  41. intentumdiff/invariances/rules.schema.json +283 -0
  42. intentumdiff/invariances/rules.yaml +349 -0
  43. intentumdiff/live_server.py +1314 -0
  44. intentumdiff/lsp/__init__.py +67 -0
  45. intentumdiff/lsp/client.py +544 -0
  46. intentumdiff/lsp/config.py +96 -0
  47. intentumdiff/lsp/enricher.py +227 -0
  48. intentumdiff/lsp/exceptions.py +27 -0
  49. intentumdiff/lsp/launcher.py +273 -0
  50. intentumdiff/lsp/servers.py +356 -0
  51. intentumdiff/lsp_server/__init__.py +14 -0
  52. intentumdiff/lsp_server/_codelens.py +67 -0
  53. intentumdiff/lsp_server/_diagnostics.py +62 -0
  54. intentumdiff/lsp_server/_handlers.py +175 -0
  55. intentumdiff/lsp_server/server.py +205 -0
  56. intentumdiff/plugins/__init__.py +1 -0
  57. intentumdiff/plugins/adapter.py +397 -0
  58. intentumdiff/plugins/builtins.py +323 -0
  59. intentumdiff/plugins/exceptions.py +93 -0
  60. intentumdiff/plugins/hub.py +1155 -0
  61. intentumdiff/plugins/language_metadata.py +250 -0
  62. intentumdiff/plugins/loader.py +1252 -0
  63. intentumdiff/plugins/registry.py +1079 -0
  64. intentumdiff/plugins/registry.schema.json +99 -0
  65. intentumdiff/plugins/registry_schema.py +280 -0
  66. intentumdiff/plugins/wit/plugin.wit +351 -0
  67. intentumdiff/rust_core.py +2370 -0
  68. intentumdiff/serve/__init__.py +19 -0
  69. intentumdiff/serve/_app.py +485 -0
  70. intentumdiff/serve/_static/index.html +326 -0
  71. intentumdiff/serve/_static/playground.js +567 -0
  72. intentumdiff/sources/__init__.py +1 -0
  73. intentumdiff/sources/base.py +28 -0
  74. intentumdiff/sources/file_source.py +48 -0
  75. intentumdiff/sources/git_source.py +375 -0
  76. intentumdiff/sources/live_buffer_source.py +113 -0
  77. intentumdiff/sources/patch_source.py +126 -0
  78. intentumdiff/sources/string_source.py +44 -0
  79. intentumdiff/testing.py +165 -0
  80. intentumdiff/vcs/__init__.py +37 -0
  81. intentumdiff/vcs/_safe_path.py +50 -0
  82. intentumdiff/vcs/base.py +136 -0
  83. intentumdiff/vcs/git_backend.py +97 -0
  84. intentumdiff/vcs/git_cli.py +100 -0
  85. intentumdiff/vcs/hg_backend.py +94 -0
  86. intentumdiff/vcs/perforce_backend.py +109 -0
  87. intentumdiff/vcs/svn_backend.py +105 -0
  88. intentumdiff/wasm/.gitkeep +2 -0
  89. intentumdiff/wasm/abap_parser.wasm +0 -0
  90. intentumdiff/wasm/adf_parser.wasm +0 -0
  91. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  92. intentumdiff/wasm/asm_parser.wasm +0 -0
  93. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  94. intentumdiff/wasm/astro_parser.wasm +0 -0
  95. intentumdiff/wasm/bash_parser.wasm +0 -0
  96. intentumdiff/wasm/clojure_parser.wasm +0 -0
  97. intentumdiff/wasm/cmake_parser.wasm +0 -0
  98. intentumdiff/wasm/cpp_parser.wasm +0 -0
  99. intentumdiff/wasm/csharp_parser.wasm +0 -0
  100. intentumdiff/wasm/css_parser.wasm +0 -0
  101. intentumdiff/wasm/dart_parser.wasm +0 -0
  102. intentumdiff/wasm/databricks_parser.wasm +0 -0
  103. intentumdiff/wasm/dax_parser.wasm +0 -0
  104. intentumdiff/wasm/delphi_parser.wasm +0 -0
  105. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  106. intentumdiff/wasm/elixir_parser.wasm +0 -0
  107. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  108. intentumdiff/wasm/generic_parser.wasm +0 -0
  109. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  110. intentumdiff/wasm/go_parser.wasm +0 -0
  111. intentumdiff/wasm/gomod_parser.wasm +0 -0
  112. intentumdiff/wasm/graphql_parser.wasm +0 -0
  113. intentumdiff/wasm/groovy_parser.wasm +0 -0
  114. intentumdiff/wasm/haskell_parser.wasm +0 -0
  115. intentumdiff/wasm/html_parser.wasm +0 -0
  116. intentumdiff/wasm/ini_parser.wasm +0 -0
  117. intentumdiff/wasm/java_parser.wasm +0 -0
  118. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  119. intentumdiff/wasm/json_parser.wasm +0 -0
  120. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  121. intentumdiff/wasm/latex_parser.wasm +0 -0
  122. intentumdiff/wasm/lua_parser.wasm +0 -0
  123. intentumdiff/wasm/make_parser.wasm +0 -0
  124. intentumdiff/wasm/markdown_parser.wasm +0 -0
  125. intentumdiff/wasm/mdx_parser.wasm +0 -0
  126. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  127. intentumdiff/wasm/odin_parser.wasm +0 -0
  128. intentumdiff/wasm/parser_manifest.json +590 -0
  129. intentumdiff/wasm/perl_parser.wasm +0 -0
  130. intentumdiff/wasm/php_parser.wasm +0 -0
  131. intentumdiff/wasm/plsql_parser.wasm +0 -0
  132. intentumdiff/wasm/po_parser.wasm +0 -0
  133. intentumdiff/wasm/postscript_parser.wasm +0 -0
  134. intentumdiff/wasm/powershell_parser.wasm +0 -0
  135. intentumdiff/wasm/proto_parser.wasm +0 -0
  136. intentumdiff/wasm/puppet_parser.wasm +0 -0
  137. intentumdiff/wasm/python_parser.wasm +0 -0
  138. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  139. intentumdiff/wasm/r_parser.wasm +0 -0
  140. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  141. intentumdiff/wasm/ruby_parser.wasm +0 -0
  142. intentumdiff/wasm/rust_parser.wasm +0 -0
  143. intentumdiff/wasm/sas_parser.wasm +0 -0
  144. intentumdiff/wasm/scala_parser.wasm +0 -0
  145. intentumdiff/wasm/scss_parser.wasm +0 -0
  146. intentumdiff/wasm/sql_parser.wasm +0 -0
  147. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  148. intentumdiff/wasm/svelte_parser.wasm +0 -0
  149. intentumdiff/wasm/swift_parser.wasm +0 -0
  150. intentumdiff/wasm/terraform_parser.wasm +0 -0
  151. intentumdiff/wasm/toml_parser.wasm +0 -0
  152. intentumdiff/wasm/tsql_parser.wasm +0 -0
  153. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  154. intentumdiff/wasm/vue_parser.wasm +0 -0
  155. intentumdiff/wasm/wat_parser.wasm +0 -0
  156. intentumdiff/wasm/xml_parser.wasm +0 -0
  157. intentumdiff/wasm/yaml_parser.wasm +0 -0
  158. intentumdiff/wasm/zig_parser.wasm +0 -0
  159. intentumdiff/watcher.py +259 -0
  160. intentumdiff_python-0.0.1.dist-info/METADATA +112 -0
  161. intentumdiff_python-0.0.1.dist-info/RECORD +165 -0
  162. intentumdiff_python-0.0.1.dist-info/WHEEL +4 -0
  163. intentumdiff_python-0.0.1.dist-info/entry_points.txt +80 -0
  164. intentumdiff_python-0.0.1.dist-info/licenses/LICENSE +21 -0
  165. intentumdiff_python-0.0.1.dist-info/sboms/intentumdiff-rust-core.cyclonedx.json +10968 -0
@@ -0,0 +1,599 @@
1
+ """
2
+ intentumdiff.core.commit_differ
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ ``CommitDiffer`` — per-commit semantic diff with cross-file analysis.
6
+
7
+ This class extends the single-file ``SemanticDiffer`` to work at the commit
8
+ level:
9
+
10
+ 1. Runs ``SemanticDiffer.diff_commit()`` to get per-file ``SemanticDiff``
11
+ objects for every changed file.
12
+ 2. Parses all changed files (old and new versions) into ``SemanticNode``
13
+ trees to build two ``SemanticIndex`` objects.
14
+ 3. Calls ``detect_cross_file_changes()`` (Rust core) to detect
15
+ ``MOVE_TO_MODULE`` / ``SPLIT_MODULE`` / ``CROSS_FILE_RENAME`` changes.
16
+ 4. Returns a ``CommitDiff`` with both the per-file diffs and the cross-file
17
+ changes.
18
+
19
+ Usage::
20
+
21
+ from intentumdiff import CommitDiffer, DiffConfig
22
+
23
+ differ = CommitDiffer(DiffConfig(detect_refactorings=True))
24
+ commit_diff = differ.diff_commit("/path/to/repo", "HEAD~1", "HEAD")
25
+
26
+ for file_diff in commit_diff.file_diffs:
27
+ print(file_diff.new_filename, file_diff.has_semantic_changes)
28
+
29
+ for cross_change in commit_diff.cross_file_changes:
30
+ print(cross_change.change_type, cross_change.symbol_name)
31
+ """
32
+
33
+ from __future__ import annotations
34
+
35
+ import json
36
+ import logging
37
+ import os
38
+ from dataclasses import dataclass
39
+ from typing import TYPE_CHECKING, Iterator
40
+
41
+ from intentumdiff.core.models import CommitDiff, CrossFileChange, DiffConfig, SemanticDiff
42
+ from intentumdiff.differ import SemanticDiffer
43
+ from intentumdiff.analysis.cross_file import detect_cross_file_changes
44
+ from intentumdiff.core.index import SemanticIndex
45
+ from intentumdiff.plugins.exceptions import PluginError, PluginNotFoundError
46
+ from intentumdiff.sources.git_source import iter_changed_sources
47
+ from intentumdiff.vcs.base import VcsBackend
48
+
49
+ if TYPE_CHECKING:
50
+ from intentumdiff.core.models import SemanticNode
51
+ from intentumdiff.plugins.registry import PluginRegistry
52
+
53
+ logger = logging.getLogger(__name__)
54
+
55
+
56
+ @dataclass
57
+ class FileDiffResult:
58
+ """A single per-file diff plus the raw contents needed for index building."""
59
+
60
+ file_diff: SemanticDiff
61
+ old_path: str
62
+ new_path: str
63
+ old_content: str
64
+ new_content: str
65
+
66
+
67
+ @dataclass
68
+ class FileDiffError:
69
+ """A per-file diff that could not be produced (no parser or pipeline failure)."""
70
+
71
+ old_path: str
72
+ new_path: str
73
+ reason: str
74
+ kind: str # "no_parser" | "pipeline_error"
75
+
76
+
77
+ class CommitDiffer:
78
+ """
79
+ Commit-level semantic differ with cross-file change detection.
80
+
81
+ Parameters
82
+ ----------
83
+ config:
84
+ Optional ``DiffConfig`` shared with the underlying ``SemanticDiffer``.
85
+ registry:
86
+ Optional pre-built ``PluginRegistry``. Useful in test environments.
87
+ """
88
+
89
+ def __init__(
90
+ self,
91
+ config: DiffConfig | None = None,
92
+ registry: "PluginRegistry | None" = None,
93
+ ) -> None:
94
+ self._differ = SemanticDiffer(config=config, registry=registry)
95
+
96
+ # ------------------------------------------------------------------
97
+ # Public API
98
+ # ------------------------------------------------------------------
99
+
100
+ def diff_commit(
101
+ self,
102
+ repo_path: str | os.PathLike[str] = ".",
103
+ old_ref: str = "HEAD",
104
+ new_ref: str = "",
105
+ *,
106
+ backend: VcsBackend | None = None,
107
+ ) -> CommitDiff:
108
+ """
109
+ Run a full semantic diff for a commit.
110
+
111
+ Parameters
112
+ ----------
113
+ repo_path:
114
+ Path to (any directory inside) the git repository. Ignored when
115
+ *backend* is provided.
116
+ old_ref:
117
+ Old VCS ref (git SHA, SVN revision, Perforce changelist, …).
118
+ Defaults to ``"HEAD"``.
119
+ new_ref:
120
+ New VCS ref. Defaults to ``""`` (git working tree — diff against
121
+ current unsaved/uncommitted files). Ignored when *backend* is
122
+ provided with a non-empty *new_ref*.
123
+ backend:
124
+ Optional :class:`~intentumdiff.vcs.base.VcsBackend` instance.
125
+ When provided, all VCS operations are delegated to *backend* and
126
+ the git-specific *repo_path* argument is ignored. When ``None``
127
+ (default), a :class:`~intentumdiff.vcs.git_backend.GitVcsBackend`
128
+ is created from *repo_path* for backward compatibility.
129
+
130
+ Returns
131
+ -------
132
+ CommitDiff
133
+ Per-file diffs + cross-file semantic changes.
134
+ """
135
+ if backend is not None:
136
+ return self._diff_with_backend(backend, old_ref, new_ref)
137
+
138
+ # Pre-pass: collect git-deletion paths and any new .gitignore content so
139
+ # we can tag SemanticDiff objects whose deletion was triggered by a .gitignore
140
+ # rule change rather than actual code removal.
141
+ deleted_paths, gitignore_spec = self._collect_gitignore_state(
142
+ repo_path, old_ref, new_ref
143
+ )
144
+
145
+ results, errors, changed_file_count = self._collect_file_diffs(
146
+ iter_changed_sources(repo_path, old_ref, new_ref)
147
+ )
148
+
149
+ return self._finalize_commit_diff(
150
+ results=results,
151
+ errors=errors,
152
+ changed_file_count=changed_file_count,
153
+ old_ref=old_ref,
154
+ new_ref=new_ref,
155
+ deleted_paths=deleted_paths,
156
+ gitignore_spec=gitignore_spec,
157
+ )
158
+
159
+ def iter_file_diffs(
160
+ self,
161
+ repo_path: str | os.PathLike[str] = ".",
162
+ old_ref: str = "HEAD",
163
+ new_ref: str = "",
164
+ ) -> Iterator[FileDiffResult | FileDiffError]:
165
+ """
166
+ Yield per-file diffs one at a time for progressive/streaming review.
167
+
168
+ Performs the gitignore pre-pass (so deletions are classifiable) and
169
+ then yields each changed source's :class:`FileDiffResult` (on success)
170
+ or :class:`FileDiffError` (when no parser is available or the pipeline
171
+ failed). Cross-file analysis is NOT performed here — callers that need
172
+ a complete :class:`CommitDiff` should collect the results and pass them
173
+ to :meth:`finalize_commit_diff`.
174
+
175
+ Always uses the git backend (the ``backend`` parameter of
176
+ :meth:`diff_commit` is not supported by this streaming entry point).
177
+ """
178
+ def _looks_binary(text: str) -> bool:
179
+ # Content is decoded with errors="replace"; a NUL byte (valid UTF-8
180
+ # U+0000) survives and reliably marks binary/image assets, as does a
181
+ # high ratio of U+FFFD replacement characters. Feeding such content to
182
+ # a text parser (the generic catch-all) explodes the CST — e.g. a PNG
183
+ # producing >100 MB of output that the plugin host then rejects.
184
+ head = text[:8192]
185
+ if not head:
186
+ return False
187
+ if "\x00" in head:
188
+ return True
189
+ return head.count("�") / len(head) > 0.1
190
+
191
+ for source in iter_changed_sources(repo_path, old_ref, new_ref):
192
+ old_content, new_content, old_path, new_path, staging_status = source
193
+ # Backstop: primary content-based routing happens at the git read
194
+ # boundary (magic-byte detection); this NUL-byte check catches any
195
+ # binary that reaches the streaming path through another route,
196
+ # before it explodes the text parser.
197
+ if _looks_binary(new_content) or _looks_binary(old_content):
198
+ logger.debug("Skipping %r — binary/non-text asset", old_path)
199
+ continue
200
+ try:
201
+ file_diff = self._differ._run_pipeline(
202
+ old_content, new_content, old_path, None, new_filename=new_path
203
+ )
204
+ if staging_status is not None:
205
+ file_diff = file_diff.model_copy(
206
+ update={"staging_status": staging_status}
207
+ )
208
+ yield FileDiffResult(
209
+ file_diff=file_diff,
210
+ old_path=old_path,
211
+ new_path=new_path or old_path,
212
+ old_content=old_content,
213
+ new_content=new_content,
214
+ )
215
+ except PluginNotFoundError:
216
+ logger.debug("Skipping %r — no parser available", old_path)
217
+ yield FileDiffError(
218
+ old_path=old_path,
219
+ new_path=new_path or old_path,
220
+ reason="no parser available",
221
+ kind="no_parser",
222
+ )
223
+ except (PluginError, ValueError, RuntimeError) as exc:
224
+ logger.warning("Failed to diff %r: %s", old_path, exc)
225
+ yield FileDiffError(
226
+ old_path=old_path,
227
+ new_path=new_path or old_path,
228
+ reason=str(exc),
229
+ kind="pipeline_error",
230
+ )
231
+
232
+ def finalize_commit_diff(
233
+ self,
234
+ results: list[FileDiffResult],
235
+ errors: list[FileDiffError],
236
+ *,
237
+ old_ref: str,
238
+ new_ref: str,
239
+ repo_path: str | os.PathLike[str] | None = None,
240
+ ) -> CommitDiff:
241
+ """
242
+ Build a complete :class:`CommitDiff` from streamed per-file results.
243
+
244
+ Applies the gitignore-deletion tagging (when *repo_path* is supplied so
245
+ the pre-pass can run), runs cross-file analysis, and assembles the
246
+ terminal ``CommitDiff``. This is the counterpart to
247
+ :meth:`iter_file_diffs`.
248
+ """
249
+ deleted_paths: set[str] = set()
250
+ gitignore_spec = None
251
+ if repo_path is not None:
252
+ deleted_paths, gitignore_spec = self._collect_gitignore_state(
253
+ repo_path, old_ref, new_ref
254
+ )
255
+ changed_file_count = len(results) + len(errors)
256
+ return self._finalize_commit_diff(
257
+ results=results,
258
+ errors=errors,
259
+ changed_file_count=changed_file_count,
260
+ old_ref=old_ref,
261
+ new_ref=new_ref,
262
+ deleted_paths=deleted_paths,
263
+ gitignore_spec=gitignore_spec,
264
+ )
265
+
266
+ def _collect_file_diffs(
267
+ self,
268
+ sources: Iterator[tuple[str, str, str, str, object]],
269
+ ) -> tuple[list[FileDiffResult], list[FileDiffError], int]:
270
+ """Run the per-file pipeline over an iterator of changed sources."""
271
+ results: list[FileDiffResult] = []
272
+ errors: list[FileDiffError] = []
273
+ changed_file_count = 0
274
+ for old_content, new_content, old_path, new_path, staging_status in sources:
275
+ changed_file_count += 1
276
+ try:
277
+ file_diff = self._differ._run_pipeline(
278
+ old_content, new_content, old_path, None, new_filename=new_path
279
+ )
280
+ if staging_status is not None:
281
+ file_diff = file_diff.model_copy(
282
+ update={"staging_status": staging_status}
283
+ )
284
+ results.append(
285
+ FileDiffResult(
286
+ file_diff=file_diff,
287
+ old_path=old_path,
288
+ new_path=new_path or old_path,
289
+ old_content=old_content,
290
+ new_content=new_content,
291
+ )
292
+ )
293
+ except PluginNotFoundError:
294
+ logger.debug("Skipping %r — no parser available", old_path)
295
+ errors.append(
296
+ FileDiffError(
297
+ old_path=old_path,
298
+ new_path=new_path or old_path,
299
+ reason="no parser available",
300
+ kind="no_parser",
301
+ )
302
+ )
303
+ except (PluginError, ValueError, RuntimeError) as exc:
304
+ logger.warning("Failed to diff %r: %s", old_path, exc)
305
+ errors.append(
306
+ FileDiffError(
307
+ old_path=old_path,
308
+ new_path=new_path or old_path,
309
+ reason=str(exc),
310
+ kind="pipeline_error",
311
+ )
312
+ )
313
+ return results, errors, changed_file_count
314
+
315
+ def _finalize_commit_diff(
316
+ self,
317
+ *,
318
+ results: list[FileDiffResult],
319
+ errors: list[FileDiffError],
320
+ changed_file_count: int,
321
+ old_ref: str,
322
+ new_ref: str,
323
+ deleted_paths: set[str] | None = None,
324
+ gitignore_spec=None,
325
+ ) -> CommitDiff:
326
+ """Assemble the terminal CommitDiff from per-file results + errors."""
327
+ file_diffs = [result.file_diff for result in results]
328
+ parse_errors = [
329
+ f"{error.old_path}: {error.reason}"
330
+ for error in errors
331
+ if error.kind == "pipeline_error"
332
+ ]
333
+
334
+ self._raise_if_all_parsers_failed(changed_file_count, file_diffs, parse_errors)
335
+
336
+ # Tag diffs whose file deletion was caused by a .gitignore rule addition.
337
+ if gitignore_spec is not None and deleted_paths:
338
+ file_diffs = [
339
+ fd.model_copy(update={"gitignore_excluded": True})
340
+ if fd.old_filename in deleted_paths
341
+ and gitignore_spec.match_file(fd.old_filename)
342
+ else fd
343
+ for fd in file_diffs
344
+ ]
345
+
346
+ # Build semantic indexes for changed files only.
347
+ old_file_contents = [
348
+ (result.old_path, result.file_diff.language, result.old_content)
349
+ for result in results
350
+ ]
351
+ new_file_contents = [
352
+ (result.new_path, result.file_diff.language, result.new_content)
353
+ for result in results
354
+ ]
355
+ cross_file_changes: list[CrossFileChange] = []
356
+ if old_file_contents:
357
+ old_index = self._build_index(old_file_contents)
358
+ new_index = self._build_index(new_file_contents)
359
+ if old_index is not None and new_index is not None:
360
+ cross_file_changes = self._detect_cross_file(old_index, new_index)
361
+
362
+ return CommitDiff(
363
+ old_ref=old_ref,
364
+ new_ref=new_ref,
365
+ guardrail_violations=[
366
+ violation
367
+ for file_diff in file_diffs
368
+ for violation in file_diff.guardrail_violations
369
+ ],
370
+ file_diffs=file_diffs,
371
+ cross_file_changes=cross_file_changes,
372
+ parse_errors=parse_errors,
373
+ )
374
+
375
+ # ------------------------------------------------------------------
376
+ # Internal helpers
377
+ # ------------------------------------------------------------------
378
+
379
+ def _diff_with_backend(
380
+ self,
381
+ backend: VcsBackend,
382
+ old_ref: str,
383
+ new_ref: str,
384
+ ) -> CommitDiff:
385
+ """
386
+ Run a full semantic diff using an arbitrary :class:`VcsBackend`.
387
+
388
+ Called by :meth:`diff_commit` when a *backend* is provided. The
389
+ gitignore pre-pass is skipped because it is git-specific.
390
+ """
391
+ changed_files = backend.list_changed_files(old_ref, new_ref)
392
+
393
+ file_diffs = []
394
+ parse_errors: list[str] = []
395
+ old_file_contents: list[tuple[str, str, str]] = []
396
+ new_file_contents: list[tuple[str, str, str]] = []
397
+ changed_file_count = 0
398
+
399
+ for cf in changed_files:
400
+ if cf.is_binary:
401
+ continue
402
+ changed_file_count += 1
403
+
404
+ old_path = cf.old_path or cf.new_path
405
+ new_path = cf.new_path or cf.old_path
406
+
407
+ if old_path is None:
408
+ continue
409
+
410
+ old_content = backend.get_blob(old_path, old_ref) if cf.old_path else ""
411
+ new_content = backend.get_blob(new_path, new_ref) if cf.new_path else "" # type: ignore[arg-type]
412
+
413
+ try:
414
+ file_diff = self._differ._run_pipeline(
415
+ old_content, new_content, old_path, None, new_filename=new_path
416
+ )
417
+ file_diffs.append(file_diff)
418
+ old_file_contents.append((old_path, file_diff.language, old_content))
419
+ new_file_contents.append(
420
+ (new_path or old_path, file_diff.language, new_content)
421
+ )
422
+ except PluginNotFoundError:
423
+ logger.debug("Skipping %r — no parser available", old_path)
424
+ except (PluginError, ValueError, RuntimeError) as exc:
425
+ logger.warning("Failed to diff %r: %s", old_path, exc)
426
+ parse_errors.append(f"{old_path}: {exc}")
427
+
428
+ self._raise_if_all_parsers_failed(changed_file_count, file_diffs, parse_errors)
429
+
430
+ cross_file_changes: list[CrossFileChange] = []
431
+ if old_file_contents:
432
+ old_index = self._build_index(old_file_contents)
433
+ new_index = self._build_index(new_file_contents)
434
+ if old_index is not None and new_index is not None:
435
+ cross_file_changes = self._detect_cross_file(old_index, new_index)
436
+
437
+ return CommitDiff(
438
+ old_ref=old_ref,
439
+ new_ref=new_ref,
440
+ guardrail_violations=[
441
+ violation
442
+ for file_diff in file_diffs
443
+ for violation in file_diff.guardrail_violations
444
+ ],
445
+ file_diffs=file_diffs,
446
+ cross_file_changes=cross_file_changes,
447
+ parse_errors=parse_errors,
448
+ )
449
+
450
+ def _raise_if_all_parsers_failed(
451
+ self,
452
+ changed_file_count: int,
453
+ file_diffs: list,
454
+ parse_errors: list[str],
455
+ ) -> None:
456
+ """Fail loudly when changed files exist but no parser plugin loaded."""
457
+ if changed_file_count <= 0 or file_diffs or parse_errors:
458
+ return
459
+ summary_fn = getattr(self._differ._registry, "parser_load_failure_summary", None)
460
+ if not callable(summary_fn):
461
+ return
462
+ summary = summary_fn()
463
+ if summary:
464
+ raise RuntimeError(summary)
465
+
466
+ def _collect_gitignore_state(
467
+ self,
468
+ repo_path: "str | os.PathLike[str]",
469
+ old_ref: str,
470
+ new_ref: str,
471
+ ) -> "tuple[set[str], object]":
472
+ """
473
+ Lightweight pre-scan of the commit diff.
474
+
475
+ Returns a 2-tuple ``(deleted_paths, gitignore_spec)``:
476
+
477
+ * ``deleted_paths`` — set of file paths that git marks as deleted (change
478
+ type ``"D"``). Used later to distinguish genuine deletions from files
479
+ evicted by a ``.gitignore`` rule change.
480
+ * ``gitignore_spec`` — a compiled ``pathspec.PathSpec`` built from the
481
+ *new* content of any ``.gitignore`` file modified in this commit, or
482
+ ``None`` when no ``.gitignore`` changed. Only root-level and
483
+ subdirectory ``.gitignore`` files that appear in the commit diff are
484
+ considered; ``.git/info/exclude`` is outside the commit tree and is
485
+ therefore ignored.
486
+ """
487
+ deleted_paths: set[str] = set()
488
+ gitignore_spec = None
489
+ try:
490
+ import subprocess
491
+
492
+ from intentumdiff.sources.git_source import _parse_name_status_z
493
+ from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
494
+
495
+ root = resolve_repo_root(repo_path)
496
+ # old -> new (or old -> working tree when new_ref is ""); a=old, b=new,
497
+ # matching the former old_commit.diff(None|new_commit).
498
+ args = ["diff", "--name-status", "-z", old_ref]
499
+ if new_ref:
500
+ args.append(new_ref)
501
+ for code, a_path, b_path in _parse_name_status_z(run_git_bytes(root, args)):
502
+ if code[:1] == "D":
503
+ deleted_paths.add(a_path)
504
+
505
+ # Capture the new .gitignore content for root or subdir gitignore files.
506
+ if code[:1] in ("A", "M") and (
507
+ b_path == ".gitignore" or b_path.endswith("/.gitignore")
508
+ ):
509
+ raw: str | None = None
510
+ if new_ref: # commit-to-commit: read the new blob
511
+ try:
512
+ raw = run_git_bytes(
513
+ root, ["cat-file", "blob", f"{new_ref}:{b_path}"]
514
+ ).decode("utf-8", errors="replace")
515
+ except subprocess.CalledProcessError:
516
+ raw = None
517
+ else: # working-tree mode: read from disk
518
+ from pathlib import Path as _Path
519
+ disk_gi = _Path(root) / b_path
520
+ if disk_gi.exists():
521
+ try:
522
+ raw = disk_gi.read_text(encoding="utf-8", errors="replace")
523
+ except OSError:
524
+ pass
525
+ if raw is not None:
526
+ try:
527
+ import pathspec as _pathspec # lazy import
528
+ # For subdirectory gitignore files (e.g. src/.gitignore),
529
+ # prepend the directory so patterns are matched against
530
+ # full repo-relative paths.
531
+ gitignore_dir = b_path[: -len(".gitignore")].rstrip("/")
532
+ if gitignore_dir:
533
+ lines = [
534
+ f"{gitignore_dir}/{ln}" if ln and not ln.startswith("#")
535
+ else ln
536
+ for ln in raw.splitlines()
537
+ ]
538
+ else:
539
+ lines = raw.splitlines()
540
+ gitignore_spec = _pathspec.PathSpec.from_lines("gitignore", lines)
541
+ except Exception as exc: # pragma: no cover
542
+ logger.debug("Failed to parse .gitignore blob: %s", exc)
543
+ except Exception as exc:
544
+ logger.debug("gitignore pre-pass failed: %s", exc)
545
+ return deleted_paths, gitignore_spec
546
+
547
+ def _build_index(
548
+ self, file_contents: list[tuple[str, str, str]]
549
+ ) -> SemanticIndex | None:
550
+ """
551
+ Build a ``SemanticIndex`` from a list of (filename, language, content)
552
+ triples.
553
+
554
+ The symbol/reference tables are built by the Rust core
555
+ (index-engine-lib) inside ``SemanticIndex.build()``. Returns ``None`` if
556
+ no files could be parsed.
557
+ """
558
+ index = SemanticIndex()
559
+ for filename, language, content in file_contents:
560
+ try:
561
+ tree = self._parse_to_tree(filename, language, content)
562
+ if tree is not None:
563
+ index.add_tree(filename, language, tree)
564
+ except Exception as exc:
565
+ logger.debug("Could not parse %r for index: %s", filename, exc)
566
+
567
+ if not index._files: # type: ignore[attr-defined] # pylint: disable=protected-access
568
+ return None
569
+
570
+ index.build()
571
+ return index
572
+
573
+ def _detect_cross_file(
574
+ self, old_index: SemanticIndex, new_index: SemanticIndex
575
+ ) -> list[CrossFileChange]:
576
+ """Detect cross-file changes between two indexes via the Rust core."""
577
+ return detect_cross_file_changes(old_index, new_index)
578
+
579
+ def _parse_to_tree(
580
+ self, filename: str, language: str, content: str
581
+ ) -> "SemanticNode | None":
582
+ """
583
+ Parse *content* to a SemanticNode tree.
584
+
585
+ Re-uses the ``SemanticDiffer`` parser pipeline so both FullParse and
586
+ host-CST parser plugins can participate in commit-wide symbol indexing.
587
+ """
588
+ try:
589
+ tree, _language = self._differ.parse(
590
+ content,
591
+ filename,
592
+ language_hint=language,
593
+ )
594
+ return tree
595
+ except PluginNotFoundError:
596
+ return None
597
+ except Exception as exc:
598
+ logger.debug("parse_to_tree failed for %r: %s", filename, exc)
599
+ return None
@@ -0,0 +1,51 @@
1
+ """Project configuration loading for ``intentumdiff.yaml`` (Rust-authoritative, #99).
2
+
3
+ The file walk, YAML parse, and ``config``-section validation run in the Rust core
4
+ (``config.rs`` via ``load_project_diff_config_json`` / ``find_intentumdiff_config_path``).
5
+ Python keeps only the public API surface and the ``DiffConfig`` DTO construction, so
6
+ every binding resolves project config identically.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from pathlib import Path
12
+
13
+ from intentumdiff.core.models import DiffConfig
14
+
15
+ INTENTUMDIFF_CONFIG_FILENAME = "intentumdiff.yaml"
16
+
17
+
18
+ def load_project_diff_config(
19
+ start_path: str | Path | None = None,
20
+ *,
21
+ explicit_path: str | Path | None = None,
22
+ ) -> DiffConfig:
23
+ """Load ``DiffConfig`` defaults from the nearest ``intentumdiff.yaml``.
24
+
25
+ The project file uses a top-level ``config`` mapping for diff tuning and a
26
+ separate ``guardrails`` mapping for protected semantic paths. Missing files or
27
+ missing ``config`` sections return ``DiffConfig()``; an unsupported ``config``
28
+ key raises ``ValueError`` (from the Rust core).
29
+ """
30
+ from intentumdiff.rust_core import load_project_diff_config_data
31
+
32
+ data = load_project_diff_config_data(
33
+ None if start_path is None else str(start_path),
34
+ None if explicit_path is None else str(explicit_path),
35
+ )
36
+ return DiffConfig(**data)
37
+
38
+
39
+ def find_intentumdiff_config(
40
+ start_path: str | Path | None = None,
41
+ *,
42
+ explicit_path: str | Path | None = None,
43
+ ) -> Path | None:
44
+ """Return the nearest ``intentumdiff.yaml`` from *start_path* upward (Rust core)."""
45
+ from intentumdiff.rust_core import resolve_intentumdiff_config_path
46
+
47
+ resolved = resolve_intentumdiff_config_path(
48
+ None if start_path is None else str(start_path),
49
+ None if explicit_path is None else str(explicit_path),
50
+ )
51
+ return Path(resolved) if resolved is not None else None