hexops / gotextdiff Goto Github PK
View Code? Open in Web Editor NEWUnified text diffing in Go (copy of the internal diffing packages the officlal Go language server uses)
License: BSD 3-Clause "New" or "Revised" License
Unified text diffing in Go (copy of the internal diffing packages the officlal Go language server uses)
License: BSD 3-Clause "New" or "Revised" License
Currently, there are hardcoded constants that define the amount of contextual information in a diff (3 lines above and below the change). It would be useful to have control over these variables.
Let me know if you would be willing to merge such a change, I am happy to open a PR.
I believe the hunk headers (starting and ending with @@
) should function similarly to those that occur when running git diff
. However, this is not what occurs in the following example (Using version 1.0.3 with go 1.20).
Code
package main
import (
"fmt"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
)
var old = `line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
`
var new = `line1
line_CHANGED
line3
line4
line_CHANGED
line_CHANGED
line7
line8
line9
line10
line11
line12
line13
line14
line_CHANGED
line16
`
func main() {
edits := myers.ComputeEdits(span.URIFromPath("a.txt"), old, new)
unified := gotextdiff.ToUnified("a.txt", "a.txt", old, edits)
fmt.Println(unified)
}
Output
--- a.txt
+++ a.txt
@@ -1,9 +1,9 @@
line1
-line2
+line_CHANGED
line3
line4
-line5
-line6
+line_CHANGED
+line_CHANGED
line7
line8
line9
@@ -12,5 +10,5 @@
line12
line13
line14
-line15
+line_CHANGED
line16
Output when running git diff --no-index test1.txt test2.txt
, where test1.txt
and test2.txt
have the same contents as old
and new
respectively
--- a/test1.txt
+++ b/test2.txt
@@ -1,9 +1,9 @@
line1
-line2
+line_CHANGED
line3
line4
-line5
-line6
+line_CHANGED
+line_CHANGED
line7
line8
line9
@@ -12,5 +12,5 @@ line11
line12
line13
line14
-line15
+line_CHANGED
line16
I would expect the second hunk header to be @@ -12,5 +12,5 @@
(as provided by git) instead of @@ -12,5 +10,5 @@
since the hunk below the header starts on line 12 for both the previous and modified content.
https://github.com/golang/tools/tree/master/internal/lsp/diff gives a 404, would you mind updating the README to reflect where you would like contributions to be sent?
Hello,
I'm curious if a better merge algorithm would be considered in-scope. Right now the patch always applies to the same line position regardless of context. When I found this package I was looking for something with more similar behaviour to Git, but I wasn't able to find such a thing and will need to implement it myself.
In the README, it writes that the output will be shown in colors
--- a.txt
+++ b.txt
@@ -1,13 +1,28 @@
-foo
+bar
However, there is no color if I copy-paste the code example.
--- a.txt
+++ b.txt
@@ -1,13 +1,28 @@
-foo
+bar
If that intended? If there is no support, it would be nice to add support for colors.
Just comparing 2 solaris pkginfo output used about 1.5Gb of memory (attached).
edits := myers.ComputeEdits(span.URIFromPath(""), PrevContent, Content)
unifiedPatch := gotextdiff.ToUnified("src", "dst", PrevContent, edits)
var DiffContent = fmt.Sprint(unifiedPatch)
fmt.Printf("!!!! len(DiffContent):%+v\n", len(DiffContent))
/usr/bin/time -v go run scratch.go
!!!! len(DiffContent):1152378
Command being timed: "go run scratch.go"
User time (seconds): 2.07
System time (seconds): 2.51
Percent of CPU this job got: 102%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.46
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 15095320
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 5
Minor (reclaiming a frame) page faults: 3793204
Voluntary context switches: 3058
Involuntary context switches: 434
Swaps: 0
File system inputs: 0
File system outputs: 5640
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.