Skip to main content

text_patch

Function text_patch 

Source
pub fn text_patch(
    old_bytes: &[u8],
    new_bytes: &[u8],
    old_path: &str,
    new_path: &str,
) -> String
Expand description

Render a unified-diff patch between two byte blobs.

old_path / new_path are the a/… and b/… labels for the ---/+++ headers (callers conventionally pass the same repo path for both). The output is a Git-compatible unified diff:

--- a/<old_path>
+++ b/<new_path>
@@ -<l>,<n> +<l>,<n> @@
 context
-removed
+added

Either side that is binary by Git’s heuristic — a NUL byte in the first 8000 bytes — yields a single Binary files a/<old> and b/<new> differ line instead of hunks, matching Git.

The algorithm is the greedy Myers diff with Git-style hunk compaction (see unified_hunks); the hunks byte-match git diff. Trailing-newline handling follows Git’s \ No newline at end of file convention.

Note this returns a String via lossy UTF-8 conversion, so a non-UTF-8 (but non-binary) blob’s raw bytes are not preserved here — use unified_hunks for byte-exact output.