udiff

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package udiff provides parsing and application of unified diffs with elision support.

The format supports:

  • Standard unified diff headers (--- and +++)
  • Context headers (@@ func Name @@) for locating code
  • Elision markers (-...) for matching large blocks without listing every line
  • Context lines (space prefix), remove lines (-), and add lines (+)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(content string, hunks []Hunk) (string, error)

Apply applies the hunks to the given content and returns the modified content. Returns an error on the first failed hunk (strict mode).

func ApplyFileDiffs

func ApplyFileDiffs(files map[string]string, diffs []FileDiff) (map[string]string, error)

ApplyFileDiffs applies multiple file diffs to a map of file contents. Returns a new map with the modified contents.

Types

type ApplyResult added in v0.0.7

type ApplyResult struct {
	Content  string   // Modified content (with successful hunks applied)
	Warnings []string // Warnings for hunks that failed to apply
}

ApplyResult contains the result of applying hunks with warnings for failures.

func ApplyWithWarnings added in v0.0.7

func ApplyWithWarnings(content string, hunks []Hunk) ApplyResult

ApplyWithWarnings applies hunks, skipping failures and collecting warnings. Returns modified content with all successful hunks applied.

type FileDiff

type FileDiff struct {
	Path  string // File path from --- and +++ headers
	Hunks []Hunk
}

FileDiff represents all changes to a single file.

func Parse

func Parse(diff string) ([]FileDiff, error)

Parse parses a unified diff string into a slice of FileDiff. The format expected is:

--- path/to/file
+++ path/to/file
@@ optional context @@
 context line
-removed line
+added line
-...

Multiple files can be included in a single diff.

type Hunk

type Hunk struct {
	Context string // The @@ header content (e.g., "func ProcessData")
	Lines   []Line // All lines in order
}

Hunk represents a single change block within a file diff.

type Line

type Line struct {
	Type    LineType
	Content string // Content without the prefix
}

Line represents a single line in a diff hunk.

type LineType

type LineType int

LineType represents the type of a diff line.

const (
	// Context is an unchanged line (space prefix)
	Context LineType = iota
	// Remove is a line to be removed (- prefix)
	Remove
	// Add is a line to be added (+ prefix)
	Add
	// Elision is a marker that matches any content (-...)
	Elision
)

func (LineType) String

func (t LineType) String() string

String returns the line type as a string for debugging.

Jump to

Keyboard shortcuts

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