intentumdiff-python 0.0.1__py3-none-macosx_11_0_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.
- intentumdiff/__init__.py +134 -0
- intentumdiff/_differ_gate.py +210 -0
- intentumdiff/_differ_presentation.py +786 -0
- intentumdiff/_differ_runtime.py +320 -0
- intentumdiff/analysis/__init__.py +1 -0
- intentumdiff/analysis/compile_commands.py +151 -0
- intentumdiff/analysis/cross_file.py +71 -0
- intentumdiff/analysis/diagnostics.py +309 -0
- intentumdiff/analysis/guardrail_reports.py +254 -0
- intentumdiff/analysis/guardrails.py +318 -0
- intentumdiff/analysis/invariances.py +1207 -0
- intentumdiff/analysis/keyed_profiles.py +25 -0
- intentumdiff/analysis/resource_profiles.py +14 -0
- intentumdiff/analysis/schema_resolver.py +816 -0
- intentumdiff/analysis/text_review.py +46 -0
- intentumdiff/analysis/user_schemas.py +430 -0
- intentumdiff/assets.py +45 -0
- intentumdiff/cache/__init__.py +37 -0
- intentumdiff/cache/duckdb_store.py +98 -0
- intentumdiff/cache/sqlite_store.py +149 -0
- intentumdiff/cache/store.py +190 -0
- intentumdiff/cli/__init__.py +199 -0
- intentumdiff/cli/_commands.py +2141 -0
- intentumdiff/cli/_parser.py +1061 -0
- intentumdiff/cli/_shared.py +644 -0
- intentumdiff/content_type.py +59 -0
- intentumdiff/core/__init__.py +1 -0
- intentumdiff/core/commit_differ.py +599 -0
- intentumdiff/core/config.py +51 -0
- intentumdiff/core/cst_serializer.py +101 -0
- intentumdiff/core/diffignore.py +87 -0
- intentumdiff/core/index.py +189 -0
- intentumdiff/core/indexer.py +792 -0
- intentumdiff/core/models.py +837 -0
- intentumdiff/differ.py +2195 -0
- intentumdiff/github_app.py +287 -0
- intentumdiff/intentumdiff_rust_core/__init__.py +7 -0
- intentumdiff/intentumdiff_rust_core/ffi.py +8 -0
- intentumdiff/intentumdiff_rust_core/libintentumdiff_rust_core.dylib +0 -0
- intentumdiff/invariances/__init__.py +2 -0
- intentumdiff/invariances/rules.schema.json +283 -0
- intentumdiff/invariances/rules.yaml +349 -0
- intentumdiff/live_server.py +1314 -0
- intentumdiff/lsp/__init__.py +67 -0
- intentumdiff/lsp/client.py +544 -0
- intentumdiff/lsp/config.py +96 -0
- intentumdiff/lsp/enricher.py +227 -0
- intentumdiff/lsp/exceptions.py +27 -0
- intentumdiff/lsp/launcher.py +273 -0
- intentumdiff/lsp/servers.py +356 -0
- intentumdiff/lsp_server/__init__.py +14 -0
- intentumdiff/lsp_server/_codelens.py +67 -0
- intentumdiff/lsp_server/_diagnostics.py +62 -0
- intentumdiff/lsp_server/_handlers.py +175 -0
- intentumdiff/lsp_server/server.py +205 -0
- intentumdiff/plugins/__init__.py +1 -0
- intentumdiff/plugins/adapter.py +397 -0
- intentumdiff/plugins/builtins.py +323 -0
- intentumdiff/plugins/exceptions.py +93 -0
- intentumdiff/plugins/hub.py +1155 -0
- intentumdiff/plugins/language_metadata.py +250 -0
- intentumdiff/plugins/loader.py +1252 -0
- intentumdiff/plugins/registry.py +1079 -0
- intentumdiff/plugins/registry.schema.json +99 -0
- intentumdiff/plugins/registry_schema.py +280 -0
- intentumdiff/plugins/wit/plugin.wit +351 -0
- intentumdiff/rust_core.py +2370 -0
- intentumdiff/serve/__init__.py +19 -0
- intentumdiff/serve/_app.py +485 -0
- intentumdiff/serve/_static/index.html +326 -0
- intentumdiff/serve/_static/playground.js +567 -0
- intentumdiff/sources/__init__.py +1 -0
- intentumdiff/sources/base.py +28 -0
- intentumdiff/sources/file_source.py +48 -0
- intentumdiff/sources/git_source.py +375 -0
- intentumdiff/sources/live_buffer_source.py +113 -0
- intentumdiff/sources/patch_source.py +126 -0
- intentumdiff/sources/string_source.py +44 -0
- intentumdiff/testing.py +165 -0
- intentumdiff/vcs/__init__.py +37 -0
- intentumdiff/vcs/_safe_path.py +50 -0
- intentumdiff/vcs/base.py +136 -0
- intentumdiff/vcs/git_backend.py +97 -0
- intentumdiff/vcs/git_cli.py +100 -0
- intentumdiff/vcs/hg_backend.py +94 -0
- intentumdiff/vcs/perforce_backend.py +109 -0
- intentumdiff/vcs/svn_backend.py +105 -0
- intentumdiff/wasm/.gitkeep +2 -0
- intentumdiff/wasm/abap_parser.wasm +0 -0
- intentumdiff/wasm/adf_parser.wasm +0 -0
- intentumdiff/wasm/asciidoc_parser.wasm +0 -0
- intentumdiff/wasm/asm_parser.wasm +0 -0
- intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
- intentumdiff/wasm/astro_parser.wasm +0 -0
- intentumdiff/wasm/bash_parser.wasm +0 -0
- intentumdiff/wasm/clojure_parser.wasm +0 -0
- intentumdiff/wasm/cmake_parser.wasm +0 -0
- intentumdiff/wasm/cpp_parser.wasm +0 -0
- intentumdiff/wasm/csharp_parser.wasm +0 -0
- intentumdiff/wasm/css_parser.wasm +0 -0
- intentumdiff/wasm/dart_parser.wasm +0 -0
- intentumdiff/wasm/databricks_parser.wasm +0 -0
- intentumdiff/wasm/dax_parser.wasm +0 -0
- intentumdiff/wasm/delphi_parser.wasm +0 -0
- intentumdiff/wasm/dockerfile_parser.wasm +0 -0
- intentumdiff/wasm/elixir_parser.wasm +0 -0
- intentumdiff/wasm/freebasic_parser.wasm +0 -0
- intentumdiff/wasm/generic_parser.wasm +0 -0
- intentumdiff/wasm/gitignore_parser.wasm +0 -0
- intentumdiff/wasm/go_parser.wasm +0 -0
- intentumdiff/wasm/gomod_parser.wasm +0 -0
- intentumdiff/wasm/graphql_parser.wasm +0 -0
- intentumdiff/wasm/groovy_parser.wasm +0 -0
- intentumdiff/wasm/haskell_parser.wasm +0 -0
- intentumdiff/wasm/html_parser.wasm +0 -0
- intentumdiff/wasm/ini_parser.wasm +0 -0
- intentumdiff/wasm/java_parser.wasm +0 -0
- intentumdiff/wasm/js_ts_parser.wasm +0 -0
- intentumdiff/wasm/json_parser.wasm +0 -0
- intentumdiff/wasm/kotlin_parser.wasm +0 -0
- intentumdiff/wasm/latex_parser.wasm +0 -0
- intentumdiff/wasm/lua_parser.wasm +0 -0
- intentumdiff/wasm/make_parser.wasm +0 -0
- intentumdiff/wasm/markdown_parser.wasm +0 -0
- intentumdiff/wasm/mdx_parser.wasm +0 -0
- intentumdiff/wasm/ocaml_parser.wasm +0 -0
- intentumdiff/wasm/odin_parser.wasm +0 -0
- intentumdiff/wasm/parser_manifest.json +590 -0
- intentumdiff/wasm/perl_parser.wasm +0 -0
- intentumdiff/wasm/php_parser.wasm +0 -0
- intentumdiff/wasm/plsql_parser.wasm +0 -0
- intentumdiff/wasm/po_parser.wasm +0 -0
- intentumdiff/wasm/postscript_parser.wasm +0 -0
- intentumdiff/wasm/powershell_parser.wasm +0 -0
- intentumdiff/wasm/proto_parser.wasm +0 -0
- intentumdiff/wasm/puppet_parser.wasm +0 -0
- intentumdiff/wasm/python_parser.wasm +0 -0
- intentumdiff/wasm/qsharp_parser.wasm +0 -0
- intentumdiff/wasm/r_parser.wasm +0 -0
- intentumdiff/wasm/reasonml_parser.wasm +0 -0
- intentumdiff/wasm/ruby_parser.wasm +0 -0
- intentumdiff/wasm/rust_parser.wasm +0 -0
- intentumdiff/wasm/sas_parser.wasm +0 -0
- intentumdiff/wasm/scala_parser.wasm +0 -0
- intentumdiff/wasm/scss_parser.wasm +0 -0
- intentumdiff/wasm/sql_parser.wasm +0 -0
- intentumdiff/wasm/squirrel_parser.wasm +0 -0
- intentumdiff/wasm/svelte_parser.wasm +0 -0
- intentumdiff/wasm/swift_parser.wasm +0 -0
- intentumdiff/wasm/terraform_parser.wasm +0 -0
- intentumdiff/wasm/toml_parser.wasm +0 -0
- intentumdiff/wasm/tsql_parser.wasm +0 -0
- intentumdiff/wasm/vbnet_parser.wasm +0 -0
- intentumdiff/wasm/vue_parser.wasm +0 -0
- intentumdiff/wasm/wat_parser.wasm +0 -0
- intentumdiff/wasm/xml_parser.wasm +0 -0
- intentumdiff/wasm/yaml_parser.wasm +0 -0
- intentumdiff/wasm/zig_parser.wasm +0 -0
- intentumdiff/watcher.py +259 -0
- intentumdiff_python-0.0.1.dist-info/METADATA +112 -0
- intentumdiff_python-0.0.1.dist-info/RECORD +165 -0
- intentumdiff_python-0.0.1.dist-info/WHEEL +4 -0
- intentumdiff_python-0.0.1.dist-info/entry_points.txt +80 -0
- intentumdiff_python-0.0.1.dist-info/licenses/LICENSE +21 -0
- intentumdiff_python-0.0.1.dist-info/sboms/intentumdiff-rust-core.cyclonedx.json +10968 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"""
|
|
2
|
+
intentumdiff.sources.git_source
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Retrieve file content from git commits using GitPython.
|
|
6
|
+
|
|
7
|
+
Security
|
|
8
|
+
────────
|
|
9
|
+
All file paths are validated against the repository root to prevent
|
|
10
|
+
path-traversal attacks before any I/O is performed.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
import subprocess
|
|
17
|
+
from collections.abc import Iterator
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from intentumdiff import content_type as _content_type
|
|
21
|
+
from intentumdiff.sources.base import Source
|
|
22
|
+
from intentumdiff.vcs._safe_path import safe_relative_path, safe_working_tree_path
|
|
23
|
+
from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _decode_text_or_none(data: bytes) -> str | None:
|
|
27
|
+
"""Decode *data* as text, or return ``None`` when it is a binary/image asset.
|
|
28
|
+
|
|
29
|
+
Uses the Rust magic-byte content-type detector (not the filename) so binary
|
|
30
|
+
assets are never fed to the text engine — a PNG parsed as text explodes the
|
|
31
|
+
CST past the plugin output limit.
|
|
32
|
+
"""
|
|
33
|
+
if not _content_type.is_text_bytes(data):
|
|
34
|
+
return None
|
|
35
|
+
return data.decode("utf-8", errors="replace")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _safe_file_path(file_path: str) -> str:
|
|
39
|
+
"""
|
|
40
|
+
Validate that ``file_path`` does not escape the repository root.
|
|
41
|
+
|
|
42
|
+
Returns the normalised posix-style path relative to the repo root.
|
|
43
|
+
Raises ``ValueError`` on traversal attempts.
|
|
44
|
+
"""
|
|
45
|
+
return safe_relative_path(file_path)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Change-type codes that produce binary-safe text blobs on both sides.
|
|
49
|
+
_TEXT_CHANGE_TYPES = frozenset({"A", "D", "M", "R", "C"})
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# Sentinel new_ref values recognised by iter_changed_sources.
|
|
53
|
+
_REF_STAGED = ":staged" # compare HEAD to the git index (staged files only)
|
|
54
|
+
_REF_UNPUSHED = ":unpushed" # compare remote tracking branch to HEAD (unpushed commits)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
_ChangedSource = tuple[str, str, str, str, str | None]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _is_python_path(path: str) -> bool:
|
|
61
|
+
return path.lower().endswith((".py", ".pyi"))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _decode_git_path(path: bytes) -> str:
|
|
65
|
+
return path.decode("utf-8", errors="replace")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _untracked_paths(repo_path: str) -> list[str]:
|
|
69
|
+
data = run_git_bytes(repo_path, ["ls-files", "--others", "--exclude-standard", "-z"])
|
|
70
|
+
return sorted(_decode_git_path(token) for token in data.split(b"\0") if token)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _read_working_tree_text(path: Path) -> str | None:
|
|
74
|
+
try:
|
|
75
|
+
data = path.read_bytes()
|
|
76
|
+
except OSError:
|
|
77
|
+
return None
|
|
78
|
+
# Content-based routing: binary/image assets are skipped (reviewed via the
|
|
79
|
+
# perceptual asset diff), never decoded and sent to the text engine.
|
|
80
|
+
return _decode_text_or_none(data)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _parse_name_status_z(data: bytes) -> list[tuple[str, str, str]]:
|
|
84
|
+
tokens = [token for token in data.split(b"\0") if token]
|
|
85
|
+
entries: list[tuple[str, str, str]] = []
|
|
86
|
+
idx = 0
|
|
87
|
+
while idx < len(tokens):
|
|
88
|
+
status = tokens[idx].decode("ascii", errors="replace")
|
|
89
|
+
idx += 1
|
|
90
|
+
code = status[:1]
|
|
91
|
+
if code in {"R", "C"}:
|
|
92
|
+
if idx + 1 >= len(tokens):
|
|
93
|
+
break
|
|
94
|
+
old_path = _decode_git_path(tokens[idx])
|
|
95
|
+
new_path = _decode_git_path(tokens[idx + 1])
|
|
96
|
+
idx += 2
|
|
97
|
+
else:
|
|
98
|
+
if idx >= len(tokens):
|
|
99
|
+
break
|
|
100
|
+
old_path = _decode_git_path(tokens[idx])
|
|
101
|
+
new_path = old_path
|
|
102
|
+
idx += 1
|
|
103
|
+
entries.append((code, old_path, new_path))
|
|
104
|
+
return entries
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _read_commit_paths_batch(
|
|
108
|
+
repo_path: str,
|
|
109
|
+
rev: str,
|
|
110
|
+
paths: list[str],
|
|
111
|
+
) -> dict[str, str]:
|
|
112
|
+
unique_paths = list(dict.fromkeys(paths))
|
|
113
|
+
if not unique_paths:
|
|
114
|
+
return {}
|
|
115
|
+
if any("\n" in path or "\r" in path for path in unique_paths):
|
|
116
|
+
raise ValueError("git cat-file batch path contains a newline")
|
|
117
|
+
|
|
118
|
+
request = "".join(f"{rev}:{path}\n" for path in unique_paths).encode("utf-8")
|
|
119
|
+
output = run_git_bytes(repo_path, ["cat-file", "--batch"], input_bytes=request)
|
|
120
|
+
result: dict[str, str] = {}
|
|
121
|
+
pos = 0
|
|
122
|
+
for path in unique_paths:
|
|
123
|
+
header_end = output.find(b"\n", pos)
|
|
124
|
+
if header_end < 0:
|
|
125
|
+
raise ValueError("malformed git cat-file batch output")
|
|
126
|
+
header = output[pos:header_end]
|
|
127
|
+
pos = header_end + 1
|
|
128
|
+
if header.endswith(b" missing"):
|
|
129
|
+
result[path] = ""
|
|
130
|
+
continue
|
|
131
|
+
parts = header.split()
|
|
132
|
+
if len(parts) < 3:
|
|
133
|
+
raise ValueError("malformed git cat-file batch header")
|
|
134
|
+
try:
|
|
135
|
+
size = int(parts[2])
|
|
136
|
+
except ValueError as exc:
|
|
137
|
+
raise ValueError("malformed git cat-file batch size") from exc
|
|
138
|
+
content = output[pos : pos + size]
|
|
139
|
+
if len(content) != size:
|
|
140
|
+
raise ValueError("truncated git cat-file batch content")
|
|
141
|
+
pos += size
|
|
142
|
+
if pos < len(output) and output[pos : pos + 1] == b"\n":
|
|
143
|
+
pos += 1
|
|
144
|
+
result[path] = content.decode("utf-8", errors="replace")
|
|
145
|
+
return result
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def collect_working_tree_python_sources_fast(
|
|
149
|
+
repo_path: "str | os.PathLike[str]",
|
|
150
|
+
old_ref: str,
|
|
151
|
+
) -> "tuple[list[_ChangedSource], str | None] | None":
|
|
152
|
+
"""
|
|
153
|
+
Fast working-tree collector for the certified Rust Python batch path.
|
|
154
|
+
|
|
155
|
+
Returns ``None`` when the conservative Git CLI path cannot be used and the
|
|
156
|
+
caller should fall back to ``iter_changed_sources``. A non-empty fallback
|
|
157
|
+
reason means the diff contains a non-Python changed file, so the certified
|
|
158
|
+
Python-only path must not run.
|
|
159
|
+
"""
|
|
160
|
+
try:
|
|
161
|
+
working_dir = resolve_repo_root(repo_path)
|
|
162
|
+
# git resolves the ref on each call; using it directly (vs a pre-resolved
|
|
163
|
+
# sha) is equivalent here and avoids an extra round-trip.
|
|
164
|
+
old_rev = old_ref
|
|
165
|
+
diff_output = run_git_bytes(working_dir, ["diff", "--name-status", "-z", old_rev])
|
|
166
|
+
entries = [
|
|
167
|
+
entry
|
|
168
|
+
for entry in _parse_name_status_z(diff_output)
|
|
169
|
+
if entry[0] in _TEXT_CHANGE_TYPES
|
|
170
|
+
]
|
|
171
|
+
untracked_paths = _untracked_paths(working_dir)
|
|
172
|
+
if not entries and not untracked_paths:
|
|
173
|
+
return [], None
|
|
174
|
+
for _code, old_path, new_path in entries:
|
|
175
|
+
if not (_is_python_path(old_path) or _is_python_path(new_path)):
|
|
176
|
+
return (
|
|
177
|
+
[],
|
|
178
|
+
"certified commit JSON requires all changed files to be Python",
|
|
179
|
+
)
|
|
180
|
+
for untracked_path in untracked_paths:
|
|
181
|
+
if not _is_python_path(untracked_path):
|
|
182
|
+
return (
|
|
183
|
+
[],
|
|
184
|
+
"certified commit JSON requires all changed files to be Python",
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
staged_output = run_git_bytes(
|
|
188
|
+
working_dir,
|
|
189
|
+
["diff", "--cached", "--name-only", "-z", old_rev],
|
|
190
|
+
)
|
|
191
|
+
staged_paths = {
|
|
192
|
+
_decode_git_path(token)
|
|
193
|
+
for token in staged_output.split(b"\0")
|
|
194
|
+
if token
|
|
195
|
+
}
|
|
196
|
+
old_paths = [old_path for code, old_path, _new_path in entries if code != "A"]
|
|
197
|
+
old_contents = _read_commit_paths_batch(working_dir, old_rev, old_paths)
|
|
198
|
+
sources: list[_ChangedSource] = []
|
|
199
|
+
root = Path(working_dir)
|
|
200
|
+
for code, old_path, new_path in entries:
|
|
201
|
+
_safe_file_path(old_path)
|
|
202
|
+
_safe_file_path(new_path)
|
|
203
|
+
old_content = "" if code == "A" else old_contents.get(old_path, "")
|
|
204
|
+
if code == "D":
|
|
205
|
+
new_content = ""
|
|
206
|
+
else:
|
|
207
|
+
disk_path = safe_working_tree_path(root, new_path)
|
|
208
|
+
if not disk_path.exists():
|
|
209
|
+
new_content = ""
|
|
210
|
+
else:
|
|
211
|
+
new_content = disk_path.read_text(
|
|
212
|
+
encoding="utf-8",
|
|
213
|
+
errors="replace",
|
|
214
|
+
)
|
|
215
|
+
staging_status = (
|
|
216
|
+
"staged"
|
|
217
|
+
if old_path in staged_paths or new_path in staged_paths
|
|
218
|
+
else "unstaged"
|
|
219
|
+
)
|
|
220
|
+
sources.append((old_content, new_content, old_path, new_path, staging_status))
|
|
221
|
+
for untracked_path in untracked_paths:
|
|
222
|
+
_safe_file_path(untracked_path)
|
|
223
|
+
disk_path = safe_working_tree_path(root, untracked_path)
|
|
224
|
+
new_content = _read_working_tree_text(disk_path)
|
|
225
|
+
if new_content is None:
|
|
226
|
+
continue
|
|
227
|
+
sources.append(("", new_content, untracked_path, untracked_path, "untracked"))
|
|
228
|
+
return sources, None
|
|
229
|
+
except (OSError, subprocess.SubprocessError, ValueError):
|
|
230
|
+
return None
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def iter_changed_sources(
|
|
234
|
+
repo_path: "str | os.PathLike[str]",
|
|
235
|
+
old_ref: str,
|
|
236
|
+
new_ref: str = "",
|
|
237
|
+
) -> "Iterator[tuple[str, str, str, str, str | None]]":
|
|
238
|
+
"""
|
|
239
|
+
Yield ``(old_content, new_content, old_path, new_path, staging_status)``
|
|
240
|
+
for every text file that changed between *old_ref* and *new_ref*.
|
|
241
|
+
|
|
242
|
+
Special *new_ref* sentinels:
|
|
243
|
+
* ``""`` (default) — working tree: all saved changes, annotated as
|
|
244
|
+
``"staged"`` or ``"unstaged"`` per file.
|
|
245
|
+
* ``":staged"`` — only files in the git index (staged changes).
|
|
246
|
+
* ``":unpushed"`` — commits on the current branch not yet pushed to the
|
|
247
|
+
remote tracking branch (*old_ref* is ignored in this mode).
|
|
248
|
+
|
|
249
|
+
For regular commit-to-commit diffs, ``staging_status`` is ``None``.
|
|
250
|
+
|
|
251
|
+
Added files have ``old_content == ""`` and ``old_path == new_path``.
|
|
252
|
+
Deleted files have ``new_content == ""`` and ``new_path == old_path``.
|
|
253
|
+
Binary blobs and change types that cannot be diffed semantically are
|
|
254
|
+
silently skipped.
|
|
255
|
+
"""
|
|
256
|
+
# All four modes (working-tree "", ":staged", ":unpushed", commit-to-commit) are
|
|
257
|
+
# dispatched by the shared Rust core (#98, A2.4.2) — one entrypoint every binding
|
|
258
|
+
# shares. resolve_repo_root validates the path (NotAGitRepositoryError) and yields
|
|
259
|
+
# the work-tree root the core operates on.
|
|
260
|
+
from intentumdiff import rust_core
|
|
261
|
+
|
|
262
|
+
root = resolve_repo_root(repo_path)
|
|
263
|
+
yield from rust_core.changed_sources(root, old_ref, new_ref)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class GitSource(Source):
|
|
267
|
+
"""
|
|
268
|
+
Compare a file between two git refs (commits, tags, or branch names).
|
|
269
|
+
|
|
270
|
+
Parameters
|
|
271
|
+
----------
|
|
272
|
+
repo_path:
|
|
273
|
+
Path to the git repository (any directory inside it works — GitPython
|
|
274
|
+
will locate the root).
|
|
275
|
+
file_path:
|
|
276
|
+
Path to the file relative to the repository root, using forward
|
|
277
|
+
slashes (e.g. ``"src/foo.py"``).
|
|
278
|
+
old_ref:
|
|
279
|
+
Old git ref (commit SHA, tag, branch). Defaults to ``"HEAD~1"``.
|
|
280
|
+
new_ref:
|
|
281
|
+
New git ref. Defaults to ``"HEAD"``.
|
|
282
|
+
language_hint:
|
|
283
|
+
Optional language override; if omitted the plugin registry detects it.
|
|
284
|
+
"""
|
|
285
|
+
|
|
286
|
+
def __init__(
|
|
287
|
+
self,
|
|
288
|
+
repo_path: str | os.PathLike[str],
|
|
289
|
+
file_path: str,
|
|
290
|
+
old_ref: str = "HEAD~1",
|
|
291
|
+
new_ref: str = "HEAD",
|
|
292
|
+
language_hint: str | None = None,
|
|
293
|
+
) -> None:
|
|
294
|
+
# resolve_repo_root validates the path + locates the work-tree root (raising
|
|
295
|
+
# NotAGitRepositoryError for a non-repo path); the blob reads are Rust-
|
|
296
|
+
# authoritative (#98, A2.4.1): resolve ref + cat-file in the core.
|
|
297
|
+
self._repo_path = resolve_repo_root(repo_path)
|
|
298
|
+
self._file_path = _safe_file_path(file_path)
|
|
299
|
+
self._old_ref = old_ref
|
|
300
|
+
self._new_ref = new_ref
|
|
301
|
+
self._language_hint = language_hint
|
|
302
|
+
|
|
303
|
+
def get_content(self) -> tuple[str, str, str, str | None]:
|
|
304
|
+
from intentumdiff import rust_core
|
|
305
|
+
|
|
306
|
+
old_content, new_content = rust_core.git_source_content(
|
|
307
|
+
self._repo_path, self._file_path, self._old_ref, self._new_ref
|
|
308
|
+
)
|
|
309
|
+
return old_content, new_content, self._file_path, self._language_hint
|
|
310
|
+
|
|
311
|
+
@classmethod
|
|
312
|
+
def working_tree(
|
|
313
|
+
cls,
|
|
314
|
+
repo_path: "str | os.PathLike[str]",
|
|
315
|
+
file_path: str,
|
|
316
|
+
*,
|
|
317
|
+
ref: str = "HEAD",
|
|
318
|
+
language_hint: str | None = None,
|
|
319
|
+
) -> "WorkingTreeSource":
|
|
320
|
+
"""
|
|
321
|
+
Convenience factory — diff the committed version of a file at ``ref``
|
|
322
|
+
(default: ``HEAD``) against the current un-staged working-tree copy.
|
|
323
|
+
|
|
324
|
+
Returns a ``WorkingTreeSource`` instance.
|
|
325
|
+
"""
|
|
326
|
+
return WorkingTreeSource(
|
|
327
|
+
repo_path=repo_path,
|
|
328
|
+
file_path=file_path,
|
|
329
|
+
ref=ref,
|
|
330
|
+
language_hint=language_hint,
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
class WorkingTreeSource(Source):
|
|
335
|
+
"""
|
|
336
|
+
Compare the committed version of a file against the current working tree.
|
|
337
|
+
|
|
338
|
+
The **old** content is the blob stored at ``ref`` (default: ``HEAD``).
|
|
339
|
+
The **new** content is read directly from the file on disk — i.e. whatever
|
|
340
|
+
the developer has saved but not yet staged or committed.
|
|
341
|
+
|
|
342
|
+
Parameters
|
|
343
|
+
----------
|
|
344
|
+
repo_path:
|
|
345
|
+
Path to the git repository (or any directory inside it).
|
|
346
|
+
file_path:
|
|
347
|
+
Path to the file relative to the repository root, using forward
|
|
348
|
+
slashes (e.g. ``"src/foo.py"``).
|
|
349
|
+
ref:
|
|
350
|
+
Git ref to use as the "old" version. Defaults to ``"HEAD"``.
|
|
351
|
+
language_hint:
|
|
352
|
+
Optional language override; if omitted the plugin registry detects it.
|
|
353
|
+
"""
|
|
354
|
+
|
|
355
|
+
def __init__(
|
|
356
|
+
self,
|
|
357
|
+
repo_path: "str | os.PathLike[str]",
|
|
358
|
+
file_path: str,
|
|
359
|
+
ref: str = "HEAD",
|
|
360
|
+
language_hint: str | None = None,
|
|
361
|
+
) -> None:
|
|
362
|
+
# See GitSource.__init__: resolve_repo_root validates + locates the root; the
|
|
363
|
+
# blob read (old side) + working-tree read (new side) are Rust (A2.4.1).
|
|
364
|
+
self._repo_path = resolve_repo_root(repo_path)
|
|
365
|
+
self._file_path = _safe_file_path(file_path)
|
|
366
|
+
self._ref = ref
|
|
367
|
+
self._language_hint = language_hint
|
|
368
|
+
|
|
369
|
+
def get_content(self) -> tuple[str, str, str, str | None]:
|
|
370
|
+
from intentumdiff import rust_core
|
|
371
|
+
|
|
372
|
+
old_content, new_content = rust_core.working_tree_source_content(
|
|
373
|
+
self._repo_path, self._file_path, self._ref
|
|
374
|
+
)
|
|
375
|
+
return old_content, new_content, self._file_path, self._language_hint
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""
|
|
2
|
+
intentumdiff.sources.live_buffer_source
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Source that compares a git-committed version of a file against an in-memory
|
|
6
|
+
buffer — the primary input for ``LiveServer`` (keystroke-level diffing).
|
|
7
|
+
|
|
8
|
+
Security
|
|
9
|
+
────────
|
|
10
|
+
File paths are validated against the repository root to prevent path-traversal
|
|
11
|
+
attacks before any git I/O is performed.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import subprocess
|
|
17
|
+
from pathlib import Path, PurePosixPath
|
|
18
|
+
from typing import TYPE_CHECKING
|
|
19
|
+
|
|
20
|
+
from intentumdiff.sources.base import Source
|
|
21
|
+
from intentumdiff.vcs._safe_path import safe_relative_path
|
|
22
|
+
from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from intentumdiff.core.models import EditDelta
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class LiveBufferSource(Source):
|
|
29
|
+
"""Diff the committed (``ref``) version of a file against a live in-memory buffer.
|
|
30
|
+
|
|
31
|
+
Intended for editor integrations and ``LiveServer``: the editor sends the
|
|
32
|
+
current buffer contents and (optionally) incremental edit deltas so the
|
|
33
|
+
differ can skip a full re-parse.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
repo_path:
|
|
38
|
+
Path to any directory inside the git repository (or the root itself).
|
|
39
|
+
file_path:
|
|
40
|
+
Repository-relative path to the file, e.g. ``"src/main.py"``.
|
|
41
|
+
Must not be absolute or contain ``".."``.
|
|
42
|
+
live_content:
|
|
43
|
+
The current (unsaved / in-buffer) content to compare against.
|
|
44
|
+
ref:
|
|
45
|
+
Git ref to use as the ``old`` version. Defaults to ``"HEAD"``.
|
|
46
|
+
language_hint:
|
|
47
|
+
Optional language override (e.g. ``"python"``). Pass ``None`` to
|
|
48
|
+
auto-detect from the file extension.
|
|
49
|
+
edit_deltas:
|
|
50
|
+
Optional list of ``EditDelta`` objects describing the incremental
|
|
51
|
+
byte-range edits that produced ``live_content`` from the previously
|
|
52
|
+
known version. When provided, ``SemanticDiffer`` can reuse an
|
|
53
|
+
existing tree-sitter parse tree instead of re-parsing from scratch.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
repo_path: str | Path,
|
|
59
|
+
file_path: str,
|
|
60
|
+
live_content: str,
|
|
61
|
+
*,
|
|
62
|
+
ref: str = "HEAD",
|
|
63
|
+
language_hint: str | None = None,
|
|
64
|
+
edit_deltas: "list[EditDelta] | None" = None,
|
|
65
|
+
) -> None:
|
|
66
|
+
# Validate the file path before storing it.
|
|
67
|
+
safe = PurePosixPath(file_path)
|
|
68
|
+
if safe.is_absolute() or ".." in safe.parts:
|
|
69
|
+
raise ValueError(
|
|
70
|
+
f"Unsafe file path: {file_path!r} — must be relative and must not "
|
|
71
|
+
"contain '..'."
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
self._repo_path = Path(repo_path)
|
|
75
|
+
self._file_path = safe_relative_path(file_path)
|
|
76
|
+
self._live_content = live_content
|
|
77
|
+
self._ref = ref
|
|
78
|
+
self._language_hint = language_hint
|
|
79
|
+
self.edit_deltas: list[EditDelta] | None = edit_deltas
|
|
80
|
+
|
|
81
|
+
# ------------------------------------------------------------------
|
|
82
|
+
# Source interface
|
|
83
|
+
# ------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
def get_content(self) -> tuple[str, str, str, str | None]:
|
|
86
|
+
"""Return ``(old_content, new_content, filename, language_hint)``.
|
|
87
|
+
|
|
88
|
+
*old_content* is read from the committed blob at ``self._ref``.
|
|
89
|
+
*new_content* is the in-memory buffer ``live_content``.
|
|
90
|
+
"""
|
|
91
|
+
old_content = self._read_committed()
|
|
92
|
+
filename = Path(self._file_path).name
|
|
93
|
+
return old_content, self._live_content, filename, self._language_hint
|
|
94
|
+
|
|
95
|
+
# ------------------------------------------------------------------
|
|
96
|
+
# Internal helpers
|
|
97
|
+
# ------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
def _read_committed(self) -> str:
|
|
100
|
+
"""Read the committed blob at ``self._ref:self._file_path``.
|
|
101
|
+
|
|
102
|
+
Uses the git CLI (``cat-file blob``) rather than the GitPython object model
|
|
103
|
+
(#98, A2.4.3); a file not tracked at this ref yields an empty old side.
|
|
104
|
+
"""
|
|
105
|
+
root = resolve_repo_root(self._repo_path)
|
|
106
|
+
try:
|
|
107
|
+
data = run_git_bytes(
|
|
108
|
+
root, ["cat-file", "blob", f"{self._ref}:{self._file_path}"]
|
|
109
|
+
)
|
|
110
|
+
except subprocess.CalledProcessError:
|
|
111
|
+
# File not yet tracked at this ref — treat old side as empty.
|
|
112
|
+
return ""
|
|
113
|
+
return data.decode("utf-8", errors="replace")
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""
|
|
2
|
+
intentumdiff.sources.patch_source
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Reconstruct old/new content from a unified diff patch string.
|
|
6
|
+
|
|
7
|
+
Typical use-case: diffing an LLM-generated patch (e.g. GitHub Copilot output)
|
|
8
|
+
without access to the original file.
|
|
9
|
+
|
|
10
|
+
The patch must follow the unified diff format (``--- a/...`` / ``+++ b/...``).
|
|
11
|
+
``unidiff`` (third-party) is used for robust parsing.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import io
|
|
17
|
+
from pathlib import PurePosixPath
|
|
18
|
+
|
|
19
|
+
from unidiff import PatchSet
|
|
20
|
+
|
|
21
|
+
from intentumdiff.sources.base import Source
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _apply_patch_to_original(original: str, patch_text: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Reconstruct the patched (new) content by applying hunks from a unified diff.
|
|
27
|
+
|
|
28
|
+
We do a line-based apply; this is intentionally simple — we are not a full
|
|
29
|
+
``patch`` utility. For complex patches (binary, context mismatch) an
|
|
30
|
+
error is raised.
|
|
31
|
+
"""
|
|
32
|
+
patchset = PatchSet(io.StringIO(patch_text))
|
|
33
|
+
if not patchset:
|
|
34
|
+
return original
|
|
35
|
+
|
|
36
|
+
patched_file = patchset[0] # we support single-file patches here
|
|
37
|
+
old_lines = original.splitlines(keepends=True)
|
|
38
|
+
new_lines: list[str] = []
|
|
39
|
+
old_idx = 0 # 1-based line cursor
|
|
40
|
+
|
|
41
|
+
for hunk in patched_file:
|
|
42
|
+
# Copy context lines before this hunk
|
|
43
|
+
hunk_start = hunk.source_start # 1-based
|
|
44
|
+
while old_idx < hunk_start - 1:
|
|
45
|
+
new_lines.append(old_lines[old_idx])
|
|
46
|
+
old_idx += 1
|
|
47
|
+
|
|
48
|
+
for line in hunk:
|
|
49
|
+
if line.is_context:
|
|
50
|
+
new_lines.append(old_lines[old_idx])
|
|
51
|
+
old_idx += 1
|
|
52
|
+
elif line.is_added:
|
|
53
|
+
new_lines.append(line.value)
|
|
54
|
+
elif line.is_removed:
|
|
55
|
+
old_idx += 1 # skip removed original line
|
|
56
|
+
|
|
57
|
+
# Trailing context
|
|
58
|
+
while old_idx < len(old_lines):
|
|
59
|
+
new_lines.append(old_lines[old_idx])
|
|
60
|
+
old_idx += 1
|
|
61
|
+
|
|
62
|
+
return "".join(new_lines)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _extract_filename(patch_text: str) -> str:
|
|
66
|
+
"""Extract the target filename from the ``+++`` header of a patch."""
|
|
67
|
+
for line in patch_text.splitlines():
|
|
68
|
+
if line.startswith("+++ "):
|
|
69
|
+
raw = line[4:].strip()
|
|
70
|
+
# Strip leading "b/" git prefix
|
|
71
|
+
path = raw[2:] if raw.startswith("b/") else raw
|
|
72
|
+
return path
|
|
73
|
+
return "unknown"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class PatchSource(Source):
|
|
77
|
+
"""
|
|
78
|
+
Reconstruct old and new content from a unified diff patch.
|
|
79
|
+
|
|
80
|
+
Parameters
|
|
81
|
+
----------
|
|
82
|
+
patch_text:
|
|
83
|
+
Full unified diff string (as produced by ``git diff``, GitHub Copilot,
|
|
84
|
+
or similar tools).
|
|
85
|
+
original_content:
|
|
86
|
+
The original (old) file content. If omitted, the old content is
|
|
87
|
+
reconstructed from the patch's ``-`` lines (best-effort).
|
|
88
|
+
language_hint:
|
|
89
|
+
Optional language override.
|
|
90
|
+
filename:
|
|
91
|
+
Display filename. If omitted, extracted from the ``+++`` header.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
def __init__(
|
|
95
|
+
self,
|
|
96
|
+
patch_text: str,
|
|
97
|
+
original_content: str | None = None,
|
|
98
|
+
language_hint: str | None = None,
|
|
99
|
+
filename: str | None = None,
|
|
100
|
+
) -> None:
|
|
101
|
+
self._patch_text = patch_text
|
|
102
|
+
self._original = original_content
|
|
103
|
+
self._language_hint = language_hint
|
|
104
|
+
self._filename = filename or _extract_filename(patch_text)
|
|
105
|
+
|
|
106
|
+
def get_content(self) -> tuple[str, str, str, str | None]:
|
|
107
|
+
if self._original is not None:
|
|
108
|
+
old_content = self._original
|
|
109
|
+
else:
|
|
110
|
+
old_content = self._reconstruct_original()
|
|
111
|
+
|
|
112
|
+
new_content = _apply_patch_to_original(old_content, self._patch_text)
|
|
113
|
+
return old_content, new_content, self._filename, self._language_hint
|
|
114
|
+
|
|
115
|
+
def _reconstruct_original(self) -> str:
|
|
116
|
+
"""Build the original content from ``-`` and context lines in the patch."""
|
|
117
|
+
lines: list[str] = []
|
|
118
|
+
for line in self._patch_text.splitlines(keepends=True):
|
|
119
|
+
if line.startswith("---") or line.startswith("+++") or line.startswith("@@"):
|
|
120
|
+
continue
|
|
121
|
+
if line.startswith("-"):
|
|
122
|
+
lines.append(line[1:])
|
|
123
|
+
elif line.startswith(" "):
|
|
124
|
+
lines.append(line[1:])
|
|
125
|
+
# Lines starting with '+' are new — skip them
|
|
126
|
+
return "".join(lines)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""
|
|
2
|
+
intentumdiff.sources.string_source
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
|
|
5
|
+
Compare two in-memory strings — useful for diffing LLM-generated code or
|
|
6
|
+
test fixtures.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from intentumdiff.sources.base import Source
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class StringSource(Source):
|
|
15
|
+
"""
|
|
16
|
+
Compare two in-memory source strings.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
old_content:
|
|
21
|
+
The old (before) version of the source code.
|
|
22
|
+
new_content:
|
|
23
|
+
The new (after) version.
|
|
24
|
+
filename:
|
|
25
|
+
Logical filename used for language detection and diff output.
|
|
26
|
+
language_hint:
|
|
27
|
+
Optional explicit language. If omitted, the plugin registry infers it
|
|
28
|
+
from ``filename``.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
old_content: str,
|
|
34
|
+
new_content: str,
|
|
35
|
+
filename: str,
|
|
36
|
+
language_hint: str | None = None,
|
|
37
|
+
) -> None:
|
|
38
|
+
self._old = old_content
|
|
39
|
+
self._new = new_content
|
|
40
|
+
self._filename = filename
|
|
41
|
+
self._language_hint = language_hint
|
|
42
|
+
|
|
43
|
+
def get_content(self) -> tuple[str, str, str, str | None]:
|
|
44
|
+
return self._old, self._new, self._filename, self._language_hint
|