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 ¶
- func Apply(a, b []string, changes []Change) []string
- func BlankKey(line string) string
- func Context(w io.Writer, f1, f2 File, a, b []string, changes []Change, context int, ...) error
- func Ed(w io.Writer, b []string, changes []Change, o ...LineOpts) error
- func ForwardEd(w io.Writer, b []string, changes []Change, o ...LineOpts) error
- func Ifdef(w io.Writer, name string, a, b []string, changes []Change, o ...LineOpts) error
- func Normal(w io.Writer, a, b []string, changes []Change, o ...LineOpts) error
- func RCS(w io.Writer, b []string, changes []Change, o ...LineOpts) error
- func Side(w io.Writer, a, b []string, changes []Change, o SideOpts) error
- func SplitLines(data []byte) []string
- func Unified(w io.Writer, f1, f2 File, a, b []string, changes []Change, context int, ...) error
- func WriteHunk(w io.Writer, h Hunk, f PatchFormat) error
- type Applier
- type Change
- type File
- type Hunk
- type HunkLine
- type KeyOpts
- type LineKind
- type LineOpts
- type Outcome
- type Patch
- type PatchFormat
- type SideOpts
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
ParseContext parses patch data as copied context diffs, returning one Patch per file. The lines around the patches are skipped.
func ParseUnified ¶
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.
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.