diferenco

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

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

View Source
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 = "|||||||"
)
View Source
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
)
View Source
const (
	NEWLINE_RAW = iota
	NEWLINE_LF
	NEWLINE_CRLF
)
View Source
const (
	MAX_DIFF_SIZE = 100 << 20 // MAX_DIFF_SIZE 100MiB
	BINARY        = "binary"
	UTF8          = "UTF-8"
)
View Source
const DefaultContextLines = 3

DefaultContextLines is the number of unchanged lines of surrounding context displayed by Unified. Use ToUnified to specify a different value.

View Source
const MaxChainLen = 63
View Source
const (
	ZERO_OID = "0000000000000000000000000000000000000000000000000000000000000000" // zeta zero OID
)

Variables

View Source
var (
	ErrNonTextContent = errors.New("non-text content")
)
View Source
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

func Merge(ctx context.Context, opts *MergeOptions) (string, bool, error)

Merge implements the diff3 algorithm to merge two texts into a common base.

Support multiple diff algorithms and multiple conflict styles

func NewTextReader added in v0.16.0

func NewTextReader(r io.Reader) (io.Reader, error)

func NewUnifiedReader added in v0.16.0

func NewUnifiedReader(r io.Reader) (io.Reader, error)

func NewUnifiedReaderEx added in v0.16.0

func NewUnifiedReaderEx(r io.Reader, textconv bool) (io.Reader, string, error)

func ParseConflictStyle added in v0.16.0

func ParseConflictStyle(s string) int

func ReadUnifiedText added in v0.16.0

func ReadUnifiedText(r io.Reader, size int64, textconv bool) (content string, charset string, err error)

Types

type Algorithm

type Algorithm int
const (
	Unspecified Algorithm = iota
	Histogram
	ONP
	Myers
	Minimal
	Patience
)

func AlgorithmFromName added in v0.16.0

func AlgorithmFromName(s string) (Algorithm, error)

func (Algorithm) String

func (a Algorithm) String() string

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 File

type File struct {
	Name string `json:"name"`
	Hash string `json:"hash"`
	Mode uint32 `json:"mode"`
}

type FileStat

type FileStat struct {
	Addition, Deletion, Hunks int
}

func Stat

func Stat(ctx context.Context, opts *Options) (*FileStat, error)

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.

func (Hunk) Stat

func (h Hunk) Stat() (int, int)

type Lcs

type Lcs struct {
	// contains filtered or unexported fields
}

type LcsSearch

type LcsSearch[E comparable] struct {
	// contains filtered or unexported fields
}

type Line

type Line struct {
	Kind    Operation `json:"kind"`
	Content string    `json:"content"`
}

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 Operation

type Operation int8

Operation defines the operation of a diff item.

const (
	// Delete item represents a delete hunk.
	Delete Operation = -1
	// Insert item represents an insert hunk.
	Insert Operation = 1
	// Equal item represents an equal hunk.
	Equal Operation = 0
)

type Options

type Options struct {
	From, To *File
	S1, S2   string
	R1, R2   io.Reader
	A        Algorithm // algorithm
}

type Sink

type Sink struct {
	Lines   []string
	Index   map[string]int
	NewLine int
}

func NewSink

func NewSink(newLineMode int) *Sink

func (*Sink) AsStringDiff

func (s *Sink) AsStringDiff(o []Dfio[int]) []StringDiff

func (*Sink) ScanLines

func (s *Sink) ScanLines(r io.Reader) ([]int, error)

func (*Sink) ScanRawLines

func (s *Sink) ScanRawLines(r io.Reader) ([]int, error)

func (*Sink) SplitLines

func (s *Sink) SplitLines(text string) []int

func (*Sink) SplitRawLines

func (s *Sink) SplitRawLines(text string) []int

func (*Sink) ToUnified

func (s *Sink) ToUnified(from, to *File, changes []Change, linesA, linesB []int, contextLines int) *Unified

func (*Sink) WriteLine

func (s *Sink) WriteLine(w io.Writer, E ...int)

type SnakePath

type SnakePath struct {
	// contains filtered or unexported fields
}

func NewSnakePath

func NewSnakePath(pre *SnakePath, x, y, length int) *SnakePath

type StringDiff

type StringDiff struct {
	Type Operation
	Text string
}

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.

func DoUnified

func DoUnified(ctx context.Context, opts *Options) (*Unified, error)

func (Unified) Stat

func (u Unified) Stat() FileStat

func (Unified) String

func (u Unified) String() string

String converts a unified diff to the standard textual form for that diff. The output of this function can be passed to tools like patch.

type UnifiedEncoder

type UnifiedEncoder struct {
	io.Writer
	// contains filtered or unexported fields
}

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.

Directories

Path Synopsis
package lcs contains code to find longest-common-subsequences (and diffs)
package lcs contains code to find longest-common-subsequences (and diffs)

Jump to

Keyboard shortcuts

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