MCPcopy Index your code
hub / github.com/aymanbagabas/go-udiff

github.com/aymanbagabas/go-udiff @v0.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.4.1 ↗ · + Follow
142 symbols 334 edges 23 files 74 documented · 52% 16 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

µDiff

Latest Release Go Docs Build Status Go Report Card

Micro diff (µDiff) is a Go library that implements the Myers' diffing algorithm. It aims to provide a minimal API to compute and apply diffs with zero dependencies. It also supports generating diffs in the Unified Format. If you are looking for a way to parse unified diffs, check out sourcegraph/go-diff.

This is merely a copy of the Golang tools internal diff package with a few modifications to export package symbols. All credit goes to the Go authors.

Usage

You can import the package using the following command:

go get github.com/aymanbagabas/go-udiff

Examples

Generate a unified diff for strings a and b with the default number of context lines (3). Use udiff.ToUnified to specify the number of context lines.

package main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
)

func main() {
    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 main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
)

func main() {
    a := "Hello, world!\n"
    b := "Hello, Go!\nSay hi to µDiff"

    edits := udiff.Strings(a, b)
    final, err := udiff.Apply(a, edits)
    if err != nil {
        panic(err)
    }

    fmt.Println(final)
}
Hello, Go!
Say hi to µDiff

To get a line-by-line diff and edits:

package main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
)

func main() {
    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)
    if err != nil {
        panic(err)
    }

    for _, h := range d.Hunks {
        fmt.Printf("hunk: -%d, +%d\n", h.FromLine, h.ToLine)
        for _, l := range h.Lines {
            fmt.Printf("%s %q\n", l.Kind, l.Content)
        }
    }
}
hunk: -1, +1
delete "Hello, world!\n"
insert "Hello, Go!\n"
insert "Say hi to µDiff"

Alternatives

Contributing

Please send any contributions upstream. Pull requests made against the upstream diff package are welcome.

License

BSD 3-Clause and MIT.

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

compute
called by 21
lcs/old.go
get
called by 14
lcs/labels.go
getForward
called by 13
lcs/old.go
getBackward
called by 13
lcs/old.go
valid
called by 11
lcs/common.go
setForward
called by 10
lcs/old.go
backwardlcs
called by 10
lcs/old.go
setBackward
called by 10
lcs/old.go

Shape

Function 80
Method 42
Struct 14
TypeAlias 5
Interface 1

Languages

Go100%

Modules by API surface

lcs/old.go23 symbols
lcs/sequence.go18 symbols
lcs/old_test.go13 symbols
lcs/common_test.go13 symbols
unified.go12 symbols
diff.go12 symbols
lcs/common.go10 symbols
diff_test.go9 symbols
ndiff.go8 symbols
myers/diff.go8 symbols
lcs/labels.go5 symbols
difftest/difftest_test.go2 symbols

For agents

$ claude mcp add go-udiff \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact