merge

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package merge implements three-way merge for text files.

A three-way merge uses three versions of a file:

  • Base: the common ancestor version (what both sides started from)
  • Local: the version with our changes
  • Remote: the version with their changes

The algorithm computes line-level diffs from base→local and base→remote, then walks through both diffs simultaneously to classify each region:

  • Neither side changed → keep base lines
  • Only one side changed → take that side's changes (auto-merge)
  • Both sides changed identically → take either (they agree)
  • Both sides changed differently → conflict (insert markers around just those lines)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conflict

type Conflict struct {
	// OutputStartLine is the 1-based line number in the merged output
	// where the <<<<<<< marker appears.
	OutputStartLine int

	// LocalLines are the local side's lines for this conflict.
	LocalLines []string

	// RemoteLines are the remote side's lines for this conflict.
	RemoteLines []string
}

Conflict describes a single conflicting region in the merged output.

type Result

type Result struct {
	// Content is the merged file content. If there are conflicts,
	// the conflicting regions are wrapped in conflict markers.
	Content []byte

	// HasConflicts is true if any regions could not be auto-merged.
	HasConflicts bool

	// Conflicts lists each conflicting region in the output.
	Conflicts []Conflict

	// AutoResolved is the number of regions where only one side changed
	// and the change was applied automatically.
	AutoResolved int
}

Result holds the outcome of a three-way merge.

func ThreeWay

func ThreeWay(base, local, remote []byte, remoteName string) *Result

ThreeWay performs a three-way merge of base, local, and remote content. remoteName is used to annotate the conflict marker (e.g., "origin").

The merge works at line granularity:

  1. Compute edit scripts base→local and base→remote
  2. Convert each edit script into a sequence of "edit regions" — contiguous groups of lines that were changed together
  3. Walk both edit region lists simultaneously against the base, detecting overlaps and classifying each region
  4. Produce merged output with inline conflict markers only where needed

Jump to

Keyboard shortcuts

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