patch

package
v0.0.0-...-9e80811 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package patch implements parsing and execution of the textual and binary patch descriptions used by version control tools such as CVS, Git, Mercurial, and Subversion.

Index

Constants

This section is empty.

Variables

View Source
var ErrPatchFailure = errors.New("patch did not apply cleanly")

Functions

This section is empty.

Types

type Diff

type Diff interface {
	// Apply applies the changes listed in the diff
	// to the string s, returning the new version of the string.
	// Note that the string s need not be a text string.
	Apply(old []byte) (new []byte, err error)
}

A Diff is any object that describes changes to transform an old byte stream to a new one.

var NoDiff Diff = noDiffType(0)

NoDiff is a no-op Diff implementation: it passes the old data through unchanged.

func ParseGitBinary

func ParseGitBinary(raw []byte) (Diff, error)

ParseGitBinary parses raw as a Git binary patch.

type File

type File struct {
	Verb             Verb
	Src              string // source for Verb == Copy, Verb == Rename
	Dst              string
	OldMode, NewMode int // 0 indicates not used
	Diff                 // changes to data; == NoDiff if operation does not edit file
}

A File represents a collection of changes to be made to a single file.

type GitBinaryLiteral

type GitBinaryLiteral struct {
	OldSHA1 []byte // if non-empty, the SHA1 hash of the original
	New     []byte // the new contents
}

GitBinaryLiteral represents a Git binary literal diff.

func (*GitBinaryLiteral) Apply

func (d *GitBinaryLiteral) Apply(old []byte) ([]byte, error)

Apply implements the Diff interface's Apply method.

type Op

type Op struct {
	Verb Verb   // action
	Src  string // source file
	Dst  string // destination file
	Mode int    // mode for destination (if non-zero)
	Data []byte // data for destination (if non-nil)
}

An Op is a single operation to execute to apply a patch.

type Set

type Set struct {
	Header string // free-form text
	File   []*File
}

A Set represents a set of patches to be applied as a single atomic unit. Patch sets are often preceded by a descriptive header.

func Parse

func Parse(text []byte) (*Set, error)

Parse patches the patch text to create a patch Set. The patch text typically comprises a textual header and a sequence of file patches, as would be generated by CVS, Subversion, Mercurial, or Git.

func (*Set) Apply

func (set *Set) Apply(readFile func(string) ([]byte, error)) ([]Op, error)

Apply applies the patch set to the files named in the patch set, constructing an in-memory copy of the new file state. It is the client's job to write the changes to the file system if desired.

The function readFile should return the contents of the named file. Typically this function will be io.ReadFile.

type SyntaxError

type SyntaxError string

A SyntaxError represents a syntax error encountered while parsing a patch.

func (SyntaxError) Error

func (e SyntaxError) Error() string

type TextChunk

type TextChunk struct {
	Line int
	Old  []byte
	New  []byte
}

A TextChunk specifies an edit to a section of a file: the text beginning at Line, which should be exactly Old, is to be replaced with New.

type TextDiff

type TextDiff []TextChunk

func ParseTextDiff

func ParseTextDiff(raw []byte) (TextDiff, error)

func (TextDiff) Apply

func (d TextDiff) Apply(data []byte) ([]byte, error)

Apply applies the changes listed in the diff to the data, returning the new version.

type Verb

type Verb string

A Verb is an action performed on a file.

const (
	Add    Verb = "add"
	Copy   Verb = "copy"
	Delete Verb = "delete"
	Edit   Verb = "edit"
	Rename Verb = "rename"
)

Notes

Bugs

  • The Git binary delta format is not implemented, only Git binary literals.

Jump to

Keyboard shortcuts

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