- Notifications
You must be signed in to change notification settings - Fork6
µDiff - a micro Go diffing library
License
BSD-3-Clause, MIT licenses found
Licenses found
aymanbagabas/go-udiff
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Micro diff (µDiff) is a Go library that implements theMyers' diffing algorithm. It aims toprovide a minimal API to compute and apply diffs with zero dependencies. Italso supports generating diffs in theUnified Format.If you are looking for a way to parse unified diffs, check outsourcegraph/go-diff.
This is merely a copy of theGolang tools internal diff packagewith a few modifications to export package symbols. All credit goes to theGo authors.
You can import the package using the following command:
go get github.com/aymanbagabas/go-udiff
Generate a unified diff for stringsa
andb
with the default number ofcontext lines (3). Useudiff.ToUnified
to specify the number of contextlines.
package mainimport ("fmt""github.com/aymanbagabas/go-udiff")funcmain() {a:="Hello, world!\n"b:="Hello, Go!\nSay hi to µDiff"unified:=udiff.Unified("a.txt","b.txt",a,b)fmt.Println(unified)}
--- a.txt+++ b.txt@@ -1 +1,2 @@-Hello, world!+Hello, Go!+Say hi to µDiff\ No newline at end of file
Apply changes to a string.
package mainimport ("fmt""github.com/aymanbagabas/go-udiff")funcmain() {a:="Hello, world!\n"b:="Hello, Go!\nSay hi to µDiff"edits:=udiff.Strings(a,b)final,err:=udiff.Apply(a,edits)iferr!=nil {panic(err) }fmt.Println(final)}
Hello, Go!Say hi to µDiff
To get a line-by-line diff and edits:
package mainimport ("fmt""github.com/aymanbagabas/go-udiff")funcmain() {a:="Hello, world!\n"b:="Hello, Go!\nSay hi to µDiff"edits:=udiff.Strings(a,b)d,err:=udiff.ToUnifiedDiff("a.txt","b.txt",a,edits,udiff.DefaultContextLines)iferr!=nil {panic(err) }for_,h:=ranged.Hunks {fmt.Printf("hunk: -%d, +%d\n",h.FromLine,h.ToLine)for_,l:=rangeh.Lines {fmt.Printf("%s %q\n",l.Kind,l.Content) } }}
hunk: -1, +1delete "Hello, world!\n"insert "Hello, Go!\n"insert "Say hi to µDiff"
- sergi/go-diff No longer reliable. See#123 and#141.
- hexops/gotextdiff Takes the same approach but looks like the project is abandoned.
- sourcegraph/go-diff It doesn't compute diffs. Great package for parsing and printing unified diffs.
Please send any contributionsupstream. Pullrequests made againstthe upstream diff packageare welcome.
BSD 3-Clause andMIT.
About
µDiff - a micro Go diffing library