Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Incorrect line numbers in unified hunk headers #6

@keithpang1238

Description

@keithpang1238

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions