textmerge

package
v0.22.0 Latest Latest
Warning

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

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

Documentation

Overview

Package textmerge merges three versions of one file's text: a base, and two sides ("ours" and "theirs") that each changed it. It's the text merger behind treemerge (docs/designs/treemerge.md), and usable alone, by anything merging a single file.

Its behaviour follows git's: `git merge-file --diff3`, at what git calls its "eager" level, is the reference. A change only one side made is taken. The same change on both sides is taken once. Changes that overlap, or touch with no unchanged line between them, are a conflict. git's code is not used: the rules are written here from its documented behaviour, and checked against git by the tests.

Text is handled as bytes, split into lines after each "\n", each line keeping its ending. Nothing is decoded or normalised, so a merge that takes every line from one side gives back that side's bytes exactly.

Index

Constants

View Source
const DefaultMarkerSize = 7

DefaultMarkerSize is git's marker size.

Variables

This section is empty.

Functions

func MarkerSize

func MarkerSize(versions ...[]byte) int

MarkerSize is the marker size for a conflict in these versions: 7, unless a line in one of them already starts with 7 or more of the same marker character, in which case it's that run's length + 4 (jj's rule), so no line of content can be mistaken for a marker. git takes the same size per path, as its conflict-marker-size attribute.

func Resolved

func Resolved(content []byte, size int) bool

Resolved says whether content is free of conflict markers of size: no line starts with a marker character repeated at least size times, followed by a space or the line's end. It's deliberately strict: a malformed marker line still counts, because what's resolved may be published, and a stray marker line never should be.

Types

type Hunk

type Hunk struct {
	// Conflict says which kind of hunk this is.
	Conflict bool
	// Lines are a clean hunk's lines, each with its line ending.
	Lines []string
	// Base, Ours and Theirs are a conflict's three versions of the stretch.
	// Any may be empty: a side that deleted it, or a base that had nothing
	// where both sides added lines.
	Base, Ours, Theirs []string
	// CRLF says whether a conflict's markers end their lines with "\r\n",
	// decided from the lines around it, as git decides.
	CRLF bool
}

Hunk is a stretch of a merged file: clean lines, or a conflict.

type Options

type Options struct {
	// Labels follow each marker's characters: "<<<<<<< ours (local)". Empty,
	// the marker stands alone.
	Ours, Base, Theirs string
	// MarkerSize is how many characters each marker has: 7 when 0. Use
	// MarkerSize to pick one that can't be confused with the content.
	MarkerSize int
}

Options are how conflicts are written.

type Result

type Result struct {
	Hunks     []Hunk
	Conflicts int
}

Result is a merge: its hunks, in order, and how many of them conflict.

func Merge

func Merge(base, ours, theirs []byte) Result

Merge merges ours and theirs, two changed versions of base.

func Whole

func Whole(base, ours, theirs []byte) Result

Whole is one conflict over the whole of each version, for a file one side changed and the other deleted: the deleted side is empty. Its markers' line endings are decided as Merge's are.

func (Result) Render

func (r Result) Render(o Options) []byte

Render writes a merge as text: clean hunks as they are, and conflicts between git's diff3 markers.

Jump to

Keyboard shortcuts

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