Documentation
¶
Overview ¶
Package merge implements line diffs, unified diff output and three-way merges of text files.
Index ¶
- func HasConflictMarkers(lines []string) bool
- func IsBinary(data []byte) bool
- func JoinLines(lines []string) []byte
- func LineStats(a, b []byte) (added, deleted int)
- func SplitLines(data []byte) []string
- func Stats(edits []Edit) (added, deleted int)
- func Unified(oldName, newName string, a, b []byte, context int) string
- type Edit
- type EditKind
- type Labels
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasConflictMarkers ¶ added in v0.2.0
HasConflictMarkers reports whether lines contain unresolved conflict markers: a "<<<<<<< ", a "=======" and a ">>>>>>> " line, in that order (the rule vx land uses to refuse files with markers).
func IsBinary ¶
IsBinary reports whether data looks like binary content (a NUL byte in the first 8000 bytes, the same rule git uses).
func LineStats ¶
LineStats returns the added and deleted line counts between two texts. Binary content counts as zero lines.
func SplitLines ¶
SplitLines splits text into lines that keep their "\n", so joining the lines gives back the exact input. A final line without "\n" is kept.
Types ¶
type Edit ¶
Edit is a run of lines: Equal covers a[A0:A1] == b[B0:B1], Delete removes a[A0:A1], Insert adds b[B0:B1].
type Labels ¶
type Labels struct {
Base, Ours, Theirs string
}
Labels name the sides in conflict markers.
type Result ¶
type Result struct {
Lines []string // merged text; contains conflict markers if Conflicts > 0
Conflicts int // number of conflicting regions
// KeptBoth counts regions where both sides inserted different lines at
// the same place and the merge kept both insertions.
KeptBoth int
// BothDeleted counts regions where each side deleted different,
// neighbouring lines and the merge applied both deletions.
BothDeleted int
}
Result is the outcome of a three-way merge.
func Merge3 ¶
Merge3 merges ours and theirs, which both started from base. Regions changed on one side only take that side. Regions changed identically on both sides take the change once.
Regions where both sides only inserted lines at the same place (the base region is empty) keep both insertions: theirs first, then ours. Callers pass the side that landed earlier (the effort) as theirs. Nothing either side had is lost; only the order may need a look. This does not apply when base is empty (both sides created the file) or when ours or theirs already contains conflict markers.
Regions where both sides only deleted lines, and no line was deleted by both (neighbouring but separate blocks), apply both deletions. Overlapping deletions, or a deletion next to an edit, stay conflicts.
Other regions changed differently on both sides are conflicts and are written with diff3-style markers.
func Merge3Bytes ¶
Merge3Bytes is Merge3 on raw file contents. It reports ok=false without merging when any side is binary and the sides differ.