patch

package
v0.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package patch provides file patching operations using a context-aware diff format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SeekLine

func SeekLine(lines []string, pattern string, startIdx int) int

SeekLine finds a single line containing the pattern, starting from startIdx. Uses 4-level fuzzy matching.

func SeekSequence

func SeekSequence(lines, pattern []string, startIdx int) (int, bool)

SeekSequence finds a sequence of lines in the file, starting from startIdx. Uses 4-level fuzzy matching.

Types

type AddFile

type AddFile struct {
	Path    string
	Content string
}

AddFile represents a file creation operation.

func (*AddFile) Execute

func (h *AddFile) Execute(ctx context.Context) (string, error)

func (*AddFile) Track

func (h *AddFile) Track(ctx context.Context)

func (*AddFile) Validate

func (h *AddFile) Validate(_ context.Context) error

func (*AddFile) WritePaths

func (h *AddFile) WritePaths() []string

type Chunk

type Chunk struct {
	Context  string   // @@ marker content for locating the change
	OldLines []string // Lines to find and remove (from - and space lines)
	NewLines []string // Lines to insert (from + and space lines)
}

Chunk represents a single change within an UpdateFile.

type DeleteFile

type DeleteFile struct {
	Path string
}

DeleteFile represents a file deletion operation.

func (*DeleteFile) Execute

func (h *DeleteFile) Execute(ctx context.Context) (string, error)

func (*DeleteFile) Track

func (h *DeleteFile) Track(ctx context.Context)

func (*DeleteFile) Validate

func (h *DeleteFile) Validate(_ context.Context) error

func (*DeleteFile) WritePaths

func (h *DeleteFile) WritePaths() []string

type Hunk

type Hunk interface {
	Execute(ctx context.Context) (string, error)
	Validate(ctx context.Context) error
	Track(ctx context.Context)
	WritePaths() []string
	// contains filtered or unexported methods
}

Hunk represents a single patch operation with behavioral methods.

func Parse

func Parse(input string) ([]Hunk, error)

Parse parses a patch string into a slice of Hunks. Supports multiple *** Begin Patch / *** End Patch blocks in a single input.

type Inputs

type Inputs struct {
	Patch string `json:"patch" jsonschema:"required,description=Patch content in context-aware diff format (see Usage)" validate:"required"`
}

Inputs defines the parameters for the Patch tool.

type Tool

type Tool struct {
	tool.Base
}

Tool implements the Patch tool for applying file patches.

func New

func New() *Tool

New creates a new Patch tool.

func (*Tool) Execute

func (t *Tool) Execute(ctx context.Context, args map[string]any) (string, error)

Execute parses, validates, and applies hunks.

func (*Tool) Name

func (t *Tool) Name() string

Name returns the tool's identifier.

func (*Tool) Paths

func (t *Tool) Paths(ctx context.Context, args map[string]any) (read, write []string, err error)

Paths returns the filesystem paths this tool call will access.

func (*Tool) Post

func (t *Tool) Post(ctx context.Context, args map[string]any)

Post updates filetime state after successful patch. Uses Parse (not parseAndValidate) because Validate checks file existence, which may be wrong after Execute (files created/deleted/moved).

func (*Tool) Pre

func (t *Tool) Pre(ctx context.Context, args map[string]any) error

Pre parses the patch, validates hunks, and enforces filetime per policy. Delete hunks check policy.Delete; all other hunks check policy.Write.

func (*Tool) Preview

func (t *Tool) Preview(ctx context.Context, args map[string]any) (string, error)

Preview generates a unified diff preview for confirmation dialogs.

func (*Tool) Schema

func (t *Tool) Schema() tool.Schema

Schema returns the provider-agnostic tool definition.

func (*Tool) WantsLSP

func (t *Tool) WantsLSP() bool

WantsLSP indicates that Patch produces source files that benefit from LSP diagnostics.

type UpdateFile

type UpdateFile struct {
	Path   string
	MoveTo string // Optional: rename/move destination
	Chunks []Chunk
}

UpdateFile represents a file modification operation.

func (*UpdateFile) ApplyToContent

func (h *UpdateFile) ApplyToContent(content string) (string, error)

ApplyToContent applies the chunks to the given content string and returns the result. Does not touch the filesystem.

func (*UpdateFile) Execute

func (h *UpdateFile) Execute(ctx context.Context) (string, error)

func (*UpdateFile) Track

func (h *UpdateFile) Track(ctx context.Context)

func (*UpdateFile) Validate

func (h *UpdateFile) Validate(ctx context.Context) error

func (*UpdateFile) WritePaths

func (h *UpdateFile) WritePaths() []string

Jump to

Keyboard shortcuts

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