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

gitdiff

package
v0.233.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRepo

func IsRepo(workdir string) bool

IsRepo reports whether workdir is inside a git work tree.

Types

type FileChange

type FileChange struct {
	Path     string
	OrigPath string
	Status   Status
	Staged   bool
}

FileChange describes one changed path, in either the staged (index) group or the unstaged (working-tree) group. A path with both staged and unstaged edits (porcelain "MM") yields one FileChange in each group, like VS Code.

type FilePatch

type FilePatch struct {
	Preamble string
	Hunks    []Hunk
}

FilePatch is a single file's unified diff, split into its preamble (the "diff --git"/"index"/"---"/"+++" lines) and hunks. It is the unit operated on by hunk-level (git add -p style) staging.

func SplitFilePatchHunk

func SplitFilePatchHunk(fp FilePatch, idx int) FilePatch

SplitFilePatchHunk returns a copy of fp with hunk idx replaced by the smallest independent hunks it can be broken into (see splitHunk). An out-of-range idx returns fp unchanged.

type Hunk

type Hunk struct {
	Header string
	Lines  []string
}

Hunk is one "@@ ... @@" section of a unified diff for a file.

type ReadSource

type ReadSource interface {
	Changes() (staged, unstaged []FileChange, err error)
	Diff(fc FileChange) (oldContent, newContent string, isBinary bool, err error)
	Workdir() string
}

ReadSource is the read-only subset of Source consumed by the diff viewer's PR tab: it lists changed files and returns their before/after content, but cannot stage, discard, or patch. gitSource satisfies it, and so does rangeSource.

func NewPRSource

func NewPRSource(workdir string) ReadSource

NewPRSource returns a read-only source for the current branch's PR diff, rooted at workdir.

type Source

type Source interface {
	// Changes returns the staged and unstaged file groups.
	Changes() (staged, unstaged []FileChange, err error)
	// Diff returns the before/after content for a change. Refs are chosen by
	// group: a staged entry compares HEAD→index, an unstaged entry compares
	// index(→HEAD fallback)→working tree. Added/untracked → old "", deleted →
	// new "". isBinary is true for binary or oversized content (skip diffing).
	Diff(fc FileChange) (oldContent, newContent string, isBinary bool, err error)
	// Stage runs `git add` on the path.
	Stage(path string) error
	// Unstage removes the path from the index.
	Unstage(path string) error
	// StageAll stages every working-tree change (`git add -A`): modifications,
	// additions, deletions, and untracked files.
	StageAll() error
	// UnstageAll removes all paths from the index (`git reset -q HEAD`), leaving
	// the working tree untouched.
	UnstageAll() error
	// Discard reverts a working-tree change: it restores a tracked file from the
	// index (HEAD when nothing is staged) and deletes an untracked file. This is
	// destructive - the discarded working-tree changes cannot be recovered.
	Discard(fc FileChange) error
	// WorktreePatch returns the unstaged (index→working tree) patch for a path,
	// for hunk-level staging.
	WorktreePatch(path string) (FilePatch, error)
	// IndexPatch returns the staged (HEAD→index) patch for a path, for
	// hunk-level unstaging.
	IndexPatch(path string) (FilePatch, error)
	// ApplyHunk applies a single hunk to the index. reverse=false stages a
	// worktree hunk; reverse=true unstages a staged hunk.
	ApplyHunk(fp FilePatch, hunkIndex int, reverse bool) error
	// ApplyLines applies only the selected change-lines of one hunk to the index.
	// selected holds 0-based indices into the hunk's Lines slice ('+'/'-' lines
	// only; others are ignored). reverse=false stages the selected worktree lines,
	// reverse=true unstages the selected staged lines. Unselected changes are
	// neutralized so they keep their prior staged/unstaged state.
	ApplyLines(fp FilePatch, hunkIndex int, selected map[int]bool, reverse bool) error
	// Workdir returns the repository working directory that change paths are
	// relative to, for resolving an absolute path (e.g. to open in an editor).
	Workdir() string
}

Source provides the data behind the changes panel.

func NewGitSource

func NewGitSource(workdir string) Source

NewGitSource returns a Source rooted at workdir (the repository working dir).

type Status

type Status rune

Status is a single-letter git status code for a changed file.

const (
	StatusModified   Status = 'M'
	StatusAdded      Status = 'A'
	StatusDeleted    Status = 'D'
	StatusTypeChange Status = 'T'
	StatusUntracked  Status = '?'
)

Jump to

Keyboard shortcuts

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