diff

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package diff turns unified diff text into a structure the UI can render, and turns a selection of hunks back into a patch git can apply.

Index

Constants

View Source
const DefaultContext = 3

DefaultContext is how many unchanged lines surround a hunk by default — git's -U3.

Variables

This section is empty.

Functions

func Generate

func Generate(oldPath, newPath string, contextLines int) (string, error)

Generate diffs two files or two directories and returns unified diff text.

ponytail: generate-then-reparse. Producing text and handing it straight back to Parse keeps one interpretation of a diff in the codebase instead of two. Swap in a direct []File builder only if profiling says this matters.

func NewFilePatch

func NewFilePatch(path string, content []byte) string

NewFilePatch renders an untracked file as an add-everything patch, so a file git has never seen can be reviewed and staged like any other change.

Types

type File

type File struct {
	OldPath  string
	NewPath  string
	IsNew    bool
	IsDelete bool
	IsRename bool
	IsBinary bool
	Hunks    []Hunk

	Added   int
	Removed int
}

File is one file's worth of changes.

func ParseString

func ParseString(s string) ([]File, error)

ParseString reads unified diff text. Every input to hunk becomes unified diff text first, so this is the only place a diff is interpreted.

func (File) Patch

func (f File) Patch(hunks []int) string

Patch renders a patch containing only the named hunks of a file, ready for "git apply --cached".

The hunks are emitted exactly as they were parsed, never recomputed: git is asked to recount the line numbers, so no arithmetic of ours can silently corrupt a patch and stage the wrong thing.

func (File) Path

func (f File) Path() string

Path is the name to show for a file: the new name, except for deletions.

type Hunk

type Hunk struct {
	Header   string // the full "@@ -1,4 +1,6 @@ func foo()" line
	OldStart int
	NewStart int
	Lines    []Line
	// contains filtered or unexported fields
}

Hunk is one @@ block.

func (Hunk) Key

func (h Hunk) Key() string

Key is a content-stable identity for a hunk: a hash of what each line does and says, but not of its line numbers. Two hunks with the same body match even after edits elsewhere shift their positions, and a hunk stops matching the moment its own content changes — which is how live-follow tells an untouched mark from one that needs re-approving.

type Kind

type Kind int

Kind is what a line does: it stays, it arrives, or it leaves.

const (
	Context Kind = iota
	Added
	Removed
)

The three things a line can do in a diff.

type Line

type Line struct {
	Kind   Kind
	Text   string
	OldNum int
	NewNum int
}

Line is one line of a hunk. OldNum/NewNum are 1-based line numbers in the old and new file; they are 0 when the line does not exist on that side.

type Range

type Range struct {
	Start, End int
}

Range is a half-open byte range [Start, End) inside a line.

func Intraline

func Intraline(before, after string) (beforeRanges, afterRanges []Range)

Intraline finds the parts of two paired lines that actually differ, so a one-word change reads as a one-word change instead of a whole red line next to a whole green one.

Ranges are byte ranges into before and after respectively. It returns nothing when the lines are too different to be worth comparing.

Jump to

Keyboard shortcuts

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