patience

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2022 License: MIT Imports: 2 Imported by: 2

README

patience

CI GoReportCard GoDoc

Go implementation of the Patience Diff algorithm.

This library generates line-oriented diffs between source and destination inputs, using the Patience Diff algorithm.

Features

Supports both plain format and Unified format (unidiff).

Plain format:

 the
 quick
 brown
-chicken
+fox
 jumps
 over
 the
+lazy
 dog

Unified format (unidiff):

--- a.txt
+++ b.txt
@@ -3,3 +3,3 @@
 brown
-chicken
+fox
 jumps
@@ -7,2 +7,3 @@
 the
+lazy
 dog

Installation

go get github.com/peter-evans/patience

Usage

a := strings.Split(textA, "\n")
b := strings.Split(textB, "\n")

diffs := patience.Diff(a, b)

// Combined diff
diff := patience.DiffText(diffs)

// Split diffs
diffA := patience.DiffTextA(diffs)
diffB := patience.DiffTextB(diffs)

// Unified diff
unidiff := patience.UnifiedDiffText(diffs)

// Unified diff with options
unidiffopts := patience.UnifiedDiffTextWithOptions(
     diffs,
     UnifiedDiffOptions{
          Precontext: 2, 
          Postcontext: 2,
          SrcHeader:   "a.txt",
          DstHeader:   "b.txt",
     },
)

About

Patience Diff is an algorithm credited to Bram Cohen that produces diffs tending to be more human-readable than the common diff algorithm.

The common diff algorithm is based on the longest common subsequence problem. It is credited to Eugene Myers and is the default diff algorithm in Git. While the diffs generated by this algorithm are efficient, in many cases they tend not to correspond to what humans would naturally identify.

Patience Diff, while also relying on computing the longest common subsequence, takes a different approach. It only computes the longest common subsequence of the unique, common elements of both texts. This means that lines that are frequently non-unique, such as those containing a single brace or new line character, are ignored. The result is that distinctive lines, such as function declarations, become the anchor points of commonality between the two texts.

This is an example comparing Patience Diff to the common diff algorithm (Myers).

Patience Diff

 #include <stdio.h>
 
+int fib(int n)
+{
+    if(n > 2)
+    {
+        return fib(n-1) + fib(n-2);
+    }
+    return 1;
+}
+
 // Frobs foo heartily
 int frobnitz(int foo)
 {
     int i;
     for(i = 0; i < 10; i++)
     {
-        printf("Your answer is: ");
         printf("%d\n", foo);
     }
 }
 
-int fact(int n)
-{
-    if(n > 1)
-    {
-        return fact(n-1) * n;
-    }
-    return 1;
-}
- 
 int main(int argc, char **argv)
 {
-    frobnitz(fact(10));
+    frobnitz(fib(10));
 }

Common diff (Myers)

 #include <stdio.h>
 
-// Frobs foo heartily
-int frobnitz(int foo)
+int fib(int n)
 {
-    int i;
-    for(i = 0; i < 10; i++)
+    if(n > 2)
     {
-        printf("Your answer is: ");
-        printf("%d\n", foo);
+        return fib(n-1) + fib(n-2);
     }
+    return 1;
 }
 
-int fact(int n)
+// Frobs foo heartily
+int frobnitz(int foo)
 {
-    if(n > 1)
+    int i;
+    for(i = 0; i < 10; i++)
     {
-        return fact(n-1) * n;
+        printf("%d\n", foo);
     }
-    return 1;
 }
 
 int main(int argc, char **argv)
 {
-    frobnitz(fact(10));
+    frobnitz(fib(10));
 }

References

Documentation

Overview

Package patience implements the Patience Diff algorithm.

Package patience implements the Patience Diff algorithm.

Package patience implements the Patience Diff algorithm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffText

func DiffText(diffs []DiffLine) string

DiffText returns the source and destination texts (all equalities, insertions and deletions).

func DiffTextA added in v0.2.0

func DiffTextA(diffs []DiffLine) string

DiffTextA returns the source text (all equalities and deletions).

func DiffTextB added in v0.2.0

func DiffTextB(diffs []DiffLine) string

DiffTextB returns the destination text (all equalities and insertions).

func LCS

func LCS(a, b []string) [][2]int

LCS computes the longest common subsequence of two string slices and returns the index pairs of the LCS.

func UnifiedDiffText added in v0.3.0

func UnifiedDiffText(diffs []DiffLine) string

UnifiedDiffText returns the diff text in unidiff format with a context of 3 lines.

func UnifiedDiffTextWithOptions added in v0.3.0

func UnifiedDiffTextWithOptions(diffs []DiffLine, opts UnifiedDiffOptions) string

UnifiedDiffTextWithOptions returns the diff text in unidiff format.

Types

type DiffLine

type DiffLine struct {
	Text string
	Type DiffType
}

DiffLine represents a single line and its diff type.

func Diff

func Diff(a, b []string) []DiffLine

Diff returns the patience diff of two slices of strings.

type DiffType

type DiffType int8

DiffType defines the type of a diff element.

const (
	// Delete represents a diff delete operation.
	Delete DiffType = -1
	// Insert represents a diff insert operation.
	Insert DiffType = 1
	// Equal represents no diff.
	Equal DiffType = 0
)

type Hunk added in v0.3.0

type Hunk struct {
	Diffs    []DiffLine
	SrcStart int
	SrcLines int
	DstStart int
	DstLines int
}

Hunk represents a subsection of a diff.

type UnifiedDiffOptions added in v0.3.0

type UnifiedDiffOptions struct {
	// Precontext is the number of lines of context before each change in a hunk.
	Precontext int
	// Postcontext is the number of lines of context after each change in a hunk.
	Postcontext int
	// SrcHeader is the header for the source file.
	SrcHeader string
	// DstHeader is the header for the destination file.
	DstHeader string
}

UnifiedDiffOptions represents the options for UnifiedDiffTextWithOptions.

Jump to

Keyboard shortcuts

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