Documentation
¶
Overview ¶
Package gitdiff parses and applies patches generated by Git. It supports line-oriented text patches, binary patches, and can also parse standard unified diffs generated by other tools.
Index ¶
- type Applier
- func (a *Applier) ApplyBinaryFragment(dst io.Writer, f *BinaryFragment) error
- func (a *Applier) ApplyFile(dst io.Writer, f *File) error
- func (a *Applier) ApplyTextFragment(dst io.Writer, f *TextFragment) error
- func (a *Applier) Flush(dst io.Writer) (err error)
- func (a *Applier) Reset(src io.ReaderAt)
- type ApplyError
- type BinaryFragment
- type BinaryPatchMethod
- type Conflict
- type File
- type Line
- type LineOp
- type LineReaderAt
- type TextFragment
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Applier ¶ added in v0.2.0
type Applier struct {
// contains filtered or unexported fields
}
Applier applies changes described in fragments to source data. If changes are described in multiple fragments, those fragments must be applied in order, usually by calling ApplyFile.
By default, Applier operates in "strict" mode, where fragment content and positions must exactly match those of the source.
If an error occurs while applying, methods on Applier return instances of *ApplyError that annotate the wrapped error with additional information when available. If the error is because of a conflict between a fragment and the source, the wrapped error will be a *Conflict.
While an Applier can apply both text and binary fragments, only one fragment type can be used without resetting the Applier. The first fragment applied sets the type for the Applier. Mixing fragment types or mixing fragment-level and file-level applies results in an error.
func NewApplier ¶ added in v0.2.0
NewApplier creates an Applier that reads data from src. If src is a LineReaderAt, it is used directly to apply text fragments.
func (*Applier) ApplyBinaryFragment ¶ added in v0.2.0
func (a *Applier) ApplyBinaryFragment(dst io.Writer, f *BinaryFragment) error
ApplyBinaryFragment applies the changes in the fragment f and writes the result to dst. At most one binary fragment can be applied before a call to Reset.
func (*Applier) ApplyFile ¶ added in v0.2.0
ApplyFile applies the changes in all of the fragments of f and writes the result to dst.
func (*Applier) ApplyTextFragment ¶ added in v0.2.0
func (a *Applier) ApplyTextFragment(dst io.Writer, f *TextFragment) error
ApplyTextFragment applies the changes in the fragment f and writes unwritten data before the start of the fragment and the result to dst. If multiple text fragments apply to the same source, ApplyTextFragment must be called in order of increasing start position. As a result, each fragment can be applied at most once before a call to Reset.
type ApplyError ¶ added in v0.2.0
type ApplyError struct { // Line is the one-indexed line number in the source data Line int64 // Fragment is the one-indexed fragment number in the file Fragment int // FragmentLine is the one-indexed line number in the fragment FragmentLine int // contains filtered or unexported fields }
ApplyError wraps an error that occurs during patch application with additional location information, if it is available.
func (*ApplyError) Error ¶ added in v0.2.0
func (e *ApplyError) Error() string
func (*ApplyError) Unwrap ¶ added in v0.2.0
func (e *ApplyError) Unwrap() error
Unwrap returns the wrapped error.
type BinaryFragment ¶
type BinaryFragment struct { Method BinaryPatchMethod Size int64 Data []byte }
BinaryFragment describes changes to a binary file.
type BinaryPatchMethod ¶
type BinaryPatchMethod int
BinaryPatchMethod is the method used to create and apply the binary patch.
const ( // BinaryPatchDelta indicates the data uses Git's packfile encoding BinaryPatchDelta BinaryPatchMethod = iota // BinaryPatchLiteral indicates the data is the exact file content BinaryPatchLiteral )
type Conflict ¶ added in v0.2.0
type Conflict struct {
// contains filtered or unexported fields
}
Conflict indicates an apply failed due to a conflict between the patch and the source content.
Users can test if an error was caused by a conflict by using errors.Is with an empty Conflict:
if errors.Is(err, &Conflict{}) { // handle conflict }
type File ¶
type File struct { OldName string NewName string IsNew bool IsDelete bool IsCopy bool IsRename bool OldMode os.FileMode NewMode os.FileMode OldOIDPrefix string NewOIDPrefix string Score int // TextFragments contains the fragments describing changes to a text file. It // may be empty if the file is empty or if only the mode changes. TextFragments []*TextFragment // IsBinary is true if the file is a binary file. If the patch includes // binary data, BinaryFragment will be non-nil and describe the changes to // the data. If the patch is reversible, ReverseBinaryFragment will also be // non-nil and describe the changes needed to restore the original file // after applying the changes in BinaryFragment. IsBinary bool BinaryFragment *BinaryFragment ReverseBinaryFragment *BinaryFragment }
File describes changes to a single file. It can be either a text file or a binary file.
type Line ¶
Line is a line in a text fragment.
func (Line) New ¶ added in v0.2.0
New returns true if the line appears in the new content of the fragment.
func (Line) NoEOL ¶ added in v0.2.0
NoEOL returns true if the line is missing a trailing newline character.
type LineOp ¶
type LineOp int
LineOp describes the type of a text fragment line: context, added, or removed.
type LineReaderAt ¶ added in v0.2.0
LineReaderAt is the interface that wraps the ReadLinesAt method.
ReadLinesAt reads len(lines) into lines starting at line offset in the input source. It returns number of full lines read (0 <= n <= len(lines)) and any error encountered. Line numbers are zero-indexed.
If n < len(lines), ReadLinesAt returns a non-nil error explaining why more lines were not returned.
Each full line includes the line ending character(s). If the last line of the input does not have a line ending character, ReadLinesAt returns the content of the line and io.EOF.
If the content of the input source changes after the first call to ReadLinesAt, the behavior of future calls is undefined.
type TextFragment ¶
type TextFragment struct { Comment string OldPosition int64 OldLines int64 NewPosition int64 NewLines int64 LinesAdded int64 LinesDeleted int64 LeadingContext int64 TrailingContext int64 Lines []Line }
TextFragment describes changed lines starting at a specific line in a text file.
func (*TextFragment) Header ¶
func (f *TextFragment) Header() string
Header returns the canonical header of this fragment.
func (*TextFragment) Validate ¶ added in v0.2.0
func (f *TextFragment) Validate() error
Validate checks that the fragment is self-consistent and appliable. Validate returns an error if and only if the fragment is invalid.