Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

µDiff - a micro Go diffing library

License

BSD-3-Clause, MIT licenses found

Licenses found

BSD-3-Clause
LICENSE-BSD
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

aymanbagabas/go-udiff

Repository files navigation

Latest ReleaseGo DocsBuild StatusGo Report Card

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.

Usage

You can import the package using the following command:

go get github.com/aymanbagabas/go-udiff

Examples

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"

Alternatives

Contributing

Please send any contributionsupstream. Pullrequests made againstthe upstream diff packageare welcome.

License

BSD 3-Clause andMIT.


[8]ページ先頭

©2009-2025 Movatter.jp