diffmyers

package
v1.75.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Print

func Print(from, to [][]byte, edits []Edit, options ...PrintOption) ([]byte, error)

Print prints the edits in the unified diff format without the header.

Ref: https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html

Types

type Edit

type Edit struct {
	// Kind is the kind of edit. It is either an insert or a delete.
	Kind EditKind
	// FromPosition is the line to edit in the original sequence.
	FromPosition int
	// ToPosition is the line in the new sequence. It is only valid for
	// inserts.
	ToPosition int
}

Edit is an delete or insert operation.

func Diff

func Diff(from, to [][]byte) []Edit

Diff does a diff. It returns a [[]Edit] which when applied to the original sequence will result in the new sequence.

The algorithm is based on the paper "An O(ND) Difference Algorithm and Its Variations" by Eugene W. Myers. The paper is available at https://citeseerx.ist.psu.edu/doc/10.1.1.4.6927.

It implements the linear space refinement of the algorithm described in section 4b. This is the same algorithm used by git.

The script is then normalized similar to GNU diff and git normalize theirs.

type EditKind

type EditKind int

EditKind is the kind of edit.

const (
	// EditKindDelete is a delete.
	EditKindDelete EditKind = iota + 1
	// EditKindInsert is an insert.
	EditKindInsert
)

type PrintOption added in v1.74.0

type PrintOption func(*printOptions)

PrintOption is an option for Print.

func PrintWithContext added in v1.74.0

func PrintWithContext(context int) PrintOption

PrintWithContext sets the number of carried over lines kept either side of a change. The default is 3, as for diff -u and git. A negative value is treated as zero.

Mutually exclusive with PrintWithFullContext - the last one wins.

func PrintWithFullContext added in v1.74.0

func PrintWithFullContext() PrintOption

PrintWithFullContext keeps every line of the original sequence rather than a window around each change.

Mutually exclusive with PrintWithContext - the last one wins.

The result is not a valid unified diff: the lines before the first change precede every hunk header, so patch and git apply reject it. In exchange the whole original sequence can be recovered from the output, which a caller that re-slices the context itself needs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL