MCPcopy Create free account
hub / github.com/ParzivalHack/PySpector / get_changed_chunks

Method get_changed_chunks

src/pyspector/ast_cache.py:295–322  ·  view source on GitHub ↗

Return the IDs of top-level chunks that differ between two versions of a file, without updating the cache. Useful for incremental analysis drivers that want to know exactly what changed.

(
        self, file_path: Path, old_content: str, new_content: str
    )

Source from the content-addressed store, hash-verified

293 p.unlink(missing_ok=True)
294
295 def get_changed_chunks(
296 self, file_path: Path, old_content: str, new_content: str
297 ) -> List[str]:
298 """
299 Return the IDs of top-level chunks that differ between two versions
300 of a file, without updating the cache. Useful for incremental
301 analysis drivers that want to know exactly what changed.
302 """
303 def _chunk_hashes(source: str) -> Dict[str, str]:
304 with warnings.catch_warnings():
305 warnings.filterwarnings("ignore", category=SyntaxWarning)
306 try:
307 tree = ast.parse(source, filename=str(file_path))
308 except SyntaxError:
309 return {}
310 lines = source.splitlines(keepends=True)
311 seen: Dict[str, int] = {}
312 out: Dict[str, str] = {}
313 for node in tree.body:
314 cid = _make_chunk_id(node, seen)
315 out[cid] = hashlib.sha256(_source_slice(lines, node).encode()).hexdigest()
316 return out
317
318 old_h = _chunk_hashes(old_content)
319 new_h = _chunk_hashes(new_content)
320 changed = [cid for cid, h in new_h.items() if old_h.get(cid) != h]
321 changed += [cid for cid in old_h if cid not in new_h]
322 return changed
323
324 # ── Internal ─────────────────────────────────────────────────────────────
325

Calls

no outgoing calls