gitdiff

package
v0.119.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

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

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.

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

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