Documentation
¶
Overview ¶
Package diff provides types and functions for parsing and manipulating unified diffs.
Index ¶
- func All(lines iter.Seq[DiffLine], pred func(DiffLine) bool) bool
- func Any(lines iter.Seq[DiffLine], pred func(DiffLine) bool) bool
- func ChunkByOp(lines iter.Seq[DiffLine]) iter.Seq[[]DiffLine]
- func CountLines(lines iter.Seq[DiffLine]) int
- func FilteredLines(lines iter.Seq[DiffLine], pred func(DiffLine) bool) iter.Seq[DiffLine]
- func ForEach(lines iter.Seq[DiffLine], fn func(DiffLine))
- func LinesInRange(lines iter.Seq[DiffLine], start, end int) iter.Seq[DiffLine]
- func MapLines[T any](lines iter.Seq[DiffLine], fn func(DiffLine) T) iter.Seq[T]
- func SelectedLines(lines iter.Seq[DiffLine], sel *FileSelection) iter.Seq[DiffLine]
- func SkipLines(lines iter.Seq[DiffLine], n int) iter.Seq[DiffLine]
- func TakeLines(lines iter.Seq[DiffLine], n int) iter.Seq[DiffLine]
- func ZipWithIndex(lines iter.Seq[DiffLine]) iter.Seq2[int, DiffLine]
- type DiffLine
- type FileDiff
- func (f *FileDiff) AllChanges() iter.Seq2[int, DiffLine]
- func (f *FileDiff) AllHunks() iter.Seq2[int, *Hunk]
- func (f *FileDiff) AllLines() iter.Seq2[int, DiffLine]
- func (f *FileDiff) Format() string
- func (f *FileDiff) HunkContainingLine(lineNum int) *Hunk
- func (f *FileDiff) HunksInRange(start, end int) []*Hunk
- func (f *FileDiff) Path() string
- func (f *FileDiff) Stats() (added, deleted int)
- type FileSelection
- type Hunk
- func (h *Hunk) Additions() iter.Seq[DiffLine]
- func (h *Hunk) All() iter.Seq[DiffLine]
- func (h *Hunk) CanSplit() bool
- func (h *Hunk) Changes() iter.Seq[DiffLine]
- func (h *Hunk) ContainsLine(lineNum int) bool
- func (h *Hunk) ContainsRange(start, end int) bool
- func (h *Hunk) Deletions() iter.Seq[DiffLine]
- func (h *Hunk) Header() string
- func (h *Hunk) RecalculateLineCounts()
- func (h *Hunk) Stats() (added, deleted int)
- type LineOp
- type LineRange
- type LineWithContext
- type ParsedDiff
- func (d *ParsedDiff) AllFiles() []*FileDiff
- func (d *ParsedDiff) FileByPath(path string) *FileDiff
- func (d *ParsedDiff) FileCount() int
- func (d *ParsedDiff) Files() iter.Seq[*FileDiff]
- func (d *ParsedDiff) FilesWithIndex() iter.Seq2[int, *FileDiff]
- func (d *ParsedDiff) LinesWithContext() iter.Seq[LineWithContext]
- func (d *ParsedDiff) Stats() (added, deleted int)
- type SelectionMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountLines ¶
CountLines counts the number of lines in an iterator.
func FilteredLines ¶
FilteredLines returns an iterator over lines matching a predicate.
func LinesInRange ¶
LinesInRange returns an iterator over lines with NewLineNum in the range.
func SelectedLines ¶
SelectedLines returns lines matching any of the selections.
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 ¶
CollectLines collects all lines from an iterator into a slice.
func (DiffLine) EffectiveLineNum ¶
EffectiveLineNum returns the relevant line number for selection purposes. For added lines, returns NewLineNum. For deleted and context lines, returns OldLineNum.
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 ¶
AllChanges returns an iterator over only changed lines across all hunks.
func (*FileDiff) AllLines ¶
AllLines returns an iterator over all lines across all hunks. Yields (hunk index, line) pairs.
func (*FileDiff) HunkContainingLine ¶
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 ¶
HunksInRange returns all hunks that have changes within the given range.
type FileSelection ¶
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) CanSplit ¶
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) ContainsLine ¶
ContainsLine checks if any change in this hunk affects the given line. Uses NewLineNum for additions, OldLineNum for deletions.
func (*Hunk) ContainsRange ¶
ContainsRange checks if any change in this hunk falls within the range.
func (*Hunk) RecalculateLineCounts ¶
func (h *Hunk) RecalculateLineCounts()
RecalculateLineCounts updates OldLines and NewLines based on Lines slice.
type LineOp ¶
type LineOp int
LineOp represents the type of diff operation for a line.
type LineRange ¶
LineRange represents a range of lines to select.
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.