gitdiff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2020 License: MIT Imports: 10 Imported by: 71

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 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.

func Parse

func Parse(r io.Reader) ([]*File, string, error)

Parse parses a patch with changes to one or more files. Any content before the first file is returned as the second value. If an error occurs while parsing, it returns all files parsed before the error.

If r is a LineReader or StringReader, it is used directly. Otherwise, it is wrapped in a way that may read extra data from the underlying input.

type Line

type Line struct {
	Op   LineOp
	Line string
}

Line is a line in a text fragment.

func (Line) String

func (fl Line) String() string

type LineOp

type LineOp int

LineOp describes the type of a text fragment line: context, added, or removed.

const (
	// OpContext indicates a context line
	OpContext LineOp = iota
	// OpDelete indicates a deleted line
	OpDelete
	// OpAdd indicates an added line
	OpAdd
)

func (LineOp) String

func (op LineOp) String() string

type LineReader

type LineReader interface {
	// ReadLine reads the next full line in the input, returing the the data
	// including the line ending character(s) and the zero-indexed line number.
	// If ReadLine encounters an error before reaching the end of the line, it
	// returns the data read before the error and the error itself (often
	// io.EOF). ReadLine returns err != nil if and only if the returned data is
	// not a complete line.
	//
	// If the implementation defines other methods for reading the same input,
	// line numbers may be incorrect if calls to ReadLine are mixed with calls
	// to other read methods.
	ReadLine() (string, int, error)
}

LineReader is the interface that wraps the ReadLine method.

func NewLineReader

func NewLineReader(r io.Reader, lineno int) LineReader

NewLineReader returns a LineReader for a reader starting at a specific line using the newline character, \n, as a line separator. If r is a StringReader, it is used directly. Otherwise, it is wrapped in a way that may read extra data from the underlying input.

type StringReader

type StringReader interface {
	// ReadString reads until the first occurrence of delim in the input,
	// returning a string containing the data up to and including the
	// delimiter. If ReadString encounters an error before finding a delimiter,
	// it returns the data read before the error and the error itself (often
	// io.EOF). ReadString returns err != nil if and only if the returned data
	// does not end in delim.
	ReadString(delim byte) (string, error)
}

StringReader is the interface that wraps the ReadString method.

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.

Jump to

Keyboard shortcuts

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