diff

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package diff parses unified diff patches into line-level data used by the diff review view. It is independent of where the patch came from (GitHub API or a local git working tree).

Index

Constants

View Source
const (
	SideRight = "RIGHT" // new file (added and context lines)
	SideLeft  = "LEFT"  // old file (removed lines)
)

Side constants identify which side of a diff a line belongs to. They match the values GitHub's PR review API expects.

Variables

This section is empty.

Functions

func AddedFilePatch

func AddedFilePatch(content []byte) string

AddedFilePatch builds a patch that marks every line of content as added. Used for untracked files, which have no git diff but should still be reviewable in the diff view.

func StripHeader

func StripHeader(patch string) string

StripHeader removes everything before the first hunk header ("@@"). Local `git diff` output starts with "diff --git", "index", "---", and "+++" lines that ParsePatch would otherwise misread as removed/added content. GitHub's API patches already start at the first hunk, so they pass through unchanged.

Types

type Info

type Info struct {
	Lines []Line
}

Info contains parsed diff data for a single file.

func ParsePatch

func ParsePatch(patch string) *Info

ParsePatch parses a unified diff patch string into Info. The patch must start at the first hunk header (see StripHeader for git output). Returns nil for an empty patch.

func (*Info) FormatDiffLines

func (d *Info) FormatDiffLines() []string

FormatDiffLines returns display strings for the diff lines with +/-/space prefix. A space separator is inserted between the prefix and content for readability.

func (*Info) LineSideMap

func (d *Info) LineSideMap() (lineMap []int, sideMap []string, typeMap []byte)

LineSideMap builds line number, side, and type maps from the parsed diff. lineMap maps display index → file line number, sideMap → "RIGHT"/"LEFT", typeMap → LineType byte ('+', '-', ' ').

type Line

type Line struct {
	Type    LineType
	Content string // line content without the +/-/space prefix
	NewLine int    // 1-based line number in the new file (0 for removed lines)
	OldLine int    // 1-based line number in the old file (0 for added lines)
}

Line represents a single line in a unified diff.

func (Line) Position added in v0.12.0

func (l Line) Position() (line int, side string)

Position returns the file line number and side a comment on the line targets: removed lines live on the LEFT side and are numbered by the old file, the others on the RIGHT side numbered by the new file.

type LineType

type LineType byte

LineType represents the type of a diff line.

const (
	Context LineType = ' '
	Added   LineType = '+'
	Removed LineType = '-'
)

Jump to

Keyboard shortcuts

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