diff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff implements the line diffing shared by the diff and patch utilities: the line splitting, the blank comparison of diff's -b, and the Myers algorithm that computes the changes between two sequences of lines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(a, b []string, changes []Change) []string

Apply returns the result of applying the changes that turn a into b. It's meant to check scripts in tests: Apply(a, b, Lines(a, b, key)) equals b.

func BlankKey

func BlankKey(line string) string

BlankKey is the key of diff's -b: it drops the trailing blanks of a line and maps every other run of blanks to a single space, so lines only differing in the amount of blanks compare equal. It takes lines as returned by SplitLines, with their trailing newline.

func Context

func Context(w io.Writer, f1, f2 File, a, b []string, changes []Change, context int, o ...LineOpts) error

Context writes the changes that turn a into b in the copied context format: file headers, then per group of changes the affected lines with context ones around them, shown for each file in turn.

func Ed

func Ed(w io.Writer, b []string, changes []Change, o ...LineOpts) error

Ed writes the changes as an ed script that turns file1 into file2 when fed to ed with a trailing w command. The changes come from the end of the file to its beginning, so the line numbers of the edits still to apply stay valid.

func ForwardEd

func ForwardEd(w io.Writer, b []string, changes []Change, o ...LineOpts) error

ForwardEd writes the changes in the alternative script form: like Ed, but from the beginning of the file to its end, with the command letter before the lines, which are then space separated. The result isn't meant to be fed to ed.

func Ifdef

func Ifdef(w io.Writer, name string, a, b []string, changes []Change, o ...LineOpts) error

Ifdef writes the changes that turn a into b in the merged if-then-else format of diff's -D option: the whole contents of both files, with each differing group of lines wrapped in C preprocessor conditionals on the given name. A line without a trailing newline ends completed, as the format can't express otherwise.

func Normal

func Normal(w io.Writer, a, b []string, changes []Change, o ...LineOpts) error

Normal writes the changes that turn a into b in the diff default format: an ed-like command line per change, such as "1,3c5,7", followed by the "< " lines of a and the "> " lines of b, split by "---".

func RCS

func RCS(w io.Writer, b []string, changes []Change, o ...LineOpts) error

RCS writes the changes that turn a into b in the RCS format, which the Revision Control System reads: a deletion command "d start count" and an append command "a start count" followed by its lines, per change, from the beginning of the file to its end. A line without a trailing newline keeps the output without one too, which that format can express.

func Side

func Side(w io.Writer, a, b []string, changes []Change, o SideOpts) error

Side writes the changes that turn a into b in the side-by-side format: the lines of both files in two columns around a gutter, with each line marked by how it compares, '|' for a changed pair, '<' and '>' for lines of one file only, '(' for the common lines of the left column only and ')' for the unpaired common lines of the right one.

func SplitLines

func SplitLines(data []byte) []string

SplitLines splits data into lines, keeping the trailing newline of each one, except for a last line without it when data doesn't end in a newline.

func Unified

func Unified(w io.Writer, f1, f2 File, a, b []string, changes []Change, context int, o ...LineOpts) error

Unified writes the changes that turn a into b in the unified context format: file headers, then per group of changes the affected lines and the context around them, interleaved and taken from a.

func WriteHunk

func WriteHunk(w io.Writer, h Hunk, f PatchFormat) error

WriteHunk writes the hunk in the given format, without file headers, as reject files hold them.

Types

type Applier

type Applier struct {
	// Blank makes sequences of blanks match any sequence of blanks, as with
	// patch's -l.
	Blank bool
}

Applier applies the hunks of a patch to the lines of a file, searching for their places when the file has moved them around.

func (Applier) Apply

func (a Applier) Apply(lines []string, hunks []Hunk) ([]string, []Outcome)

Apply tries each hunk at its stated place, then scanning around it, then giving up context lines, and finally checks whether its changes are already in the file. It returns the new lines and the outcome of every hunk, leaving failed hunks unapplied.

type Change

type Change struct {
	A0, A1, B0, B1 int
}

Change replaces the lines [A0, A1) of one file with the lines [B0, B1) of the other. An empty a-side makes it an insertion, an empty b-side a deletion, and both non-empty a replacement.

func Lines

func Lines(a, b []string, key func(string) string) []Change

Lines returns the changes that turn a into b, in ascending order and without overlaps. key maps each line to the form it's compared in, and may be nil to compare lines as-is.

The lines are as returned by SplitLines, keeping their trailing newline. The equal runs between changes always come from a, so a key that makes lines equal doesn't join lines that only look alike.

type File

type File struct {
	Name  string
	Time  time.Time
	Label string
}

File describes one of the compared files for the headers of the context and unified formats: its name and modification time, which is the current time for the standard input. Label, when set, takes the place of both.

type Hunk

type Hunk struct {
	FromLine int        // 1-based first old line, or the one before an empty range
	ToLine   int        // 1-based first new line, or the one before an empty range
	Old      []HunkLine // context and deleted lines, in order
	New      []HunkLine // context and inserted lines, in order
}

Hunk is one hunk of a patch: the old lines it replaces, with the context around them, and the new lines that take their place.

type HunkLine

type HunkLine struct {
	Kind      LineKind
	Content   string
	NoNewline bool // the line ends its file without a trailing newline
}

HunkLine is one line of a hunk, without its trailing newline.

type KeyOpts

type KeyOpts struct {
	IgnoreCase          bool
	IgnoreAllSpace      bool
	IgnoreSpaceChange   bool
	IgnoreTabExpansion  bool
	IgnoreTrailingSpace bool

	// TabSize is the width of the tab stops of IgnoreTabExpansion,
	// 8 when zero.
	TabSize int
}

KeyOpts describes a line comparison which ignores some of the contents of lines, as the white space and case options of diff ask.

func (KeyOpts) Blank

func (o KeyOpts) Blank(line string) bool

Blank reports whether a line holds nothing but what the comparison ignores, which is what the -B option of diff takes as a blank line.

func (KeyOpts) Key

func (o KeyOpts) Key() func(string) string

Key returns the function which maps lines to the form they're compared in, or nil when the options don't ask anything of the comparison.

A line which ends without a newline, which can only be the last one of a file, keeps that ending in its key: it compares equal only to another line without one, so a missing final newline stays a difference even when the contents of the lines are compared loosely. The exceptions are the comparisons which already ignore the blanks at the end of a line (-Z, -b and -w), where GNU diff treats a missing newline like one more trailing blank.

type LineKind

type LineKind uint8

LineKind tells what a hunk line does: stay as context, get deleted, or get inserted.

const (
	ContextLine LineKind = iota
	DeleteLine
	InsertLine
)

type LineOpts

type LineOpts struct {
	// ExpandTabs writes the tabs of the lines as the spaces they stand
	// for, so the output keeps its alignment when piped.
	ExpandTabs bool

	// InitialTab marks the lines with a tab after the flag of each one,
	// instead of a space, so tabbed lines stay aligned.
	InitialTab bool

	// TabSize is the width of the tab stops, 8 when zero.
	TabSize int
}

LineOpts adjusts the way the output formats write their lines, as the -t and -T options of diff ask.

type Outcome

type Outcome struct {
	Offset  int // how far the hunk landed from its stated place
	Fuzz    int // context lines each end gave up to match
	Failed  bool
	Already bool // the changes of the hunk are in the file already
}

Outcome tells what happened to one hunk.

type Patch

type Patch struct {
	From  string // name of the file the changes came from
	To    string // name of the file to apply them to
	Index string // name of the Index: header, when the patch has one
	Hunks []Hunk
}

Patch is the set of hunks that apply to one file, with the names found in its headers.

func ParseContext

func ParseContext(data string) ([]Patch, error)

ParseContext parses patch data as copied context diffs, returning one Patch per file. The lines around the patches are skipped.

func ParseUnified

func ParseUnified(data string) ([]Patch, error)

ParseUnified parses patch data as unified diffs, returning one Patch per file. The lines around the patches, like the headers diff writes between them, are skipped.

func Reverse

func Reverse(p Patch) Patch

Reverse swaps the sides of every hunk of the patch, so it applies the changes backwards.

type PatchFormat

type PatchFormat uint8

PatchFormat is the format a patch file is written in.

const (
	// UnknownPatch marks data no patch was found in.
	UnknownPatch PatchFormat = iota
	// UnifiedPatch is the unified context format, the output of diff -u.
	UnifiedPatch
	// ContextPatch is the copied context format, the output of diff -c.
	ContextPatch
)

func DetectPatch

func DetectPatch(data string) PatchFormat

DetectPatch reports the format the patch data seems to be in.

type SideOpts

type SideOpts struct {
	// Width is the total output width in columns, 130 when zero.
	Width int

	// TabSize is the width of the tab stops, 8 when zero.
	TabSize int

	// ExpandTabs writes the tabs of the text as the spaces they stand
	// for, instead of keeping the tabs which fit.
	ExpandTabs bool

	// LeftColumn writes the common lines in the left column only, with
	// a '(' marking them.
	LeftColumn bool

	// SuppressCommon omits the common lines.
	SuppressCommon bool
}

SideOpts configures the side-by-side output format of diff.

Jump to

Keyboard shortcuts

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