hexops/gotextdiff

Incorrect line numbers in unified hunk headers

keithpang1238 opened this issue · 0 comments

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.