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.
#ErrBinaryFile
var ErrBinaryFile = errors.New("binary file: hunk staging not supported")ErrBinaryFile reports that a hunk spec was given for a file git reports as binary, where only whole-file staging exists. It is exported because the refusal has its own exit code (exitcode.BinaryHunkSpec): the commit pipeline recognizes it with errors.Is and reports it as that code rather than as an undifferentiated failure.
#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.
A patch that does not apply is a hard error. It used to be answered by silently running the same patch again with --3way, which is not the same staging action: the direct apply stages exactly the hunks the caller selected, while the three-way retry MERGES the patch into whatever the index holds. A caller who asked for one hunk could be given a merge result nobody named, and nothing in the output said a retry had happened.
The error carries git's own reason and no path: the caller attaches the repo-relative spelling (internal/commit's stagingHunksError), which is the only spelling safegit says back to a caller -- this package is handed the absolute path.
#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.