patch

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func GenerateTwoFilesPatch(filePath, oldContent, newContent string) string

GenerateTwoFilesPatch wraps the line diff in an Index/---/+++ header.

func GenerateUnifiedDiff

func GenerateUnifiedDiff(oldContent string, newContent string) string

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

func (h Hunk) MarshalJSON() ([]byte, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL