Skip to content

patch

Parses, creates, encodes, and applies unified and git-style patches.

Types

TypeDescription
ApplyError Raised when a patch does not apply cleanly.
ParseError Raised for malformed patch data.
Patch One file-level patch operation.
PatchIter Iterator over a patch stream.

Functions

decode input -> PatchIter

Parses a patch stream.

Parameters

NameTypeDescription
input (Str | Bin) Unified or git-style patch.

Errors

ParseError when iteration reaches malformed patch data.

Example

let patches = [...patch.decode diff_text]

diff before after … -> Patch

Builds a text patch from two versions of the same content.

Parameters

NameTypeDescription
before (Str | Bin) Original content. Must be the same kind of value as after.
after (Str | Bin) Modified content.
:source? (fs.Path | Str) Source filename for the patch headers.
:target? (fs.Path | Str) Target filename for the patch headers.

Errors

Exception Condition
TypeError before and after are not both text or both binary
TypeError source or target is not a Path or Str

Example

let p = patch.diff "alpha\n" "beta\n" source: old.txt target: new.txt
echo (patch.encode p)

encode value -> (Str | Bin)

Encodes a Patch or iterable of patches back to patch text.

Parameters

NameTypeDescription
value (Patch | Iterable[Patch]) One patch or an iterable of patches.

Returns

Str when every encoded byte is valid UTF-8, and Bin otherwise.

Errors

TypeError if an iterable contains a non-Patch value.

Example

let patches = [...patch.decode diff_text]
write output.patch (patch.encode patches)