Coder Social home page Coder Social logo

go-udiff's Introduction

µ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.

package main

import (
    "fmt"

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

func main() {
    a := "Hello, world!\n"
    b := "Hello, Go!\nSay hi to µDiff"
    edits := myers.ComputeEdits(a, b)
    unified, _ := udiff.ToUnified("a.txt", "b.txt", a, edits)
    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"
    "github.com/aymanbagabas/go-udiff/myers"
)

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

    edits := myers.ComputeEdits(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"
    "github.com/aymanbagabas/go-udiff/myers"
)

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

    edits := myers.ComputeEdits(a, b)
    d, err := udiff.ToUnifiedDiff("a.txt", "b.txt", a, edits)
    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.

go-udiff's People

Contributors

aymanbagabas avatar steverusso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.