diff

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SideRight = "RIGHT"
	SideLeft  = "LEFT"
)

GitHub inline-review-comment `side` values. RIGHT is the post-image (new file), LEFT is the pre-image (old file).

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(s string) int

EstimateTokens returns a rough token count from byte length using the chars/4 heuristic. Backed by the shared internal/tokens helper so providers, compress, and the diff layer never drift apart. Provider-side exact tokenizers can replace this when they're cheap to call from outside the provider's process.

Types

type Diff

type Diff struct {
	Files  []FileDiff
	Origin git.Origin
	Args   map[string]string
	// contains filtered or unexported fields
}

func Filter

func Filter(d Diff, m *ignore.Matcher) Diff

func KeepPaths added in v0.9.0

func KeepPaths(d Diff, files, dirs []string) Diff

KeepPaths narrows a Diff to only files matching the supplied allowlists. files is an exact-path allowlist (post-change Path or OldPath equality, slash-normalized). dirs is a prefix allowlist — a file is kept when its path starts with `<dir>/` for any entry (trailing slash on the input is stripped, then added by the matcher).

Both empty → no filtering (returns d unchanged). When at least one is non-empty, the union semantics apply: a file is kept if it matches ANY file OR sits under ANY dir.

Paths are compared after `filepath.ToSlash` normalization on both sides so a user passing `app\Models` on Windows still matches `app/Models/User.go` in the parsed diff.

func Parse

func Parse(input git.Diff) (Diff, error)

func (Diff) AddedLines

func (d Diff) AddedLines() int

AddedLines is the count of `+` lines across all hunks in all files. O(1) — the value is computed once in Parse / Filter / KeepPaths and memoized in the struct.

func (Diff) Anchors added in v1.2.1

func (d Diff) Anchors() map[string]FileAnchors

Anchors builds a per-file index of postable comment positions. Keyed by the new-file path (FileDiff.Path), falling back to OldPath for pure deletions where Path is empty — the same key submitPRReview looks up with a finding's File.

func (Diff) DeletedLines

func (d Diff) DeletedLines() int

DeletedLines is the count of `-` lines across all hunks in all files. Memoized like AddedLines.

func (Diff) Empty

func (d Diff) Empty() bool

func (Diff) EstimateTokens

func (d Diff) EstimateTokens() int

func (Diff) FileCount

func (d Diff) FileCount() int

func (Diff) NumberedString added in v1.2.1

func (d Diff) NumberedString() string

NumberedString renders the diff like String, but prefixes every hunk line with the line number a GitHub inline comment would anchor to: the new-file number for added and context lines, the old-file number for removed lines. The format is `<n>| <marker><text>` (marker is `+`/`-`/space). LLMs count poorly across long hunks and tend to echo the `@@` header's start line; handing them the number per line turns the `line` field of a finding from an estimate into a copy. The cache key still keys off String() — NumberedString is a deterministic function of the same diff, so it carries no extra cache identity.

func (Diff) String

func (d Diff) String() string

type FileAnchors added in v1.2.1

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

FileAnchors indexes the line numbers in one file that a GitHub inline review comment can legally attach to. GitHub accepts a comment on any line that appears in the PR diff: on the RIGHT side that is every context and added line (numbered in the new file); on the LEFT side every context and removed line (numbered in the old file). Indexing both lets a finding be pinned to the side it actually lives on instead of unconditionally guessing RIGHT — and lets a finding whose line is outside the diff be detected before the POST 422s.

func (FileAnchors) Resolve added in v1.2.1

func (fa FileAnchors) Resolve(line int, preferLeft bool) (side string, ok bool)

Resolve maps a finding's reported line number to a postable GitHub comment side. preferLeft flips the lookup order so a line valid on both sides resolves to LEFT — used for findings about removed code. ok is false when the line matches no postable position; the caller must not POST it (it would 422) and should fall back to the review summary instead of dropping it.

type FileDiff

type FileDiff struct {
	Path    string
	OldPath string
	Mode    Mode
	Hunks   []Hunk
	Binary  bool

	// PathParts is Path pre-split on `/`, populated once during
	// parseDiffHeader. The ignore-matcher backend takes `[]string`
	// parts; without this cache every Match call would re-split.
	// On a 500-file diff with two filter layers that's 1000+
	// redundant allocations.
	PathParts []string
	// OldPathParts mirrors PathParts for renamed/deleted entries.
	OldPathParts []string
}

func (FileDiff) String

func (f FileDiff) String() string

type Hunk

type Hunk struct {
	OldStart int
	OldLines int
	NewStart int
	NewLines int
	Header   string
	Lines    []HunkLine
}

func (Hunk) String

func (h Hunk) String() string

type HunkLine

type HunkLine struct {
	Kind LineKind
	Text string
}

type LineKind

type LineKind int
const (
	LineContext LineKind = iota
	LineAdd
	LineDel
)

type Mode

type Mode int
const (
	ModeModified Mode = iota
	ModeAdded
	ModeDeleted
	ModeRenamed
	ModeCopied
)

func (Mode) String

func (m Mode) String() string

Jump to

Keyboard shortcuts

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