Diff processing
The PyReprism.diffs module parses unified/git diffs and analyzes the
changed code in each file’s own language. It powers churn metrics (added/removed
lines split into code, comment and blank), cosmetic-change detection, and
ML-oriented extraction of the changed code:
from PyReprism import diffs
d = diffs.parse(diff_text)
report = diffs.diff_stats(d)
report.totals()
diffs.cosmetic_files(d)
f = d.files[0]
f.added_text()
f.normalize("added")
By default analysis is fragment mode (from the diff text alone). Setting a
DiffFile’s new_source / old_source to the full
file contents switches it to accurate full-file mode for that file.
Process unified/git diffs with PyReprism’s language-aware operations.
A diff is parsed into files and hunks; each file’s path is used to detect its language, so the added/removed code can be tokenized, normalized, or measured.
Two accuracy modes:
Fragment mode (default) works from the diff text alone. It is self-contained but best-effort where a block comment or string spans a hunk boundary.
Full-file mode is used automatically for a
DiffFilewhen itsnew_source/old_sourceare set (e.g. fromgit show); changed lines are then classified against the whole file for accuracy.
- class PyReprism.diffs.DiffFile(old_path: Optional[str] = None, new_path: Optional[str] = None, hunks: List[PyReprism.diffs.Hunk] = <factory>, is_binary: bool = False, is_new: bool = False, is_deleted: bool = False, is_rename: bool = False, new_source: Optional[str] = None, old_source: Optional[str] = None)
Bases:
object- property language
Detected language class for this file, or
None.
- class PyReprism.diffs.DiffFileStats(path: str | None, language: str | None, added_code: int = 0, added_comment: int = 0, added_blank: int = 0, removed_code: int = 0, removed_comment: int = 0, removed_blank: int = 0, is_binary: bool = False)
Bases:
object
- class PyReprism.diffs.DiffLine(kind: PyReprism.diffs.LineKind, text: str, old_lineno: int | None, new_lineno: int | None)
Bases:
object
- class PyReprism.diffs.DiffReport(files: List[PyReprism.diffs.DiffFileStats] = <factory>)
Bases:
object- files: List[DiffFileStats]
- class PyReprism.diffs.Hunk(old_start: int, new_start: int, section: str = '', lines: List[PyReprism.diffs.DiffLine] = <factory>)
Bases:
object
- class PyReprism.diffs.LineKind(value)
-
An enumeration.
- ADDED = 'added'
- CONTEXT = 'context'
- REMOVED = 'removed'
- PyReprism.diffs.cosmetic_files(diff: Diff) List[DiffFile]
Return the files whose change is comment/whitespace only.
- PyReprism.diffs.diff_stats(diff: Diff) DiffReport
Compute per-file added/removed churn split into code / comment / blank.