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
}
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. Existence checks and content reads happen here, so every failure (missing file for an update, an existing file for an add, a poor context match) surfaces before the first byte is written. Operations are applied over an in-memory overlay, so a patch that touches the same file twice sees its own earlier change.
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 {
// Header is the text after "@@" when present (informational; it does not
// constrain the match, matching codex's parser).
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 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
}
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