Documentation
¶
Overview ¶
Package patch implements the apply_patch format: the patch parser, fuzzy chunk matching, direct filesystem application, and detection of apply_patch invocations inside shell commands.
Index ¶
- Constants
- func GenerateTwoFilesPatch(filePath, oldContent, newContent string) string
- func GenerateUnifiedDiff(oldContent string, newContent string) string
- type AffectedPaths
- type ApplyPatchAction
- type ApplyPatchArgs
- type ApplyPatchFileChange
- type ApplyPatchFileUpdate
- type Hunk
- type MaybeResult
- type ParseResult
- type UpdateFileChunk
- type VerifiedResult
Constants ¶
const ( MaybeBody = "Body" MaybeShellParseError = "ShellParseError" MaybePatchParseError = "PatchParseError" MaybeNotApplyPatch = "NotApplyPatch" VerifiedBody = "Body" VerifiedShellParseError = "ShellParseError" VerifiedCorrectnessError = "CorrectnessError" VerifiedNotApplyPatch = "NotApplyPatch" ErrorParseError = "ParseError" ErrorIOError = "IoError" ErrorComputeReplacements = "ComputeReplacements" ErrorImplicitInvocation = "ImplicitInvocation" )
Variables ¶
This section is empty.
Functions ¶
func GenerateTwoFilesPatch ¶
GenerateTwoFilesPatch wraps the line diff in an Index/---/+++ header.
func GenerateUnifiedDiff ¶
GenerateUnifiedDiff exposes the patch package's mutation preview to live edit/write tools.
Types ¶
type AffectedPaths ¶
type AffectedPaths struct {
Added []string `json:"added"`
Modified []string `json:"modified"`
Deleted []string `json:"deleted"`
}
AffectedPaths records filesystem application results.
func ApplyHunksToFiles ¶
func ApplyHunksToFiles(hunks []Hunk) (AffectedPaths, error)
ApplyHunksToFiles applies already-parsed hunks to their literal paths.
func ApplyPatch ¶
func ApplyPatch(patchText string) (AffectedPaths, error)
ApplyPatch parses and applies patchText.
type ApplyPatchAction ¶
type ApplyPatchAction struct {
Changes map[string]ApplyPatchFileChange
Patch string
CWD string
}
ApplyPatchAction is the verified patch preview.
type ApplyPatchArgs ¶
type ApplyPatchArgs struct {
Patch string `json:"patch"`
Hunks []Hunk `json:"hunks"`
Workdir string `json:"workdir,omitempty"`
}
ApplyPatchArgs is a detected apply_patch invocation.
type ApplyPatchFileChange ¶
type ApplyPatchFileChange struct {
Type string `json:"type"`
Content string `json:"content,omitempty"`
UnifiedDiff string `json:"unified_diff,omitempty"`
MovePath string `json:"move_path,omitempty"`
NewContent string `json:"new_content,omitempty"`
}
ApplyPatchFileChange is one previewed file change.
type ApplyPatchFileUpdate ¶
type ApplyPatchFileUpdate struct {
UnifiedDiff string `json:"unified_diff"`
Content string `json:"content"`
BOM bool `json:"bom"`
}
ApplyPatchFileUpdate is DeriveNewContentsFromChunks' result.
func DeriveNewContentsFromChunks ¶
func DeriveNewContentsFromChunks(filePath string, chunks []UpdateFileChunk) (ApplyPatchFileUpdate, error)
DeriveNewContentsFromChunks reads filePath and applies update chunks.
type Hunk ¶
type Hunk struct {
Type string
Path string
Contents string
MovePath string
Chunks []UpdateFileChunk
}
Hunk is one add, delete, or update section.
func (Hunk) MarshalJSON ¶
type MaybeResult ¶
type MaybeResult struct {
Type string
Args *ApplyPatchArgs
Err error
}
MaybeResult is the discriminated result of MaybeParseApplyPatch.
func MaybeParseApplyPatch ¶
func MaybeParseApplyPatch(argv []string) MaybeResult
MaybeParseApplyPatch detects direct and bash-heredoc invocations.
type ParseResult ¶
type ParseResult struct {
Hunks []Hunk `json:"hunks"`
}
ParseResult is the parsed hunk list.
func ParsePatch ¶
func ParsePatch(patchText string) (ParseResult, error)
ParsePatch parses the stripped patch envelope.
type UpdateFileChunk ¶
type UpdateFileChunk struct {
OldLines []string `json:"old_lines"`
NewLines []string `json:"new_lines"`
ChangeContext string `json:"change_context,omitempty"`
}
UpdateFileChunk is the parser's line-oriented update block.
type VerifiedResult ¶
type VerifiedResult struct {
Type string
Action *ApplyPatchAction
Err error
}
VerifiedResult is the discriminated result of MaybeParseApplyPatchVerified.
func MaybeParseApplyPatchVerified ¶
func MaybeParseApplyPatchVerified(argv []string, cwd string) VerifiedResult
MaybeParseApplyPatchVerified previews a detected patch against cwd.