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 ¶
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.
func SplitFilePatchHunk ¶ added in v0.121.0
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 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 ¶
NewGitSource returns a Source rooted at workdir (the repository working dir).