On this page
Package stage implements hunk-level staging against temporary indexes by parsing unified diffs and applying selective patches for partial-file commits.
#internal/stage
#internal/stage
Package stage implements hunk-level staging against temporary indexes by parsing unified diffs and applying selective patches for partial-file commits.
#Hunk
type Hunk structHunk represents a single change block from a unified diff.
#ExtractHunks
func ExtractHunks(ctx context.Context, indexPath, file string) (header []string, hunks []Hunk, err error)ExtractHunks diffs the working tree against a tmp index for a single file. Returns the diff header lines and parsed hunks.
#ParseDiff
func ParseDiff(raw string) (header []string, hunks []Hunk)ParseDiff splits unified diff output into header lines and hunks. It handles a single file's diff block (one "diff --git" header + hunks).
#BuildPatch
func BuildPatch(header []string, hunks []Hunk, selected []int) ([]byte, error)BuildPatch builds a synthetic unified diff from header + selected hunks. selected is a slice of 1-based hunk indices.
#ApplyPatch
func ApplyPatch(ctx context.Context, indexPath string, patch []byte) errorApplyPatch applies a patch to a tmp index using git apply --cached. Falls back to --3way on first failure.
#StageHunks
func StageHunks(ctx context.Context, indexPath, file string, hunkIndices []int) errorStageHunks stages only specific hunks of a file into a tmp index. hunkIndices are 1-based.
#ParseHunkSpec
func ParseHunkSpec(spec string) ([]int, error)ParseHunkSpec parses a hunk specifier string like "1,3,5" or "2-4" or "1,3-5". Returns 1-based hunk indices.