Documentation
¶
Overview ¶
Package patch implements the apply_patch DSL: a stripped-down, file-oriented diff format carried by one tool call, so a single invocation can add, update, move, and delete files. The grammar is codex's apply_patch:
*** Begin Patch *** Add File: path + one "+" line per content line *** Delete File: path *** Update File: path optionally followed by "*** Move to: path" @@ [context] then " ", "-", "+" lines; "*** End of File" *** End Patch anchors a chunk to the end of the file
This package parses and resolves a patch into the changes it would make; it never writes to disk. The caller decides how to persist the result, which keeps workspace-path policy, snapshots, and prompts in the tool layer where the rest of the agent already handles them. It imports only the standard library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ApplyError ¶
type ApplyError struct {
Msg string
}
ApplyError reports a patch that parsed but cannot be applied; the message is written for the model (it names the operation and path), mirroring codex's error vocabulary so existing model habits hold.
func (*ApplyError) Error ¶
func (e *ApplyError) Error() string
type Change ¶
type Change struct {
Kind ChangeKind
// Path is the resolved destination path (for Delete and Update, the file
// itself; for Move, the new location).
Path string
// OldPath is the resolved source path for a Move ("" otherwise).
OldPath string
// OldContent is the file content before the change ("" for Add).
OldContent string
// NewContent is the content after the change ("" for Delete).
NewContent string
// Note, when non-empty, says how a hunk was located when it was not a
// literal match (today: how many context lines were dropped). It is shown
// with the summary so a reduced match is never silent.
Note string
}
Change is one resolved, in-memory file change. Nothing has been written when Compute returns; the caller persists each change (and can snapshot it first).
func Compute ¶
Compute resolves a parsed patch against the working tree and returns the changes it would make, in operation order, without touching the filesystem. It has no byte budget; callers that accept model-generated patches should use ComputeLimited so one oversized call cannot retain an unbounded working set.
func ComputeLimited ¶ added in v1.5.9
ComputeLimited is Compute with a cap on the file content it retains while resolving the patch. The cap covers content read from disk plus content synthesized by updates/moves; a patch that would exceed it fails before any write, naming the budget instead of exhausting memory first. maxBytes <= 0 keeps Compute's unbounded behavior (used by direct library callers/tests).
type ChangeKind ¶
type ChangeKind int
ChangeKind is what an operation does to one file.
const ( // ChangeAdd creates a file. ChangeAdd ChangeKind = iota + 1 // ChangeUpdate rewrites a file in place. ChangeUpdate // ChangeDelete removes a file. ChangeDelete // ChangeMove rewrites a file and renames it. ChangeMove )
type Chunk ¶
type Chunk struct {
// Anchors are the literal lines named after one or more "@@" markers, in
// order. They are hints with codex's meaning — the function or section the
// hunk belongs to — and the matcher seeks each one forward before looking
// for the hunk's own lines. A second "@@" is the further enclosing scope,
// exactly as codex's instructions describe it.
Anchors []string
// Header is the first non-empty anchor, kept for error messages.
Header string
// Lines are the chunk's context, add, and delete lines in file order.
Lines []Line
// EOF anchors the search at the end of the file first (the
// "*** End of File" marker).
EOF bool
}
Chunk is one hunk of an update: the context/removal lines locate the region (fuzzy-matched; see seek.go), and the additions replace it.
type Failure ¶ added in v1.3.8
type Failure struct {
Path string
// Hunk is the 1-based hunk number inside an Update; 0 for a file operation.
Hunk int
Err error
}
Failure is one part of a patch that could not be applied. Compute collects every failure in one pass, so a patch with two broken hunks costs one round trip instead of two.
type LineKind ¶
type LineKind int
LineKind is one line's role inside a chunk.
const ( // LineContext is unchanged text used to locate the hunk (" " prefix). LineContext LineKind = iota // LineAdd is inserted text ("+" prefix). LineAdd // LineDelete is removed text ("-" prefix). LineDelete // LineUnknown is a line that arrived without a prefix. It only survives // until repairUnknownLines decides what it must have been; a patch never // applies one as-is. LineUnknown )
type Op ¶
type Op struct {
Kind OpKind
// Path is the target path exactly as written in the patch (the caller
// resolves it against its workspace roots).
Path string
// MoveTo, for an update, is the rename target ("" = no rename). It is
// resolved like Path.
MoveTo string
// Body holds the added lines for an Add (without the "+" prefix; each
// entry is one line, and the created file ends with a newline).
Body []string
// Chunks holds the hunks for an Update.
Chunks []Chunk
// Notes records repairs the parser made (a hunk line that arrived without a
// prefix and was unambiguous), so the tool result can say what was assumed.
Notes []string
}
Op is one file operation in a patch.
type ParseError ¶
ParseError is a malformed patch; it carries the 1-based input line so the model can see exactly which line broke the grammar.
func (*ParseError) Error ¶
func (e *ParseError) Error() string