merge

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package merge implements line diffs, unified diff output and three-way merges of text files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasConflictMarkers added in v0.2.0

func HasConflictMarkers(lines []string) bool

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

func IsBinary(data []byte) bool

IsBinary reports whether data looks like binary content (a NUL byte in the first 8000 bytes, the same rule git uses).

func JoinLines

func JoinLines(lines []string) []byte

JoinLines is the inverse of SplitLines.

func LineStats

func LineStats(a, b []byte) (added, deleted int)

LineStats returns the added and deleted line counts between two texts. Binary content counts as zero lines.

func SplitLines

func SplitLines(data []byte) []string

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.

func Stats

func Stats(edits []Edit) (added, deleted int)

Stats counts added and deleted lines.

func Unified

func Unified(oldName, newName string, a, b []byte, context int) string

Unified renders a unified diff between a and b with the given number of context lines. Paths are written as "a/<path>" and "b/<path>"; an empty name means /dev/null (file added or deleted). It returns "" when equal.

Types

type Edit

type Edit struct {
	Kind   EditKind
	A0, A1 int
	B0, B1 int
}

Edit is a run of lines: Equal covers a[A0:A1] == b[B0:B1], Delete removes a[A0:A1], Insert adds b[B0:B1].

func Diff

func Diff(a, b []string) []Edit

Diff returns the shortest edit script from a to b (Myers' algorithm, with the common prefix and suffix trimmed first).

type EditKind

type EditKind int

EditKind is the kind of an edit.

const (
	Equal EditKind = iota
	Delete
	Insert
)

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

func Merge3(base, ours, theirs []string, labels Labels) Result

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

func Merge3Bytes(base, ours, theirs []byte, labels Labels) (Result, bool)

Merge3Bytes is Merge3 on raw file contents. It reports ok=false without merging when any side is binary and the sides differ.

func (Result) Bytes

func (r Result) Bytes() []byte

Bytes returns the merged text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL