diff3

package module
v0.0.0-...-1cdef3e Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 6 Imported by: 2

README

diff3

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

How to use:

Import the package and call Merge:

Merge(a, o, b io.Reader, detailed bool, labelA string, labelB string) (*MergeResult, error)

Where:

  • a and b are the current and incoming versions of content
  • o is the common ancestor
  • detailed specifies whether you want the merge conflicts to be detailed.
  • labelA and labelB indicate a label to mark conflicts with, usually the branch names.

Returns a MergeResult that includes a flag indicating whether there were conflicts or not and a io.Reader stream with the result.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conflict

type Conflict[T any] struct {
	A      []T
	AIndex int
	O      []T
	OIndex int
	B      []T
	BIndex int
}

Conflict describes a merge conflict

type Diff3MergeResult

type Diff3MergeResult[T any] struct {
	Ok       []T
	Conflict *Conflict[T]
}

Diff3MergeResult describes a merge result

func Diff3Merge

func Diff3Merge[T comparable](a, o, b []T, excludeFalseConflicts bool) []*Diff3MergeResult[T]

Diff3Merge applies the output of diff3MergeIndices to actually construct the merged file; the returned result alternates between 'Ok' and 'Conflict' blocks.

type DiffResult

type DiffResult[T any] struct {
	Common []T
	File1  []T
	File2  []T
}

func DiffComm

func DiffComm[T comparable](file1, file2 []T) []*DiffResult[T]

We apply the LCS to build a 'comm'-style picture of the differences between file1 and file2.

type MergeIndices

type MergeIndices struct {
	Side    Side
	AOffset int
	ALength int
	OOffset int
	OLength int
	BOffset int
	BLength int
}

func Diff3MergeIndices

func Diff3MergeIndices[T comparable](a, o, b []T) []MergeIndices

Given three files, A, O, and B, where both A and B are independently derived from O, returns a fairly complicated internal representation of merge decisions it's taken. The interested reader may wish to consult

Sanjeev Khanna, Keshav Kunal, and Benjamin C. Pierce. 'A Formal Investigation of ' In Arvind and Prasad, editors, Foundations of Software Technology and Theoretical Computer Science (FSTTCS), December 2007.

(http://www.cis.upenn.edu/~bcpierce/papers/diff3-short.pdf)

type MergeResult

type MergeResult struct {
	Conflicts bool      // Conflict indicates if there is any merge conflict
	Result    io.Reader // returns a reader that contains the merge result
}

MergeResult describes a merge result

func Merge

func Merge(a, o, b io.Reader, detailed bool, labelA string, labelB string) (*MergeResult, error)

Merge takes three streams and returns the merged result

type Side

type Side int
const (
	SideConflict Side = iota - 1
	SideA
	SideCommon
	SideB
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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