safegit v0.26.0 /internal/stage
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

Go go
type Hunk struct

Hunk represents a single change block from a unified diff.

#ExtractHunks

Go go
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

Go go
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

Go go
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

Go go
func ApplyPatch(ctx context.Context, indexPath string, patch []byte) error

ApplyPatch applies a patch to a tmp index using git apply --cached. Falls back to --3way on first failure.

#StageHunks

Go go
func StageHunks(ctx context.Context, indexPath, file string, hunkIndices []int) error

StageHunks stages only specific hunks of a file into a tmp index. hunkIndices are 1-based.

#ParseHunkSpec

Go go
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.

Search