patch

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

onpatch: transactional file patching service with rollback support.

This package exposes:

  • Session‑scoped file operations (Add, Delete, Move, Update)
  • Diff generation (F‑04)
  • Unified‑patch application (F‑03)

Key change in this revision ➜ **each mutating call now stores its own unique backup snapshot**, preventing the original‑overwrite bug when the same file is patched multiple times within a single session.

External deps (add to go.mod):

github.com/pmezard/go-difflib/difflib
github.com/sourcegraph/go-diff/diff

Example:

s, _ := onpatch.NewSession()
_ = s.Update("foo.txt", []byte("v1\n"))
_ = s.Update("foo.txt", []byte("v2\n")) // second update gets its own backup
_ = s.Rollback() // restores original pre‑session content

Index

Constants

View Source
const Name = "system/patch"

Name of the system/patch action service.

Variables

View Source
var ErrNoChange = errors.New("no change between old and new")

Functions

This section is empty.

Types

type Action

type Action string
const (
	Delete Action = "delete"
	Move   Action = "move"
	Update Action = "update"
	Add    Action = "add"
)

type ApplyInput

type ApplyInput struct {
	// Patch must be in the *standard* unified-diff format as produced by
	// tools such as `git diff` or `diff -u`.
	// A valid payload therefore starts with file header lines, for example:
	//
	//     --- a/path/to/file.txt
	//     +++ b/path/to/file.txt
	//     @@ -10,2 +10,3 @@
	//     -old line
	//     +new line
	//
	// and continues with the usual @@ hunk blocks.  Multi-file patches are
	// accepted as well.  The service applies the patch relative to the
	// current working directory of the Fluxor runtime.
	Patch string `json:"patch" description:"Unified-diff text (---/+++ file headers with @@ hunk markers) to apply"`
}

ApplyInput is the payload for Service.apply

type ApplyOutput

type ApplyOutput struct {
	Stats DiffStats `json:"stats,omitempty"`
}

ApplyOutput summarises the changes applied.

type DiffInput

type DiffInput struct {
	OldContent   string `json:"old" description:"Original file content"`
	NewContent   string `json:"new" description:"Updated file content"`
	Path         string `json:"path,omitempty" description:"Display path for diff headers"`
	ContextLines int    `json:"contextLines,omitempty" description:"Number of context lines to include in diff (default 3)"`
}

DiffInput is the payload for Service.diff

type DiffOutput

type DiffOutput DiffResult

DiffOutput is identical to DiffResult, re-exported for JSON tags.

type DiffResult

type DiffResult struct {
	Patch string
	Stats DiffStats
}

func GenerateDiff

func GenerateDiff(old, new []byte, path string, contextLines int) (DiffResult, error)

type DiffStats

type DiffStats struct {
	FilesChanged int
	Insertions   int
	Deletions    int
	Hunks        int
}

type EmptyInput

type EmptyInput struct{}

EmptyInput/Output used by commit/rollback methods.

type EmptyOutput

type EmptyOutput struct{}

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service exposes filesystem patching capabilities as a Fluxor action service. It is stateless – every method call operates with its own ephemeral Session.

func New

func New() *Service

New creates the patch service instance.

func (*Service) Method

func (s *Service) Method(name string) (types.Executable, error)

Method maps method names to executable handlers.

func (*Service) Methods

func (s *Service) Methods() types.Signatures

Methods returns service method catalogue.

func (*Service) Name

func (s *Service) Name() string

Name returns service identifier.

type Session

type Session struct {
	ID string
	// contains filtered or unexported fields
}

func NewSession

func NewSession() (*Session, error)

func (*Session) Add

func (s *Session) Add(path string, data []byte) error

func (*Session) ApplyPatch

func (s *Session) ApplyPatch(patchText string) error

func (*Session) Commit

func (s *Session) Commit() error

func (*Session) Delete

func (s *Session) Delete(path string) error

func (*Session) Move

func (s *Session) Move(src, dst string) error

func (*Session) Rollback

func (s *Session) Rollback() error

func (*Session) Update

func (s *Session) Update(path string, newData []byte) error

Jump to

Keyboard shortcuts

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