diffhunk

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAddedLines

func GetAddedLines(hunk string) []string

GetAddedLines extracts all added lines from a diff hunk

func GetModifiedContentFromDiffHunk

func GetModifiedContentFromDiffHunk(originalContent string, patch string) (string, error)

GetModifiedContentFromDiffHunk applies a diff hunk to original content

func GetRemovedLines

func GetRemovedLines(hunk string) []string

GetRemovedLines extracts all removed lines from a diff hunk

func GetZeroBased

func GetZeroBased(line int) int

GetZeroBased converts 1-based line numbers to 0-based Special case: 0 stays 0 (used for empty files)

Types

type DiffChangeType

type DiffChangeType int

DiffChangeType represents the type of change in a diff line

const (
	Context DiffChangeType = iota
	Add
	Delete
	Control
)

type DiffHunk

type DiffHunk struct {
	OldStart int         // Starting line in old file (1-based)
	OldLines int         // Number of lines in old file
	NewStart int         // Starting line in new file (1-based)
	NewLines int         // Number of lines in new file
	Lines    []*DiffLine // The actual diff lines
}

DiffHunk represents a hunk in a unified diff

func ParseDiffHunk

func ParseDiffHunk(hunk string) (*DiffHunk, error)

ParseDiffHunk parses a unified diff hunk into a structured format Expected format: @@ -oldStart,oldLines +newStart,newLines @@ context line +added line -removed line

func ParsePatch

func ParsePatch(patch string) ([]*DiffHunk, error)

ParsePatch parses a complete unified diff patch into multiple hunks

type DiffLine

type DiffLine struct {
	Type           DiffChangeType
	OldLineNumber  int    // 1-based, -1 if not applicable
	NewLineNumber  int    // 1-based, -1 if not applicable
	Text           string // Line content without the prefix (+, -, or space)
	PositionInHunk int    // Position within the hunk (0-based)
}

DiffLine represents a single line in a diff

func GetDiffLineByPosition

func GetDiffLineByPosition(hunks []*DiffHunk, diffLineNumber int) *DiffLine

GetDiffLineByPosition finds a diff line by its position in the overall diff diffLineNumber is 1-based (GitHub's position)

Jump to

Keyboard shortcuts

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