diff

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package diff provides types and functions for parsing and manipulating unified diffs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(lines iter.Seq[DiffLine], pred func(DiffLine) bool) bool

All returns true if all lines match the predicate.

func Any

func Any(lines iter.Seq[DiffLine], pred func(DiffLine) bool) bool

Any returns true if any line matches the predicate.

func ChunkByOp

func ChunkByOp(lines iter.Seq[DiffLine]) iter.Seq[[]DiffLine]

ChunkByOp groups consecutive lines by their operation type.

func CountLines

func CountLines(lines iter.Seq[DiffLine]) int

CountLines counts the number of lines in an iterator.

func FilteredLines

func FilteredLines(
	lines iter.Seq[DiffLine], pred func(DiffLine) bool,
) iter.Seq[DiffLine]

FilteredLines returns an iterator over lines matching a predicate.

func ForEach

func ForEach(lines iter.Seq[DiffLine], fn func(DiffLine))

ForEach applies a function to each line.

func LinesInRange

func LinesInRange(lines iter.Seq[DiffLine], start, end int) iter.Seq[DiffLine]

LinesInRange returns an iterator over lines with NewLineNum in the range.

func MapLines

func MapLines[T any](
	lines iter.Seq[DiffLine], fn func(DiffLine) T,
) iter.Seq[T]

MapLines transforms lines using a mapping function.

func SelectedLines

func SelectedLines(
	lines iter.Seq[DiffLine], sel *FileSelection,
) iter.Seq[DiffLine]

SelectedLines returns lines matching any of the selections.

func SkipLines

func SkipLines(lines iter.Seq[DiffLine], n int) iter.Seq[DiffLine]

SkipLines returns an iterator that skips the first n lines.

func TakeLines

func TakeLines(lines iter.Seq[DiffLine], n int) iter.Seq[DiffLine]

TakeLines returns an iterator that yields at most n lines.

func ZipWithIndex

func ZipWithIndex(lines iter.Seq[DiffLine]) iter.Seq2[int, DiffLine]

ZipWithIndex pairs each line with its index.

Types

type DiffLine

type DiffLine struct {
	// Op is the type of operation (context, add, delete).
	Op LineOp

	// Content is the line content without the prefix (+/-/space).
	Content string

	// OldLineNum is the line number in the original file.
	// Zero if this is an added line.
	OldLineNum int

	// NewLineNum is the line number in the new file.
	// Zero if this is a deleted line.
	NewLineNum int
}

DiffLine represents a single line in a diff hunk.

func CollectLines

func CollectLines(lines iter.Seq[DiffLine]) []DiffLine

CollectLines collects all lines from an iterator into a slice.

func FindFirst

func FindFirst(
	lines iter.Seq[DiffLine], pred func(DiffLine) bool,
) (DiffLine, bool)

FindFirst returns the first line matching the predicate.

func (DiffLine) EffectiveLineNum

func (l DiffLine) EffectiveLineNum() int

EffectiveLineNum returns the relevant line number for selection purposes. For added lines, returns NewLineNum. For deleted and context lines, returns OldLineNum.

func (DiffLine) Format

func (l DiffLine) Format() string

Format returns a human-readable format with line numbers.

func (DiffLine) IsChange

func (l DiffLine) IsChange() bool

IsChange returns true if this line represents a change (add or delete).

func (DiffLine) LineRef

func (l DiffLine) LineRef() string

LineRef returns a parseable reference for this line. Format: "OLD:NEW" where missing numbers are represented as "-".

func (DiffLine) String

func (l DiffLine) String() string

String returns the line in unified diff format.

type FileDiff

type FileDiff struct {
	// OldName is the path of the original file (with a/ prefix stripped).
	OldName string

	// NewName is the path of the new file (with b/ prefix stripped).
	NewName string

	// Hunks contains all hunks in this file diff.
	Hunks []*Hunk

	// IsBinary is true if this is a binary file.
	IsBinary bool

	// IsNew is true if this is a new file.
	IsNew bool

	// IsDeleted is true if this file is being deleted.
	IsDeleted bool

	// IsRenamed is true if this file was renamed.
	IsRenamed bool
}

FileDiff represents all changes to a single file.

func (*FileDiff) AllChanges

func (f *FileDiff) AllChanges() iter.Seq2[int, DiffLine]

AllChanges returns an iterator over only changed lines across all hunks.

func (*FileDiff) AllHunks

func (f *FileDiff) AllHunks() iter.Seq2[int, *Hunk]

AllHunks returns an iterator over all hunks with their indices.

func (*FileDiff) AllLines

func (f *FileDiff) AllLines() iter.Seq2[int, DiffLine]

AllLines returns an iterator over all lines across all hunks. Yields (hunk index, line) pairs.

func (*FileDiff) Format

func (f *FileDiff) Format() string

Format returns the file diff in unified diff format.

func (*FileDiff) HunkContainingLine

func (f *FileDiff) HunkContainingLine(lineNum int) *Hunk

HunkContainingLine finds the hunk containing a change at the given line. Returns nil if no hunk contains a change at that line.

func (*FileDiff) HunksInRange

func (f *FileDiff) HunksInRange(start, end int) []*Hunk

HunksInRange returns all hunks that have changes within the given range.

func (*FileDiff) Path

func (f *FileDiff) Path() string

Path returns the canonical file path. Uses NewName for additions, OldName for deletions, NewName otherwise.

func (*FileDiff) Stats

func (f *FileDiff) Stats() (added, deleted int)

Stats returns total addition and deletion counts across all hunks.

type FileSelection

type FileSelection struct {
	Path   string
	Ranges []LineRange
}

FileSelection represents selected lines for a file.

func ParseFileSelection

func ParseFileSelection(s string) (*FileSelection, error)

ParseFileSelection parses "FILE:LINES" syntax. Examples:

  • "main.go:10-20" - lines 10 through 20
  • "main.go:10,15,20-25" - lines 10, 15, and 20-25
  • "main.go:10" - just line 10

func ParseSelections

func ParseSelections(args []string) ([]*FileSelection, error)

ParseSelections parses multiple FILE:LINES arguments.

func (*FileSelection) AllLines

func (fs *FileSelection) AllLines() []int

AllLines returns all individual line numbers covered by the ranges.

func (*FileSelection) Contains

func (fs *FileSelection) Contains(lineNum int) bool

Contains checks if a line number is within any of the ranges.

func (*FileSelection) Merge

func (fs *FileSelection) Merge()

Merge merges overlapping and adjacent ranges.

func (*FileSelection) String

func (fs *FileSelection) String() string

String returns the selection as a string.

type Hunk

type Hunk struct {
	// OldStart is the starting line in the original file.
	OldStart int

	// OldLines is the number of lines from the original file.
	OldLines int

	// NewStart is the starting line in the new file.
	NewStart int

	// NewLines is the number of lines in the new file.
	NewLines int

	// Section is the optional section header (e.g., function name).
	Section string

	// Lines contains all lines in this hunk.
	Lines []DiffLine
}

Hunk represents a contiguous block of changes in a file.

func (*Hunk) Additions

func (h *Hunk) Additions() iter.Seq[DiffLine]

Additions returns an iterator over only added lines.

func (*Hunk) All

func (h *Hunk) All() iter.Seq[DiffLine]

All returns an iterator over all lines in this hunk.

func (*Hunk) CanSplit

func (h *Hunk) CanSplit() bool

CanSplit returns true if this hunk can be split into smaller hunks. A hunk can be split if there are context lines between change groups.

func (*Hunk) Changes

func (h *Hunk) Changes() iter.Seq[DiffLine]

Changes returns an iterator over only changed lines (add/delete).

func (*Hunk) ContainsLine

func (h *Hunk) ContainsLine(lineNum int) bool

ContainsLine checks if any change in this hunk affects the given line. Uses NewLineNum for additions, OldLineNum for deletions.

func (*Hunk) ContainsRange

func (h *Hunk) ContainsRange(start, end int) bool

ContainsRange checks if any change in this hunk falls within the range.

func (*Hunk) Deletions

func (h *Hunk) Deletions() iter.Seq[DiffLine]

Deletions returns an iterator over only deleted lines.

func (*Hunk) Header

func (h *Hunk) Header() string

Header returns the hunk header in unified diff format.

func (*Hunk) RecalculateLineCounts

func (h *Hunk) RecalculateLineCounts()

RecalculateLineCounts updates OldLines and NewLines based on Lines slice.

func (*Hunk) Stats

func (h *Hunk) Stats() (added, deleted int)

Stats returns addition and deletion counts.

type LineOp

type LineOp int

LineOp represents the type of diff operation for a line.

const (
	// OpContext indicates an unchanged line (context).
	OpContext LineOp = iota
	// OpAdd indicates an added line.
	OpAdd
	// OpDelete indicates a deleted line.
	OpDelete
)

func (LineOp) Prefix

func (op LineOp) Prefix() byte

Prefix returns the diff prefix character for the operation.

func (LineOp) String

func (op LineOp) String() string

String returns the string representation of the operation.

type LineRange

type LineRange struct {
	Start int // Inclusive.
	End   int // Inclusive.
}

LineRange represents a range of lines to select.

func (LineRange) Contains

func (r LineRange) Contains(lineNum int) bool

Contains checks if a line number is within this range.

func (LineRange) String

func (r LineRange) String() string

String returns the range as a string (e.g., "10-20" or "15").

type LineWithContext

type LineWithContext struct {
	// GlobalIndex is the index of this line across all files.
	GlobalIndex int

	// File is the file containing this line.
	File *FileDiff

	// HunkIndex is the index of the hunk within the file.
	HunkIndex int

	// LineIndex is the index of the line within the hunk.
	LineIndex int

	// Line is the actual diff line.
	Line DiffLine
}

LineWithContext provides full context for a diff line.

type ParsedDiff

type ParsedDiff struct {
	// contains filtered or unexported fields
}

ParsedDiff wraps a parsed multi-file diff.

func Parse

func Parse(diffText string) (*ParsedDiff, error)

Parse parses a unified diff string into a structured representation.

func (*ParsedDiff) AllFiles

func (d *ParsedDiff) AllFiles() []*FileDiff

AllFiles returns a slice of all file diffs.

func (*ParsedDiff) FileByPath

func (d *ParsedDiff) FileByPath(path string) *FileDiff

FileByPath finds a file diff by path.

func (*ParsedDiff) FileCount

func (d *ParsedDiff) FileCount() int

FileCount returns the number of files in the diff.

func (*ParsedDiff) Files

func (d *ParsedDiff) Files() iter.Seq[*FileDiff]

Files returns an iterator over all file diffs.

func (*ParsedDiff) FilesWithIndex

func (d *ParsedDiff) FilesWithIndex() iter.Seq2[int, *FileDiff]

FilesWithIndex returns an iterator with indices.

func (*ParsedDiff) LinesWithContext

func (d *ParsedDiff) LinesWithContext() iter.Seq[LineWithContext]

LinesWithContext returns an iterator over all lines with full context.

func (*ParsedDiff) Stats

func (d *ParsedDiff) Stats() (added, deleted int)

Stats returns total addition and deletion counts across all files.

type SelectionMap

type SelectionMap map[string]*FileSelection

SelectionMap groups selections by file path for efficient lookup.

func NewSelectionMap

func NewSelectionMap(selections []*FileSelection) SelectionMap

NewSelectionMap creates a SelectionMap from a slice of selections.

func (SelectionMap) Contains

func (m SelectionMap) Contains(path string, lineNum int) bool

Contains checks if a specific line in a file is selected.

func (SelectionMap) Get

func (m SelectionMap) Get(path string) *FileSelection

Get returns the selection for a file path, or nil if not found.

Jump to

Keyboard shortcuts

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