Skip to content
internal/stage
Edit
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

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

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.

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

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.

More tools from this site

  • claudestream Drive Claude Code from Python: run it as a subprocess and read its output as typed events, with async and sync sessions, sandbox policies, and tools you define in Python
  • claudewheel A TUI Claude Code Launcher that lets you have more than one profile, manage sessions lifecycle, pick the exact CC version, model to use (even older unlisted ones), pick which GitHub account to use, etc.
  • dirstat Fast, single-binary directory statistics CLI: every file under a tree grouped by format, with counts, sizes, and lines of code, as a colored terminal table or as JSON
  • fastware A batteries-included ASGI framework: msgspec JSON, a managed Granian server, dependency injection, SSE, WebSockets, auth, and a test client
  • go-toml-edit Zero-dep TOML editing library for Go with comment preservation
  • howmuchleft The fastest Claude Code statusline: context window, 5-hour, and weekly limit usage as three customizable gradient bars, rendering in about 6 ms
  • orxtra
  • pgdesign
  • predraw Declarative rendering pipeline: describe a scene in JSON and get SVG, PNG and WebP out, with light and dark style tokens, reusable components and text converted to path outlines
  • reposummary Turn a git repository's history into a Markdown journal: pick a time window or revision range and get a readable digest of what changed, optionally narrated by an LLM
  • rlsbl Release orchestration and project scaffolding CLI that bumps versions, validates a structured JSONL changelog, tags only the commit CI verified, and publishes to npm, PyPI, Go and more
  • saferm Command-line replacement for rm that archives every deletion with a mandatory reason and the context it ran in, so deleted files can be listed, inspected and restored
  • selfdoc Static Site Generator that builds a project's documentation site directly from its source code, so the docs can never drift from the code they describe, with SEO/AEO, first-class blog, search, and cross-project linking built in
  • strictcli
  • stricttest An always-on test-isolation floor: a pytest plugin and a Go env-hygiene module that make a test suite structurally unable to reach real credentials, the real HOME, the network, or the development repository.
  • wesktop A Python framework that turns an ASGI web app into a desktop application, serving it from a local Granian server and displaying it in a native OS window via pywebview
Search