Help us shape the future of Go! Take the 2026 Go Developer Survey

diff

package
v0.2.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package diff compares local and remote files and produces structured diff results.

Index

Constants

View Source
const DefaultContext = 3

DefaultContext is the number of unchanged lines kept beside each change hunk.

Variables

This section is empty.

Functions

func FoldableGapIDs

func FoldableGapIDs(lines []DiffLine, context int) []int

FoldableGapIDs lists equal-run starts that collapse under the default view.

func IndexOfSourceLine

func IndexOfSourceLine(rows []DisplayRow, lineIdx int) int

IndexOfSourceLine returns the display index covering lineIdx, or 0 if none.

func RenderUnifiedRows

func RenderUnifiedRows(result *DiffResult, rows []DisplayRow, width, scrollOffset, count int, flip, highlight bool) []string

RenderUnifiedRows renders flattened display rows (hunk headers, folds, lines). scrollOffset is the first DisplayRow index; count is the number of viewport rows. Use the same flip value as Flatten so headers, row order and gutters agree. highlight enables addition/removal colours and markers for planned actions.

Types

type DiffLine

type DiffLine struct {
	Text      string
	Kind      LineKind // Equal, Added, Removed
	LocalNum  int      // 0 = this side has no number
	RemoteNum int
}

DiffLine holds one line of a unified diff sequence.

type DiffResult

type DiffResult struct {
	LocalPath  string
	RemotePath string
	Lines      []DiffLine

	// Existence flags
	LocalOnly  bool // file exists only locally (not on remote)
	RemoteOnly bool // file exists only on remote

	// Metadata
	Binary      bool
	ContentDiff bool // content differs but has no renderable line diff
	SizeLocal   int64
	SizeRemote  int64
	ModLocal    time.Time
	ModRemote   time.Time
	// contains filtered or unexported fields
}

DiffResult is the comparison output for a single file pair.

func Compare

func Compare(root *fs.Root, localPath, remotePath string, client RemoteClient) (*DiffResult, error)

Compare produces a DiffResult for a local/remote file pair. Local reads go through root, so a local path that only looks like a project path — because a symlinked component points elsewhere — fails instead of pulling in a file from outside the project.

func CompareWithActivity

func CompareWithActivity(root *fs.Root, localPath, remotePath string, client RemoteClient, activity func() error) (*DiffResult, error)

CompareWithActivity reports local read progress, including bytes hashed from large files. Remote activity is observed by the supplied client's reader. activity may be nil; otherwise it must be safe for concurrent comparisons.

func (*DiffResult) Counts

func (r *DiffResult) Counts() (added, removed int)

Counts returns the number of added and removed lines.

func (*DiffResult) HasDiff

func (r *DiffResult) HasDiff() bool

HasDiff returns true if the files differ in any way.

type DisplayKind

type DisplayKind int

DisplayKind classifies a row in the folded unified view.

const (
	DisplayLine DisplayKind = iota
	DisplayHunkHeader
	DisplayFold
)

type DisplayRow

type DisplayRow struct {
	Kind      DisplayKind
	LineIndex int    // Lines index for DisplayLine; change-run start for DisplayHunkHeader
	Header    string // @@ -before,count +after,count @@ for DisplayHunkHeader
	GapID     int    // start index of the equal run (stable across expand)
	Hidden    int    // folded equal-line count
	FoldStart int    // first hidden Lines index (inclusive)
	FoldEnd   int    // first Lines index after the fold (exclusive)
}

DisplayRow is one viewport row after hunk headers and context folding.

func Flatten

func Flatten(lines []DiffLine, context int, expanded map[int]struct{}, flip bool) []DisplayRow

Flatten builds display rows: hunk headers, limited context, and fold markers. Each @@ header is the first row of its hunk, followed by context then changes. expanded holds GapIDs whose equal runs should be shown in full. flip previews an upload: remote is the old state, local the new state. LineIndex and GapID always refer to the original comparison, even when reordered.

func (DisplayRow) SourceLine

func (r DisplayRow) SourceLine() int

SourceLine is the DiffLine index this row represents for scroll anchoring.

type LineKind

type LineKind int

LineKind classifies a line in a unified diff.

const (
	LineEqual   LineKind = iota
	LineAdded            // present on remote, absent on local
	LineRemoved          // present on local, absent on remote
)

type RemoteClient

type RemoteClient interface {
	Stat(path string) (os.FileInfo, error)
	Open(path string) (io.ReadCloser, error)
	ReadFile(path string) ([]byte, error)
}

RemoteClient is the subset of remote operations needed for diffing.

type Session

type Session struct {
	LocalPath  string
	RemotePath string
	Result     *DiffResult
	Loaded     bool
	Err        error
}

Session pairs a browser selection entry with its loaded DiffResult.

Jump to

Keyboard shortcuts

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