Documentation ¶
Overview ¶
Copyright (c) 2012-2016 The go-diff authors. All rights reserved. https://github.com/sergi/go-diff See the included LICENSE file for license details.
go-diff is a Go implementation of Google's Diff, Match, and Patch library Original library is Copyright (c) 2006 Google Inc. http://code.google.com/p/google-diff-match-patch/
Refer to https://github.com/pascalkuthe/imara-diff reimplemented in Golang.
Copyright (c) 2024 epic labs Package diff3 implements a three-way merge algorithm Original version in Javascript by Bryan Housel @bhousel: https://github.com/bhousel/node-diff3, which in turn is based on project Synchrotron, created by Tony Garnock-Jones. For more detail please visit: http://homepages.kcbbs.gen.nz/tonyg/projects/synchrotron.html https://github.com/tonyg/synchrotron
Ported to go by Javier Peletier @jpeletier
SOURCE: https://github.com/epiclabs-io/diff3
SPDX-License-Identifier: MIT
Copyright (c) 2014-2021 Akinori Hattori <hattya@gmail.com> SPDX-License-Identifier: MIT SOURCE: https://github.com/hattya/go.diff
Package diff implements the difference algorithm, which is based upon S. Wu, U. Manber, G. Myers, and W. Miller, "An O(NP) Sequence Comparison Algorithm" August 1989.
Copyright 2019 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Index ¶
- Constants
- Variables
- func DefaultMerge(ctx context.Context, o, a, b string, labelO, labelA, labelB string) (string, bool, error)
- func Merge(ctx context.Context, opts *MergeOptions) (string, bool, error)
- func NewTextReader(r io.Reader) (io.Reader, error)
- func NewUnifiedReader(r io.Reader) (io.Reader, error)
- func NewUnifiedReaderEx(r io.Reader, textconv bool) (io.Reader, string, error)
- func ParseConflictStyle(s string) int
- func ReadUnifiedText(r io.Reader, size int64, textconv bool) (content string, charset string, err error)
- type Algorithm
- type Change
- func HistogramDiff[E comparable](ctx context.Context, L1, L2 []E) ([]Change, error)
- func MinimalDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
- func MyersDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
- func OnpDiff[E comparable](ctx context.Context, L1, L2 []E) ([]Change, error)
- func PatienceDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
- type Conflict
- type Dfio
- type Diff3MergeResult
- type FastArrayNegativeIndices
- type FastIntArray
- type File
- type FileStat
- type Hunk
- type Lcs
- type LcsSearch
- type Line
- type MergeOptions
- type Operation
- type Options
- type Sink
- func (s *Sink) AsStringDiff(o []Dfio[int]) []StringDiff
- func (s *Sink) ScanLines(r io.Reader) ([]int, error)
- func (s *Sink) ScanRawLines(r io.Reader) ([]int, error)
- func (s *Sink) SplitLines(text string) []int
- func (s *Sink) SplitRawLines(text string) []int
- func (s *Sink) ToUnified(from, to *File, changes []Change, linesA, linesB []int, contextLines int) *Unified
- func (s *Sink) WriteLine(w io.Writer, E ...int)
- type SnakePath
- type StringDiff
- type Unified
- type UnifiedEncoder
- func (e *UnifiedEncoder) Encode(patches []*Unified) error
- func (e *UnifiedEncoder) SetColor(colorConfig color.ColorConfig) *UnifiedEncoder
- func (e *UnifiedEncoder) SetDstPrefix(prefix string) *UnifiedEncoder
- func (e *UnifiedEncoder) SetNoRename() *UnifiedEncoder
- func (e *UnifiedEncoder) SetSrcPrefix(prefix string) *UnifiedEncoder
Constants ¶
const ( // Sep1 signifies the start of a conflict. Sep1 = "<<<<<<<" // Sep2 signifies the middle of a conflict. Sep2 = "=======" // Sep3 signifies the end of a conflict. Sep3 = ">>>>>>>" // SepO origin content SepO = "|||||||" )
const ( // Only show the zealously minified conflicting lines of the local changes and the incoming (other) changes, // hiding the base version entirely. // // “`text // line1-changed-by-both // <<<<<<< local // line2-to-be-changed-in-incoming // ======= // line2-changed // >>>>>>> incoming // “` STYLE_DEFAULT = iota // Show non-minimized hunks of local changes, the base, and the incoming (other) changes. // // This mode does not hide any information. // // “`text // <<<<<<< local // line1-changed-by-both // line2-to-be-changed-in-incoming // ||||||| 9a8d80c // line1-to-be-changed-by-both // line2-to-be-changed-in-incoming // ======= // line1-changed-by-both // line2-changed // >>>>>>> incoming // “` STYLE_DIFF3 // Like diff3, but will show *minimized* hunks of local change and the incoming (other) changes, // as well as non-minimized hunks of the base. // // “`text // line1-changed-by-both // <<<<<<< local // line2-to-be-changed-in-incoming // ||||||| 9a8d80c // line1-to-be-changed-by-both // line2-to-be-changed-in-incoming // ======= // line2-changed // >>>>>>> incoming // “` STYLE_ZEALOUS_DIFF3 )
const ( NEWLINE_RAW = iota NEWLINE_LF NEWLINE_CRLF )
const ( MAX_DIFF_SIZE = 100 << 20 // MAX_DIFF_SIZE 100MiB BINARY = "binary" UTF8 = "UTF-8" )
const DefaultContextLines = 3
DefaultContextLines is the number of unchanged lines of surrounding context displayed by Unified. Use ToUnified to specify a different value.
const MaxChainLen = 63
const (
ZERO_OID = "0000000000000000000000000000000000000000000000000000000000000000" // zeta zero OID
)
Variables ¶
var (
ErrNonTextContent = errors.New("non-text content")
)
var (
ErrUnsupportedAlgorithm = errors.New("unsupport algorithm")
)
Functions ¶
func DefaultMerge ¶ added in v0.16.0
func DefaultMerge(ctx context.Context, o, a, b string, labelO, labelA, labelB string) (string, bool, error)
DefaultMerge implements the diff3 algorithm to merge two texts into a common base.
func Merge ¶
Merge implements the diff3 algorithm to merge two texts into a common base.
Support multiple diff algorithms and multiple conflict styles
func NewUnifiedReaderEx ¶ added in v0.16.0
func ParseConflictStyle ¶ added in v0.16.0
Types ¶
type Change ¶
type Change struct { P1 int // before: position in before P2 int // after: position in after Del int // number of elements that deleted from a Ins int // number of elements that inserted into b }
func HistogramDiff ¶
func HistogramDiff[E comparable](ctx context.Context, L1, L2 []E) ([]Change, error)
HistogramDiff: calculates the difference using the histogram algorithm
func MinimalDiff ¶
func MinimalDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
MinimalDiff: Myers: An O(ND) Difference Algorithm and Its Variations
func MyersDiff ¶
func MyersDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
MyersDiff: An O(ND) diff algorithm that has a quadratic space worst-case complexity.
func OnpDiff ¶
func OnpDiff[E comparable](ctx context.Context, L1, L2 []E) ([]Change, error)
OnpDiff returns the differences between []E. It makes O(NP) (the worst case) calls to equal.
func PatienceDiff ¶
func PatienceDiff[E comparable](ctx context.Context, L1 []E, L2 []E) ([]Change, error)
PatienceDiff: Calculates the difference using the patience algorithm
type Conflict ¶
type Conflict[E comparable] struct { // contains filtered or unexported fields }
Conflict describes a merge conflict
type Dfio ¶
type Dfio[E comparable] struct { T Operation E []E }
func DiffSlices ¶
func DiffSlices[E comparable](ctx context.Context, S1, S2 []E) ([]Dfio[E], error)
type Diff3MergeResult ¶
type Diff3MergeResult[E comparable] struct { // contains filtered or unexported fields }
Diff3MergeResult describes a merge result
func Diff3Merge ¶
func Diff3Merge[E comparable](ctx context.Context, o, a, b []E, algo Algorithm, excludeFalseConflicts bool) ([]*Diff3MergeResult[E], error)
Diff3Merge applies the output of diff3MergeIndices to actually construct the merged file; the returned result alternates between 'ok' and 'conflict' blocks.
type FastArrayNegativeIndices ¶
type FastArrayNegativeIndices struct {
// contains filtered or unexported fields
}
An array that supports fast negative indices.
type FastIntArray ¶
type FastIntArray struct {
// contains filtered or unexported fields
}
func NewFastIntArray ¶
func NewFastIntArray() *FastIntArray
type Hunk ¶
type Hunk struct { // The line in the original source where the hunk starts. FromLine int `json:"from_line"` // The line in the original source where the hunk finishes. ToLine int `json:"to_line"` // The set of line based edits to apply. Lines []Line `json:"lines,omitempty"` }
Hunk represents a contiguous set of line edits to apply.
type LcsSearch ¶
type LcsSearch[E comparable] struct { // contains filtered or unexported fields }
type MergeOptions ¶ added in v0.16.0
type MergeOptions struct {
TextO, TextA, TextB string
RO, R1, R2 io.Reader // when if set
LabelO, LabelA, LabelB string
A Algorithm
Style int // Conflict Style
}
func (*MergeOptions) ValidateOptions ¶ added in v0.16.0
func (opts *MergeOptions) ValidateOptions() error
type Sink ¶
func (*Sink) AsStringDiff ¶
func (s *Sink) AsStringDiff(o []Dfio[int]) []StringDiff
func (*Sink) SplitLines ¶
func (*Sink) SplitRawLines ¶
type SnakePath ¶
type SnakePath struct {
// contains filtered or unexported fields
}
func NewSnakePath ¶
type StringDiff ¶
StringDiff represents one diff operation
type Unified ¶
type Unified struct { // From is the name of the original file. From *File `json:"from,omitempty"` // To is the name of the modified file. To *File `json:"to,omitempty"` // IsBinary returns true if this patch is representing a binary file. IsBinary bool `json:"binary"` // Fragments returns true if this patch is representing a fragments file. IsFragments bool `json:"fragments"` // Message prefix, eg: warning: something Message string `json:"message"` // Hunks is the set of edit Hunks needed to transform the file content. Hunks []*Hunk `json:"hunks,omitempty"` }
unified represents a set of edits as a unified diff.
type UnifiedEncoder ¶
UnifiedEncoder encodes an unified diff into the provided Writer. It does not support similarity index for renames or sorting hash representations.
func NewUnifiedEncoder ¶
func NewUnifiedEncoder(w io.Writer) *UnifiedEncoder
NewUnifiedEncoder returns a new UnifiedEncoder that writes to w.
func (*UnifiedEncoder) Encode ¶
func (e *UnifiedEncoder) Encode(patches []*Unified) error
func (*UnifiedEncoder) SetColor ¶
func (e *UnifiedEncoder) SetColor(colorConfig color.ColorConfig) *UnifiedEncoder
SetColor sets e's color configuration and returns e.
func (*UnifiedEncoder) SetDstPrefix ¶
func (e *UnifiedEncoder) SetDstPrefix(prefix string) *UnifiedEncoder
SetDstPrefix sets e's dstPrefix and returns e.
func (*UnifiedEncoder) SetNoRename ¶ added in v0.16.0
func (e *UnifiedEncoder) SetNoRename() *UnifiedEncoder
func (*UnifiedEncoder) SetSrcPrefix ¶
func (e *UnifiedEncoder) SetSrcPrefix(prefix string) *UnifiedEncoder
SetSrcPrefix sets e's srcPrefix and returns e.