Skip to content

patch

The patch module parses, creates, encodes, and applies unified and git-style patches.

Types

Type Description
Patch One file-level patch operation
PatchIter Iterator over a patch stream
ParseError Patch parsing error
ApplyError Error while applying a patch

Functions

decode input

Parses a patch stream.

Parameters

Name Type Description
input Str|Bin Unified or git-style patch

Returns

PatchIter

Errors

Exception Condition
ParseError Iteration reaches malformed patch data
let patches = [...patch.decode diff_text]

diff before after :source? :target?

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

before and after must both be Str or both be Bin.

Parameters

Name Type Description
before Str|Bin Original content
after Str|Bin Modified content
source Path|Str? Source filename for the patch headers
target Path|Str? Target filename for the patch headers

Returns

Patch

Errors

Exception Condition
TypeError before and after are not both text or both binary
TypeError source or target is not a Path or Str
let p = patch.diff "alpha\n" "beta\n" source: old.txt target: new.txt
echo (patch.encode p)

encode value

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

When every encoded byte is valid UTF-8, this returns a Str. Otherwise it returns Bin.

Parameters

Name Type Description
value Patch|iterable One patch or an iterable of patches

Returns

Str|Bin

Errors

Exception Condition
TypeError An iterable contains a non-Patch value
let patches = [...patch.decode diff_text]
write output.patch (patch.encode patches)