merge
gedw99 opened this issue · 2 comments
gedw99 commented
just playing with this.
can i get a merge out of this ? Given the 2 files, can i actually merge to a final.txt ?
-- edits --
[{Start:25,End:27,New:CHANGED} {Start:29,End:38,New:} {Start:51,End:51,New:
full example !!
main.go:
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/aymanbagabas/go-udiff"
)
func main() {
a := "Hello, world!"
b := "Hello, Go!"
edits := udiff.Strings(a, b)
d, err := udiff.ToUnifiedDiff("a.txt", "b.txt", a, edits)
if err != nil {
panic(err)
}
fmt.Println(d.String())
txtSmall()
}
func txtSmall() {
fmt.Println("-- txt small --")
a := fileToString("small/test-01.txt")
b := fileToString("small/test-02.txt")
edits := udiff.Strings(a, b)
d, err := udiff.ToUnifiedDiff("a.txt", "b.txt", a, edits)
if err != nil {
panic(err)
}
fmt.Println(d.String())
}
func fileToString(file string) string {
fmt.Println(file)
content, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
// Convert []byte to string and print to screen
text := string(content)
fmt.Println(text)
return text
}
small/test-01.txt:
mid=50
ty=90
titleX=50
titleY=92
titleSize=4
small/test-02.txt:
mid=50
ty=90
titleX=CHANGED
titleSize=4
NEW=xxxx
result:
go run .
--- a.txt
+++ b.txt
@@ -1 +1 @@
-Hello, world!
\ No newline at end of file
+Hello, Go!
\ No newline at end of file
-- txt small --
small/test-01.txt
mid=50
ty=90
titleX=50
titleY=92
titleSize=4
small/test-02.txt
mid=50
ty=90
titleX=CHANGED
titleSize=4
NEW=xxxx
--- a.txt
+++ b.txt
@@ -2,6 +2,8 @@
mid=50
ty=90
- titleX=50
+ titleX=CHANGED
- titleY=92
+
- titleSize=4
+ titleSize=4
+
+ NEW=xxxx
aymanbagabas commented
Hi @gedw99, you can use the Apply
function to apply the changes to a string. https://go.dev/play/p/Z_Qqx2OyMvj.
-- a.txt --
mid=50
ty=90
titleX=50
titleY=92
titleSize=4
-- b.txt --
mid=50
ty=90
titleX=CHANGED
titleSize=4
NEW=xxxx
-- diff --
--- a.txt
+++ b.txt
@@ -1,5 +1,7 @@
mid=50
ty=90
-titleX=50
+titleX=CHANGED
-titleY=92
+
-titleSize=4
+titleSize=4
+
+NEW=xxxx
-- final.txt --
mid=50
ty=90
titleX=CHANGED
titleSize=4
NEW=xxxx
For more information, take a look at the API documents https://pkg.go.dev/github.com/aymanbagabas/go-udiff
gedw99 commented
@aymanbagabas works .. thank you for the help