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 ¶
Types ¶
type FileChange ¶
type FileChange struct {
Path string // working-tree path (the new path for renames)
OrigPath string // rename/copy source path, else ""
Status Status
Staged bool // true → index/staged group, false → working-tree group
}
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 ¶
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.
type Hunk ¶
type Hunk struct {
Header string // the "@@ -a,b +c,d @@" line (plus any trailing section heading)
Lines []string // body lines, each prefixed with ' ', '+', '-' or '\'
}
Hunk is one "@@ ... @@" section of a unified diff for a file.
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
// 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
// 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 ¶
NewGitSource returns a Source rooted at workdir (the repository working dir).