gitdiff

package
v0.0.0-...-d915811 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2019 License: MIT Imports: 12 Imported by: 0

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

func ParsePatch

func ParsePatch(r io.Reader) ([]*File, *Patches, error)

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 Commit

type Commit struct {
	CommitSha string
	Content   string
}

func EmitCommit

func EmitCommit(content string) (Commit, bool)

func (Commit) Equal

func (c Commit) Equal(rhs Commit) bool

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.

func (*File) Funcs

func (f *File) Funcs() []string

func (*File) HasFunc

func (f *File) HasFunc(match func(string) bool) bool

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 Patches

type Patches struct {
	FilesToSha  map[*File]string
	ShaToFiles  map[string][]*File
	ShaToCommit map[string]Commit
}

func (*Patches) AppendFile

func (p *Patches) AppendFile(cmt Commit, file *File)

func (*Patches) GetCommit

func (p *Patches) GetCommit(file *File) (Commit, bool)

func (*Patches) GetFiles

func (p *Patches) GetFiles(sha string) ([]*File, bool)

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) FuncNames

func (f *TextFragment) FuncNames() []string

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