gitdiff

package
v0.171.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package gitdiff is a small, focused data layer behind the `/diff` changes panel. It shells out to the git CLI (mirroring the exec.Command("git", ...) style used elsewhere in the codebase) to list working-tree changes and to fetch the before/after content for a single changed file.

It deliberately returns raw before/after content rather than a preformatted patch: the UI renders the diff via the diffview package, which computes the line diff itself. The Source interface lives here (not in internal/domain) so it does not trigger counterfeiter mock regeneration - the only consumer is the diff viewer component, which can be tested with a hand-written fake.

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.

func RunGit added in v0.142.2

func RunGit(ctx context.Context, workdir string, args ...string) ([]byte, error)

RunGit runs a git command in workdir (process cwd when empty) and returns its stdout. The context bounds the command's lifetime; stderr is folded into the returned error.

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 added in v0.121.0

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 added in v0.140.0

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 added in v0.140.0

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'
	StatusRenamed    Status = 'R'
	StatusCopied     Status = 'C'
	StatusTypeChange Status = 'T'
	StatusUnmerged   Status = 'U'
	StatusUntracked  Status = '?'
)

Jump to

Keyboard shortcuts

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