Documentation
¶
Overview ¶
Package diff says what changed between two texts, line by line.
It computes a line-oriented edit script without deciding how that script is stored, rendered or applied. Lines are the unit because line-oriented producers and formats can consume the result without reconstructing their boundaries.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hunk ¶
type Hunk struct {
Lines Script
// Old and New are where the hunk begins in each text, counting from one.
Old, New int
}
Hunk is a run of a script worth showing: what changed, and a few lines either side of it so a reader can see where they are.
type Kind ¶
type Kind uint8
Kind is what happened to a line.
What happened to a line. Context is the zero value, because a line that was left alone is what most lines are.
type Line ¶
type Line struct {
Kind Kind
Text string
// Old and New are the line's number in each text, counting from one, and zero
// where the line is not in that text. A reader looking for what to open and where
// needs the number in the text that still exists; a reader reading a change needs
// both.
Old, New int
}
Line is one line of a diff.
type Script ¶
type Script []Line
Script is a change to a text, as the lines that make it.
It reads top to bottom as the change itself: every line of both texts appears once, in an order in which the removals and the additions of the same passage sit together.
func Between ¶
Between is what changed between two texts.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
before := strings.Split("a\nb\nc", "\n")
after := strings.Split("a\nB\nc", "\n")
fmt.Print(diff.Between(before, after))
}
Output: a -b +B c
func (Script) Hunks ¶
Hunks is the changed parts of the script with context lines around them, and everything else left out.
A file that changed in two places may have hundreds of unchanged lines. Overlapping runs of context are one hunk rather than two, because a gap of one unchanged line is not a gap worth drawing a break across.
A context of zero is the changed lines alone. A script with nothing changed in it has no hunks at all, which is how "these are the same" is said.
Each hunk owns its line storage and text. Retaining a small changed passage therefore does not retain the complete script or the source documents its lines were sliced from, and changing the script after this call cannot change an already-built hunk.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
// A file that changed in one place is a hundred lines of which three matter, and
// hunks are what is left once the rest is dropped.
before := strings.Split("1\n2\n3\n4\n5\n6\n7", "\n")
after := strings.Split("1\n2\n3\nX\n5\n6\n7", "\n")
for _, hunk := range diff.Between(before, after).Hunks(1) {
fmt.Printf("@@ -%d +%d @@\n%s", hunk.Old, hunk.New, hunk)
}
}
Output: @@ -3 +3 @@ 3 -4 +X 5
func (Script) Same ¶
Same reports whether the two texts were the same, which is a script with nothing changed in it.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
same := strings.Split("a\nb", "\n")
fmt.Println(diff.Between(same, same).Same())
}
Output: true